Skip to content

Commit 33a3942

Browse files
committed
Allow close_read in closed state, as this is idempotent and may be required when closing the response body.
1 parent f1b92a5 commit 33a3942

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

lib/protocol/http1/connection.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,11 @@ def hijack!
214214

215215
# Close the read end of the connection and transition to the half-closed remote state (or closed if already in the half-closed local state).
216216
def close_read
217-
@persistent = false
218-
@stream&.close_read
219-
self.receive_end_stream!
217+
unless @state == :closed
218+
@persistent = false
219+
@stream&.close_read
220+
self.receive_end_stream!
221+
end
220222
end
221223

222224
# Close the connection and underlying stream and transition to the closed state.

test/protocol/http1/connection.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,5 +738,39 @@
738738
server.close(error)
739739
expect(server).to be(:closed?)
740740
end
741+
742+
it "can close the body before client is closed" do
743+
client.open!
744+
745+
# Simulate a CONNECT request, which yields a Body::Remainder:
746+
server.stream.write("HTTP/1.1 200 Connection Established\r\n\r\n")
747+
server.stream.close
748+
749+
version, status, reason, headers, body = client.read_response("CONNECT")
750+
expect(body).to be_a(Protocol::HTTP1::Body::Remainder)
751+
752+
# Close the body before closing the client:
753+
body.close
754+
755+
# Now, close the client:
756+
client.close
757+
end
758+
759+
it "allows closing remainder body after client is closed" do
760+
client.open!
761+
762+
# Simulate a CONNECT request, which yields a Body::Remainder:
763+
server.stream.write("HTTP/1.1 200 Connection Established\r\n\r\n")
764+
server.stream.close
765+
766+
version, status, reason, headers, body = client.read_response("CONNECT")
767+
expect(body).to be_a(Protocol::HTTP1::Body::Remainder)
768+
769+
# Close the client before reading/closing the body:
770+
client.close
771+
772+
# Now, close the body, which should not raise:
773+
body.close
774+
end
741775
end
742776
end

0 commit comments

Comments
 (0)