From 90acede056f75a337ef023c4ae335d0d6e33d142 Mon Sep 17 00:00:00 2001 From: euri10 Date: Fri, 11 Feb 2022 12:29:09 +0100 Subject: [PATCH] Moved warning about missing ws library only in upgrade case (#1370) --- scripts/coverage | 2 +- uvicorn/protocols/http/h11_impl.py | 11 +++++------ uvicorn/protocols/http/httptools_impl.py | 11 +++++------ 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/scripts/coverage b/scripts/coverage index ec9f1b8d3..0ff7aae74 100755 --- a/scripts/coverage +++ b/scripts/coverage @@ -8,4 +8,4 @@ export SOURCE_FILES="uvicorn tests" set -x -${PREFIX}coverage report --show-missing --skip-covered --fail-under=96.93 +${PREFIX}coverage report --show-missing --skip-covered --fail-under=96.87 diff --git a/uvicorn/protocols/http/h11_impl.py b/uvicorn/protocols/http/h11_impl.py index 6c1520f05..a81aaea5d 100644 --- a/uvicorn/protocols/http/h11_impl.py +++ b/uvicorn/protocols/http/h11_impl.py @@ -225,6 +225,11 @@ def handle_upgrade(self, event): if upgrade_value != b"websocket" or self.ws_protocol_class is None: msg = "Unsupported upgrade request." self.logger.warning(msg) + from uvicorn.protocols.websockets.auto import AutoWebSocketsProtocol + + if AutoWebSocketsProtocol is None: # pragma: no cover + msg = "No supported WebSocket library detected. Please use 'pip install uvicorn[standard]', or install 'websockets' or 'wsproto' manually." # noqa: E501 + self.logger.warning(msg) self.send_400_response(msg) return @@ -246,12 +251,6 @@ def handle_upgrade(self, event): def send_400_response(self, msg: str): - from uvicorn.protocols.websockets.auto import AutoWebSocketsProtocol - - if AutoWebSocketsProtocol is None: - msg = "No supported WebSocket library detected. Please use 'pip install uvicorn[standard]', or install 'websockets' or 'wsproto' manually." # noqa: E501 - self.logger.warning(msg) - reason = STATUS_PHRASES[400] headers = [ (b"content-type", b"text/plain; charset=utf-8"), diff --git a/uvicorn/protocols/http/httptools_impl.py b/uvicorn/protocols/http/httptools_impl.py index 78afec0de..838c177bb 100644 --- a/uvicorn/protocols/http/httptools_impl.py +++ b/uvicorn/protocols/http/httptools_impl.py @@ -140,6 +140,11 @@ def handle_upgrade(self): if upgrade_value != b"websocket" or self.ws_protocol_class is None: msg = "Unsupported upgrade request." self.logger.warning(msg) + from uvicorn.protocols.websockets.auto import AutoWebSocketsProtocol + + if AutoWebSocketsProtocol is None: # pragma: no cover + msg = "No supported WebSocket library detected. Please use 'pip install uvicorn[standard]', or install 'websockets' or 'wsproto' manually." # noqa: E501 + self.logger.warning(msg) self.send_400_response(msg) return @@ -162,12 +167,6 @@ def handle_upgrade(self): def send_400_response(self, msg: str): - from uvicorn.protocols.websockets.auto import AutoWebSocketsProtocol - - if AutoWebSocketsProtocol is None: - msg = "No supported WebSocket library detected. Please use 'pip install uvicorn[standard]', or install 'websockets' or 'wsproto' manually." # noqa: E501 - self.logger.warning(msg) - content = [STATUS_LINE[400]] for name, value in self.default_headers: content.extend([name, b": ", value, b"\r\n"])