From 2e913054b36848a05f7ba06c3accbe164c666708 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Thu, 16 Jul 2009 01:13:33 -0700 Subject: move all #gets logic to tee_input out of chunked_reader This simplifies chunked_reader substantially with a slight increase in tee_input complexity. This is beneficial because chunked_reader is more complex to begin with and more likely to experience correctness issues. --- lib/unicorn/chunked_reader.rb | 17 ----------------- lib/unicorn/tee_input.rb | 31 +++++++++++++++++-------------- 2 files changed, 17 insertions(+), 31 deletions(-) (limited to 'lib') diff --git a/lib/unicorn/chunked_reader.rb b/lib/unicorn/chunked_reader.rb index 539b350..b813da6 100644 --- a/lib/unicorn/chunked_reader.rb +++ b/lib/unicorn/chunked_reader.rb @@ -32,23 +32,6 @@ module Unicorn buf end - def gets - line = nil - begin - line = readpartial(Const::CHUNK_SIZE) - begin - if line.sub!(%r{\A(.*?#{$/})}, Z) - @chunk_left += line.size - @buf = @buf ? (line << @buf) : line - return $1.dup - end - line << readpartial(Const::CHUNK_SIZE) - end while true - rescue EOFError - return line - end - end - private def last_block(max = nil) diff --git a/lib/unicorn/tee_input.rb b/lib/unicorn/tee_input.rb index 06028a6..1bcbf1d 100644 --- a/lib/unicorn/tee_input.rb +++ b/lib/unicorn/tee_input.rb @@ -73,22 +73,25 @@ module Unicorn @input or return @tmp.gets nil == $/ and return read - line = nil - if @tmp.pos < @tmp.stat.size - line = @tmp.gets # cannot be nil here - $/ == line[-$/.size, $/.size] and return line - - # half the line was already read, and the rest of has not been read - if buf = @input.gets - @tmp.write(buf) - line << buf - else - @input = nil - end - elsif line = @input.gets - @tmp.write(line) + orig_size = @tmp.stat.size + if @tmp.pos == orig_size + tee(Const::CHUNK_SIZE, Z.dup) or return nil + @tmp.seek(orig_size) end + line = @tmp.gets # cannot be nil here since size > pos + $/ == line[-$/.size, $/.size] and return line + + # unlikely, if we got here, then @tmp is at EOF + begin + orig_size = @tmp.stat.size + tee(Const::CHUNK_SIZE, Z.dup) or break + @tmp.seek(orig_size) + line << @tmp.gets + $/ == line[-$/.size, $/.size] and return line + # @tmp is at EOF again here, retry the loop + end while true + line end -- cgit v1.2.3-24-ge0c7