about summary refs log tree commit homepage
path: root/lib/unicorn/preread_input.rb
diff options
context:
space:
mode:
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