From a14f746a8a861b4449ce31c9449a5e3aaddf14c4 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 5 Dec 2022 16:44:30 +1300 Subject: [PATCH 1/3] Allow passing through streaming bodies. --- lib/rack/response.rb | 14 +++++++++++++- test/spec_response.rb | 10 ++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/rack/response.rb b/lib/rack/response.rb index 365ca058d..088966f74 100644 --- a/lib/rack/response.rb +++ b/lib/rack/response.rb @@ -95,11 +95,23 @@ def chunked? CHUNKED == get_header(TRANSFER_ENCODING) end + def enumerable_body? + @body.respond_to?(:each) + end + + def streaming_body? + !enumerable_body? + end + + def no_entity_body? + enumerable_body? && 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") From 065e2b522902831b7cfa85c496026b0f25c56112 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 5 Dec 2022 17:13:13 +1300 Subject: [PATCH 2/3] Don't expose new public interface. --- lib/rack/response.rb | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/lib/rack/response.rb b/lib/rack/response.rb index 088966f74..9541ef77c 100644 --- a/lib/rack/response.rb +++ b/lib/rack/response.rb @@ -95,16 +95,9 @@ def chunked? CHUNKED == get_header(TRANSFER_ENCODING) end - def enumerable_body? - @body.respond_to?(:each) - end - - def streaming_body? - !enumerable_body? - end - def no_entity_body? - enumerable_body? && STATUS_WITH_NO_ENTITY_BODY[@status] + # 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. From ecff0e73175c048a84bf82d51f0765a221931dad Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Mon, 5 Dec 2022 17:30:24 +1300 Subject: [PATCH 3/3] Update lib/rack/response.rb --- lib/rack/response.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/rack/response.rb b/lib/rack/response.rb index 9541ef77c..4a6af373e 100644 --- a/lib/rack/response.rb +++ b/lib/rack/response.rb @@ -96,7 +96,7 @@ def chunked? end def no_entity_body? - # The response body is an enumerable body and it is not allowed to have an 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