Skip to content

Commit 9be3116

Browse files
authored
Bump httpx and h11 (#2048)
1 parent cd18c3b commit 9be3116

File tree

2 files changed

+36
-51
lines changed

2 files changed

+36
-51
lines changed

‎requirements.txt‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
-e .[standard]
22

3+
# TODO: Remove this after h11 makes a release. By this writing, h11 was on 0.14.0.
4+
# Core dependencies
5+
h11 @ git+https://github.com/python-hyper/h11.git@master
6+
37
# Explicit optionals
48
a2wsgi==1.7.0
59
wsproto==1.2.0
@@ -21,7 +25,7 @@ trustme==0.9.0
2125
cryptography==41.0.2
2226
coverage==7.2.7
2327
coverage-conditional-plugin==0.9.0
24-
httpx==0.23.0
28+
httpx==0.24.1
2529
watchgod==0.8.2
2630

2731
# Documentation

‎uvicorn/protocols/http/h11_impl.py‎

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
Literal,
1010
Optional,
1111
Tuple,
12-
Union,
1312
cast,
1413
)
1514
from urllib.parse import unquote
@@ -21,7 +20,6 @@
2120
ASGI3Application,
2221
ASGIReceiveEvent,
2322
ASGISendEvent,
24-
HTTPDisconnectEvent,
2523
HTTPRequestEvent,
2624
HTTPResponseBodyEvent,
2725
HTTPResponseStartEvent,
@@ -44,15 +42,6 @@
4442
)
4543
from uvicorn.server import ServerState
4644

47-
H11Event = Union[
48-
h11.Request,
49-
h11.InformationalResponse,
50-
h11.Response,
51-
h11.Data,
52-
h11.EndOfMessage,
53-
h11.ConnectionClosed,
54-
]
55-
5645

5746
def _get_status_phrase(status_code: int) -> bytes:
5847
try:
@@ -201,20 +190,19 @@ def handle_events(self) -> None:
201190
self.logger.warning(msg)
202191
self.send_400_response(msg)
203192
return
204-
event_type = type(event)
205193

206-
if event_type is h11.NEED_DATA:
194+
if event is h11.NEED_DATA:
207195
break
208196

209-
elif event_type is h11.PAUSED:
197+
elif event is h11.PAUSED:
210198
# This case can occur in HTTP pipelining, so we need to
211199
# stop reading any more data, and ensure that at the end
212200
# of the active request/response cycle we handle any
213201
# events that have been buffered up.
214202
self.flow.pause_reading()
215203
break
216204

