From 7a6d4789f8e6e210383c015aa87a36ce5dafc7e1 Mon Sep 17 00:00:00 2001 From: Lee Jeonghun Date: Fri, 16 Oct 2020 23:04:31 +0900 Subject: [PATCH] Fix closing connection after HEAD request (#5012) * Fix closing connection after HEAD request * Fix a linter error Co-authored-by: Andrew Svetlov * Improve changes in a more elegant way Co-authored-by: Andrew Svetlov --- CHANGES/5012.bugfix | 1 + CONTRIBUTORS.txt | 1 + aiohttp/client_proto.py | 1 + tests/test_client_functional.py | 31 +++++++++++++++++++++++++++++++ 4 files changed, 34 insertions(+) create mode 100644 CHANGES/5012.bugfix diff --git a/CHANGES/5012.bugfix b/CHANGES/5012.bugfix new file mode 100644 index 00000000000..8c429c231f6 --- /dev/null +++ b/CHANGES/5012.bugfix @@ -0,0 +1 @@ +Fix connection closing issue in HEAD request. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 8b81ba5c6b7..5950d414ae1 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -140,6 +140,7 @@ Jakob Ackermann Jakub Wilk Jashandeep Sohi Jens Steinhauser +Jeonghun Lee Jeongkyu Shin Jeroen van der Heijden Jesus Cea diff --git a/aiohttp/client_proto.py b/aiohttp/client_proto.py index 5526bf26757..859398f4495 100644 --- a/aiohttp/client_proto.py +++ b/aiohttp/client_proto.py @@ -153,6 +153,7 @@ def set_response_params(self, *, timer: BaseTimerContext=None, self._parser = HttpResponseParser( self, self._loop, timer=timer, payload_exception=ClientPayloadError, + response_with_body=not skip_payload, read_until_eof=read_until_eof, auto_decompress=auto_decompress) diff --git a/tests/test_client_functional.py b/tests/test_client_functional.py index ecebea642d2..8fe0e2e1d4d 100644 --- a/tests/test_client_functional.py +++ b/tests/test_client_functional.py @@ -54,6 +54,37 @@ async def handler(request): assert 1 == len(client._session.connector._conns) +async def test_keepalive_after_head_requests_success( + aiohttp_client) -> None: + async def handler(request): + body = await request.read() + assert b'' == body + return web.Response(body=b'OK') + + cnt_conn_reuse = 0 + + async def on_reuseconn(session, ctx, params): + nonlocal cnt_conn_reuse + cnt_conn_reuse += 1 + + trace_config = aiohttp.TraceConfig() + trace_config._on_connection_reuseconn.append(on_reuseconn) + + app = web.Application() + app.router.add_route('GET', '/', handler) + + connector = aiohttp.TCPConnector(limit=1) + client = await aiohttp_client(app, connector=connector, + trace_configs=[trace_config]) + + resp1 = await client.head('/') + await resp1.read() + resp2 = await client.get('/') + await resp2.read() + + assert 1 == cnt_conn_reuse + + async def test_keepalive_response_released(aiohttp_client) -> None: async def handler(request): body = await request.read()