1 # -*- encoding: binary -*-
3 # Copyright (c) 2009 Eric Wong
4 # You can redistribute it and/or modify it under the same terms as Ruby 1.8 or
5 # the GPLv2+ (GPLv3+ preferred)
7 # this class *must* be used with Rack::Chunked
9 class Inetd < Struct.new(:cmd)
11 class CatBody < Struct.new(:errors, :err_rd, :out_rd, :pid_map)
12 def initialize(env, cmd)
13 self.errors = env['rack.errors']
14 in_rd, in_wr = IO.pipe
15 self.err_rd, err_wr = IO.pipe
16 self.out_rd, out_wr = IO.pipe
19 inp, out, err = (0..2).map { |i| IO.new(i) }
23 [ in_rd, in_wr, err_rd, err_wr, out_rd, out_wr ].each { |i| i.close }
26 [ in_rd, err_wr, out_wr ].each { |io| io.close }
27 [ in_wr, err_rd, out_rd ].each { |io| io.binmode }
30 # Unfortunately, input here must be processed inside a seperate
31 # thread/process using blocking I/O since env['rack.input'] is not
32 # IO.select-able and attempting to make it so would trip Rack::Lint
34 input = env['rack.input']
35 [ err_rd, out_rd ].each { |io| io.close }
37 # this is dependent on input.read having readpartial semantics:
38 buf = input.read(16384)
41 end while input.read(16384, buf)
45 inp_pid => 'input streamer',
46 cmd_pid => cmd.inspect,
52 rd, = IO.select([err_rd, out_rd])
53 rd && rd.first or next
55 if rd.include?(err_rd)
57 errors.write(err_rd.read_nonblock(16384))
64 rd.include?(out_rd) or next
67 yield out_rd.read_nonblock(16384)
72 rescue EOFError,Errno::EPIPE,Errno::EBADF,Errno::EINVAL
80 pid_map.each { |pid, str|
82 pid, status = Process.waitpid2(pid)
84 errors.write("#{str}: #{status.inspect} (PID:#{pid})\n")
86 errors.write("Failed to reap #{str} (PID:#{pid})\n")
100 /\A100-continue\z/i =~ env[Unicorn::Const::HTTP_EXPECT] and
101 return [ 100, {} , [] ]
103 [ 200, { 'Content-Type' => 'application/octet-stream' },
104 CatBody.new(env, cmd) ]