Class: Thin::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/thin/request.rb

Overview

A request sent by the client to the server.

Constant Summary collapse

MAX_BODY =

Maximum request body size before it is moved out of memory and into a tempfile for reading.

1024 * (80 + 32)
BODY_TMPFILE =
'thin-body'.freeze
MAX_HEADER =
1024 * (80 + 32)
SERVER_SOFTWARE =

Freeze some HTTP header names & values

'SERVER_SOFTWARE'.freeze
HTTP_VERSION =
'HTTP_VERSION'.freeze
HTTP_1_0 =
'HTTP/1.0'.freeze
REMOTE_ADDR =
'REMOTE_ADDR'.freeze
FORWARDED_FOR =
'HTTP_X_FORWARDED_FOR'.freeze
CONTENT_LENGTH =
'CONTENT_LENGTH'.freeze
CONNECTION =
'HTTP_CONNECTION'.freeze
KEEP_ALIVE_REGEXP =
/\bkeep-alive\b/i.freeze
CLOSE_REGEXP =
/\bclose\b/i.freeze
RACK_INPUT =

Freeze some Rack header names

'rack.input'.freeze
RACK_VERSION =
'rack.version'.freeze
RACK_ERRORS =
'rack.errors'.freeze
RACK_MULTITHREAD =
'rack.multithread'.freeze
RACK_MULTIPROCESS =
'rack.multiprocess'.freeze
RACK_RUN_ONCE =
'rack.run_once'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequest

Returns a new instance of Request.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/thin/request.rb', line 45

def initialize
  @parser   = HttpParser.new
  @data     = ''
  @nparsed  = 0
  @body     = StringIO.new
  @env      = {
    SERVER_SOFTWARE   => SERVER,
    
    # Rack stuff
    RACK_INPUT        => @body,
    
    RACK_VERSION      => VERSION::RACK,
    RACK_ERRORS       => STDERR,
    
    RACK_MULTITHREAD  => false,
    RACK_MULTIPROCESS => false,
    RACK_RUN_ONCE     => false
  }
end

Instance Attribute Details

#bodyObject (readonly)

Request body



43
44
45
# File 'lib/thin/request.rb', line 43

def body
  @body
end

#dataObject (readonly)

Unparsed data of the request



40
41
42
# File 'lib/thin/request.rb', line 40

def data
  @data
end

#envObject (readonly)

CGI-like request environment variables



37
38
39
# File 'lib/thin/request.rb', line 37

def env
  @env
end

Instance Method Details

#closeObject

Close any resource used by the request



130
131
132
# File 'lib/thin/request.rb', line 130

def close
  @body.delete if @body.class == Tempfile
end

#content_lengthObject

Expected size of the body



97
98
99
# File 'lib/thin/request.rb', line 97

def content_length
  @env[CONTENT_LENGTH].to_i
end

#finished?Boolean

true if headers and body are finished parsing

Returns:

  • (Boolean)


92
93
94
# File 'lib/thin/request.rb', line 92

def finished?
  @parser.finished? && @body.size >= content_length
end

#forwarded_forObject



121
122
123
# File 'lib/thin/request.rb', line 121

def forwarded_for
  @env[FORWARDED_FOR]
end

#parse(data) ⇒ Object

Parse a chunk of data into the request environment Raises a InvalidRequest if invalid. Returns true if the parsing is complete.



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/thin/request.rb', line 68

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
    raise InvalidRequest, 'Header longer than allowed' if @data.size > MAX_HEADER 
    
    @nparsed = @parser.execute(@env, @data, @nparsed)

    # Transfert to a tempfile if body is very big
    move_body_to_tempfile if @parser.finished? && content_length > MAX_BODY
  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)


102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/thin/request.rb', line 102

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

#remote_address=(address) ⇒ Object



117
118
119
# File 'lib/thin/request.rb', line 117

def remote_address=(address)
  @env[REMOTE_ADDR] = address
end

#threaded=(value) ⇒ Object



125
126
127
# File 'lib/thin/request.rb', line 125

def threaded=(value)
  @env[RACK_MULTITHREAD] = value
end