aboutsummaryrefslogblamecommitdiffstats
path: root/actionpack/lib/action_dispatch/middleware/rewindable_input.rb
blob: 725414efc4766783865d1ef67ddfd3945c5e0698 (plain) (tree)
1
2
3
4
5
6
7
8
9
                     



                                                   
                                          

         
                                               
                          
                                        
                            
           


                                           











                                                             
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'])
      @app.call(env)
    end
  end
end