Skip to content

Commit

Permalink
Fix 0.30.4 issue with connection close header (#2408)
Browse files Browse the repository at this point in the history
* fix server waiting state with connection close header

* added test case for closing post connection with connection header

* removed post without body test case with connection close header

* removed unnecessary line removal
  • Loading branch information
theyashl authored Aug 2, 2024
1 parent 8efa41c commit 2f25107
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
31 changes: 31 additions & 0 deletions tests/protocols/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@

CONNECTION_CLOSE_REQUEST = b"\r\n".join([b"GET / HTTP/1.1", b"Host: example.org", b"Connection: close", b"", b""])

CONNECTION_CLOSE_POST_REQUEST = b"\r\n".join(
[
b"POST / HTTP/1.1",
b"Host: example.org",
b"Connection: close",
b"Content-Type: application/json",
b"Content-Length: 18",
b"",
b"{'hello': 'world'}",
]
)

REQUEST_AFTER_CONNECTION_CLOSE = b"\r\n".join(
[
b"GET / HTTP/1.1",
Expand Down Expand Up @@ -1011,6 +1023,25 @@ async def test_close_connection_with_multiple_requests(http_protocol_cls: HTTPPr
assert b"connection: close" in protocol.transport.buffer.lower()


async def test_close_connection_with_post_request(http_protocol_cls: HTTPProtocol):
async def app(scope: Scope, receive: ASGIReceiveCallable, send: ASGISendCallable):
body = b""
more_body = True
while more_body:
message = await receive()
assert message["type"] == "http.request"
body += message.get("body", b"")
more_body = message.get("more_body", False)
response = Response(b"Body: " + body, media_type="text/plain")
await response(scope, receive, send)

protocol = get_connected_protocol(app, http_protocol_cls)
protocol.data_received(CONNECTION_CLOSE_POST_REQUEST)
await protocol.loop.run_one()
assert b"HTTP/1.1 200 OK" in protocol.transport.buffer
assert b"Body: {'hello': 'world'}" in protocol.transport.buffer


async def test_iterator_headers(http_protocol_cls: HTTPProtocol):
async def app(scope: Scope, receive: ASGIReceiveCallable, send: ASGISendCallable):
headers = iter([(b"x-test-header", b"test value")])
Expand Down
4 changes: 2 additions & 2 deletions uvicorn/protocols/http/h11_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ def handle_events(self) -> None:
self.transport.resume_reading()
self.conn.start_next_cycle()
continue
if self.conn.their_state == h11.MUST_CLOSE:
break
self.cycle.more_body = False
self.cycle.message_event.set()
if self.conn.their_state == h11.MUST_CLOSE:
break

def handle_websocket_upgrade(self, event: h11.Request) -> None:
if self.logger.level <= TRACE_LOG_LEVEL: # pragma: full coverage
Expand Down

0 comments on commit 2f25107

Please sign in to comment.