From 855c02a9720a17854a2f1c715efbe502cdba54e2 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 12 Nov 2010 10:59:14 +0800 Subject: *_input: make life easier for subclasses/modules Avoid having specific knowledge of internals in TeeInput and instead move that to StreamInput when dealing with byte counts. This makes things easier for Rainbows! which will need to extends these classes. --- lib/unicorn/stream_input.rb | 24 ++++++++++++++---------- lib/unicorn/tee_input.rb | 5 +---- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/unicorn/stream_input.rb b/lib/unicorn/stream_input.rb index 2a05337..2c8e40a 100644 --- a/lib/unicorn/stream_input.rb +++ b/lib/unicorn/stream_input.rb @@ -43,15 +43,7 @@ class Unicorn::StreamInput length = args.shift rv = args.shift || '' if length.nil? - rv.replace(@rbuf) - @rbuf.replace('') - @socket or return rv - until eof? - @socket.kgio_read(@@io_chunk_size, @buf) or eof! - filter_body(@rbuf, @buf) - rv << @rbuf - end - @rbuf.replace('') + read_all(rv) else if length <= @rbuf.size rv.replace(@rbuf.slice(0, length)) @@ -85,7 +77,7 @@ class Unicorn::StreamInput def gets sep = $/ if sep.nil? - rv = read + read_all(rv = '') return rv.empty? ? nil : rv end re = /\A(.*?#{Regexp.escape(sep)})/ @@ -141,6 +133,18 @@ private rv end + def read_all(dst) + dst.replace(@rbuf) + @socket or return + until eof? + @socket.kgio_read(@@io_chunk_size, @buf) or eof! + filter_body(@rbuf, @buf) + dst << @rbuf + end + ensure + @rbuf.replace('') + end + def eof! # in case client only did a premature shutdown(SHUT_WR) # we do support clients that shutdown(SHUT_WR) after the diff --git a/lib/unicorn/tee_input.rb b/lib/unicorn/tee_input.rb index 74d9df6..0e937ff 100644 --- a/lib/unicorn/tee_input.rb +++ b/lib/unicorn/tee_input.rb @@ -81,10 +81,7 @@ class Unicorn::TeeInput < Unicorn::StreamInput # This takes zero arguments for strict Rack::Lint compatibility, # unlike IO#gets. def gets - @socket or return @tmp.gets - rv = super - # the $/.nil? case is implemented using read, so don't tee() again - $/.nil? ? rv : tee(rv) + @socket ? tee(super) : @tmp.gets end # :call-seq: -- cgit v1.2.3-24-ge0c7