217-
elif event_type is h11.Request:
205+
elif isinstance(event, h11.Request):
218206
self.headers = [(key.lower(), value) for key, value in event.headers]
219207
raw_path, _, query_string = event.target.partition(b"?")
220208
self.scope = {
@@ -268,23 +256,23 @@ def handle_events(self) -> None:
268256
task.add_done_callback(self.tasks.discard)
269257
self.tasks.add(task)
270258

271-
elif event_type is h11.Data:
259+
elif isinstance(event, h11.Data):
272260
if self.conn.our_state is h11.DONE:
273261
continue
274262
self.cycle.body += event.data
275263
if len(self.cycle.body) > HIGH_WATER_LIMIT:
276264
self.flow.pause_reading()
277265
self.cycle.message_event.set()
278266

279-
elif event_type is h11.EndOfMessage:
267+
elif isinstance(event, h11.EndOfMessage):
280268
if self.conn.our_state is h11.DONE:
281269
self.transport.resume_reading()
282270
self.conn.start_next_cycle()
283271
continue
284272
self.cycle.more_body = False
285273
self.cycle.message_event.set()
286274

287-
def handle_websocket_upgrade(self, event: H11Event) -> None:
275+
def handle_websocket_upgrade(self, event: h11.Request) -> None:
288276
if self.logger.level <= TRACE_LOG_LEVEL:
289277
prefix = "%s:%d - " % self.client if self.client else ""
290278
self.logger.log(TRACE_LOG_LEVEL, "%sUpgrading to WebSocket", prefix)
@@ -305,19 +293,20 @@ def handle_websocket_upgrade(self, event: H11Event) -> None:
305293

306294
def send_400_response(self, msg: str) -> None:
307295
reason = STATUS_PHRASES[400]
308-
headers = [
296+
headers: List[Tuple[bytes, bytes]] = [
309297
(b"content-type", b"text/plain; charset=utf-8"),
310298
(b"connection", b"close"),
311299
]
312300
event = h11.Response(status_code=400, headers=headers, reason=reason)
313301
output = self.conn.send(event)
314302
self.transport.write(output)
315-
event = h11.Data(data=msg.encode("ascii"))
316-
output = self.conn.send(event)
303+
304+
output = self.conn.send(event=h11.Data(data=msg.encode("ascii")))
317305
self.transport.write(output)
318-
event = h11.EndOfMessage()
319-
output = self.conn.send(event)
306+
307+
output = self.conn.send(event=h11.EndOfMessage())
320308
self.transport.write(output)
309+
321310
self.transport.close()
322311

323312
def on_response_complete(self) -> None:
@@ -479,7 +468,7 @@ async def send(self, message: "ASGISendEvent") -> None:
479468
self.response_started = True
480469
self.waiting_for_100_continue = False
481470

482-
status_code = message["status"]
471+
status = message["status"]
483472
headers = self.default_headers + list(message.get("headers", []))
484473

485474
if CLOSE_HEADER in self.scope["headers"] and CLOSE_HEADER not in headers:
@@ -492,15 +481,13 @@ async def send(self, message: "ASGISendEvent") -> None:
492481
self.scope["method"],
493482
get_path_with_query_string(self.scope),
494483
self.scope["http_version"],
495-
status_code,
484+
status,
496485
)
497486

498487
# Write response status line and headers
499-
reason = STATUS_PHRASES[status_code]
500-
event = h11.Response(
501-
status_code=status_code, headers=headers, reason=reason
502-
)
503-
output = self.conn.send(event)
488+
reason = STATUS_PHRASES[status]
489+
response = h11.Response(status_code=status, headers=headers, reason=reason)
490+
output = self.conn.send(event=response)
504491
self.transport.write(output)
505492

506493
elif not self.response_complete:
@@ -514,19 +501,15 @@ async def send(self, message: "ASGISendEvent") -> None:
514501
more_body = message.get("more_body", False)
515502

516503
# Write response body
517-
if self.scope["method"] == "HEAD":
518-
event = h11.Data(data=b"")
519-
else:
520-
event = h11.Data(data=body)
521-
output = self.conn.send(event)
504+
data = b"" if self.scope["method"] == "HEAD" else body
505+
output = self.conn.send(event=h11.Data(data=data))
522506
self.transport.write(output)
523507

524508
# Handle response completion
525509
if not more_body:
526510
self.response_complete = True
527511
self.message_event.set()
528-
event = h11.EndOfMessage()
529-
output = self.conn.send(event)
512+
output = self.conn.send(event=h11.EndOfMessage())
530513
self.transport.write(output)
531514

532515
else:
@@ -536,17 +519,17 @@ async def send(self, message: "ASGISendEvent") -> None:
536519

537520
if self.response_complete:
538521
if self.conn.our_state is h11.MUST_CLOSE or not self.keep_alive:
539-
event = h11.ConnectionClosed()
540-
self.conn.send(event)
522+
self.conn.send(event=h11.ConnectionClosed())
541523
self.transport.close()
542524
self.on_response()
543525

544526
async def receive(self) -> "ASGIReceiveEvent":
545527
if self.waiting_for_100_continue and not self.transport.is_closing():
528+
headers: List[Tuple[str, str]] = []
546529
event = h11.InformationalResponse(
547-
status_code=100, headers=[], reason="Continue"
530+
status_code=100, headers=headers, reason="Continue"
548531
)
549-
output = self.conn.send(event)
532+
output = self.conn.send(event=event)
550533
self.transport.write(output)
551534
self.waiting_for_100_continue = False
552535

@@ -555,15 +538,13 @@ async def receive(self) -> "ASGIReceiveEvent":
555538
await self.message_event.wait()
556539
self.message_event.clear()
557540

558-
message: "Union[HTTPDisconnectEvent, HTTPRequestEvent]"
559541
if self.disconnected or self.response_complete:
560-
message = {"type": "http.disconnect"}
561-
else:
562-
message = {
563-
"type": "http.request",
564-
"body": self.body,
565-
"more_body": self.more_body,
566-
}
567-
self.body = b""
542+
return {"type": "http.disconnect"}
568543

544+
message: "HTTPRequestEvent" = {
545+
"type": "http.request",
546+
"body": self.body,
547+
"more_body": self.more_body,
548+
}
549+
self.body = b""
569550
return message

0 commit comments

Comments
 (0)