diff options
Diffstat (limited to 'lib/unicorn')
-rw-r--r-- | lib/unicorn/app/exec_cgi.rb | 10 | ||||
-rw-r--r-- | lib/unicorn/tee_input.rb | 9 | ||||
-rw-r--r-- | lib/unicorn/util.rb | 17 |
3 files changed, 21 insertions, 15 deletions
diff --git a/lib/unicorn/app/exec_cgi.rb b/lib/unicorn/app/exec_cgi.rb index 147b279..67817c8 100644 --- a/lib/unicorn/app/exec_cgi.rb +++ b/lib/unicorn/app/exec_cgi.rb @@ -42,11 +42,8 @@ module Unicorn::App # Calls the app def call(env) - out, err = Tempfile.new(nil), Tempfile.new(nil) - out.unlink - err.unlink + out, err = Unicorn::Util.tmpio, Unicorn::Util.tmpio inp = force_file_input(env) - out.sync = err.sync = true pid = fork { run_child(inp, out, err, env) } inp.close pid, status = Process.waitpid2(pid) @@ -125,10 +122,7 @@ module Unicorn::App if inp.size == 0 # inp could be a StringIO or StringIO-like object ::File.open('/dev/null', 'rb') else - tmp = Tempfile.new(nil) - tmp.unlink - tmp.binmode - tmp.sync = true + tmp = Unicorn::Util.tmpio buf = Z.dup while inp.read(CHUNK_SIZE, buf) diff --git a/lib/unicorn/tee_input.rb b/lib/unicorn/tee_input.rb index 1bcbf1d..bbc496b 100644 --- a/lib/unicorn/tee_input.rb +++ b/lib/unicorn/tee_input.rb @@ -1,10 +1,8 @@ # Copyright (c) 2009 Eric Wong # You can redistribute it and/or modify it under the same terms as Ruby. -require 'tempfile' - # acts like tee(1) on an input input to provide a input-like stream -# while providing rewindable semantics through a Tempfile/StringIO +# while providing rewindable semantics through a File/StringIO # backing store. On the first pass, the input is only read on demand # so your Rack application can use input notification (upload progress # and like). This should fully conform to the Rack::InputWrapper @@ -16,10 +14,7 @@ module Unicorn class TeeInput def initialize(input, size, body) - @tmp = Tempfile.new(nil) - @tmp.unlink - @tmp.binmode - @tmp.sync = true + @tmp = Unicorn::Util.tmpio if body @tmp.write(body) diff --git a/lib/unicorn/util.rb b/lib/unicorn/util.rb index 2d3f827..d2214b7 100644 --- a/lib/unicorn/util.rb +++ b/lib/unicorn/util.rb @@ -1,4 +1,5 @@ require 'fcntl' +require 'tmpdir' module Unicorn class Util @@ -39,6 +40,22 @@ module Unicorn nr end + # creates and returns a new File object. The File is unlinked + # immediately, switched to binary mode, and userspace output + # buffering is disabled + def tmpio + fp = begin + File.open("#{Dir::tmpdir}/#{rand}", + File::RDWR|File::CREAT|File::EXCL, 0600) + rescue Errno::EEXIST + retry + end + File.unlink(fp.path) + fp.binmode + fp.sync = true + fp + end + end end |