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

Allow passing through streaming bodies. #1993

Merged
merged 3 commits into from Dec 5, 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
7 changes: 6 additions & 1 deletion lib/rack/response.rb
Expand Up @@ -95,11 +95,16 @@ def chunked?
CHUNKED == get_header(TRANSFER_ENCODING)
end

def no_entity_body?
# The response body is an enumerable body and it is not allowed to have an entity body.
@body.respond_to?(:each) && STATUS_WITH_NO_ENTITY_BODY[@status]
end

# Generate a response array consistent with the requirements of the SPEC.
# @return [Array] a 3-tuple suitable of `[status, headers, body]`
# which is suitable to be returned from the middleware `#call(env)` method.
def finish(&block)
if STATUS_WITH_NO_ENTITY_BODY[@status]
if no_entity_body?
delete_header CONTENT_TYPE
delete_header CONTENT_LENGTH
close
Expand Down
10 changes: 10 additions & 0 deletions test/spec_response.rb
Expand Up @@ -638,6 +638,16 @@ def obj.each
res.body.wont_be :closed?
end

it "doesn't clear #body when 101 and streaming" do
res = Rack::Response.new

streaming_body = proc{|stream| stream.close}
res.body = streaming_body
res.status = 101
res.finish
res.body.must_equal streaming_body
end

it "flatten doesn't cause infinite loop" do
# https://github.com/rack/rack/issues/419
res = Rack::Response.new("Hello World")
Expand Down