Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(mime): share document response MIME extraction

ldm0156cc3ffabdb
diff --git a/moli-web-mime/src/headers.rs b/moli-web-mime/src/headers.rs
index 29b0733df..23f8a6165 100644
--- a/moli-web-mime/src/headers.rs
+++ b/moli-web-mime/src/headers.rs
@@ -89,7 +89,7 @@ pub fn response_headers_indicate_attachment_download(headers: &[(String, Vec<u8>
}
pub fn response_headers_indicate_binary_document(headers: &[(String, Vec<u8>)]) -> bool {
- response_content_type(headers)
+ response_document_content_type(headers)
.as_deref()
.is_some_and(is_binary_document_mime_type)
}
@@ -100,12 +100,7 @@ pub fn response_headers_indicate_raw_document(headers: &[(String, Vec<u8>)]) ->
}
pub fn response_document_content_type(headers: &[(String, Vec<u8>)]) -> Option<String> {
- let content_type = response_header_values(headers, "content-type")
- .into_iter()
- .last()?;
- (!content_type.trim().is_empty())
- .then(|| mime_essence(&content_type))
- .flatten()
+ extract_response_mime_essence(headers)
}
pub fn effective_response_mime_type(
diff --git a/moli-web-mime/src/tests.rs b/moli-web-mime/src/tests.rs
index b2c49be9d..52b11bfa1 100644
--- a/moli-web-mime/src/tests.rs
+++ b/moli-web-mime/src/tests.rs
@@ -87,6 +87,55 @@ fn extracts_request_header_content_type_essence_for_fetch_rules() {
assert_eq!(request_header_content_type_essence("text / plain"), None);
}
+#[test]
+fn document_classification_uses_the_extracted_content_type() {
+ let cases: &[(&[&str], Option<&str>, bool)] = &[
+ (&["", "invalid", "*/*"], None, false),
+ (&["text/plain", ""], Some("text/plain"), false),
+ (&["text/html", "text/plain"], Some("text/plain"), false),
+ (&["text/html", "*/*;charset=GBK"], Some("text/html"), false),
+ (
+ &[r#"text/html;x=""#, "text/plain"],
+ Some("text/html"),
+ false,
+ ),
+ (
+ &["application/octet-stream", "text/html", "invalid"],
+ Some("text/html"),
+ false,
+ ),
+ (
+ &["text/html", "application/octet-stream", "*/*"],
+ Some("application/octet-stream"),
+ true,
+ ),
+ (
+ &["text/html", "application/xhtml+xml", ""],
+ Some("application/xhtml+xml"),
+ false,
+ ),
+ ];
+ for (values, expected, binary) in cases {
+ for fields in [values.to_vec(), vec![&values.join(",")]] {
+ let headers: Vec<_> = fields
+ .iter()
+ .map(|value| ("cOnTeNt-TyPe".to_owned(), value.as_bytes().to_vec()))
+ .collect();
+ assert_eq!(
+ response_document_content_type(&headers).as_deref(),
+ *expected,
+ "{fields:?}"
+ );
+ assert_eq!(response_headers_indicate_binary_document(&headers), *binary);
+ assert_eq!(response_headers_indicate_raw_document(&headers), *binary);
+
+ let mut attachment = headers;
+ attachment.push(("Content-Disposition".to_owned(), b"attachment".to_vec()));
+ assert!(response_headers_indicate_raw_document(&attachment));
+ }
+ }
+}
+
#[test]
fn matches_document_types() {
assert!(is_html_document_mime("text/html;charset=utf-8"));