Skip to content

Commit

Permalink
Fix badly encoded charset exception instead of falling back to detect…
Browse files Browse the repository at this point in the history
…or (#9160)
  • Loading branch information
PLPeeters authored Sep 17, 2024
1 parent c17afe4 commit 8ad118b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES/9160.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed badly encoded charset crashing when getting response text instead of falling back to charset detector.
1 change: 1 addition & 0 deletions CONTRIBUTORS.txt
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ Pawel Kowalski
Pawel Miech
Pepe Osca
Philipp A.
Pierre-Louis Peeters
Pieter van Beek
Qiao Han
Rafael Viotti
Expand Down
2 changes: 1 addition & 1 deletion aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ def get_encoding(self) -> str:

encoding = mimetype.parameters.get("charset")
if encoding:
with contextlib.suppress(LookupError):
with contextlib.suppress(LookupError, ValueError):
return codecs.lookup(encoding).name

if mimetype.type == "application" and (
Expand Down
32 changes: 32 additions & 0 deletions tests/test_client_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,38 @@ def side_effect(*args: object, **kwargs: object) -> "asyncio.Future[bytes]":
assert response._connection is None


async def test_text_badly_encoded_encoding_header(
loop: asyncio.AbstractEventLoop, session: ClientSession
) -> None:
session._resolve_charset = lambda *_: "utf-8"
response = ClientResponse(
"get",
URL("http://def-cl-resp.org"),
request_info=mock.Mock(),
writer=WriterMock(),
continue100=None,
timer=TimerNoop(),
traces=[],
loop=loop,
session=session,
)

def side_effect(*args: object, **kwargs: object) -> "asyncio.Future[bytes]":
fut = loop.create_future()
fut.set_result(b"foo")
return fut

h = {"Content-Type": "text/html; charset=\udc81gutf-8\udc81\udc8d"}
response._headers = CIMultiDictProxy(CIMultiDict(h))
content = response.content = mock.Mock()
content.read.side_effect = side_effect

await response.read()
encoding = response.get_encoding()

assert encoding == "utf-8"


async def test_text_custom_encoding(
loop: asyncio.AbstractEventLoop, session: ClientSession
) -> None:
Expand Down

0 comments on commit 8ad118b

Please sign in to comment.