about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-12-02 07:38:14 +0000
committerEric Wong <normalperson@yhbt.net>2010-12-02 19:37:45 +0000
commit64889d9136fa5466269232c26a2f235dd763d8f0 (patch)
tree86b9e7b5b6833afe1ae9b0ea2ba34abd9b4de83b
parent945b5760d86b9dd00e65bd0625b98cf75f6a8257 (diff)
downloadrainbows-64889d9136fa5466269232c26a2f235dd763d8f0.tar.gz
Hopefully it makes more sense now and is easier to
digest for new hackers.
-rw-r--r--lib/rainbows/ev_core.rb54
-rw-r--r--lib/rainbows/ev_core/cap_input.rb21
2 files changed, 40 insertions, 35 deletions
diff --git a/lib/rainbows/ev_core.rb b/lib/rainbows/ev_core.rb
index 4dd884a..e1f5918 100644
--- a/lib/rainbows/ev_core.rb
+++ b/lib/rainbows/ev_core.rb
@@ -7,6 +7,7 @@ module Rainbows::EvCore
   G = Rainbows::G
   NULL_IO = Unicorn::HttpRequest::NULL_IO
   HttpParser = Unicorn::HttpParser
+  autoload :CapInput, 'rainbows/ev_core/cap_input'
 
   # Apps may return this Rack response: AsyncResponse = [ -1, {}, [] ]
   ASYNC_CALLBACK = "async.callback".freeze
@@ -50,7 +51,7 @@ module Rainbows::EvCore
       write(EXPECT_100_RESPONSE)
       @env.delete(HTTP_EXPECT)
     end
-    @input = CapInput.new(@hp.content_length, self)
+    @input = mkinput
     @hp.filter_body(@buf2 = "", @buf)
     @input << @buf2
     on_read("")
@@ -88,42 +89,25 @@ module Rainbows::EvCore
       handle_error(e)
   end
 
-  class CapInput < Struct.new(:io, :client, :bytes_left)
-    MAX_BODY = Unicorn::Const::MAX_BODY
-    TmpIO = Unicorn::TmpIO
-
-    def self.err(client, msg)
-      client.write(Rainbows::Const::ERROR_413_RESPONSE)
-      client.quit
-
-      # zip back up the stack
-      raise IOError, msg, []
-    end
-
-    def self.new(len, client)
-      max = Rainbows.max_bytes
-      if len
-        if max && (len > max)
-          err(client, "Content-Length too big: #{len} > #{max}")
-        end
-        len <= MAX_BODY ? StringIO.new("") : TmpIO.new
-      else
-        max ? super(TmpIO.new, client, max) : TmpIO.new
-      end
-    end
+  def err_413(msg)
+    write(Rainbows::Const::ERROR_413_RESPONSE)
+    quit
+    # zip back up the stack
+    raise IOError, msg, []
+  end
 
-    def <<(buf)
-      if (self.bytes_left -= buf.size) < 0
-        io.close
-        CapInput.err(client, "chunked request body too big")
+  MAX_BODY = Unicorn::Const::MAX_BODY
+  TmpIO = Unicorn::TmpIO
+  def mkinput
+    max = Rainbows.max_bytes
+    len = @hp.content_length
+    if len
+      if max && (len > max)
+        err_413("Content-Length too big: #{len} > #{max}")
       end
-      io << buf
+      len <= MAX_BODY ? StringIO.new("") : TmpIO.new
+    else
+      max ? CapInput.new(TmpIO.new, self) : TmpIO.new
     end
-
-    def gets; io.gets; end
-    def each(&block); io.each(&block); end
-    def size; io.size; end
-    def rewind; io.rewind; end
-    def read(*args); io.read(*args); end
   end
 end
diff --git a/lib/rainbows/ev_core/cap_input.rb b/lib/rainbows/ev_core/cap_input.rb
new file mode 100644
index 0000000..a2eecab
--- /dev/null
+++ b/lib/rainbows/ev_core/cap_input.rb
@@ -0,0 +1,21 @@
+# -*- encoding: binary -*-
+# :enddoc:
+class Rainbows::EvCore::CapInput
+  def initialize(io, client)
+    @io, @client, @bytes_left = io, client, Rainbows.max_bytes
+  end
+
+  def <<(buf)
+    if (@bytes_left -= buf.size) < 0
+      @io.close
+      @client.err_413("chunked request body too big")
+    end
+    @io << buf
+  end
+
+  def gets; @io.gets; end
+  def each(&block); @io.each(&block); end
+  def size; @io.size; end
+  def rewind; @io.rewind; end
+  def read(*args); @io.read(*args); end
+end