about summary refs log tree commit homepage
path: root/lib/mongrel.rb
diff options
context:
space:
mode:
authorwhy <why@19e92222-5c0b-0410-8929-a290d50e31e9>2006-04-11 00:00:52 +0000
committerwhy <why@19e92222-5c0b-0410-8929-a290d50e31e9>2006-04-11 00:00:52 +0000
commit3cf03ae9f3f12c5dd754f4d02cd9086fe804e0c2 (patch)
tree2446e1c29383603bb1fdc9d9c1d44990f421dfe2 /lib/mongrel.rb
parent894f4a0bd3407d99b779e739ef3c8c42c64bd4b1 (diff)
downloadunicorn-3cf03ae9f3f12c5dd754f4d02cd9086fe804e0c2.tar.gz
git-svn-id: svn+ssh://rubyforge.org/var/svn/mongrel/trunk@155 19e92222-5c0b-0410-8929-a290d50e31e9
Diffstat (limited to 'lib/mongrel.rb')
-rw-r--r--lib/mongrel.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/mongrel.rb b/lib/mongrel.rb
index 830aba1..228fd9f 100644
--- a/lib/mongrel.rb
+++ b/lib/mongrel.rb
@@ -9,6 +9,15 @@ require 'mongrel/tcphack'
 require 'yaml'
 require 'time'
 
+begin
+  require 'rubygems'
+  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
+
 # Mongrel module containing all of the classes (include C extensions) for running
 # a Mongrel web server.  It contains a minimalist HTTP server with just enough
 # functionality to service web application requests fast as possible.
@@ -324,6 +333,25 @@ module Mongrel
       end
     end
 
+    # Appends the contents of +path+ to the response stream.  The file is opened for binary
+    # reading and written in chunks to the socket.  If the
+    # <a href="http://rubyforge.org/projects/ruby-sendfile">sendfile</a> library is found,
+    # it is used to send the file, often with greater speed and less memory/cpu usage.
+    def send_file(path)
+      File.open(path, "rb") do |f|
+        if @socket.respond_to? :sendfile
+          @socket.sendfile(f)
+        else
+          while chunk = f.read(Const::CHUNK_SIZE)
+            @socket.write(chunk)
+          end
+        end
+          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
+
     def write(data)
       @socket.write(data)
     end