coolio: rename deferred_response => response_pipe
[rainbows.git] / lib / rainbows / dev_fd_response.rb
blob2f7b1cf9cd8814331473719709b81636a428c71a
1 # -*- encoding: binary -*-
3 # Rack response middleware wrapping any IO-like object with an
4 # OS-level file descriptor associated with it.  May also be used to
5 # create responses from integer file descriptors or existing +IO+
6 # objects.  This may be used in conjunction with the #to_path method
7 # on servers that support it to pass arbitrary file descriptors into
8 # the HTTP response without additional open(2) syscalls
10 # This middleware is currently a no-op for Rubinius, as it lacks
11 # IO.copy_stream in 1.9 and also due to a bug here:
12 #   http://github.com/evanphx/rubinius/issues/379
14 class Rainbows::DevFdResponse < Struct.new(:app)
16   # :stopdoc:
17   FD_MAP = Rainbows::FD_MAP
19   # make this a no-op under Rubinius, it's pointless anyways
20   # since Rubinius doesn't have IO.copy_stream
21   def self.new(app)
22     app
23   end if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
24   include Rack::Utils
26   # Rack middleware entry point, we'll just pass through responses
27   # unless they respond to +to_io+ or +to_path+
28   def call(env)
29     status, headers, body = response = app.call(env)
31     # totally uninteresting to us if there's no body
32     if STATUS_WITH_NO_ENTITY_BODY.include?(status.to_i) ||
33        File === body ||
34        (body.respond_to?(:to_path) && File.file?(body.to_path))
35       return response
36     end
38     io = body.to_io if body.respond_to?(:to_io)
39     io ||= File.open(body.to_path) if body.respond_to?(:to_path)
40     return response if io.nil?
42     headers = HeaderHash.new(headers)
43     st = io.stat
44     fileno = io.fileno
45     FD_MAP[fileno] = io
46     if st.file?
47       headers['Content-Length'] ||= st.size.to_s
48       headers.delete('Transfer-Encoding')
49     elsif st.pipe? || st.socket? # epoll-able things
50       unless headers['Content-Length']
51         if env['rainbows.autochunk']
52           headers['Transfer-Encoding'] = 'chunked'
53         else
54           headers['X-Rainbows-Autochunk'] = 'no'
55         end
56       end
58       # we need to make sure our pipe output is Fiber-compatible
59       case env["rainbows.model"]
60       when :FiberSpawn, :FiberPool, :RevFiberSpawn, :CoolioFiberSpawn
61         io.respond_to?(:kgio_wait_readable) or
62           io = Rainbows::Fiber::IO.new(io)
63       when :Revactor
64         io = Rainbows::Revactor::Proxy.new(io)
65       end
66     else # unlikely, char/block device file, directory, ...
67       return response
68     end
69     [ status, headers, Body.new(io, "/dev/fd/#{fileno}", body) ]
70   end
72   class Body < Struct.new(:to_io, :to_path, :orig_body)
73     # called by the webserver or other middlewares if they can't
74     # handle #to_path
75     def each(&block)
76       to_io.each(&block)
77     end
79     # remain Rack::Lint-compatible for people with wonky systems :P
80     unless File.directory?("/dev/fd")
81       alias to_path_orig to_path
82       undef_method :to_path
83     end
85     # called by the web server after #each
86     def close
87       to_io.close unless to_io.closed?
88       orig_body.close if orig_body.respond_to?(:close) # may not be an IO
89     rescue IOError # could've been IO::new()'ed and closed
90     end
91   end
92   #:startdoc:
93 end # class