fix(fetchers): decode response bodies by Content-Type charset#69
Conversation
Response bodies were always decoded with String::from_utf8_lossy, so any page served as ISO-8859-1, windows-1252, Shift_JIS, etc. came out as mojibake. Decode with encoding_rs using the charset advertised in the Content-Type header, falling back to lossy UTF-8 when the charset is missing or unknown. A BOM in the body takes precedence over the header. The charset pattern accepts RFC 7231 quoted-string values (charset="ISO-8859-1"), mirroring upstream Scrapling commit 6390c0a which fixed the same silent UTF-8 fallback for quoted charsets. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KDFsMaKk764vogjUW3nqpk
|
Warning Review limit reached
Next review available in: 13 minutes Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available. How can I continue?After more reviews become available, a review can be triggered using the To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews. How do review limits work?CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability. For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window. Please refer docs for additional details. Review details⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe fetcher now decodes response bodies according to the charset in ChangesResponse decoding
Estimated code review effort: 3 (Moderate) | ~20 minutes 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
- Fall back to lossy UTF-8 for labels the WHATWG spec maps to the replacement encoding (hz-gb-2312, iso-2022-kr, ...) — decoding with the replacement encoding turns the whole body into a single U+FFFD, strictly worse for a scraper than lossy UTF-8. - Anchor the charset parameter at the header start or after ';' so x-charset=... or a charset= inside another parameter's quoted value (e.g. a multipart boundary) cannot match. - Tolerate whitespace around '=' as misconfigured servers commonly emit it. - Scope the module doc to header-declared charsets (meta tags in the body are still decoded as lossy UTF-8). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01KDFsMaKk764vogjUW3nqpk
Summary
Upstream sync (1/4). Response bodies were always decoded with
String::from_utf8_lossy, ignoring the charset entirely — any page served as ISO-8859-1, windows-1252, Shift_JIS, etc. was turned into mojibake.fetchers::encodingmodule: extracts the charset from theContent-Typeheader and decodes the body withencoding_rs, falling back to lossy UTF-8 when the charset is missing or unknown. A BOM in the body takes precedence over the header (WHATWG behaviour).charset="ISO-8859-1"), mirroring upstream Scrapling commit6390c0awhich fixed the same silent UTF-8 fallback for quoted charsets.Fetcher::requestnow decodes via the new module.Tests
12 new unit tests covering bare/quoted/case-insensitive/missing charset extraction, ISO-8859-1 and Shift_JIS decoding, quoted-charset regression, unknown-charset and missing-header fallbacks, and BOM precedence. Full suite green locally (
cargo test,cargo clippy --all-targets -- -D warnings,cargo fmt --check).🤖 Generated with Claude Code
https://claude.ai/code/session_01KDFsMaKk764vogjUW3nqpk
Generated by Claude Code
Summary by CodeRabbit
Content-Typeheader.