Skip to content

Commit

Permalink
Fix closing connection after HEAD request (#5012)
Browse files Browse the repository at this point in the history
* Fix closing connection after HEAD request

* Fix a linter error

Co-authored-by: Andrew Svetlov <[email protected]>

* Improve changes in a more elegant way

Co-authored-by: Andrew Svetlov <[email protected]>
  • Loading branch information
leejeonghun and asvetlov committed Oct 16, 2020
1 parent 9983227 commit 7a6d478
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES/5012.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix connection closing issue in HEAD request.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ Jakob Ackermann
Jakub Wilk
Jashandeep Sohi
Jens Steinhauser
Jeonghun Lee
Jeongkyu Shin
Jeroen van der Heijden
Jesus Cea
Expand Down
1 change: 1 addition & 0 deletions aiohttp/client_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
31 changes: 31 additions & 0 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 7a6d478

Please sign in to comment.