about summary refs log tree commit homepage
path: root/lib/rainbows/max_body/wrapper.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rainbows/max_body/wrapper.rb')
-rw-r--r--lib/rainbows/max_body/wrapper.rb26
1 files changed, 26 insertions, 0 deletions
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