Class: Mongrel2::Request

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uuid, conn_id, path, headers, body, connection) ⇒ Request

Returns a new instance of Request.



16
17
18
19
20
21
22
23
24
25
# File 'lib/mongrel2/request.rb', line 16

def initialize(uuid, conn_id, path, headers, body, connection)
  @uuid, @conn_id, @path, @headers = uuid, conn_id, path, headers
  if (body_path = headers['x-mongrel2-upload-done'])
    @body = File.open(File.join(connection.chroot, body_path))
  else
    @body = StringIO.new(body)
  end
  @data = headers['METHOD'] == 'JSON' ? MultiJson.decode(body) : {}
  @connection = connection
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#conn_idObject (readonly)

Returns the value of attribute conn_id.



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

def conn_id
  @conn_id
end

#connectionObject (readonly)

Returns the value of attribute connection.



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

def connection
  @connection
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#uuidObject (readonly)

Returns the value of attribute uuid.



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

def uuid
  @uuid
end

Class Method Details

.parse(msg, connection) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/mongrel2/request.rb', line 6

def parse(msg, connection)
  # UUID CONN_ID PATH SIZE:HEADERS,SIZE:BODY,
  uuid, conn_id, path, rest = msg.split(' ', 4)
  headers, rest = TNetstring.parse(rest)
  headers = MultiJson.decode(headers)
  body, _ = TNetstring.parse(rest)
  new(uuid, conn_id, path, headers, body, connection)
end

Instance Method Details

#close?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/mongrel2/request.rb', line 31

def close?
  headers['connection'] == 'close' || headers['VERSION'] == 'HTTP/1.0'
end

#disconnect?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/mongrel2/request.rb', line 27

def disconnect?
  headers['METHOD'] == 'JSON' && @data['type'] == 'disconnect'
end

#envObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mongrel2/request.rb', line 35

def env
  return @env if @env
  script_name = ENV['RACK_RELATIVE_URL_ROOT'] || headers['PATTERN'].split('(', 2).first.gsub(/\/$/, '')
  @env = {
    'rack.version' => Rack::VERSION,
    'rack.url_scheme' => 'http', # Only HTTP for now
    'rack.input' => body,
    'rack.errors' => $stderr,
    'rack.multithread' => true,
    'rack.multiprocess' => true,
    'rack.run_once' => false,
    'mongrel2.pattern' => headers['PATTERN'],
    'REQUEST_METHOD' => headers['METHOD'],
    'SCRIPT_NAME' => script_name,
    'PATH_INFO' => headers['PATH'].gsub(script_name, ''),
    'QUERY_STRING' => headers['QUERY'] || '',
    'async.callback' => Proc.new { |resp|
      connection.method(:post_process).call(resp, self)
    },
    'async.close' => EM::DefaultDeferrable.new
  }
  
  @env['SERVER_NAME'], @env['SERVER_PORT'] = headers['host'].split(':', 2)
  @env['SERVER_PORT'] ||= '80'
  headers.each do |key, val|
    key = key.upcase.gsub('-', '_')
    unless %w[CONTENT_TYPE CONTENT_LENGTH].include?(key)
      key = "HTTP_#{key}"
    end
    @env[key] = val
  end

  @env
end