Skip to content

Truly independent web browser

LibWeb: Don’t repeat XHR’s last progress event at the end of a body

sideshowbarker5423b0b200d9
diff --git a/Libraries/LibWeb/Bindings/Rust/src/xhr/xml_http_request.rs b/Libraries/LibWeb/Bindings/Rust/src/xhr/xml_http_request.rs
index ffd001a10b5..b8b39d29dac 100644
--- a/Libraries/LibWeb/Bindings/Rust/src/xhr/xml_http_request.rs
+++ b/Libraries/LibWeb/Bindings/Rust/src/xhr/xml_http_request.rs
@@ -412,6 +412,8 @@ pub(crate) struct XMLHttpRequest {
last_upload_progress_timestamp: Cell<Option<Instant>>,
#[gc(untraced)]
last_download_progress_timestamp: Cell<Option<Instant>>,
+ #[gc(untraced)]
+ last_download_progress_loaded: Cell<Option<u64>>,
#[gc(untraced)]
async_request: RefCell<Option<CppRequest>>,
@@ -596,6 +598,7 @@ impl XMLHttpRequest {
request_body_transmitted: Cell::new(0),
last_upload_progress_timestamp: Cell::new(None),
last_download_progress_timestamp: Cell::new(None),
+ last_download_progress_loaded: Cell::new(None),
async_request: RefCell::new(None),
async_send_start_time: Cell::new(None),
timeout_timer: GcRefCell::new(None),
@@ -1471,6 +1474,7 @@ impl XMLHttpRequest {
this.request_body_transmitted.set(0);
this.last_upload_progress_timestamp.set(None);
this.last_download_progress_timestamp.set(None);
+ this.last_download_progress_loaded.set(None);
// 3. Let requestBodyLength be req’s body’s length, if req’s body is non-null; otherwise 0.
// NOTE: req's body is always this's request body, see step 6.
@@ -1742,6 +1746,7 @@ impl XMLHttpRequest {
// 5. Fire a progress event named progress at this with this’s received bytes’s length and length.
let received_length = self.received_bytes.borrow().len() as u64;
+ self.last_download_progress_loaded.set(Some(received_length));
self.fire_progress_event(vm, "progress", received_length, length);
}
@@ -2014,7 +2019,10 @@ impl XMLHttpRequest {
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() {
+ // AD-HOC: Not when the last progress event already reported transmitted, i.e. no bytes arrived since. The spec
+ // would fire that event again, though nothing progressed, and web-platform-tests require loaded to grow
+ // from one progress event to the next.
+ if !self.synchronous.get() && self.last_download_progress_loaded.get() != Some(transmitted) {
self.fire_progress_event(vm, "progress", transmitted, length);
}
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index 6c15b461ad1..a03ad898473 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -110,6 +110,7 @@ Text/input/wpt-import/html/semantics/scripting-1/the-script-element/module/dynam
Text/input/wpt-import/html/semantics/scripting-1/the-script-element/module/dynamic-import/propagate-nonce-inline-module.html
; Resource loading tests use WPT Python handlers, substitutions, or .headers files.
+Text/input/wpt-import/xhr/progress-events-response-data-gzip.htm
Text/input/wpt-import/css/css-cascade/import-mime-type-quirks.html
Text/input/wpt-import/html/semantics/document-metadata/the-style-element/style-error-01.html
Text/input/wpt-import/content-security-policy/script-src/script-src-trusted_types_eval_with_require_trusted_types_eval.html
diff --git a/Tests/LibWeb/Text/expected/wpt-import/xhr/progress-events-response-data-gzip.txt b/Tests/LibWeb/Text/expected/wpt-import/xhr/progress-events-response-data-gzip.txt
new file mode 100644
index 00000000000..1b3e701f99b
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/xhr/progress-events-response-data-gzip.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass XMLHttpRequest: progress events and GZIP encoding
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/xhr/progress-events-response-data-gzip.htm b/Tests/LibWeb/Text/input/wpt-import/xhr/progress-events-response-data-gzip.htm
new file mode 100644
index 00000000000..d4313333732
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/xhr/progress-events-response-data-gzip.htm
@@ -0,0 +1,83 @@
+<!doctype html>
+<html>
+ <head>
+ <title>XMLHttpRequest: progress events and GZIP encoding</title>
+ <meta name="timeout" content="long">
+ <script src="../resources/testharness.js"></script>
+ <script src="../resources/testharnessreport.js"></script>
+ <link rel="help" href="https://xhr.spec.whatwg.org/#firing-events-using-the-progressevent-interface-for-http" data-tested-assertations="following::p[contains(text(),'content-encodings')]" />
+ <!-- TODO: find better spec reference when https://www.w3.org/Bugs/Public/show_bug.cgi?id=25587 is fixed -->
+ </head>
+ <body>
+ <div id="log"></div>
+ <script>
+ var test = async_test()
+ test.step(function() {
+ var client = new XMLHttpRequest()
+ /*
+
+ Two behaviours are considered acceptable, so there are two ways to
+ pass this test
+
+ a) Set data for the compressed resource:
+ * event.total reflects the Content-length of the gzipp'ed resource
+ * event.loaded how many gzipped bytes have arrived over the wire so far
+ * lengthComputable is true
+
+ or
+
+ b) If the implementation does not provide progress details for the compressed
+ resource, set
+ * lengthComputable to false
+ * event.total to 0
+ * event.loaded to the number of bytes available so far after gzip decoding
+
+ Implications of this are tested here as follows:
+
+ * If lengthComputable is true:
+ * Event.total must match Content-length header
+ * event.loaded must only ever increase in progress events
+ (and may never repeat its value).
+ * event.loaded must never exceed the Content-length.
+
+ * If lengthComputable is false:
+ * event.total should be 0
+ * event.loaded must only ever increase in progress events
+ (and may never repeat its value).
+ * event.loaded should be the length of the decompressed content, i.e.
+ bigger than Content-length header value when finished loading
+
+ */
+ var lastTotal;
+ var lastLoaded = -1;
+ client.addEventListener('loadend', test.step_func(function(e){
+ var len = parseInt(client.getResponseHeader('content-length'), 10)
+ if(e.lengthComputable){
+ assert_equals(e.total, len, 'event.total is content-length')
+ assert_equals(e.loaded, len, 'event.loaded should be content-length at loadend')
+ }else{
+ assert_equals(e.total, 0, 'if implementation can\'t compute event.total for gzipped content it is 0')
+ assert_true(e.loaded >= len, 'event.loaded should be set even if total is not computable')
+ }
+ test.done();
+ }), false)
+ client.addEventListener('progress', test.step_func(function(e){
+ if(lastTotal === undefined){
+ lastTotal = e.total;
+ }
+ if(e.lengthComputable && e.total && e.loaded){
+ assert_equals(e.total, lastTotal, 'event.total should remain invariant')
+ assert_less_than_equal(e.loaded, lastTotal, 'event.loaded should not exceed content-length')
+ }else{
+ assert_equals(e.total, 0, 'event.total should be 0')
+ }
+ assert_greater_than(e.loaded, lastLoaded, 'event.loaded should only ever increase')
+ lastLoaded = e.loaded;
+ }), false)
+ // image.gif is 165375 bytes compressed. Sending 45000 bytes at a time with 1 second delay will load it in 4 seconds
+ client.open("GET", "resources/image.gif?pipe=gzip|trickle(45000:d1:r2)", true)
+ client.send()
+ })
+ </script>
+ </body>
+</html>
diff --git a/Tests/LibWeb/Text/input/wpt-import/xhr/resources/image.gif b/Tests/LibWeb/Text/input/wpt-import/xhr/resources/image.gif
new file mode 100644
index 00000000000..6d1174af119
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/xhr/resources/image.gif differ