Skip to content

Commit

Permalink
Fix double compress when compression enabled and compressed file exis…
Browse files Browse the repository at this point in the history
…ts (#8014)

(cherry picked from commit 92655a5)
  • Loading branch information
bdraco committed Jan 11, 2024
1 parent 8de4188 commit b090467
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/8014.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix double compress when compression enabled and compressed file exists
4 changes: 4 additions & 0 deletions aiohttp/web_fileresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,10 @@ async def prepare(self, request: "BaseRequest") -> Optional[AbstractStreamWriter
self.headers[hdrs.CONTENT_ENCODING] = encoding
if gzip:
self.headers[hdrs.VARY] = hdrs.ACCEPT_ENCODING
# Disable compression if we are already sending
# a compressed file since we don't want to double
# compress.
self._compression = False

self.etag = etag_value # type: ignore[assignment]
self.last_modified = st.st_mtime # type: ignore[assignment]
Expand Down
30 changes: 29 additions & 1 deletion tests/test_web_sendfile_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,35 @@ async def handler(request):
await client.close()


async def test_static_file_with_content_encoding(aiohttp_client, sender) -> None:
async def test_static_file_with_gziped_counter_part_enable_compression(
aiohttp_client: Any, sender: Any
):
"""Test that enable_compression does not double compress when a .gz file is also present."""
filepath = pathlib.Path(__file__).parent / "hello.txt"

async def handler(request):
resp = sender(filepath)
resp.enable_compression()
return resp

app = web.Application()
app.router.add_get("/", handler)
client = await aiohttp_client(app)

resp = await client.get("/")
assert resp.status == 200
body = await resp.read()
assert body == b"hello aiohttp\n"
assert resp.headers["Content-Type"] == "text/plain"
assert resp.headers.get("Content-Encoding") == "gzip"
resp.close()
await resp.release()
await client.close()


async def test_static_file_with_content_encoding(
aiohttp_client: Any, sender: Any
) -> None:
filepath = pathlib.Path(__file__).parent / "hello.txt.gz"

async def handler(request):
Expand Down

0 comments on commit b090467

Please sign in to comment.