From b1551156dc71208bd8d30dafe7d86aea57401f20 Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sat, 12 Oct 2024 11:47:16 +1300 Subject: [PATCH] If the user reads exactly the content length, rack expects the input to be closed. --- lib/protocol/rack/input.rb | 7 +++++++ test/protocol/rack/input.rb | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/lib/protocol/rack/input.rb b/lib/protocol/rack/input.rb index 46a527f..3613fdf 100644 --- a/lib/protocol/rack/input.rb +++ b/lib/protocol/rack/input.rb @@ -89,6 +89,13 @@ def read_next if @body # User's may forget to call #close... if chunk = @body.read + # If the user reads exactly the content length, we close the stream automatically: + # https://github.com/socketry/async-http/issues/183 + if @body.empty? + @body.close + @body = nil + end + return chunk else # So if we are at the end of the stream, we close it automatically: diff --git a/test/protocol/rack/input.rb b/test/protocol/rack/input.rb index f1cc3a5..159f016 100644 --- a/test/protocol/rack/input.rb +++ b/test/protocol/rack/input.rb @@ -52,6 +52,12 @@ expect(input.body).to be_nil end + it "can read exactly the content length" do + expect(body).to receive(:close) + + expect(input.read(sample_data.join.bytesize)).to be == sample_data.join + end + it "can read no input" do expect(input.read(0)).to be == "" end