Class: Pronghorn::Request

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

Constant Summary collapse

PROTO_ENV =
{
  "SERVER_SOFTWARE".freeze   => "Proghorn #{::Pronghorn::VERSION}".freeze,
  "SERVER_NAME".freeze       => "localhost",
  "SERVER_PROTOCOL".freeze   => "HTTP/1.1",
  "HTTP_VERSION".freeze      => "HTTP/1.1",
  "SCRIPT_NAME".freeze       => "",
  "QUERY_STRING".freeze      => "",
  "GATEWAY_INTERFACE"        => "CGI/1.2",

  "rack.url_scheme".freeze   => "http",
  "rack.errors".freeze       => STDERR,
  "rack.multithread".freeze  => false,
  "rack.multiprocess".freeze => false,
  "rack.run_once".freeze     => false,
  "rack.version".freeze      => Rack::VERSION
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRequest

Returns a new instance of Request.



27
28
29
30
31
32
33
34
35
# File 'lib/pronghorn/request.rb', line 27

def initialize
  @parser = Http::Parser.new
  @parser.on_body = proc { |chunk| @body << chunk }
  @parser.on_headers_complete = proc { |headers| @headers = headers }

  initial_body = ''
  initial_body.encode!(Encoding::ASCII_8BIT) if initial_body.respond_to?(:encode!)
  @body = StringIO.new(initial_body.dup)
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



8
9
10
# File 'lib/pronghorn/request.rb', line 8

def body
  @body
end

#envObject (readonly)

Returns the value of attribute env.



6
7
8
# File 'lib/pronghorn/request.rb', line 6

def env
  @env
end

#parserObject (readonly)

Returns the value of attribute parser.



4
5
6
# File 'lib/pronghorn/request.rb', line 4

def parser
  @parser
end

Instance Method Details

#set_environment(env_vars = {}) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/pronghorn/request.rb', line 37

def set_environment(env_vars = {})
  @env = PROTO_ENV.dup
  @env.merge!({
    "REQUEST_METHOD".freeze  => @parser.http_method,
    "REQUEST_URI".freeze     => @parser.request_url,
    "REQUEST_PATH".freeze    => @parser.request_path,
    "QUERY_STRING".freeze    => @parser.query_string,
    "PATH_INFO".freeze       => @parser.request_path,
    "rack.input".freeze      => @body
  })
  @env["FRAGMENT"] = @parser.fragment if @parser.fragment
  if @headers.key?("Content-Length")
    @env["CONTENT_LENGTH"] = @headers.delete("Content-Length")
  end
  if @headers.key?("Content-Type")
    @env["CONTENT_TYPE"] = @headers.delete("Content-Type")
  end
  @headers.each{|k, v| @env["HTTP_#{k.upcase.gsub('-','_')}"] = v}
  @env.merge!(env_vars)
end