Froze rails gems
[depot.git] / vendor / rails / railties / lib / rails / mongrel_server / handler.rb
1 # Copyright (c) 2005 Zed A. Shaw
2 # You can redistribute it and/or modify it under the same terms as Ruby.
3 #
4 # Additional work donated by contributors. See http://mongrel.rubyforge.org/attributions.html
5 # for more information.
6
7 require 'mongrel'
8 require 'cgi'
9 require 'action_controller/dispatcher'
10
11
12 module Rails
13 module MongrelServer
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
18 # static files.
19 #
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.
22 #
23 # It also supports page caching directly and will try to resolve a request
24 # in the following order:
25 #
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.
29 #
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?
37
38 cgi = Mongrel::CGIWrapper.new(request, response)
39 cgi.handler = self
40 # We don't want the output to be really final until we're out of the lock
41 cgi.default_really_final = false
42
43 ActionController::Dispatcher.dispatch(cgi, ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, response.body)
44
45 # This finalizes the output using the proper HttpResponse way
46 cgi.out("text/html",true) {""}
47 rescue Errno::EPIPE
48 response.socket.close
49 rescue Object => rails_error
50 STDERR.puts "#{Time.now.httpdate}: Error dispatching #{rails_error.inspect}"
51 STDERR.puts rails_error.backtrace.join("\n")
52 end
53 end
54 end
55 end