Skip to content

Commit

Permalink
Fix double compress when compression enabled and compressed file exists
Browse files Browse the repository at this point in the history
If the handler has `.enable_compression` set and we use the pre-compressed
.gz we would compress twice. The caller may not know if the `.gz` file
exists or not or have a mixed use case where some files are
pre-compressed and some are compressed on the fly based if they have
limited storage

fixes #8011
  • Loading branch information
bdraco committed Jan 9, 2024
1 parent 6529c77 commit 6c11675
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
4 changes: 4 additions & 0 deletions aiohttp/web_fileresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,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
26 changes: 26 additions & 0 deletions tests/test_web_sendfile_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,32 @@ async def handler(request):
await client.close()


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:
Expand Down

0 comments on commit 6c11675

Please sign in to comment.