Skip to content

Commit

Permalink
Always buffer rack.input if it is not rewindable
Browse files Browse the repository at this point in the history
Signed-off-by: Joshua Peek <josh@joshpeek.com>
  • Loading branch information
mislav authored and josh committed Apr 18, 2009
1 parent 878aec9 commit 35c5727
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 18 deletions.
25 changes: 8 additions & 17 deletions actionpack/lib/action_controller/rewindable_input.rb
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
module ActionController
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
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/dispatcher_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_to_prepare_with_identifier_replaces
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ def initialize(app)

def call(env)
env['rack.input'].read
env['rack.input'].rewind
@app.call(env)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ def initialize(app)

def call(env)
env['rack.input'].read
env['rack.input'].rewind
@app.call(env)
end
end
Expand Down

0 comments on commit 35c5727

Please sign in to comment.