about summary refs log tree commit homepage
path: root/lib/rainbows
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rainbows')
-rw-r--r--lib/rainbows/dev_fd_response.rb4
-rw-r--r--lib/rainbows/response/body.rb8
2 files changed, 10 insertions, 2 deletions
diff --git a/lib/rainbows/dev_fd_response.rb b/lib/rainbows/dev_fd_response.rb
index 637bcc2..7f70b8e 100644
--- a/lib/rainbows/dev_fd_response.rb
+++ b/lib/rainbows/dev_fd_response.rb
@@ -14,7 +14,8 @@
 class Rainbows::DevFdResponse < Struct.new(:app)
 
   # :stopdoc:
-  #
+  FD_MAP = Rainbows::FD_MAP
+
   # make this a no-op under Rubinius, it's pointless anyways
   # since Rubinius doesn't have IO.copy_stream
   def self.new(app)
@@ -37,6 +38,7 @@ class Rainbows::DevFdResponse < Struct.new(:app)
     headers = HeaderHash.new(headers)
     st = io.stat
     fileno = io.fileno
+    FD_MAP[fileno] = io
     if st.file?
       headers['Content-Length'] ||= st.size.to_s
       headers.delete('Transfer-Encoding')
diff --git a/lib/rainbows/response/body.rb b/lib/rainbows/response/body.rb
index cf14f08..2535374 100644
--- a/lib/rainbows/response/body.rb
+++ b/lib/rainbows/response/body.rb
@@ -30,6 +30,12 @@
 module Rainbows::Response::Body # :nodoc:
   ALIASES = {}
 
+  FD_MAP = Rainbows::FD_MAP
+
+  def io_for_fd(fd)
+    FD_MAP.delete(fd) || IO.new(fd)
+  end
+
   # to_io is not part of the Rack spec, but make an exception here
   # since we can conserve path lookups and file descriptors.
   # \Rainbows! will never get here without checking for the existence
@@ -41,7 +47,7 @@ module Rainbows::Response::Body # :nodoc:
       # try to take advantage of Rainbows::DevFdResponse, calling File.open
       # is a last resort
       path = body.to_path
-      path =~ %r{\A/dev/fd/(\d+)\z} ? IO.new($1.to_i) : File.open(path)
+      path =~ %r{\A/dev/fd/(\d+)\z} ? io_for_fd($1.to_i) : File.open(path)
     end
   end