Skip to content

Commit eea1bcc

Browse files
authored
Build a fresh asgi scope dict per request (#2977)
1 parent afed211 commit eea1bcc

5 files changed

Lines changed: 10 additions & 15 deletions

File tree

uvicorn/protocols/http/h11_impl.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
ASGI3Application,
1717
ASGIReceiveEvent,
1818
ASGISendEvent,
19-
ASGIVersions,
2019
HTTPRequestEvent,
2120
HTTPResponseBodyEvent,
2221
HTTPResponseStartEvent,
@@ -64,7 +63,7 @@ def __init__(
6463
)
6564
self.ws_protocol_class = config.ws_protocol_class
6665
self.root_path = config.root_path
67-
self.scope_asgi: ASGIVersions = {"version": config.asgi_version, "spec_version": "2.3"}
66+
self.asgi_version = config.asgi_version
6867
self.limit_concurrency = config.limit_concurrency
6968
self.app_state = app_state
7069

@@ -205,7 +204,7 @@ def handle_events(self) -> None:
205204
full_raw_path = self.root_path.encode("ascii") + raw_path
206205
self.scope = {
207206
"type": "http",
208-
"asgi": self.scope_asgi,
207+
"asgi": {"version": self.asgi_version, "spec_version": "2.3"},
209208
"http_version": event.http_version.decode("ascii"),
210209
"server": self.server,
211210
"client": self.client,

uvicorn/protocols/http/httptools_impl.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
ASGI3Application,
1919
ASGIReceiveEvent,
2020
ASGISendEvent,
21-
ASGIVersions,
2221
HTTPRequestEvent,
2322
HTTPScope,
2423
)
@@ -71,7 +70,7 @@ def __init__(
7170

7271
self.ws_protocol_class = config.ws_protocol_class
7372
self.root_path = config.root_path
74-
self.scope_asgi: ASGIVersions = {"version": config.asgi_version, "spec_version": "2.3"}
73+
self.asgi_version = config.asgi_version
7574
self.limit_concurrency = config.limit_concurrency
7675
self.app_state = app_state
7776

@@ -226,7 +225,7 @@ def on_message_begin(self) -> None:
226225
self.headers = []
227226
self.scope = { # type: ignore[typeddict-item]
228227
"type": "http",
229-
"asgi": self.scope_asgi,
228+
"asgi": {"version": self.asgi_version, "spec_version": "2.3"},
230229
"http_version": "1.1",
231230
"server": self.server,
232231
"client": self.client,

uvicorn/protocols/websockets/websockets_impl.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from uvicorn._types import (
2121
ASGI3Application,
2222
ASGISendEvent,
23-
ASGIVersions,
2423
WebSocketConnectEvent,
2524
WebSocketDisconnectEvent,
2625
WebSocketReceiveEvent,
@@ -70,7 +69,7 @@ def __init__(
7069
self.app = cast(ASGI3Application, config.loaded_app)
7170
self.loop = _loop or asyncio.get_event_loop()
7271
self.root_path = config.root_path
73-
self.scope_asgi: ASGIVersions = {"version": config.asgi_version, "spec_version": "2.4"}
72+
self.asgi_version = config.asgi_version
7473
self.app_state = app_state
7574

7675
# Shared server state
@@ -180,7 +179,7 @@ async def process_request(self, path: str, request_headers: Headers) -> HTTPResp
180179

181180
self.scope = {
182181
"type": "websocket",
183-
"asgi": self.scope_asgi,
182+
"asgi": {"version": self.asgi_version, "spec_version": "2.4"},
184183
"http_version": "1.1",
185184
"scheme": self.scheme,
186185
"server": self.server,

uvicorn/protocols/websockets/websockets_sansio_impl.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
from uvicorn._types import (
2121
ASGIReceiveEvent,
2222
ASGISendEvent,
23-
ASGIVersions,
2423
WebSocketScope,
2524
)
2625
from uvicorn.config import Config
@@ -57,7 +56,7 @@ def __init__(
5756
self.loop = _loop or asyncio.get_event_loop()
5857
self.logger = logging.getLogger("uvicorn.error")
5958
self.root_path = config.root_path
60-
self.scope_asgi: ASGIVersions = {"version": config.asgi_version, "spec_version": "2.4"}
59+
self.asgi_version = config.asgi_version
6160
self.app_state = app_state
6261

6362
# Shared server state
@@ -199,7 +198,7 @@ def handle_connect(self, event: Request) -> None:
199198
raw_path, _, query_string = event.path.partition("?")
200199
self.scope: WebSocketScope = {
201200
"type": "websocket",
202-
"asgi": self.scope_asgi,
201+
"asgi": {"version": self.asgi_version, "spec_version": "2.4"},
203202
"http_version": "1.1",
204203
"scheme": self.scheme,
205204
"server": self.server,

uvicorn/protocols/websockets/wsproto_impl.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from uvicorn._types import (
1919
ASGI3Application,
2020
ASGISendEvent,
21-
ASGIVersions,
2221
WebSocketEvent,
2322
WebSocketReceiveEvent,
2423
WebSocketScope,
@@ -82,7 +81,7 @@ def __init__(
8281
self.loop = _loop or asyncio.get_event_loop()
8382
self.logger = logging.getLogger("uvicorn.error")
8483
self.root_path = config.root_path
85-
self.scope_asgi: ASGIVersions = {"version": config.asgi_version, "spec_version": "2.4"}
84+
self.asgi_version = config.asgi_version
8685
self.app_state = app_state
8786

8887
# Shared server state
@@ -213,7 +212,7 @@ def handle_connect(self, event: events.Request) -> None:
213212
full_raw_path = self.root_path.encode("ascii") + raw_path.encode("ascii")
214213
self.scope: WebSocketScope = {
215214
"type": "websocket",
216-
"asgi": self.scope_asgi,
215+
"asgi": {"version": self.asgi_version, "spec_version": "2.4"},
217216
"http_version": "1.1",
218217
"scheme": self.scheme,
219218
"server": self.server,

0 commit comments

Comments
 (0)