about summary refs log tree commit homepage
path: root/lib/mongrel
diff options
context:
space:
mode:
Diffstat (limited to 'lib/mongrel')
-rw-r--r--lib/mongrel/camping.rb19
-rw-r--r--lib/mongrel/handlers.rb23
2 files changed, 15 insertions, 27 deletions
diff --git a/lib/mongrel/camping.rb b/lib/mongrel/camping.rb
index 26546c1..fb4efb8 100644
--- a/lib/mongrel/camping.rb
+++ b/lib/mongrel/camping.rb
@@ -34,21 +34,30 @@ module Mongrel
       def process(request, response)
         req = StringIO.new(request.body)
         controller = @klass.run(req, request.params)
+        sendfile = nil
         response.start(controller.status) do |head,out|
           controller.headers.each do |k, v|
-            [*v].each do |vi|
-              head[k] = vi
+            if k =~ /^X-SENDFILE$/i
+              sendfile = v
+            else
+              [*v].each do |vi|
+                head[k] = vi
+              end
             end
           end
-          if controller.body.respond_to? :read
+          response.send_header
+
+          if sendfile
+            response.send_file(sendfile)
+          elsif controller.body.respond_to? :read
             while chunk = controller.body.read(16384)
-              out << chunk
+              @response.write(chunk)
             end
             if controller.body.respond_to? :close
               controller.body.close
             end
           else
-            out << controller.body
+            @response.write(controller.body)
           end
         end
       end
diff --git a/lib/mongrel/handlers.rb b/lib/mongrel/handlers.rb
index 6496d90..f9ad95f 100644
--- a/lib/mongrel/handlers.rb
+++ b/lib/mongrel/handlers.rb
@@ -1,12 +1,3 @@
-require 'rubygems'
-begin
-  require 'sendfile'
-  $mongrel_has_sendfile = true
-  STDERR.puts "** You have sendfile installed, will use that to serve files."
-rescue Object
-  $mongrel_has_sendfile = false
-end
-
 module Mongrel
 
   # You implement your application handler with this.  It's very light giving
@@ -188,22 +179,10 @@ module Mongrel
       response.send_header
 
       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 EOFError,Errno::ECONNRESET,Errno::EPIPE,Errno::EINVAL,Errno::EBADF
-          # ignore these since it means the client closed off early
-          STDERR.puts "Client closed socket requesting file #{req}: #$!"
-        end
-      else
-        response.send_body # should send nothing
+        response.send_file(req)
       end
     end
 
-
     # 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)