Skip to content

Commit da9210b

Browse files
[PR #10076/fad44f6b backport][3.11] Fix request count check for BadHttpMethod (#10081)
Co-authored-by: J. Nick Koston <nick@koston.org>
1 parent 8b08c9e commit da9210b

File tree

5 files changed

+20
-5
lines changed

5 files changed

+20
-5
lines changed

CHANGES/10055.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
10076.bugfix.rst

CHANGES/10076.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed invalid method logging unexpected being logged at exception level on subsequent connections -- by :user:`bdraco`.

aiohttp/web_protocol.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ def __init__(
192192
):
193193
super().__init__(loop)
194194

195+
# _request_count is the number of requests processed with the same connection.
195196
self._request_count = 0
196197
self._keepalive = False
197198
self._current_request: Optional[BaseRequest] = None
@@ -688,11 +689,7 @@ def handle_error(
688689
Returns HTTP response with specific status code. Logs additional
689690
information. It always closes current connection.
690691
"""
691-
if (
692-
self._manager
693-
and self._manager.requests_count == 1
694-
and isinstance(exc, BadHttpMethod)
695-
):
692+
if self._request_count == 1 and isinstance(exc, BadHttpMethod):
696693
# BadHttpMethod is common when a client sends non-HTTP
697694
# or encrypted traffic to an HTTP port. This is expected
698695
# to happen when connected to the public internet so we log

aiohttp/web_server.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ def __init__(
2525
self._loop = loop or asyncio.get_running_loop()
2626
self._connections: Dict[RequestHandler, asyncio.Transport] = {}
2727
self._kwargs = kwargs
28+
# requests_count is the number of requests being processed by the server
29+
# for the lifetime of the server.
2830
self.requests_count = 0
2931
self.request_handler = handler
3032
self.request_factory = request_factory or self._make_request

tests/test_web_server.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ async def handler(request: web.BaseRequest) -> NoReturn:
8686
# be probing for TLS/SSL support which is
8787
# expected to fail
8888
logger.debug.assert_called_with("Error handling request", exc_info=exc)
89+
logger.debug.reset_mock()
90+
91+
# Now make another connection to the server
92+
# to make sure that the exception is logged
93+
# at debug on a second fresh connection
94+
cli2 = await aiohttp_client(server)
95+
resp = await cli2.get("/path/to")
96+
assert resp.status == 500
97+
assert resp.headers["Content-Type"].startswith("text/plain")
98+
# BadHttpMethod should be logged as debug
99+
# on the first request since the client may
100+
# be probing for TLS/SSL support which is
101+
# expected to fail
102+
logger.debug.assert_called_with("Error handling request", exc_info=exc)
89103

90104

91105
async def test_raw_server_logs_invalid_method_without_loop_debug(

0 commit comments

Comments
 (0)