Skip to content

Commit 6cb9c5c

Browse files
committed
Bugfix correct the dispatcher middleware root path and path
The path shouldn't have been adjusted, rather the root_path should have been changed - this matches WSGI whereby the script_name is changed. In addition the changes should have been made in a copied scope to isolate them.
1 parent 84d06b8 commit 6cb9c5c

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

src/hypercorn/middleware/dispatcher.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ async def __call__(self, scope: Scope, receive: Callable, send: Callable) -> Non
2020
else:
2121
for path, app in self.mounts.items():
2222
if scope["path"].startswith(path):
23-
scope["path"] = scope["path"][len(path) :] or "/"
24-
return await app(scope, receive, send)
23+
local_scope = scope.copy()
24+
local_scope["root_path"] += path
25+
return await app(local_scope, receive, send)
2526
await send(
2627
{
2728
"type": "http.response.start",

tests/middleware/test_dispatcher.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ async def send(message: dict) -> None:
4040
await app({**http_scope, **{"path": "/api/b"}}, None, send) # type: ignore
4141
await app({**http_scope, **{"path": "/"}}, None, send) # type: ignore
4242
assert sent_events == [
43-
{"type": "http.response.start", "status": 200, "headers": [(b"content-length", b"7")]},
44-
{"type": "http.response.body", "body": b"apix-/b"},
45-
{"type": "http.response.start", "status": 200, "headers": [(b"content-length", b"6")]},
46-
{"type": "http.response.body", "body": b"api-/b"},
43+
{"type": "http.response.start", "status": 200, "headers": [(b"content-length", b"13")]},
44+
{"type": "http.response.body", "body": b"apix-/api/x/b"},
45+
{"type": "http.response.start", "status": 200, "headers": [(b"content-length", b"10")]},
46+
{"type": "http.response.body", "body": b"api-/api/b"},
4747
{"type": "http.response.start", "status": 404, "headers": [(b"content-length", b"0")]},
4848
{"type": "http.response.body"},
4949
]

0 commit comments

Comments
 (0)