Skip to content

Commit

Permalink
Merge branch 'master' into new-exception-when-redirect-url-is-invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
setla authored Feb 3, 2024
2 parents 9733022 + 835ea8f commit 67ab859
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES/8104.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Treated values of ``Accept-Encoding`` header as case-insensitive when checking for gzip files -- by :user:`steverep`.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,7 @@ Stepan Pletnev
Stephan Jaensch
Stephen Cirelli
Stephen Granade
Steve Repsher
Steven Seguin
Sunghyun Hwang
Sunit Deshpande
Expand Down
6 changes: 5 additions & 1 deletion aiohttp/web_fileresponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,11 @@ def _get_file_path_stat_and_gzip(

async def prepare(self, request: "BaseRequest") -> Optional[AbstractStreamWriter]:
loop = asyncio.get_event_loop()
check_for_gzipped_file = "gzip" in request.headers.get(hdrs.ACCEPT_ENCODING, "")
# Encoding comparisons should be case-insensitive
# https://www.rfc-editor.org/rfc/rfc9110#section-8.4.1
check_for_gzipped_file = (
"gzip" in request.headers.get(hdrs.ACCEPT_ENCODING, "").lower()
)
filepath, st, gzip = await loop.run_in_executor(
None, self._get_file_path_stat_and_gzip, check_for_gzipped_file
)
Expand Down
3 changes: 2 additions & 1 deletion aiohttp/web_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,6 @@ def enable_chunked_encoding(self) -> None:

def enable_compression(self, force: Optional[ContentCoding] = None) -> None:
"""Enables response compression encoding."""
# Backwards compatibility for when force was a bool <0.17.
self._compression = True
self._compression_force = force

Expand Down Expand Up @@ -326,6 +325,8 @@ async def _start_compression(self, request: "BaseRequest") -> None:
if self._compression_force:
await self._do_start_compression(self._compression_force)
else:
# Encoding comparisons should be case-insensitive
# https://www.rfc-editor.org/rfc/rfc9110#section-8.4.1
accept_encoding = request.headers.get(hdrs.ACCEPT_ENCODING, "").lower()
for coding in ContentCoding:
if coding.value in accept_encoding:
Expand Down
5 changes: 4 additions & 1 deletion tests/test_web_sendfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@

def test_using_gzip_if_header_present_and_file_available(loop: Any) -> None:
request = make_mocked_request(
"GET", "http://python.org/logo.png", headers={hdrs.ACCEPT_ENCODING: "gzip"}
"GET",
"http://python.org/logo.png",
# Header uses some uppercase to ensure case-insensitive treatment
headers={hdrs.ACCEPT_ENCODING: "GZip"},
)

gz_filepath = mock.create_autospec(Path, spec_set=True)
Expand Down

0 comments on commit 67ab859

Please sign in to comment.