quiet mismatched indentation warnings
[rainbows.git] / t / hijack.ru
blob16b5f38b9dfe4e65947829aff5b004c83f284b16
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 def lazy_close(io)
14   thr = Thread.new do
15     # wait and see if Rainbows! accidentally closes us
16     sleep((ENV["DELAY"] || 10).to_i)
17     begin
18       io.close
19     rescue => e
20       warn "E: #{e.message} (#{e.class})"
21       exit!(3)
22     end
23   end
24   at_exit { thr.join }
25 end
27 run lambda { |env|
28   case env["PATH_INFO"]
29   when "/hijack_req"
30     if env["rack.hijack?"]
31       io = env["rack.hijack"].call
32       if io.respond_to?(:read_nonblock) &&
33          env["rack.hijack_io"].respond_to?(:read_nonblock)
35         # exercise both, since we Rack::Lint may use different objects
36         env["rack.hijack_io"].write("HTTP/1.0 200 OK\r\n\r\n")
37         io.write("request.hijacked")
38         lazy_close(io)
39         return [ 500, {}, DieIfUsed.new ]
40       end
41     end
42     [ 500, {}, [ "hijack BAD\n" ] ]
43   when "/hijack_res"
44     r = "response.hijacked"
45     [ 200,
46       {
47         "Content-Length" => r.bytesize.to_s,
48         "rack.hijack" => proc do |sock|
49           io.write(r)
50           lazy_close(sock)
51         end
52       },
53       DieIfUsed.new
54     ]
55   end