about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-11-12 10:59:14 +0800
committerEric Wong <normalperson@yhbt.net>2010-11-12 11:00:22 +0800
commit855c02a9720a17854a2f1c715efbe502cdba54e2 (patch)
tree52968f53cf0225a3b717c20d815e8cd1b116923c
parent3b544fb2c0e4a1e14a7bcb752a8af9819b5aaeb2 (diff)
downloadunicorn-855c02a9720a17854a2f1c715efbe502cdba54e2.tar.gz
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.
-rw-r--r--lib/unicorn/stream_input.rb24
-rw-r--r--lib/unicorn/tee_input.rb5
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: