From 1a01c3c2217736d5374c6e9d8593a123d40a0476 Mon Sep 17 00:00:00 2001 From: Ricardo Madriz Date: Sun, 18 Feb 2024 12:48:27 -0600 Subject: [PATCH] Modify the scope to include root_path in path and raw_path --- fastapi/applications.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/fastapi/applications.py b/fastapi/applications.py index 6d427cdc2..5ef22b432 100644 --- a/fastapi/applications.py +++ b/fastapi/applications.py @@ -1050,7 +1050,20 @@ class FastAPI(Starlette): async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: if self.root_path: + root_path = scope.get("root_path", "") + if root_path and self.root_path != root_path: + raise RuntimeError( + f"The ASGI server is using a different root path than the one " + f"configured in FastAPI. The configured root path is: " + f"{self.root_path}, the ASGI server root path is: {root_path}. " + ) scope["root_path"] = self.root_path + path = scope.get("path") + if path and not path.startswith(self.root_path): + scope["path"] = self.root_path + path + raw_path: bytes | None = scope.get("raw_path") + if raw_path and not raw_path.startswith(self.root_path.encode()): + scope["raw_path"] = self.root_path.encode() + raw_path await super().__call__(scope, receive, send) def add_api_route(