Class: Merb::Rack::Handler::Mongrel

Inherits:
Mongrel::HttpHandler
  • Object
show all
Defined in:
lib/merb-core/rack/handler/mongrel.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Mongrel

Parameters

app<Merb::Rack::Application>

The app that Mongrel should handle.



44
45
46
# File 'lib/merb-core/rack/handler/mongrel.rb', line 44

def initialize(app)
  @app = app
end

Class Method Details

.run(app, options = {}) {|server| ... } ⇒ Object

Runs the server and yields it to a block.

Parameters

app<Merb::Rack::Application>

The app that Mongrel should handle.

options<Hash>

Options to pass to Mongrel (see below).

Block parameters

server<Mongrel::HttpServer>

The server to run.

Options (options)

:Host<String>

The hostname on which the app should run. Defaults to “0.0.0.0”

:Port<Fixnum>

The port for the app. Defaults to 8080.

Yields:

  • (server)


34
35
36
37
38
39
40
# File 'lib/merb-core/rack/handler/mongrel.rb', line 34

def self.run(app, options={})
  server = ::Mongrel::HttpServer.new(options[:Host] || '0.0.0.0',
                                     options[:Port] || 8080)
  server.register('/', ::Merb::Rack::Handler::Mongrel.new(app))
  yield server  if block_given?
  server.run.join
end

Instance Method Details

#process(request, response) ⇒ Object

Parameters

request<Merb::Request>

The HTTP request to handle.

response<HTTPResponse>

The response object to write response to.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/merb-core/rack/handler/mongrel.rb', line 51

def process(request, response)
  env = {}.replace(request.params)
  env.delete "HTTP_CONTENT_TYPE"
  env.delete "HTTP_CONTENT_LENGTH"
  
  env["SCRIPT_NAME"] = ""  if env["SCRIPT_NAME"] == "/"
  
  env.update({"rack.version" => [0,1],
               "rack.input" => request.body || StringIO.new(""),
               "rack.errors" => STDERR,
  
               "rack.multithread" => true,
               "rack.multiprocess" => false, # ???
               "rack.run_once" => false,
  
               "rack.url_scheme" => "http",
               "rack.streaming" => true
             })
  env["QUERY_STRING"] ||= ""
  env.delete "PATH_INFO"  if env["PATH_INFO"] == ""
  
  status, headers, body = @app.call(env)
  
  begin
    response.status = status.to_i
    headers.each { |k, vs|
      vs.each { |v|
        response.header[k] = v
      }
    }
    
    if Proc === body
      body.call(response)
    else  
      body.each { |part|
        response.body << part
      }
    end
    response.finished
  ensure
    body.close  if body.respond_to? :close
  end
end