summary refs log tree commit homepage
path: root/t/close-pipe-to_path-response.ru
blob: abc3a37c09869ed3992084425dde79f1dfa96846 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# must be run without Rack::Lint since that clobbers to_path
class MyMiddleware < Struct.new(:app)
  class Body < Struct.new(:body, :to_path)
    def each(&block); body.each(&block); end
    def close
      c = body.respond_to?(:close)
      ::File.open(ENV['fifo'], 'wb') do |fp|
        fp.syswrite("CLOSING #{body.inspect} #{to_path} (#{c})\n")
      end
      body.close if c
    end
  end

  def call(env)
    status, headers, body = app.call(env)
    body.respond_to?(:to_path) and body = Body.new(body, body.to_path)
    [ status, headers, body ]
  end
end
use MyMiddleware
use Rainbows::DevFdResponse
run(lambda { |env|
  io = IO.popen('cat random_blob', 'rb')
  [ 200,
    {
      'Content-Length' => ::File.stat('random_blob').size.to_s,
      'Content-Type' => 'application/octet-stream',
    },
    io ]
})