Skip to content

armgit / moli

Public Imported from GitHub
0

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

Moved encoding_for_document_xml_declaration check after self.transport_encoding, so the HTML prescan cannot override an explicit HTTP charset and Added transport_charset_wins_over_utf16_xml_signature: sends a UTF-16LE body with <?xml but with a Content-Type: text/html; charset=windows-1252 header

BibekPathak0e597fc376db
diff --git a/moli-encoding/src/document.rs b/moli-encoding/src/document.rs
index 42b66dbc3..f6ea1e237 100644
--- a/moli-encoding/src/document.rs
+++ b/moli-encoding/src/document.rs
@@ -119,10 +119,10 @@ impl HtmlDocumentStreamingDecoder {
if !finishing && bytes_could_still_be_bom_prefix(&self.sniff_buffer) {
return None;
}
- if let Some(encoding) = encoding_for_document_xml_declaration(&self.sniff_buffer) {
+ if let Some(encoding) = self.transport_encoding {
return Some(encoding);
}
- if let Some(encoding) = self.transport_encoding {
+ if let Some(encoding) = encoding_for_document_xml_declaration(&self.sniff_buffer) {
return Some(encoding);
}
let meta_scan = self.feed_meta_charset_prescan(finishing);
diff --git a/moli-encoding/src/tests.rs b/moli-encoding/src/tests.rs
index 4d0dd6e71..6efc38a7f 100644
--- a/moli-encoding/src/tests.rs
+++ b/moli-encoding/src/tests.rs
@@ -505,6 +505,21 @@ fn bom_less_utf16le_xml_declaration_is_detected() {
assert!(text.contains("hi"), "decoded as {text:?}");
}
+#[test]
+fn transport_charset_wins_over_utf16_xml_signature() {
+ let headers = vec![(
+ "Content-Type".to_owned(),
+ "text/html; charset=windows-1252".to_owned(),
+ )];
+ let input = utf16le_bytes(
+ "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<html><head></head><body>hi</body></html>",
+ );
+
+ let (_, encoding) = decode_html_document(&input, &headers);
+
+ assert_eq!(encoding, "windows-1252");
+}
+
fn utf16be_bytes(input: &str) -> Vec<u8> {
input
.encode_utf16()