From d3c20360f9607a9e04f7d28ce0a796437f733e5f Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 5 Nov 2009 00:49:06 -0800 Subject: Util::tmpio returns a TmpIO that responds to #size Subclass off the core File class so we don't have to worry about #size being defined. This will mainly be useful to Rainbows! but allows us to simplify our TeeInput implementation a little, too. --- lib/unicorn/tee_input.rb | 10 +++------- lib/unicorn/util.rb | 14 ++++++++++++-- test/unit/test_tee_input.rb | 2 +- 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/unicorn/tee_input.rb b/lib/unicorn/tee_input.rb index b66dce0..474f10f 100644 --- a/lib/unicorn/tee_input.rb +++ b/lib/unicorn/tee_input.rb @@ -38,7 +38,7 @@ module Unicorn @tmp.seek(pos) end - @size = tmp_size + @size = @tmp.size end # call-seq: @@ -73,7 +73,7 @@ module Unicorn rv else rv = args.shift || @buf2.dup - diff = tmp_size - @tmp.pos + diff = @tmp.size - @tmp.pos if 0 == diff ensure_length(tee(length, rv), length) else @@ -87,7 +87,7 @@ module Unicorn socket or return @tmp.gets nil == $/ and return read - orig_size = tmp_size + orig_size = @tmp.size if @tmp.pos == orig_size tee(Const::CHUNK_SIZE, @buf2) or return nil @tmp.seek(orig_size) @@ -147,10 +147,6 @@ module Unicorn self.socket = nil end - def tmp_size - StringIO === @tmp ? @tmp.size : @tmp.stat.size - end - # tee()s into +buf+ until it is of +length+ bytes (or until # we've reached the Content-Length of the request body). # Returns +buf+ (the exact object, not a duplicate) diff --git a/lib/unicorn/util.rb b/lib/unicorn/util.rb index 4bbc590..6444699 100644 --- a/lib/unicorn/util.rb +++ b/lib/unicorn/util.rb @@ -4,6 +4,16 @@ require 'fcntl' require 'tmpdir' module Unicorn + + class TmpIO < ::File + + # for easier env["rack.input"] compatibility + def size + # flush if sync + stat.size + end + end + class Util class << self @@ -47,8 +57,8 @@ module Unicorn # buffering is disabled def tmpio fp = begin - File.open("#{Dir::tmpdir}/#{rand}", - File::RDWR|File::CREAT|File::EXCL, 0600) + TmpIO.open("#{Dir::tmpdir}/#{rand}", + File::RDWR|File::CREAT|File::EXCL, 0600) rescue Errno::EEXIST retry end diff --git a/test/unit/test_tee_input.rb b/test/unit/test_tee_input.rb index 2bd6541..403f698 100644 --- a/test/unit/test_tee_input.rb +++ b/test/unit/test_tee_input.rb @@ -127,7 +127,7 @@ class TestTeeInput < Test::Unit::TestCase assert ! @parser.body_eof? assert_kind_of File, ti.instance_eval { @tmp } assert_equal 0, ti.instance_eval { @tmp.pos } - assert_equal 1, ti.instance_eval { tmp_size } + assert_equal 1, ti.instance_eval { @tmp.size } assert_equal Unicorn::Const::MAX_BODY + 1, ti.size nr = Unicorn::Const::MAX_BODY / 4 pid = fork { -- cgit v1.2.3-24-ge0c7