From fd11f72b2f0550a5f630025d2606e5884e18f039 Mon Sep 17 00:00:00 2001 From: zedshaw Date: Wed, 15 Feb 2006 07:38:15 +0000 Subject: Fix for that same content-length bug, but now fixed by gutting CGI to use HttpResponse right. git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@38 19e92222-5c0b-0410-8929-a290d50e31e9 --- bin/mongrel_rails | 68 +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 41 insertions(+), 27 deletions(-) (limited to 'bin') diff --git a/bin/mongrel_rails b/bin/mongrel_rails index f36c48f..21492a9 100644 --- a/bin/mongrel_rails +++ b/bin/mongrel_rails @@ -8,44 +8,58 @@ require 'mongrel/command' class CGIFixed < ::CGI public :env_table attr_reader :options - - def initialize(params, data, out, *args) - @env_table = params + + def initialize(request, response, *args) + @request = request + @response = response @args = *args - @input = StringIO.new(data) - @out = out + @input = StringIO.new(request.body) @options = {} super(*args) end - + def header(options = "text/html") if options.class == Hash # passing in a header so need to keep the status around and other options - @options = options + @options = @options.merge(options) + else + @options["Content-Type"] = options end - super(options) + # doing this fakes out the cgi library to think the headers are empty + # we then do the real headers in the out function call later + "" end - def status - s = @options["Status"] || @options["status"] - s[0 .. s.index(' ')] || "200" + + def out(options = "text/html") + header(options) + @response.start status do |head, out| + @options.each {|k,v| head[k.capitalize] = v} + out.write(yield || "") + end end + # computes the status once, but lazily so that people who call header twice + # don't get penalized + def status + if not @status + @status = @options["Status"] || @options["status"] + + if @status + @status[0 ... @status.index(' ')] || "200" + else + @status = "200" + end + end + end + def args @args end def env_table - @env_table - end - - def stdinput - @input - end - - def stdoutput - @out + @request.params end end @@ -63,26 +77,26 @@ class RailsHandler < Mongrel::HttpHandler def process(request, response) # not static, need to talk to rails return if response.socket.closed? - + if @files.can_serve(request.params["PATH_INFO"]) @files.process(request,response) else - cgi = CGIFixed.new(request.params, request.body, response.socket) + cgi = CGIFixed.new(request, response) + begin - @guard.synchronize do # Rails is not thread safe so must be run entirely within synchronize Dispatcher.dispatch(cgi, ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, response.body) end - - response.status = cgi.status - response.send_status - response.send_body + + # This finalizes the output using the proper HttpResponse way + cgi.out {""} rescue Object => rails_error STDERR.puts "calling Dispatcher.dispatch #{rails_error}" STDERR.puts rails_error.backtrace.join("\n") end end + end end -- cgit v1.2.3-24-ge0c7