-
Notifications
You must be signed in to change notification settings - Fork 804
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: mounted apps don't eagerly match path prefix
Signed-off-by: Frost Ming <[email protected]>
- Loading branch information
Showing
2 changed files
with
29 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from __future__ import annotations | ||
|
||
from starlette.applications import Starlette | ||
from starlette.routing import Match | ||
from starlette.routing import Mount | ||
from starlette.types import Scope | ||
|
||
|
||
class PassiveMount(Mount): | ||
"""A subclass of mount that doesn't match the path prefix eagerly.""" | ||
|
||
def matches(self, scope: Scope) -> tuple[Match, Scope]: | ||
match, child_scope = super().matches(scope) | ||
if match == Match.FULL and isinstance(self.app, Starlette): | ||
scope = {**scope, **child_scope} | ||
partial_match: Match | None = None | ||
for route in self.app.routes: | ||
child_match, _ = route.matches(scope) | ||
if child_match == Match.FULL: | ||
return child_match, child_scope | ||
if child_match == Match.PARTIAL and partial_match is None: | ||
partial_match = child_match | ||
|
||
if partial_match is not None: | ||
return partial_match, child_scope | ||
return Match.NONE, child_scope | ||
return match, child_scope |