From 1e2f00c8d195c3e99c685a8dad854321472b6945 Mon Sep 17 00:00:00 2001 From: Levin Rickert Date: Sun, 11 Sep 2022 15:38:13 +0200 Subject: [PATCH] Use path with query string on WebSockets logs (#1385) Co-authored-by: Marcelo Trylesinski --- uvicorn/protocols/websockets/websockets_impl.py | 11 ++++++++--- uvicorn/protocols/websockets/wsproto_impl.py | 11 ++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/uvicorn/protocols/websockets/websockets_impl.py b/uvicorn/protocols/websockets/websockets_impl.py index eea7a65bfc..b77485136b 100644 --- a/uvicorn/protocols/websockets/websockets_impl.py +++ b/uvicorn/protocols/websockets/websockets_impl.py @@ -15,7 +15,12 @@ from uvicorn.config import Config 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, +) from uvicorn.server import ServerState if sys.version_info < (3, 8): @@ -254,7 +259,7 @@ async def asgi_send(self, message: "ASGISendEvent") -> None: 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 = cast( @@ -274,7 +279,7 @@ async def asgi_send(self, message: "ASGISendEvent") -> None: 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 37c0abb33f..8170b15164 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): @@ -246,7 +251,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") @@ -268,7 +273,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