Class: M2R::RackHandler

Inherits:
Handler show all
Defined in:
lib/m2r/rack_handler.rb

Overview

Handle Mongrel2 requests using Rack application

Instance Attribute Summary collapse

Attributes inherited from Handler

#connection

Instance Method Summary collapse

Methods inherited from Handler

#after_process, #after_reply, #listen, #on_disconnect, #on_request, #on_upload_done, #on_upload_start, #on_wait, #stop

Constructor Details

#initialize(app, connection_factory) ⇒ RackHandler

Returns a new instance of RackHandler.



10
11
12
13
14
15
# File 'lib/m2r/rack_handler.rb', line 10

def initialize(app, connection_factory)
  @app = app
  super(connection_factory)

  trap('INT') { stop }
end

Instance Attribute Details

#appObject



8
9
10
# File 'lib/m2r/rack_handler.rb', line 8

def app
  @app
end

Instance Method Details

#after_all(request, response) ⇒ Object



42
43
44
# File 'lib/m2r/rack_handler.rb', line 42

def after_all(request, response)
  request.free!
end

#process(request) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/m2r/rack_handler.rb', line 17

def process(request)
  script_name = request.pattern.split('(', 2).first.gsub(/\/$/, '')

  env = {
    'REQUEST_METHOD'    => request.method,
    'SCRIPT_NAME'       => script_name,
    'PATH_INFO'         => request.path.gsub(script_name, ''),
    'QUERY_STRING'      => request.query || "",
    'rack.version'      => ::Rack::VERSION,
    'rack.errors'       => $stderr,
    'rack.multithread'  => false,
    'rack.multiprocess' => true,
    'rack.run_once'     => false,
    'rack.url_scheme'   => request.scheme,
    'rack.input'        => request.body_io
  }
  env['SERVER_NAME'], env['SERVER_PORT'] = request.headers['Host'].split(':', 2)
  request.headers.rackify(env)

  status, headers, body = @app.call(env)
  buffer = ""
  body.each { |part| buffer << part }
  return Response.new(status, headers, buffer)
end