Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

refactor(content-type): retain response metadata outputs

ldm09c47d332c611
diff --git a/moli-content-type/src/response.rs b/moli-content-type/src/response.rs
index befe3bddd..7bbbbcdeb 100644
--- a/moli-content-type/src/response.rs
+++ b/moli-content-type/src/response.rs
@@ -2,8 +2,8 @@
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ResponseContentType {
mime_type: String,
- parameters: Vec<(String, String)>,
charset: Option<String>,
+ boundary: Option<String>,
}
impl ResponseContentType {
@@ -12,19 +12,6 @@ impl ResponseContentType {
&self.mime_type
}
- /// Parsed parameters in header order.
- pub fn parameters(&self) -> &[(String, String)] {
- &self.parameters
- }
-
- /// The first parameter with the given ASCII-case-insensitive name.
- pub fn parameter(&self, name: &str) -> Option<&str> {
- self.parameters
- .iter()
- .find(|(candidate, _)| candidate.eq_ignore_ascii_case(name))
- .map(|(_, value)| value.as_str())
- }
-
/// 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> {
@@ -33,7 +20,7 @@ impl ResponseContentType {
/// The first `boundary` value, with HTTP linear whitespace trimmed.
pub fn boundary(&self) -> Option<&str> {
- self.parameter("boundary").map(trim_http_lws)
+ self.boundary.as_deref()
}
}
@@ -66,7 +53,8 @@ pub fn parse_response_content_type(input: &str) -> Option<ResponseContentType> {
}
let mime_type = input[type_start..type_end].to_ascii_lowercase();
- let mut parameters = Vec::new();
+ let mut charset = None;
+ let mut boundary = None;
let mut offset = find_from(bytes, type_end, |byte| byte == b';').unwrap_or(input_len);
while offset < input_len {
@@ -80,7 +68,7 @@ pub fn parse_response_content_type(input: &str) -> Option<ResponseContentType> {
continue;
}
- let name = input[name_start..offset].to_owned();
+ let name = &input[name_start..offset];
offset = skip_http_lws(bytes, offset + 1);
if offset >= input_len || bytes[offset] == b';' {
continue;
@@ -96,18 +84,17 @@ pub fn parse_response_content_type(input: &str) -> Option<ResponseContentType> {
let value_end = trim_trailing_http_lws(bytes, value_start, offset);
input[value_start..value_end].to_owned()
};
- parameters.push((name, value));
+ if charset.is_none() && name.eq_ignore_ascii_case("charset") {
+ charset = Some(trim_http_lws(&value).to_ascii_lowercase());
+ } else if boundary.is_none() && name.eq_ignore_ascii_case("boundary") {
+ boundary = Some(trim_http_lws(&value).to_owned());
+ }
}
- 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,
+ boundary,
})
}
diff --git a/moli-content-type/src/tests.rs b/moli-content-type/src/tests.rs
index 66d422df8..38c420b74 100644
--- a/moli-content-type/src/tests.rs
+++ b/moli-content-type/src/tests.rs
@@ -152,11 +152,10 @@ fn response_parser_http_lws_is_only_space_and_tab() {
}
#[test]
-fn response_parser_ascii_lowercases_charset_without_changing_raw_parameters() {
+fn response_parser_ascii_lowercases_charset() {
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]
@@ -252,10 +251,12 @@ fn response_parser_matches_chromium_http_util_regression_matrix() {
assert_response_content_type(input, mime_type, charset);
}
- let boundary =
- parse_response_content_type("text/html; boundary=\"WebKit-ada-df-dsf-adsfadsfs \"")
- .unwrap();
- assert_eq!(boundary.boundary(), Some("WebKit-ada-df-dsf-adsfadsfs"));
+ let parsed = parse_response_content_type(
+ "text/html; boundary=\"WebKit-ada-df-dsf-adsfadsfs \"; charset=UTF-8; boundary=ignored",
+ )
+ .unwrap();
+ assert_eq!(parsed.charset(), Some("utf-8"));
+ assert_eq!(parsed.boundary(), Some("WebKit-ada-df-dsf-adsfadsfs"));
}
#[test]