aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_controller
diff options
context:
space:
mode:
Diffstat (limited to 'actionpack/lib/action_controller')
-rw-r--r--actionpack/lib/action_controller/middlewares.rb1
-rw-r--r--actionpack/lib/action_controller/rewindable_input.rb40
2 files changed, 41 insertions, 0 deletions
diff --git a/actionpack/lib/action_controller/middlewares.rb b/actionpack/lib/action_controller/middlewares.rb
index 793739723f..914750bc0c 100644
--- a/actionpack/lib/action_controller/middlewares.rb
+++ b/actionpack/lib/action_controller/middlewares.rb
@@ -18,4 +18,5 @@ use "ActiveRecord::QueryCache", :if => lambda { defined?(ActiveRecord) }
)
end
+use ActionController::RewindableInput
use ActionController::VerbPiggybacking
diff --git a/actionpack/lib/action_controller/rewindable_input.rb b/actionpack/lib/action_controller/rewindable_input.rb
new file mode 100644
index 0000000000..296d8aed22
--- /dev/null
+++ b/actionpack/lib/action_controller/rewindable_input.rb
@@ -0,0 +1,40 @@
+module ActionController
+ class RewindableInput
+ class RewindableIO < ActiveSupport::BasicObject
+ def initialize(io)
+ @io = io
+ end
+
+ def read(*args)
+ read_original_io
+ @io.read(*args)
+ end
+
+ def rewind
+ read_original_io
+ @io.rewind
+ end
+
+ def method_missing(method, *args, &block)
+ @io.send(method, *args, &block)
+ end
+
+ private
+ def read_original_io
+ unless @str
+ @str = @io.read
+ @io = StringIO.new(@str)
+ end
+ end
+ end
+
+ def initialize(app)
+ @app = app
+ end
+
+ def call(env)
+ env['rack.input'] = RewindableIO.new(env['rack.input'])
+ @app.call(env)
+ end
+ end
+end