Skip to content

Commit 389c804

Browse files
lesnik512claude
andcommitted
fix: charset parser strips inner whitespace from quoted values
Adds one final .strip() after the quote-stripping chain in _parse_charset so that Content-Type: ...; charset=" utf-8 " yields a clean codec name. Python's codec registry happens to normalize whitespace on lookup (so the end-to-end mojibake risk is currently masked), but the parser should still return a clean value rather than rely on codec leniency. Test verifies _parse_charset directly (the unit-level contract) plus an end-to-end smoke through Response.text. The other "concerns" listed in the deferred-work entry (substring false-positives, mismatched quotes, multi-charset directives) do not actually fire on the current code, per the spec's analysis. Closes deferred-work entry: "Charset parser robustness". Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent b64203c commit 389c804

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

‎src/httpware/response.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def _parse_charset(content_type: str) -> str | None:
2121
for raw in content_type.split(";"):
2222
part = raw.strip()
2323
if part.lower().startswith(_CHARSET_PREFIX):
24-
return part[len(_CHARSET_PREFIX) :].strip().strip('"').strip("'")
24+
return part[len(_CHARSET_PREFIX) :].strip().strip('"').strip("'").strip()
2525
return None
2626

2727

‎tests/test_response.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import pytest
77

88
from httpware import Response
9+
from httpware.response import _parse_charset
910

1011

1112
def test_response_is_frozen() -> None:
@@ -68,6 +69,24 @@ def test_response_text_strips_quotes_around_charset(content_type: str) -> None:
6869
assert resp.text == "café"
6970

7071

72+
def test_response_text_strips_inner_whitespace_in_quoted_charset() -> None:
73+
# Direct parser check: inner whitespace inside the quoted value must not survive.
74+
# (Python's codec registry happens to normalize whitespace, masking the bug
75+
# end-to-end on most charsets; verify the parser itself returns a clean value.)
76+
assert _parse_charset('text/plain; charset=" iso-8859-1 "') == "iso-8859-1"
77+
78+
# End-to-end smoke: decoding the Latin-1 body must yield the original text.
79+
body = "café".encode("iso-8859-1")
80+
resp = Response(
81+
status=HTTPStatus.OK,
82+
headers={"content-type": 'text/plain; charset=" iso-8859-1 "'},
83+
content=body,
84+
url="/",
85+
elapsed=0.0,
86+
)
87+
assert resp.text == "café"
88+
89+
7190
def test_response_text_falls_back_to_utf8_on_unknown_charset() -> None:
7291
resp = Response(
7392
status=200,

0 commit comments

Comments
 (0)