diff --git a/moli-content-type/src/response.rs b/moli-content-type/src/response.rs index c9a1269bfa..befe3bdddb 100644 --- a/moli-content-type/src/response.rs +++ b/moli-content-type/src/response.rs @@ -3,6 +3,7 @@ pub struct ResponseContentType { mime_type: String, parameters: Vec<(String, String)>, + charset: Option, } 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 { 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, }) } diff --git a/moli-content-type/src/tests.rs b/moli-content-type/src/tests.rs index fc9b5dd4d7..66d422df82 100644 --- a/moli-content-type/src/tests.rs +++ b/moli-content-type/src/tests.rs @@ -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",