From d150cae4777ae8722397901eb64f1cd53a74aa91 Mon Sep 17 00:00:00 2001 From: zedshaw Date: Sat, 4 Mar 2006 17:55:39 +0000 Subject: Implemented access to the RailsHandler from inside rails. Closed off the remaining bugs on our tracker. git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@74 19e92222-5c0b-0410-8929-a290d50e31e9 --- lib/mongrel/cgi.rb | 17 ++++++++++++++++- lib/mongrel/rails.rb | 12 ++++++++---- 2 files changed, 24 insertions(+), 5 deletions(-) (limited to 'lib/mongrel') diff --git a/lib/mongrel/cgi.rb b/lib/mongrel/cgi.rb index 3d7d25a..14982e0 100644 --- a/lib/mongrel/cgi.rb +++ b/lib/mongrel/cgi.rb @@ -1,14 +1,28 @@ require 'cgi' module Mongrel - # The beginning of a complete wrapper around Mongrel's internal HTTP processing + # The beginning of a complete wrapper around Mongrel's internal HTTP processing # system but maintaining the original Ruby CGI module. Use this only as a crutch # to get existing CGI based systems working. It should handle everything, but please # notify me if you see special warnings. This work is still very alpha so I need # testers to help work out the various corner cases. + # + # The CGIWrapper.handler attribute is normally not set and is available for + # frameworks that need to get back to the handler. Rails uses this to give + # people access to the RailsHandler#files (DirHandler really) so they can + # look-up paths and do other things withthe files managed there. + # + # In Rails you can get the real file for a request with: + # + # path = @request.cgi.handler.files.can_serve(@request['PATH_INFO']) + # + # Which is ugly but does the job. Feel free to write a Rails helper for that. + # Refer to DirHandler#can_serve for more information on this. class CGIWrapper < ::CGI public :env_table attr_reader :options + attr_reader :handler + attr_writer :handler # these are stripped out of any keys passed to CGIWrapper.header function REMOVED_KEYS = [ "nph","status","server","connection","type", @@ -143,5 +157,6 @@ module Mongrel STDERR.puts "WARNING: Your program is doing something not expected. Please tell Zed that stdoutput was used and what software you are running. Thanks." @response.body end + end end diff --git a/lib/mongrel/rails.rb b/lib/mongrel/rails.rb index 90a1165..5b12945 100644 --- a/lib/mongrel/rails.rb +++ b/lib/mongrel/rails.rb @@ -19,7 +19,11 @@ require 'mongrel' # # This means that if you are using page caching it will actually work with Mongrel # and you should see a decent speed boost (but not as fast as if you use lighttpd). +# +# An additional feature you can use is class RailsHandler < Mongrel::HttpHandler + attr_reader :files + def initialize(dir, mime_map = {}) @files = Mongrel::DirHandler.new(dir,false) @guard = Mutex.new @@ -37,19 +41,19 @@ class RailsHandler < Mongrel::HttpHandler def process(request, response) return if response.socket.closed? - path_info = request.params["PATH_INFO"] - page_cached = request.params["PATH_INFO"] + ".html" + path_info = request.params[Mongrel::Const::PATH_INFO] + page_cached = request.params[Mongrel::Const::PATH_INFO] + ".html" if @files.can_serve(path_info) # File exists as-is so serve it up @files.process(request,response) elsif @files.can_serve(page_cached) # possible cached page, serve it up - request.params["PATH_INFO"] = page_cached + request.params[Mongrel::Const::PATH_INFO] = page_cached @files.process(request,response) else cgi = Mongrel::CGIWrapper.new(request, response) - + cgi.handler = self begin @guard.synchronize do # Rails is not thread safe so must be run entirely within synchronize -- cgit v1.2.3-24-ge0c7