doc: switch bogomips.org to yhbt.net
[rainbows.git] / t / close-has-env.ru
blob3b6ec88913a88f123fe919bdabe5788149d11975
1 #\ -E none
2 use Rainbows::DevFdResponse
3 class ClosablePipe < ::IO
4   attr_accessor :env
6   def self.new(env)
7     rv = popen "echo hello", "rb"
8     rv.env = env
9     rv
10   end
12   def close
13     return if closed? # idempotent for Ruby 2.3.0 compatibility
14     super
15     $stdout.syswrite "path_info=#{@env['PATH_INFO']}\n"
16   end
17 end
19 class ClosableFile < ::File
20   attr_accessor :env
21   alias to_path path
22   def close
23     super
24     $stdout.syswrite "path_info=#{@env['PATH_INFO']}\n"
25   end
26 end
28 class Blob
29   def initialize(env)
30     @env = env
31   end
33   def each(&block)
34     yield "BLOB\n"
35   end
37   def close
38     $stdout.syswrite "path_info=#{@env['PATH_INFO']}\n"
39   end
40 end
42 run(lambda { |env|
43   case env["PATH_INFO"]
44   when %r{\A/pipe/}
45     [ 200,
46       [ %w(Content-Length 6), %w(Content-Type text/plain)],
47       ClosablePipe.new(env)
48     ]
49   when %r{\A/file/}
50     f = ClosableFile.open("env.ru", "rb")
51     f.env = env
52     [ 200, {
53       'X-Req-Path' => env["PATH_INFO"],
54       'Content-Length' => f.stat.size.to_s,
55       'Content-Type' => 'text/plain' },
56       f
57     ]
58   when %r{\A/blob/}
59     [ 200,
60       [%w(Content-Length 5), %w(Content-Type text/plain)],
61       Blob.new(env)
62     ]
63   else
64     [ 404, [%w(Content-Length 0), %w(Content-Type text/plain)], [] ]
65   end