1 # Copyright (c) 2005 Zed A. Shaw
2 # You can redistribute it and/or modify it under the same terms as Ruby.
4 # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html
5 # for more information.
9 require 'action_controller/dispatcher'
14 # Implements a handler that can run Rails and serve files out of the
15 # Rails application's public directory. This lets you run your Rails
16 # application with Mongrel during development and testing, then use it
17 # also in production behind a server that's better at serving the
20 # The RailsHandler takes a mime_map parameter which is a simple suffix=mimetype
21 # mapping that it should add to the list of valid mime types.
23 # It also supports page caching directly and will try to resolve a request
24 # in the following order:
26 # * If the requested exact PATH_INFO exists as a file then serve it.
27 # * If it exists at PATH_INFO+".html" exists then serve that.
28 # * Finally, construct a Mongrel::CGIWrapper and run Dispatcher.dispatch to have Rails go.
30 # This means that if you are using page caching it will actually work with Mongrel
31 # and you should see a decent speed boost (but not as fast as if you use a static
32 # server like Apache or Litespeed).
33 class RailsHandler
< Mongrel
::HttpHandler
34 # Construct a Mongrel::CGIWrapper and dispatch.
35 def process(request
, response
)
36 return if response
.socket
.closed
?
38 cgi
= Mongrel
::CGIWrapper.new(request
, response
)
40 # We don't want the output to be really final until we're out of the lock
41 cgi
.default_really_final
= false
43 ActionController
::Dispatcher.dispatch(cgi
, ActionController
::CgiRequest::DEFAULT_SESSION_OPTIONS, response
.body
)
45 # This finalizes the output using the proper HttpResponse way
46 cgi
.out("text/html",true) {""}
49 rescue Object
=> rails_error
50 STDERR.puts
"#{Time.now.httpdate}: Error dispatching #{rails_error.inspect}"
51 STDERR.puts rails_error
.backtrace
.join("\n")