Skip to content

Commit

Permalink
Allow passing through streaming bodies. (#1993)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed Dec 5, 2022
1 parent b28dea6 commit 0e9ea5f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
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

0 comments on commit 0e9ea5f

Please sign in to comment.