diff options
Diffstat (limited to 'lib/unicorn/app/exec_cgi.rb')
-rw-r--r-- | lib/unicorn/app/exec_cgi.rb | 48 |
1 files changed, 25 insertions, 23 deletions
diff --git a/lib/unicorn/app/exec_cgi.rb b/lib/unicorn/app/exec_cgi.rb index ff5f53a..232b681 100644 --- a/lib/unicorn/app/exec_cgi.rb +++ b/lib/unicorn/app/exec_cgi.rb @@ -1,5 +1,5 @@ # -*- encoding: binary -*- - +# :enddoc: require 'unicorn' module Unicorn::App @@ -28,6 +28,24 @@ module Unicorn::App SERVER_SOFTWARE ).map { |x| x.freeze } # frozen strings are faster for Hash assignments + class Body < Unicorn::TmpIO + def body_offset=(n) + sysseek(@body_offset = n) + end + + def each + sysseek @body_offset + # don't use a preallocated buffer for sysread since we can't + # guarantee an actual socket is consuming the yielded string + # (or if somebody is pushing to an array for eventual concatenation + begin + yield sysread(CHUNK_SIZE) + rescue EOFError + break + end while true + end + end + # Intializes the app, example of usage in a config.ru # map "/cgit" do # run Unicorn::App::ExecCgi.new("/path/to/cgit.cgi") @@ -43,7 +61,7 @@ module Unicorn::App # Calls the app def call(env) - out, err = Unicorn::Util.tmpio, Unicorn::Util.tmpio + out, err = Body.new, Unicorn::TmpIO.new inp = force_file_input(env) pid = fork { run_child(inp, out, err, env) } inp.close @@ -67,9 +85,9 @@ module Unicorn::App ENV['GATEWAY_INTERFACE'] = 'CGI/1.1' env.keys.grep(/^HTTP_/) { |key| ENV[key] = env[key] } - a = IO.new(0).reopen(inp) - b = IO.new(1).reopen(out) - c = IO.new(2).reopen(err) + $stdin.reopen(inp) + $stdout.reopen(out) + $stderr.reopen(err) exec(*args) end @@ -87,23 +105,7 @@ module Unicorn::App offset = 4 end offset += head.length - - # Allows +out+ to be used as a Rack body. - out.instance_eval { class << self; self; end }.instance_eval { - define_method(:each) { |&blk| - sysseek(offset) - - # don't use a preallocated buffer for sysread since we can't - # guarantee an actual socket is consuming the yielded string - # (or if somebody is pushing to an array for eventual concatenation - begin - blk.call(sysread(CHUNK_SIZE)) - rescue EOFError - break - end while true - } - } - + out.body_offset = offset size -= offset prev = nil headers = Rack::Utils::HeaderHash.new @@ -125,7 +127,7 @@ module Unicorn::App if inp.respond_to?(:size) && inp.size == 0 ::File.open('/dev/null', 'rb') else - tmp = Unicorn::Util.tmpio + tmp = Unicorn::TmpIO.new buf = inp.read(CHUNK_SIZE) begin |