middleware/proxy: favor __send__ for method dispatch
[raindrops.git] / lib / raindrops / middleware / proxy.rb
blob1cf437c2f0db370dc53f9a2173eb73178ee32701
1 # -*- encoding: binary -*-
2 # :stopdoc:
3 # This class is used by Raindrops::Middleware to proxy application
4 # response bodies.  There should be no need to use it directly.
5 class Raindrops::Middleware::Proxy
6   def initialize(body, stats)
7     @body, @stats = body, stats
8   end
10   # yield to the Rack server here for writing
11   def each
12     @body.each { |x| yield x }
13   end
15   # the Rack server should call this after #each (usually ensure-d)
16   def close
17     @stats.decr_writing
18     @body.close if @body.respond_to?(:close)
19   end
21   # Some Rack servers can optimize response processing if it responds
22   # to +to_path+ via the sendfile(2) system call, we proxy +to_path+
23   # to the underlying body if possible.
24   def to_path
25     @body.to_path
26   end
28   # Rack servers use +respond_to?+ to check for the presence of +close+
29   # and +to_path+ methods.
30   def respond_to?(m)
31     m = m.to_sym
32     :close == m || @body.respond_to?(m)
33   end
35   # Avoid breaking users of non-standard extensions (e.g. #body)
36   # Rack::BodyProxy does the same.
37   def method_missing(*args, &block)
38     @body.__send__(*args, &block)
39   end
40 end