105e0d700a39b5b6eb3e88af7ae753a12becca1d
[unicorn.git] / t / hijack.ru
blob105e0d700a39b5b6eb3e88af7ae753a12becca1d
1 use Rack::Lint
2 use Rack::ContentLength
3 use Rack::ContentType, "text/plain"
4 class DieIfUsed
5   def each
6     abort "body.each called after response hijack\n"
7   end
9   def close
10     abort "body.close called after response hijack\n"
11   end
12 end
13 run lambda { |env|
14   case env["PATH_INFO"]
15   when "/hijack_req"
16     if env["rack.hijack?"]
17       io = env["rack.hijack"].call
18       if io.respond_to?(:read_nonblock) &&
19          env["rack.hijack_io"].respond_to?(:read_nonblock)
20         return [ 200, {}, [ "hijack.OK\n" ] ]
21       end
22     end
23     [ 500, {}, [ "hijack BAD\n" ] ]
24   when "/hijack_res"
25     r = "response.hijacked"
26     [ 200,
27       {
28         "Content-Length" => r.bytesize.to_s,
29         "rack.hijack" => proc do |io|
30           io.write(r)
31           io.close
32         end
33       },
34       DieIfUsed.new
35     ]
36   end