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

Bump httpx and h11 #2048

Merged
merged 3 commits into from
Jul 17, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
-e .[standard]

# TODO: Remove this after h11 makes a release. By this writing, h11 was on 0.14.0.
# Core dependencies
h11 @ git+https://github.com/python-hyper/h11.git@master

# Explicit optionals
a2wsgi==1.7.0
wsproto==1.2.0
Expand All @@ -21,7 +25,7 @@ trustme==0.9.0
cryptography==41.0.2
coverage==7.2.7
coverage-conditional-plugin==0.9.0
httpx==0.23.0
httpx==0.24.1
watchgod==0.8.2

# Documentation
Expand Down
66 changes: 26 additions & 40 deletions uvicorn/protocols/http/h11_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@
)
from uvicorn.server import ServerState

H11Event = Union[
h11.Request,
h11.InformationalResponse,
h11.Response,
h11.Data,
h11.EndOfMessage,
h11.ConnectionClosed,
]


def _get_status_phrase(status_code: int) -> bytes:
try:
Expand Down Expand Up @@ -214,7 +205,7 @@ def handle_events(self) -> None:
self.flow.pause_reading()
break

elif event_type is h11.Request:
elif isinstance(event, h11.Request):
Kludex marked this conversation as resolved.
Show resolved Hide resolved
self.headers = [(key.lower(), value) for key, value in event.headers]
raw_path, _, query_string = event.target.partition(b"?")
self.scope = {
Expand Down Expand Up @@ -268,7 +259,7 @@ def handle_events(self) -> None:
task.add_done_callback(self.tasks.discard)
self.tasks.add(task)

elif event_type is h11.Data:
elif isinstance(event, h11.Data):
if self.conn.our_state is h11.DONE:
continue
self.cycle.body += event.data
Expand All @@ -284,7 +275,7 @@ def handle_events(self) -> None:
self.cycle.more_body = False
self.cycle.message_event.set()

def handle_websocket_upgrade(self, event: H11Event) -> None:
def handle_websocket_upgrade(self, event: h11.Request) -> None:
if self.logger.level <= TRACE_LOG_LEVEL:
prefix = "%s:%d - " % self.client if self.client else ""
self.logger.log(TRACE_LOG_LEVEL, "%sUpgrading to WebSocket", prefix)
Expand Down Expand Up @@ -312,12 +303,13 @@ def send_400_response(self, msg: str) -> None:
event = h11.Response(status_code=400, headers=headers, reason=reason)
output = self.conn.send(event)
self.transport.write(output)
event = h11.Data(data=msg.encode("ascii"))
output = self.conn.send(event)

output = self.conn.send(event=h11.Data(data=msg.encode("ascii")))
self.transport.write(output)
event = h11.EndOfMessage()
output = self.conn.send(event)

output = self.conn.send(event=h11.EndOfMessage())
self.transport.write(output)

self.transport.close()

def on_response_complete(self) -> None:
Expand Down Expand Up @@ -497,10 +489,10 @@ async def send(self, message: "ASGISendEvent") -> None:

# Write response status line and headers
reason = STATUS_PHRASES[status_code]
event = h11.Response(
status_code=status_code, headers=headers, reason=reason
)
output = self.conn.send(event)
# fmt: off
response = h11.Response(status_code=status_code, headers=headers, reason=reason) # noqa: E501
Kludex marked this conversation as resolved.
Show resolved Hide resolved
output = self.conn.send(event=response)
# fmt: on
self.transport.write(output)

elif not self.response_complete:
Expand All @@ -514,19 +506,16 @@ async def send(self, message: "ASGISendEvent") -> None:
more_body = message.get("more_body", False)

# Write response body
if self.scope["method"] == "HEAD":
event = h11.Data(data=b"")
else:
event = h11.Data(data=body)
output = self.conn.send(event)
data = b"" if self.scope["method"] == "HEAD" else body
output = self.conn.send(event=h11.Data(data=data))
self.transport.write(output)

# Handle response completion
if not more_body:
self.response_complete = True
self.message_event.set()
event = h11.EndOfMessage()
output = self.conn.send(event)
eom_event = h11.EndOfMessage()
output = self.conn.send(eom_event)
self.transport.write(output)

else:
Expand All @@ -543,10 +532,11 @@ async def send(self, message: "ASGISendEvent") -> None:

async def receive(self) -> "ASGIReceiveEvent":
if self.waiting_for_100_continue and not self.transport.is_closing():
event = h11.InformationalResponse(
status_code=100, headers=[], reason="Continue"
)
output = self.conn.send(event)
# fmt: off
headers: List[Tuple[str, str]] = []
informational_response = h11.InformationalResponse(status_code=100, headers=headers, reason="Continue") # noqa: E501
output = self.conn.send(event=informational_response)
# fmt: on
Kludex marked this conversation as resolved.
Show resolved Hide resolved
self.transport.write(output)
self.waiting_for_100_continue = False

Expand All @@ -557,13 +547,9 @@ async def receive(self) -> "ASGIReceiveEvent":

message: "Union[HTTPDisconnectEvent, HTTPRequestEvent]"
if self.disconnected or self.response_complete:
message = {"type": "http.disconnect"}
else:
message = {
"type": "http.request",
"body": self.body,
"more_body": self.more_body,
}
self.body = b""

return {"type": "http.disconnect"}
# fmt: off
message = {"type": "http.request", "body": self.body, "more_body": self.more_body} # noqa: E501
# fmt: on
self.body = b""
Copy link

Choose a reason for hiding this comment

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

While you're here, a comment about why we're clearing self.body might be nice

return message