about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-07-19 10:10:06 +0000
committerEric Wong <normalperson@yhbt.net>2010-07-19 17:04:37 -0700
commit60039518e03b0f1a0f530eefe008ebf72c55afe4 (patch)
treec31a8ff013634812ae71f7c935d309c652d5052a /lib
parent0cd65fa1e01be369b270c72053cf21a3d6bcb45f (diff)
downloadrainbows-60039518e03b0f1a0f530eefe008ebf72c55afe4.tar.gz
Middlewares like Clogger may wrap Rack::File responses
with another body that responds to to_path and still
rely on #close to trigger an action (writing out the log
file).
Diffstat (limited to 'lib')
-rw-r--r--lib/rainbows/event_machine.rb5
-rw-r--r--lib/rainbows/rev/client.rb3
-rw-r--r--lib/rainbows/rev/sendfile.rb12
-rw-r--r--lib/rainbows/stream_file.rb5
4 files changed, 10 insertions, 15 deletions
diff --git a/lib/rainbows/event_machine.rb b/lib/rainbows/event_machine.rb
index 4faa7a6..696f5a0 100644
--- a/lib/rainbows/event_machine.rb
+++ b/lib/rainbows/event_machine.rb
@@ -121,7 +121,10 @@ module Rainbows
           if st.file?
             write(response_header(status, headers)) if headers
             @body = stream = stream_file_data(body.to_path)
-            stream.callback { quit } unless alive
+            stream.callback do
+              body.close if body.respond_to?(:close)
+              quit unless alive
+            end
             return
           elsif st.socket? || st.pipe?
             chunk = stream_response_headers(status, headers) if headers
diff --git a/lib/rainbows/rev/client.rb b/lib/rainbows/rev/client.rb
index 4d1aaec..502615e 100644
--- a/lib/rainbows/rev/client.rb
+++ b/lib/rainbows/rev/client.rb
@@ -8,6 +8,7 @@ module Rainbows
       include Rainbows::ByteSlice
       include Rainbows::EvCore
       G = Rainbows::G
+      F = Rainbows::StreamFile
 
       def initialize(io)
         CONN[self] = false
@@ -80,7 +81,7 @@ module Rainbows
 
           if st.file?
             write(response_header(status, headers)) if headers
-            return defer_body(to_sendfile(io))
+            return defer_body(F.new(0, io, body))
           elsif st.socket? || st.pipe?
             return stream_response(status, headers, io, body)
           end
diff --git a/lib/rainbows/rev/sendfile.rb b/lib/rainbows/rev/sendfile.rb
index 414cfa1..9f421f1 100644
--- a/lib/rainbows/rev/sendfile.rb
+++ b/lib/rainbows/rev/sendfile.rb
@@ -2,12 +2,6 @@
 # :enddoc:
 module Rainbows::Rev::Sendfile
   if IO.method_defined?(:sendfile_nonblock)
-    F = Rainbows::StreamFile
-
-    def to_sendfile(io)
-      F[0, io]
-    end
-
     def rev_sendfile(body)
       body.offset += @_io.sendfile_nonblock(body, body.offset, 0x10000)
       enable_write_watcher
@@ -15,12 +9,8 @@ module Rainbows::Rev::Sendfile
         enable_write_watcher
     end
   else
-    def to_sendfile(io)
-      io
-    end
-
     def rev_sendfile(body)
-      write(body.sysread(0x4000))
+      write(body.to_io.sysread(0x4000))
     end
   end
 end
diff --git a/lib/rainbows/stream_file.rb b/lib/rainbows/stream_file.rb
index f533f18..dec58b0 100644
--- a/lib/rainbows/stream_file.rb
+++ b/lib/rainbows/stream_file.rb
@@ -5,10 +5,11 @@
 # models.  We always maintain our own file offsets in userspace because
 # because sendfile() implementations offer pread()-like idempotency for
 # concurrency (multiple clients can read the same underlying file handle).
-class Rainbows::StreamFile < Struct.new(:offset, :to_io)
+class Rainbows::StreamFile < Struct.new(:offset, :to_io, :body)
 
   def close
-    to_io.close
+    body.close if body.respond_to?(:close)
+    to_io.close unless to_io.closed?
     self.to_io = nil
   end
 end