From 0e9ea5f149ef2d611f20573d06f659eeb97171f5 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 5 Dec 2022 17:59:21 +1300 Subject: [PATCH] Allow passing through streaming bodies. (#1993) --- lib/rack/response.rb | 7 ++++++- test/spec_response.rb | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/rack/response.rb b/lib/rack/response.rb index 365ca058d..4a6af373e 100644 --- a/lib/rack/response.rb +++ b/lib/rack/response.rb @@ -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 diff --git a/test/spec_response.rb b/test/spec_response.rb index cebe38f1b..cd32f4e7e 100644 --- a/test/spec_response.rb +++ b/test/spec_response.rb @@ -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")