Class: ProxyTest::RequestParser
- Inherits:
-
Object
- Object
- ProxyTest::RequestParser
- Defined in:
- lib/proxytest/helpers/thin_http_parser.rb
Overview
A request sent by the client to the server.
Constant Summary collapse
- INITIAL_BODY =
''
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Request body.
-
#data ⇒ Object
readonly
Unparsed data of the request.
-
#env ⇒ Object
readonly
CGI-like request environment variables.
Instance Method Summary collapse
-
#content_length ⇒ Object
Expected size of the body.
-
#finished? ⇒ Boolean
true
if headers and body are finished parsing. -
#initialize ⇒ RequestParser
constructor
A new instance of RequestParser.
-
#parse(data) ⇒ Object
Parse a chunk of data into the request environment Returns
true
if the parsing is complete. -
#persistent? ⇒ Boolean
Returns
true
if the client expect the connection to be persistent.
Constructor Details
#initialize ⇒ RequestParser
Returns a new instance of RequestParser.
38 39 40 41 42 43 44 45 46 |
# File 'lib/proxytest/helpers/thin_http_parser.rb', line 38 def initialize @parser = Thin::HttpParser.new @data = '' @nparsed = 0 @body = StringIO.new(INITIAL_BODY.dup) @env = { RACK_INPUT => @body, } end |
Instance Attribute Details
#body ⇒ Object (readonly)
Request body
36 37 38 |
# File 'lib/proxytest/helpers/thin_http_parser.rb', line 36 def body @body end |
#data ⇒ Object (readonly)
Unparsed data of the request
33 34 35 |
# File 'lib/proxytest/helpers/thin_http_parser.rb', line 33 def data @data end |
#env ⇒ Object (readonly)
CGI-like request environment variables
30 31 32 |
# File 'lib/proxytest/helpers/thin_http_parser.rb', line 30 def env @env end |
Instance Method Details
#content_length ⇒ Object
Expected size of the body
73 74 75 |
# File 'lib/proxytest/helpers/thin_http_parser.rb', line 73 def content_length @env[CONTENT_LENGTH].to_i end |
#finished? ⇒ Boolean
true
if headers and body are finished parsing
68 69 70 |
# File 'lib/proxytest/helpers/thin_http_parser.rb', line 68 def finished? @parser.finished? && @body.size >= content_length end |
#parse(data) ⇒ Object
Parse a chunk of data into the request environment Returns true
if the parsing is complete.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/proxytest/helpers/thin_http_parser.rb', line 50 def parse(data) if @parser.finished? # Header finished, can only be some more body body << data else # Parse more header using the super parser @data << data @nparsed = @parser.execute(@env, @data, @nparsed) end if finished? # Check if header and body are complete @data = nil @body.rewind true # Request is fully parsed else false # Not finished, need more data end end |
#persistent? ⇒ Boolean
Returns true
if the client expect the connection to be persistent.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/proxytest/helpers/thin_http_parser.rb', line 78 def persistent? # Clients and servers SHOULD NOT assume that a persistent connection # is maintained for HTTP versions less than 1.1 unless it is explicitly # signaled. (http://www.w3.org/Protocols/rfc2616/rfc2616-sec8.html) if @env[HTTP_VERSION] == HTTP_1_0 @env[CONNECTION] =~ KEEP_ALIVE_REGEXP # HTTP/1.1 client intends to maintain a persistent connection unless # a Connection header including the connection-token "close" was sent # in the request else @env[CONNECTION].nil? || @env[CONNECTION] !~ CLOSE_REGEXP end end |