dev_fd_response: garbage reduction
[rainbows.git] / lib / rainbows / dev_fd_response.rb
blob1ee337583fab2d124c329321bc787d886ef288ea
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
18   Content_Length = "Content-Length".freeze
19   Transfer_Encoding = "Transfer-Encoding".freeze
20   Rainbows_autochunk = "rainbows.autochunk".freeze
21   Rainbows_model = "rainbows.model"
22   HTTP_1_0 = "HTTP/1.0"
23   HTTP_VERSION = "HTTP_VERSION"
24   Chunked = "chunked"
26   # make this a no-op under Rubinius, it's pointless anyways
27   # since Rubinius doesn't have IO.copy_stream
28   def self.new(app)
29     app
30   end if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'rbx'
31   include Rack::Utils
33   # Rack middleware entry point, we'll just pass through responses
34   # unless they respond to +to_io+ or +to_path+
35   def call(env)
36     status, headers, body = response = app.call(env)
38     # totally uninteresting to us if there's no body
39     if STATUS_WITH_NO_ENTITY_BODY.include?(status.to_i) ||
40        File === body ||
41        (body.respond_to?(:to_path) && File.file?(body.to_path))
42       return response
43     end
45     io = body.to_io if body.respond_to?(:to_io)
46     io ||= File.open(body.to_path) if body.respond_to?(:to_path)
47     return response if io.nil?
49     headers = Rack::Utils::HeaderHash.new(headers) unless Hash === headers
50     st = io.stat
51     fileno = io.fileno
52     FD_MAP[fileno] = io
53     if st.file?
54       headers[Content_Length] ||= st.size.to_s
55       headers.delete(Transfer_Encoding)
56     elsif st.pipe? || st.socket? # epoll-able things
57       unless headers.include?(Content_Length)
58         if env[Rainbows_autochunk] && HTTP_1_0 != env[HTTP_VERSION]
59           headers[Transfer_Encoding] = Chunked
60         else
61           env[Rainbows_autochunk] = false
62         end
63       end
65       # we need to make sure our pipe output is Fiber-compatible
66       case env[Rainbows_model]
67       when :FiberSpawn, :FiberPool, :RevFiberSpawn, :CoolioFiberSpawn
68         io.respond_to?(:kgio_wait_readable) or
69           io = Rainbows::Fiber::IO.new(io)
70       when :Revactor
71         io = Rainbows::Revactor::Proxy.new(io)
72       end
73     else # unlikely, char/block device file, directory, ...
74       return response
75     end
76     [ status, headers, Body.new(io, "/dev/fd/#{fileno}", body) ]
77   end
79   class Body < Struct.new(:to_io, :to_path, :orig_body)
80     # called by the webserver or other middlewares if they can't
81     # handle #to_path
82     def each(&block)
83       to_io.each(&block)
84     end
86     # remain Rack::Lint-compatible for people with wonky systems :P
87     unless File.directory?("/dev/fd")
88       alias to_path_orig to_path
89       undef_method :to_path
90     end
92     # called by the web server after #each
93     def close
94       to_io.close unless to_io.closed?
95       orig_body.close if orig_body.respond_to?(:close) # may not be an IO
96     rescue IOError # could've been IO::new()'ed and closed
97     end
98   end
99   #:startdoc:
100 end # class