Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 0.30.4 issue with connection close header #2408

Merged
merged 4 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions tests/protocols/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@

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_WITH_BODY = 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'}",
]
)

CONNECTION_CLOSE_POST_REQUEST_WITHOUT_BODY = b"\r\n".join(
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this one. The above case is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this case of POST without body. Kept POST with body and connection close header

[b"POST / HTTP/1.1", b"Host: example.org", b"Connection: close", b"", b""]
)

REQUEST_AFTER_CONNECTION_CLOSE = b"\r\n".join(
[
b"GET / HTTP/1.1",
Expand Down Expand Up @@ -998,8 +1014,8 @@ async def test_return_close_header(http_protocol_cls: HTTPProtocol):


async def test_close_connection_with_multiple_requests(http_protocol_cls: HTTPProtocol):
app = Response("Hello, world", media_type="text/plain")

response_content = b"Hello, world"
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't add a diff here please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed diffs

app = Response(response_content, media_type="text/plain")
protocol = get_connected_protocol(app, http_protocol_cls)
protocol.data_received(REQUEST_AFTER_CONNECTION_CLOSE)
await protocol.loop.run_one()
Expand All @@ -1009,6 +1025,29 @@ async def test_close_connection_with_multiple_requests(http_protocol_cls: HTTPPr
# NOTE: We need to use `.lower()` because H11 implementation doesn't allow Uvicorn
# to lowercase them. See: https://github.com/python-hyper/h11/issues/156
assert b"connection: close" in protocol.transport.buffer.lower()
assert response_content in protocol.transport.buffer


@pytest.mark.parametrize(
"req_body", [CONNECTION_CLOSE_POST_REQUEST_WITH_BODY, CONNECTION_CLOSE_POST_REQUEST_WITHOUT_BODY]
)
async def test_close_connection_with_post_request(req_body: bytes, 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(req_body)
await protocol.loop.run_one()
assert b"HTTP/1.1 200 OK" in protocol.transport.buffer
assert req_body.split(b"\r\n")[-1] in protocol.transport.buffer


async def test_iterator_headers(http_protocol_cls: HTTPProtocol):
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
Loading