From 7ce0778a1516110cf8015e59e2e8fac15032379c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Fri, 17 Apr 2009 21:53:44 -0500 Subject: Always buffer rack.input if it is not rewindable Signed-off-by: Joshua Peek --- .../action_dispatch/middleware/rewindable_input.rb | 25 +++++++--------------- actionpack/test/controller/dispatcher_test.rb | 2 +- .../request/multipart_params_parsing_test.rb | 1 + .../request/url_encoded_params_parsing_test.rb | 1 + 4 files changed, 11 insertions(+), 18 deletions(-) diff --git a/actionpack/lib/action_dispatch/middleware/rewindable_input.rb b/actionpack/lib/action_dispatch/middleware/rewindable_input.rb index 725414efc4..c818f28cce 100644 --- a/actionpack/lib/action_dispatch/middleware/rewindable_input.rb +++ b/actionpack/lib/action_dispatch/middleware/rewindable_input.rb @@ -1,27 +1,18 @@ module ActionDispatch class RewindableInput - class RewindableIO < ActiveSupport::BasicObject - def initialize(io) - @io = io - @rewindable = io.is_a?(::StringIO) - end - - def method_missing(method, *args, &block) - unless @rewindable - @io = ::StringIO.new(@io.read) - @rewindable = true - end - - @io.__send__(method, *args, &block) - end - end - def initialize(app) @app = app end def call(env) - env['rack.input'] = RewindableIO.new(env['rack.input']) + begin + env['rack.input'].rewind + rescue NoMethodError, Errno::ESPIPE + # Handles exceptions raised by input streams that cannot be rewound + # such as when using plain CGI under Apache + env['rack.input'] = StringIO.new(env['rack.input'].read) + end + @app.call(env) end end diff --git a/actionpack/test/controller/dispatcher_test.rb b/actionpack/test/controller/dispatcher_test.rb index 721bcf6136..569d698a03 100644 --- a/actionpack/test/controller/dispatcher_test.rb +++ b/actionpack/test/controller/dispatcher_test.rb @@ -94,7 +94,7 @@ class DispatcherTest < Test::Unit::TestCase def dispatch(cache_classes = true) ActionController::Routing::RouteSet.any_instance.stubs(:call).returns([200, {}, 'response']) Dispatcher.define_dispatcher_callbacks(cache_classes) - Dispatcher.new.call({}) + Dispatcher.new.call({'rack.input' => StringIO.new('')}) end def assert_subclasses(howmany, klass, message = klass.subclasses.inspect) diff --git a/actionpack/test/dispatch/request/multipart_params_parsing_test.rb b/actionpack/test/dispatch/request/multipart_params_parsing_test.rb index 33c909aad9..88b81dc493 100644 --- a/actionpack/test/dispatch/request/multipart_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/multipart_params_parsing_test.rb @@ -207,6 +207,7 @@ class MultipartParamsParsingTest < ActionController::IntegrationTest def call(env) env['rack.input'].read + env['rack.input'].rewind @app.call(env) end end diff --git a/actionpack/test/dispatch/request/url_encoded_params_parsing_test.rb b/actionpack/test/dispatch/request/url_encoded_params_parsing_test.rb index 44d13797f0..8de4a83d76 100644 --- a/actionpack/test/dispatch/request/url_encoded_params_parsing_test.rb +++ b/actionpack/test/dispatch/request/url_encoded_params_parsing_test.rb @@ -151,6 +151,7 @@ class UrlEncodedParamsParsingTest < ActionController::IntegrationTest def call(env) env['rack.input'].read + env['rack.input'].rewind @app.call(env) end end -- cgit v1.2.3