diff options
author | zedshaw <zedshaw@19e92222-5c0b-0410-8929-a290d50e31e9> | 2006-03-28 02:06:50 +0000 |
---|---|---|
committer | zedshaw <zedshaw@19e92222-5c0b-0410-8929-a290d50e31e9> | 2006-03-28 02:06:50 +0000 |
commit | e0860470ca8f8971d702a2fc23e2da6c6d446bb8 (patch) | |
tree | 8f65f1f1b405dd133ead31e855fce933181009df /lib | |
parent | c43250fb4073ebf767cdcafc8a7f9036159367c2 (diff) | |
download | unicorn-e0860470ca8f8971d702a2fc23e2da6c6d446bb8.tar.gz |
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@127 19e92222-5c0b-0410-8929-a290d50e31e9
Diffstat (limited to 'lib')
-rw-r--r-- | lib/mongrel.rb | 5 | ||||
-rw-r--r-- | lib/mongrel/handlers.rb | 29 | ||||
-rw-r--r-- | lib/mongrel/rails.rb | 2 |
3 files changed, 23 insertions, 13 deletions
diff --git a/lib/mongrel.rb b/lib/mongrel.rb index 19edf1e..c2dfc3b 100644 --- a/lib/mongrel.rb +++ b/lib/mongrel.rb @@ -281,9 +281,10 @@ module Mongrel end end - def send_status + def send_status(content_length=nil) if not @status_sent - status = "HTTP/1.1 #{@status} #{HTTP_STATUS_CODES[@status]}\r\nContent-Length: #{@body.length}\r\nConnection: close\r\n" + content_length ||= @body.length + status = "HTTP/1.1 #{@status} #{HTTP_STATUS_CODES[@status]}\r\nContent-Length: #{content_length}\r\nConnection: close\r\n" @socket.write(status) @status_sent = true end diff --git a/lib/mongrel/handlers.rb b/lib/mongrel/handlers.rb index d0de0ca..b0f77a8 100644 --- a/lib/mongrel/handlers.rb +++ b/lib/mongrel/handlers.rb @@ -157,7 +157,7 @@ module Mongrel # Sends the contents of a file back to the user. Not terribly efficient since it's # opening and closing the file for each read. - def send_file(req, response) + def send_file(req, response, header_only=false) # first we setup the headers and status then we do a very fast send on the socket directly response.status = 200 @@ -171,15 +171,19 @@ module Mongrel end end - response.header['Content-Length'] = File.size(req) - - response.send_status + response.send_status(File.size(req)) response.send_header - if $mongrel_has_sendfile - File.open(req, "rb") { |f| response.socket.sendfile(f) } - else - File.open(req, "rb") { |f| response.socket.write(f.read) } + if not header_only + begin + if $mongrel_has_sendfile + File.open(req, "rb") { |f| response.socket.sendfile(f) } + else + File.open(req, "rb") { |f| response.socket.write(f.read) } + end + rescue Errno::EINVAL + # ignore these since it means the client closed off early on win32 + end end end @@ -187,6 +191,7 @@ module Mongrel # Process the request to either serve a file or a directory listing # if allowed (based on the listing_allowed paramter to the constructor). def process(request, response) + req_method = request.params['REQUEST_METHOD'] || "GET" req = can_serve request.params['PATH_INFO'] if not req # not found, return a 404 @@ -197,8 +202,12 @@ module Mongrel begin if File.directory? req send_dir_listing(request.params["REQUEST_URI"],req, response) - else - send_file(req, response) + elsif req_method == "HEAD" + send_file(req, response, true) + elsif req_method == "GET" + send_file(req, response, false) + else + response.start(403) {|head,out| out.write("Only HEAD and GET allowed.") } end rescue => details STDERR.puts "Error accessing file: #{details}" diff --git a/lib/mongrel/rails.rb b/lib/mongrel/rails.rb index d2c714c..b0d3ba2 100644 --- a/lib/mongrel/rails.rb +++ b/lib/mongrel/rails.rb @@ -131,7 +131,7 @@ module Mongrel $orig_dollar_quote = $".clone ENV['RAILS_ENV'] = ops[:environment] - require 'config/environment' + require '#{ops[:docroot]}/config/environment' require 'dispatcher' require 'mongrel/rails' |