From 6eb46e422f4b2ba98c795fca5e18e7262c0c688e Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Fri, 8 Oct 2010 23:44:23 +0000 Subject: add PrereadInput middleware to get around TeeInput 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!... --- lib/unicorn/preread_input.rb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 lib/unicorn/preread_input.rb (limited to 'lib/unicorn') 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 -- cgit v1.2.3-24-ge0c7