Skip to content

Commit b97d9a9

Browse files
authored
Treat Accept-Encoding header as case-insensitive for gzip file check (#8127)
1 parent 2349cba commit b97d9a9

File tree

5 files changed

+13
-2
lines changed

5 files changed

+13
-2
lines changed

CHANGES/8104.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Treated values of ``Accept-Encoding`` header as case-insensitive when checking for gzip files -- by :user:`steverep`.

CONTRIBUTORS.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,7 @@ Stepan Pletnev
307307
Stephan Jaensch
308308
Stephen Cirelli
309309
Stephen Granade
310+
Steve Repsher
310311
Steven Seguin
311312
Sunghyun Hwang
312313
Sunit Deshpande

aiohttp/web_fileresponse.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,11 @@ def _get_file_path_stat_and_gzip(
142142

143143
async def prepare(self, request: "BaseRequest") -> Optional[AbstractStreamWriter]:
144144
loop = asyncio.get_event_loop()
145-
check_for_gzipped_file = "gzip" in request.headers.get(hdrs.ACCEPT_ENCODING, "")
145+
# Encoding comparisons should be case-insensitive
146+
# https://www.rfc-editor.org/rfc/rfc9110#section-8.4.1
147+
check_for_gzipped_file = (
148+
"gzip" in request.headers.get(hdrs.ACCEPT_ENCODING, "").lower()
149+
)
146150
filepath, st, gzip = await loop.run_in_executor(
147151
None, self._get_file_path_stat_and_gzip, check_for_gzipped_file
148152
)

aiohttp/web_response.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,8 @@ async def _start_compression(self, request: "BaseRequest") -> None:
326326
if self._compression_force:
327327
await self._do_start_compression(self._compression_force)
328328
else:
329+
# Encoding comparisons should be case-insensitive
330+
# https://www.rfc-editor.org/rfc/rfc9110#section-8.4.1
329331
accept_encoding = request.headers.get(hdrs.ACCEPT_ENCODING, "").lower()
330332
for coding in ContentCoding:
331333
if coding.value in accept_encoding:

tests/test_web_sendfile.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

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

1518
gz_filepath = mock.create_autospec(Path, spec_set=True)

0 commit comments

Comments
 (0)