From bf4933942ca844e87cdc84495db2012cce5c8db0 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Thu, 22 Feb 2024 15:56:11 -0800 Subject: [PATCH] run-dev: Abbreviate error message when receiving an https request. Signed-off-by: Anders Kaseorg --- tools/run-dev | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tools/run-dev b/tools/run-dev index 9963e4ac3d..1998b66db6 100755 --- a/tools/run-dev +++ b/tools/run-dev @@ -20,6 +20,7 @@ sanity_check.check_venv(__file__) import aiohttp from aiohttp import hdrs, web +from aiohttp.http_exceptions import BadStatusLine from returns.curry import partial from tools.lib.test_script import add_provision_check_override_param, assert_provisioning_status_ok @@ -306,6 +307,27 @@ def print_listeners() -> None: print() +def https_log_filter(record: logging.LogRecord) -> bool: + # aiohttp emits an exception with a traceback when receiving an + # https request (https://github.com/aio-libs/aiohttp/issues/8065). + # Abbreviate it to a one-line message. + if ( + record.exc_info is not None + and isinstance(error := record.exc_info[1], BadStatusLine) + and error.message.startswith( + ( + "Invalid method encountered:\n\n b'\\x16", + 'Invalid method encountered:\n\n b"\\x16', + ) + ) + ): + record.msg = "Rejected https request (this development server only supports http)" + record.exc_info = None + return True + + +logging.getLogger("aiohttp.server").addFilter(https_log_filter) + runner: web.AppRunner children: List["subprocess.Popen[bytes]"] = []