Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rack::RewindableInput::Middleware #1803

Merged
merged 1 commit into from
Feb 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ All notable changes to this project will be documented in this file. For info on

- `Rack::Headers` added to support lower-case header keys. ([@jeremyevans](https://github.com/jeremyevans))
- `Rack::RewindableInput` supports size. ([@ahorek](https://github.com/ahorek))
- `Rack::RewindableInput::Middleware` added for making `rack.input` rewindable. ([@jeremyevans](https://github.com/jeremyevans))
- Rack::Session::Pool now accepts `:allow_fallback` option to disable fallback to public id. ([#1431](https://github.com/rack/rack/issues/1431), [@jeremyevans](https://github.com/jeremyevans))

### Changed
Expand Down
20 changes: 16 additions & 4 deletions lib/rack/rewindable_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,29 @@

require 'tempfile'

require_relative 'constants'

module Rack
# Class which can make any IO object rewindable, including non-rewindable ones. It does
# this by buffering the data into a tempfile, which is rewindable.
#
# rack.input is required to be rewindable, so if your input stream IO is non-rewindable
# by nature (e.g. a pipe or a socket) then you can wrap it in an object of this class
# to easily make it rewindable.
#
# Don't forget to call #close when you're done. This frees up temporary resources that
# RewindableInput uses, though it does *not* close the original IO object.
class RewindableInput
# Makes rack.input rewindable, for compatibility with applications and middleware
# designed for earlier versions of Rack (where rack.input was required to be
# rewindable).
class Middleware
def initialize(app)
@app = app
end

def call(env)
env[RACK_INPUT] = RewindableInput.new(env[RACK_INPUT])
@app.call(env)
end
end

def initialize(io)
@io = io
@rewindable_io = nil
Expand Down
9 changes: 9 additions & 0 deletions test/spec_rewindable_input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,12 @@ def @io.rewind
include RewindableTest
end
end

describe Rack::RewindableInput::Middleware do
it "wraps rack.input in RewindableInput" do
app = proc{|env| [200, {}, [env['rack.input'].class.to_s]]}
app.call('rack.input'=>StringIO.new(''))[2].must_equal ['StringIO']
app = Rack::RewindableInput::Middleware.new(app)
app.call('rack.input'=>StringIO.new(''))[2].must_equal ['Rack::RewindableInput']
end
end