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 --- Rakefile | 2 +- bin/mongrel_rails_service | 2 +- lib/mongrel.rb | 4 +++- lib/mongrel/cgi.rb | 17 ++++++++++++++++- lib/mongrel/rails.rb | 12 ++++++++---- 5 files changed, 29 insertions(+), 8 deletions(-) diff --git a/Rakefile b/Rakefile index 9e56614..dba96a7 100644 --- a/Rakefile +++ b/Rakefile @@ -30,7 +30,7 @@ end setup_extension("http11", "http11") -version="0.3.7" +version="0.3.7.1" summary = "A small fast HTTP library and server that runs Rails, Camping, and Nitro apps." test_file = "test/test_ws.rb" author="Zed A. Shaw" diff --git a/bin/mongrel_rails_service b/bin/mongrel_rails_service index 38cd71a..79fecdb 100644 --- a/bin/mongrel_rails_service +++ b/bin/mongrel_rails_service @@ -20,7 +20,7 @@ module GenericCommand def validate valid? @svc_name != nil, "You must specify the service name to be uninstalled." - # We should validate service existance here, right Zed? + # Validate that the service exists begin valid? Service.exists?(@svc_name), "There is no service with that name, cannot proceed." rescue diff --git a/lib/mongrel.rb b/lib/mongrel.rb index 7af5df4..7bc9017 100644 --- a/lib/mongrel.rb +++ b/lib/mongrel.rb @@ -140,7 +140,7 @@ module Mongrel SERVER_SOFTWARE='SERVER_SOFTWARE' # Current Mongrel version (used for SERVER_SOFTWARE and other response headers). - MONGREL_VERSION='Mongrel 0.3.7' + MONGREL_VERSION='Mongrel 0.3.8' # The standard empty 404 response for bad requests. Use Error4040Handler for custom stuff. ERROR_404_RESPONSE="HTTP/1.1 404 Not Found\r\nConnection: close\r\nServer: #{MONGREL_VERSION}\r\n\r\nNOT FOUND" @@ -445,6 +445,8 @@ module Mongrel end @acceptor.priority = 1 + + return @acceptor end 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