Class: Reel::Request::Parser

Inherits:
Object
  • Object
show all
Includes:
HTTPVersionsMixin
Defined in:
lib/reel/request/parser.rb

Constant Summary

Constants included from HTTPVersionsMixin

HTTPVersionsMixin::DEFAULT_HTTP_VERSION, HTTPVersionsMixin::HTTP_VERSION_1_0, HTTPVersionsMixin::HTTP_VERSION_1_1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Parser

Returns a new instance of Parser.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/reel/request/parser.rb', line 7

def initialize(connection)
  @parser      = HTTP::Parser.new(self)
  @connection  = connection
  @socket      = connection.socket
  @buffer_size = connection.buffer_size
  @currently_reading = @currently_responding = nil
  @pending_reads     = []
  @pending_responses = []

  reset
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



5
6
7
# File 'lib/reel/request/parser.rb', line 5

def connection
  @connection
end

#socketObject (readonly)

Returns the value of attribute socket.



5
6
7
# File 'lib/reel/request/parser.rb', line 5

def socket
  @socket
end

Instance Method Details

#add(data) ⇒ Object Also known as: <<



19
20
21
# File 'lib/reel/request/parser.rb', line 19

def add(data)
  @parser << data
end

#current_requestObject



37
38
39
40
41
42
# File 'lib/reel/request/parser.rb', line 37

def current_request
  until @currently_responding || @currently_reading
    readpartial
  end
  @currently_responding || @currently_reading
end

#http_methodObject



24
25
26
# File 'lib/reel/request/parser.rb', line 24

def http_method
  @parser.http_method
end

#http_versionObject



28
29
30
31
# File 'lib/reel/request/parser.rb', line 28

def http_version
  # TODO: add extra HTTP_VERSION handler when HTTP/1.2 released
  @parser.http_version[1] == 1 ? HTTP_VERSION_1_1 : HTTP_VERSION_1_0
end

#on_body(chunk) ⇒ Object

Send body directly to Reel::Response to be buffered.



64
65
66
# File 'lib/reel/request/parser.rb', line 64

def on_body(chunk)
  @currently_reading.fill_buffer(chunk)
end

#on_headers_complete(headers) ⇒ Object

HTTP::Parser callbacks



52
53
54
55
56
57
58
59
60
61
# File 'lib/reel/request/parser.rb', line 52

def on_headers_complete(headers)
  info = Info.new(http_method, url, http_version, headers)
  req  = Request.new(info, connection)

  if @currently_reading
    @pending_reads << req
  else
    @currently_reading = req
  end
end

#on_message_completeObject

Mark current request as complete, set this as ready to respond.



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/reel/request/parser.rb', line 69

def on_message_complete
  @currently_reading.finish_reading! if @currently_reading.is_a?(Request)

  if @currently_responding
    @pending_responses << @currently_reading
  else
    @currently_responding = @currently_reading
  end

  @currently_reading = @pending_reads.shift
end

#readpartial(size = @buffer_size) ⇒ Object



44
45
46
47
# File 'lib/reel/request/parser.rb', line 44

def readpartial(size = @buffer_size)
  bytes = @socket.readpartial(size)
  @parser << bytes
end

#resetObject



81
82
83
84
85
86
87
88
89
90
91
# File 'lib/reel/request/parser.rb', line 81

def reset
  popped = @currently_responding

  if req = @pending_responses.shift
    @currently_responding = req
  elsif @currently_responding
    @currently_responding = nil
  end

  popped
end

#urlObject



33
34
35
# File 'lib/reel/request/parser.rb', line 33

def url
  @parser.request_url
end