Skip to content

Commit

Permalink
Handle root_path in URLRouter
Browse files Browse the repository at this point in the history
If we have a scope["root_path"], raise if it does not prefix
scope["path"], and strip it from scope["path"] if it does. Do this
only if we're in an outermost URLRouter.

Signed-off-by: Alejandro R. Sedeño <asedeno@mit.edu>
Signed-off-by: Alejandro R Sedeño <asedeno@google.com>
  • Loading branch information
asedeno authored and carltongibson committed Apr 3, 2024
1 parent c84fd58 commit b7032d7
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions channels/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,15 @@ async def __call__(self, scope, receive, send):
path = scope.get("path_remaining", scope.get("path", None))
if path is None:
raise ValueError("No 'path' key in connection scope, cannot route URLs")

if "path_remaining" not in scope:
# We are the outermost URLRouter, so handle root_path if present.
root_path = scope.get("root_path", "")
if root_path and not path.startswith(root_path):
# If root_path is present, path must start with it.
raise ValueError("No route found for path %r." % path)
path = path[len(root_path) :]

# Remove leading / to match Django's handling
path = path.lstrip("/")
# Run through the routes we have until one matches
Expand Down

0 comments on commit b7032d7

Please sign in to comment.