Class: Reifier::Request

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket, options) ⇒ Request

Returns a new instance of Request.



5
6
7
8
9
# File 'lib/reifier/request.rb', line 5

def initialize(socket, options)
  @socket  = socket
  @body    = StringIO.new('')
  @options = options
end

Instance Attribute Details

#protocolObject (readonly)

Returns the value of attribute protocol.



3
4
5
# File 'lib/reifier/request.rb', line 3

def protocol
  @protocol
end

#request_methodObject (readonly)

Returns the value of attribute request_method.



3
4
5
# File 'lib/reifier/request.rb', line 3

def request_method
  @request_method
end

#request_pathObject (readonly)

Returns the value of attribute request_path.



3
4
5
# File 'lib/reifier/request.rb', line 3

def request_path
  @request_path
end

#request_uriObject (readonly)

Returns the value of attribute request_uri.



3
4
5
# File 'lib/reifier/request.rb', line 3

def request_uri
  @request_uri
end

Instance Method Details

#handleObject



11
12
13
14
15
16
# File 'lib/reifier/request.rb', line 11

def handle
  handle_request_line
  handle_headers

  handle_body if request_with_body?
end

#rack_envObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/reifier/request.rb', line 18

def rack_env
  # See http://www.rubydoc.info/github/rack/rack/master/file/SPEC
  env = {
    'rack.version'         => Rack.version.split('.'),
    'rack.errors'          => STDERR,
    'rack.multithread'     => true,
    'rack.multiprocess'    => false,
    'rack.run_once'        => false,
    'rack.input'           => @body.set_encoding(Encoding::ASCII_8BIT),
    'rack.url_scheme'      => @protocol.split('/').first.downcase,
    'rack.hijack?'         => false,
    'REQUEST_METHOD'       => @request_method,
    'REQUEST_PATH'         => @request_path,
    'REQUEST_URI'          => @request_uri,
    'SCRIPT_NAME'          => '',
    'PATH_INFO'            => @request_path,
    'QUERY_STRING'         => @query_string,
    'SERVER_PROTOCOL'      => @protocol,
    'SERVER_SOFTWARE'      => "Reifier #{Reifier::VERSION}",
    'SERVER_NAME'          => @options[:Host],
    'SERVER_PORT'          => @options[:Port].to_s,
    'HTTP_VERSION'         => @protocol,
    'REMOTE_ADDR'          => @socket.addr.last
  }

  @headers.each do |k, v|
    # The environment must not contain the keys HTTP_CONTENT_TYPE or HTTP_CONTENT_LENGTH (use the versions without HTTP_).
    # see http://www.rubydoc.info/github/rack/rack/master/file/SPEC
    if k == 'CONTENT_LENGTH' || k == 'CONTENT_TYPE'
      env[k] = v
    else
      env["HTTP_#{k}"] = v
    end
  end

  env
end