fix(content-type): lowercase response charset

This commit is contained in:
ldm0
2026-08-27 02:57:24 +08:00
parent 8975fdb669
commit b44dac00a3
2 changed files with 19 additions and 3 deletions
+10 -2
View File
@@ -3,6 +3,7 @@
pub struct ResponseContentType {
mime_type: String,
parameters: Vec<(String, String)>,
charset: Option<String>,
}
impl ResponseContentType {
@@ -24,9 +25,10 @@ impl ResponseContentType {
.map(|(_, value)| value.as_str())
}
/// The first `charset` value, with HTTP linear whitespace trimmed.
/// The first `charset` value, with HTTP linear whitespace trimmed and
/// ASCII letters lowercased to match Chromium's response metadata.
pub fn charset(&self) -> Option<&str> {
self.parameter("charset").map(trim_http_lws)
self.charset.as_deref()
}
/// The first `boundary` value, with HTTP linear whitespace trimmed.
@@ -97,9 +99,15 @@ pub fn parse_response_content_type(input: &str) -> Option<ResponseContentType> {
parameters.push((name, value));
}
let charset = parameters
.iter()
.find(|(name, _)| name.eq_ignore_ascii_case("charset"))
.map(|(_, value)| trim_http_lws(value).to_ascii_lowercase());
Some(ResponseContentType {
mime_type,
parameters,
charset,
})
}
+9 -1
View File
@@ -151,6 +151,14 @@ fn response_parser_http_lws_is_only_space_and_tab() {
);
}
#[test]
fn response_parser_ascii_lowercases_charset_without_changing_raw_parameters() {
let parsed = parse_response_content_type("text/html; charset=\tX-\u{dc}TF-A\t").unwrap();
assert_eq!(parsed.charset(), Some("x-\u{dc}tf-a"));
assert_eq!(parsed.parameter("charset"), Some("X-\u{dc}TF-A"));
}
#[test]
fn response_parser_has_a_distinct_network_level_validity_boundary() {
assert!(parse_response_content_type("garbage; charset=utf-8").is_none());
@@ -172,7 +180,7 @@ fn response_parser_matches_chromium_http_util_regression_matrix() {
for (input, mime_type, charset) in [
("text/html", Some("text/html"), None),
("text/html;", Some("text/html"), None),
("text/html; charset=utf-8", Some("text/html"), Some("utf-8")),
("text/html; charset=UTF-8", Some("text/html"), Some("utf-8")),
("text/html; charset =utf-8", Some("text/html"), None),
(
"text/html; charset= utf-8",