Class: ProxyTest::RequestParser

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initializeRequestParser

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

#bodyObject (readonly)

Request body



36
37
38
# File 'lib/proxytest/helpers/thin_http_parser.rb', line 36

def body
  @body
end

#dataObject (readonly)

Unparsed data of the request



33
34
35
# File 'lib/proxytest/helpers/thin_http_parser.rb', line 33

def data
  @data
end

#envObject (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_lengthObject

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

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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