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