Truly independent web browser
LibWeb: Report an unknown XHR progress length for content-coded bodies
sideshowbarkerae0b39bdc6aa
diff --git a/Libraries/LibWeb/Bindings/Rust/src/fetch/infrastructure/http/response_view.rs b/Libraries/LibWeb/Bindings/Rust/src/fetch/infrastructure/http/response_view.rsindex 8beef62d9ca..44088e3eddd 100644--- a/Libraries/LibWeb/Bindings/Rust/src/fetch/infrastructure/http/response_view.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/fetch/infrastructure/http/response_view.rs@@ -33,6 +33,7 @@ unsafe extern "C" {on_header: unsafe extern "C" fn(*mut c_void, ak::Span<'_, u8>, ak::Span<'_, u8>),);fn web_fetch_response_extract_length(response: *mut c_void, out_length: *mut u64) -> bool;+ fn web_fetch_response_has_content_coding(response: *mut c_void) -> bool;fn web_fetch_response_extract_mime_type(response: *mut c_void) -> usize;fn web_fetch_response_url(response: *mut c_void, exclude_fragment: bool) -> usize;fn web_fetch_response_has_body(response: *mut c_void) -> bool;@@ -126,6 +127,13 @@ impl CppResponseView {unsafe { web_fetch_response_extract_length(self.as_ptr(), &raw mut length) }.then_some(length)}+ /// Whether the body has a content coding, i.e. Content-Encoding names a coding other than identity. That's read from+ /// the unfiltered headers, which still hold Content-Encoding when a CORS filter leaves it out.+ pub(crate) fn has_content_coding(&self) -> bool {+ // SAFETY: The response is live.+ unsafe { web_fetch_response_has_content_coding(self.as_ptr()) }+ }+/// https://fetch.spec.whatwg.org/#concept-header-extract-mime-typepub(crate) fn extract_mime_type(&self) -> Option<MimeType> {// SAFETY: The response is live, and LibWeb hands over 0 or the serialization of the MIME type.diff --git a/Libraries/LibWeb/Bindings/Rust/src/xhr/xml_http_request.rs b/Libraries/LibWeb/Bindings/Rust/src/xhr/xml_http_request.rsindex d68198b1de0..ffd001a10b5 100644--- a/Libraries/LibWeb/Bindings/Rust/src/xhr/xml_http_request.rs+++ b/Libraries/LibWeb/Bindings/Rust/src/xhr/xml_http_request.rs@@ -211,6 +211,24 @@ impl Response {fn extract_length(&self) -> Option<u64> {self.fetched().and_then(CppResponseView::extract_length)}++ /// The length progress events report: the extracted length, or 0 (unknown) for a body with a content coding.+ ///+ /// AD-HOC: The spec reports the extracted length. But a content-coded body's bytes count as received once they're+ /// decoded, while its Content-Length counts the encoded bytes, so the received length could pass it — and+ /// neither says how much of the body is left. So the length of such a body is unknown, and its progress events+ /// aren't length-computable.+ /// https://github.com/whatwg/xhr/issues/403+ fn progress_length(&self) -> u64 {+ if self.has_content_coding() {+ return 0;+ }+ self.extract_length().unwrap_or(0)+ }++ fn has_content_coding(&self) -> bool {+ self.fetched().is_some_and(CppResponseView::has_content_coding)+ }}/// https://xhr.spec.whatwg.org/#response-object@@ -1668,7 +1686,7 @@ impl XMLHttpRequest {// 8. Let length be the result of extracting a length from this’s response’s header list.// 9. If length is not an integer, then set it to 0.- let length = this.response.borrow().extract_length().unwrap_or(0);+ let length = this.response.borrow().progress_length();// Pre-allocate received bytes buffer if Content-Length is known.if let Ok(length) = usize::try_from(length) {@@ -1993,7 +2011,7 @@ impl XMLHttpRequest {// 4. Let length be the result of extracting a length from this’s response’s header list.// 5. If length is not an integer, then set it to 0.- let length = self.response.borrow().extract_length().unwrap_or(0);+ let length = self.response.borrow().progress_length();// 6. If xhr’s synchronous flag is unset, then fire a progress event named progress at xhr with transmitted and length.if !self.synchronous.get() {diff --git a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/ResponseView.cpp b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/ResponseView.cppindex 5c8c8ee7f44..bbe6e6be0c3 100644--- a/Libraries/LibWeb/Fetch/Infrastructure/HTTP/ResponseView.cpp+++ b/Libraries/LibWeb/Fetch/Infrastructure/HTTP/ResponseView.cpp@@ -21,6 +21,7 @@ void web_fetch_response_status_message(ResponseView*, void* bytes, void (*on_bytbool web_fetch_response_get_header(ResponseView*, ReadonlyBytes name, void* bytes, void (*on_bytes)(void*, ReadonlyBytes));void web_fetch_response_sort_and_combine(ResponseView*, void* headers, void (*on_header)(void*, ReadonlyBytes name, ReadonlyBytes value));bool web_fetch_response_extract_length(ResponseView*, u64* out_length);+bool web_fetch_response_has_content_coding(ResponseView*);FlatPtr web_fetch_response_extract_mime_type(ResponseView*);FlatPtr web_fetch_response_url(ResponseView*, bool exclude_fragment);bool web_fetch_response_has_body(ResponseView*);@@ -142,6 +143,20 @@ bool web_fetch_response_extract_length(ResponseView* response, u64* out_length)return true;}+// Reads the unsafe response's header list, since a filtered one may leave Content-Encoding out.+bool web_fetch_response_has_content_coding(ResponseView* response)+{+ auto codings = response->record()->unsafe_response()->header_list()->get("Content-Encoding"sv);+ if (!codings.has_value())+ return false;+ for (auto coding : codings->view().split_view(',')) {+ coding = coding.trim_whitespace();+ if (!coding.is_empty() && !coding.equals_ignoring_ascii_case("identity"sv))+ return true;+ }+ return false;+}+FlatPtr web_fetch_response_extract_mime_type(ResponseView* response){auto mime_type = extract_mime_type(response->header_list());diff --git a/Tests/LibWeb/Text/expected/XHR/XMLHttpRequest-progress-content-encoded.txt b/Tests/LibWeb/Text/expected/XHR/XMLHttpRequest-progress-content-encoded.txtnew file mode 100644index 00000000000..5e470742dde--- /dev/null+++ b/Tests/LibWeb/Text/expected/XHR/XMLHttpRequest-progress-content-encoded.txt@@ -0,0 +1,2 @@+gzip: loaded ever passed total: false; loadend: lengthComputable false, loaded 96000, total 0+plain: loaded ever passed total: false; loadend: lengthComputable true, loaded 1000, total 1000diff --git a/Tests/LibWeb/Text/input/XHR/XMLHttpRequest-progress-content-encoded.html b/Tests/LibWeb/Text/input/XHR/XMLHttpRequest-progress-content-encoded.htmlnew file mode 100644index 00000000000..b06a82b9f8c--- /dev/null+++ b/Tests/LibWeb/Text/input/XHR/XMLHttpRequest-progress-content-encoded.html@@ -0,0 +1,50 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ // Fetches the URL and reports the progress events: loaded must never pass total while lengthComputable is set.+ function progressReport(url) {+ return new Promise(resolve => {+ const xhr = new XMLHttpRequest();+ let overshot = false;+ const check = event => {+ if (event.lengthComputable && event.loaded > event.total)+ overshot = true;+ };+ xhr.onprogress = check;+ xhr.onload = check;+ xhr.onloadend = event => {+ check(event);+ resolve(`loaded ever passed total: ${overshot}; loadend: lengthComputable ${event.lengthComputable}, loaded ${event.loaded}, total ${event.total}`);+ };+ xhr.open("GET", url);+ xhr.send();+ });+ }++ promiseTest(async () => {+ // 96000 bytes of text, gzipped into 295 bytes.+ const encodedUrl = await httpTestServer().createEcho("GET", "/xhr-progress-content-encoded", {+ status: 200,+ headers: {+ "Access-Control-Allow-Origin": "*",+ "Content-Encoding": "gzip",+ "Content-Length": "295",+ "Content-Type": "text/plain",+ },+ body_encoding: "base64",+ body: "H4sIAAAAAAAC/+3IsQ2AMAwAsFdyAZf0CVArxESVdOH7HoI9up39u57sMfO9c1TFGrWOaN5777333nvvvffee++9995777333nvvvffee++9995777333nvvvffee++9995777333nvvvffee++9995777333nvvvffee++9995777333nvvvffee++9995777333nvvvffee++9995777333nvvvffee++9995777333nvvvffee++9995777333nvvvffee++9995777333nvvvffee++9995777333nvvvffee++9995777333nvvvffee++9995777333nvvvffee++9995777333nvvvffee++9995777333vvf/wbOlFW6AHcBAA==",+ });+ println(`gzip: ${await progressReport(encodedUrl)}`);++ const plainUrl = await httpTestServer().createEcho("GET", "/xhr-progress-plain", {+ status: 200,+ headers: {+ "Access-Control-Allow-Origin": "*",+ "Content-Length": "1000",+ "Content-Type": "text/plain",+ },+ body: "x".repeat(1000),+ });+ println(`plain: ${await progressReport(plainUrl)}`);+ });+</script>