From bbf33a7a1443a9436bbcd65555226ca0d3cbacfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alejandro=20R=2E=20Sede=C3=B1o?= Date: Tue, 15 Nov 2022 18:09:14 -0500 Subject: [PATCH] Handle root_path in URLRouter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Signed-off-by: Alejandro R Sedeño --- channels/routing.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/channels/routing.py b/channels/routing.py index efb428ac..5eaa78eb 100644 --- a/channels/routing.py +++ b/channels/routing.py @@ -100,6 +100,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