From 9c36f5dd1811d9cfdd8183eac9ba9433d02f819c Mon Sep 17 00:00:00 2001 From: Levin Rickert Date: Tue, 22 Feb 2022 12:01:21 +0100 Subject: [PATCH] Fix access logs not containing root path Fixes #1384 --- uvicorn/protocols/utils.py | 2 +- uvicorn/protocols/websockets/websockets_impl.py | 11 ++++++++--- uvicorn/protocols/websockets/wsproto_impl.py | 11 ++++++++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/uvicorn/protocols/utils.py b/uvicorn/protocols/utils.py index 1da733600..25139b14d 100644 --- a/uvicorn/protocols/utils.py +++ b/uvicorn/protocols/utils.py @@ -46,7 +46,7 @@ def get_client_addr(scope: WWWScope) -> str: def get_path_with_query_string(scope: WWWScope) -> str: - path_with_query_string = urllib.parse.quote(scope["path"]) + path_with_query_string = urllib.parse.quote(scope["raw_path"]) if scope["query_string"]: path_with_query_string = "{}?{}".format( path_with_query_string, scope["query_string"].decode("ascii") diff --git a/uvicorn/protocols/websockets/websockets_impl.py b/uvicorn/protocols/websockets/websockets_impl.py index a918c6f9d..90f03cb48 100644 --- a/uvicorn/protocols/websockets/websockets_impl.py +++ b/uvicorn/protocols/websockets/websockets_impl.py @@ -7,7 +7,12 @@ from websockets.extensions.permessage_deflate import ServerPerMessageDeflateFactory from uvicorn.logging import TRACE_LOG_LEVEL -from uvicorn.protocols.utils import get_local_addr, get_remote_addr, is_ssl +from uvicorn.protocols.utils import ( + get_local_addr, + get_path_with_query_string, + get_remote_addr, + is_ssl, +) class Server: @@ -212,7 +217,7 @@ async def asgi_send(self, message): self.logger.info( '%s - "WebSocket %s" [accepted]', self.scope["client"], - self.scope["path"], + get_path_with_query_string(self.scope), ) self.initial_response = None self.accepted_subprotocol = message.get("subprotocol") @@ -229,7 +234,7 @@ async def asgi_send(self, message): self.logger.info( '%s - "WebSocket %s" 403', self.scope["client"], - self.scope["path"], + get_path_with_query_string(self.scope), ) self.initial_response = (http.HTTPStatus.FORBIDDEN, [], b"") self.handshake_started_event.set() diff --git a/uvicorn/protocols/websockets/wsproto_impl.py b/uvicorn/protocols/websockets/wsproto_impl.py index a76977fb2..613372ea8 100644 --- a/uvicorn/protocols/websockets/wsproto_impl.py +++ b/uvicorn/protocols/websockets/wsproto_impl.py @@ -10,7 +10,12 @@ from wsproto.utilities import RemoteProtocolError from uvicorn.logging import TRACE_LOG_LEVEL -from uvicorn.protocols.utils import get_local_addr, get_remote_addr, is_ssl +from uvicorn.protocols.utils import ( + get_local_addr, + get_path_with_query_string, + get_remote_addr, + is_ssl, +) class WSProtocol(asyncio.Protocol): @@ -244,7 +249,7 @@ async def send(self, message): self.logger.info( '%s - "WebSocket %s" [accepted]', self.scope["client"], - self.scope["path"], + get_path_with_query_string(self.scope), ) self.handshake_complete = True subprotocol = message.get("subprotocol") @@ -266,7 +271,7 @@ async def send(self, message): self.logger.info( '%s - "WebSocket %s" 403', self.scope["client"], - self.scope["path"], + get_path_with_query_string(self.scope), ) self.handshake_complete = True self.close_sent = True