summary refs log tree commit homepage
path: root/lib/rainbows/max_body
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-11-16 16:16:42 -0800
committerEric Wong <normalperson@yhbt.net>2010-11-16 16:21:33 -0800
commit31cf77e7aa2f2e6065e7ace44e55c3f042b51f1b (patch)
tree7dfd53c499cbf7a52165d3480d56d8576a743781 /lib/rainbows/max_body
parent42747db815ad668b20849afb2a9dcdd1319713ae (diff)
This allows the client_max_body_size implementation to not rely
on Unicorn::TeeInput internals, allowing it to be used with
Unicorn::StreamInput (or any other (nearly)
Rack::Lint-compatible input object).
Diffstat (limited to 'lib/rainbows/max_body')
-rw-r--r--lib/rainbows/max_body/rewindable_wrapper.rb17
-rw-r--r--lib/rainbows/max_body/wrapper.rb26
2 files changed, 43 insertions, 0 deletions
diff --git a/lib/rainbows/max_body/rewindable_wrapper.rb b/lib/rainbows/max_body/rewindable_wrapper.rb
new file mode 100644
index 0000000..b52726e
--- /dev/null
+++ b/lib/rainbows/max_body/rewindable_wrapper.rb
@@ -0,0 +1,17 @@
+# -*- encoding: binary -*-
+# :enddoc:
+class Rainbows::MaxBody::RewindableWrapper < Rainbows::MaxBody::Wrapper
+  def initialize(rack_input, limit)
+    @orig_limit = limit
+    super
+  end
+
+  def rewind
+    @limit = @orig_limit
+    @input.rewind
+  end
+
+  def size
+    @input.size
+  end
+end
diff --git a/lib/rainbows/max_body/wrapper.rb b/lib/rainbows/max_body/wrapper.rb
new file mode 100644
index 0000000..3c38ca6
--- /dev/null
+++ b/lib/rainbows/max_body/wrapper.rb
@@ -0,0 +1,26 @@
+# -*- encoding: binary -*-
+# :enddoc:
+class Rainbows::MaxBody::Wrapper
+  def initialize(rack_input, limit)
+    @input, @limit = rack_input, limit
+  end
+
+  def check(rv)
+    throw :rainbows_EFBIG if rv && ((@limit -= rv.size) < 0)
+    rv
+  end
+
+  def each(&block)
+    while line = @input.gets
+      yield check(line)
+    end
+  end
+
+  def read(*args)
+    check(@input.read(*args))
+  end
+
+  def gets
+    check(@input.gets)
+  end
+end