about summary refs log tree commit homepage
path: root/lib/unicorn/preread_input.rb
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2010-10-08 23:44:23 +0000
committerEric Wong <normalperson@yhbt.net>2010-10-09 00:03:02 +0000
commit6eb46e422f4b2ba98c795fca5e18e7262c0c688e (patch)
tree805e041099199ce7e4154283b3e7b81d9485159d /lib/unicorn/preread_input.rb
parent9be78606355d4a0ad4ea59316ab2ce998c5b9a12 (diff)
downloadunicorn-6eb46e422f4b2ba98c795fca5e18e7262c0c688e.tar.gz
This may be useful for some apps that wish to drain the body
before acquiring an app-wide lock.  Maybe it's more useful
with Rainbows!...
Diffstat (limited to 'lib/unicorn/preread_input.rb')
-rw-r--r--lib/unicorn/preread_input.rb30
1 files changed, 30 insertions, 0 deletions
diff --git a/lib/unicorn/preread_input.rb b/lib/unicorn/preread_input.rb
new file mode 100644
index 0000000..ec83cb2
--- /dev/null
+++ b/lib/unicorn/preread_input.rb
@@ -0,0 +1,30 @@
+# -*- encoding: binary -*-
+
+module Unicorn
+# This middleware is used to ensure input is buffered to memory
+# or disk (depending on size) before the application is dispatched
+# by entirely consuming it (from TeeInput) beforehand.
+#
+# Usage (in config.ru):
+#
+#     require 'unicorn/preread_input'
+#     if defined?(Unicorn)
+#       use Unicorn::PrereadInput
+#     end
+#     run YourApp.new
+class PrereadInput
+  def initialize(app)
+    @app = app
+  end
+
+  def call(env)
+    buf = ""
+    input = env["rack.input"]
+    if buf = input.read(16384)
+      true while input.read(16384, buf)
+      input.rewind
+    end
+    @app.call(env)
+  end
+end
+end