avoid reusing env on hijack
[unicorn.git] / t / hijack.ru
blob02260e24e3066ba097156b108d75d6027dd7e5fd
1 use Rack::Lint
2 use Rack::ContentLength
3 use Rack::ContentType, "text/plain"
4 class DieIfUsed
5   @@n = 0
6   def each
7     abort "body.each called after response hijack\n"
8   end
10   def close
11     warn "closed DieIfUsed #{@@n += 1}\n"
12   end
13 end
15 envs = []
17 run lambda { |env|
18   case env["PATH_INFO"]
19   when "/hijack_req"
20     if env["rack.hijack?"]
21       io = env["rack.hijack"].call
22       envs << env
23       if io.respond_to?(:read_nonblock) &&
24          env["rack.hijack_io"].respond_to?(:read_nonblock)
26         # exercise both, since we Rack::Lint may use different objects
27         env["rack.hijack_io"].write("HTTP/1.0 200 OK\r\n\r\n")
28         io.write("request.hijacked")
29         io.close
30         return [ 500, {}, DieIfUsed.new ]
31       end
32     end
33     [ 500, {}, [ "hijack BAD\n" ] ]
34   when "/hijack_res"
35     r = "response.hijacked"
36     [ 200,
37       {
38         "Content-Length" => r.bytesize.to_s,
39         "rack.hijack" => proc do |io|
40           envs << env
41           io.write(r)
42           io.close
43         end
44       },
45       DieIfUsed.new
46     ]
47   when "/normal_env_id"
48     b = "#{env.object_id}\n"
49     h = {
50       'Content-Type' => 'text/plain',
51       'Content-Length' => b.bytesize.to_s,
52     }
53     [ 200, h, [ b ] ]
54   end