Skip to content

Truly independent web browser

LibWeb: Fire abort at an XMLHttpRequest stopped with its document

Shannon Booth2d93fb79043f
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cpp
index 44aa14adc4b..547dd1bb2da 100644
--- a/Libraries/LibWeb/DOM/Document.cpp
+++ b/Libraries/LibWeb/DOM/Document.cpp
@@ -6627,22 +6627,21 @@ void Document::abort()
// or any queued tasks or any network data getting discarded,
// then make document unsalvageable given document and "fetch".
bool canceled_fetch = false;
- if (m_ongoing_navigation_fetch_controller && m_ongoing_navigation_fetch_controller->state() == Fetch::Infrastructure::FetchController::State::Ongoing) {
- m_ongoing_navigation_fetch_controller->stop_fetch();
+ auto cancel_fetch = [&](GC::Ptr<Fetch::Infrastructure::FetchController> controller) {
+ if (!controller || controller->state() != Fetch::Infrastructure::FetchController::State::Ongoing)
+ return;
+ controller->stop_fetch();
canceled_fetch = true;
- }
+ };
+
+ cancel_fetch(m_ongoing_navigation_fetch_controller);
m_ongoing_navigation_fetch_controller = nullptr;
// AD-HOC: The fetch group belongs to the document's Window. Once the Window is associated with another document,
// as when the initial about:blank document is replaced, its fetches are not in the context of this document.
if (auto window = this->window(); window && window->associated_document_if_any().ptr() == this) {
- for (auto& fetch_record : relevant_settings_object().fetch_group()) {
- auto controller = fetch_record.fetch_controller();
- if (!controller || controller->state() != Fetch::Infrastructure::FetchController::State::Ongoing)
- continue;
- controller->stop_fetch();
- canceled_fetch = true;
- }
+ for (auto& fetch_record : relevant_settings_object().fetch_group())
+ cancel_fetch(fetch_record.fetch_controller());
}
if (canceled_fetch)
diff --git a/Libraries/LibWeb/XHR/XMLHttpRequest.cpp b/Libraries/LibWeb/XHR/XMLHttpRequest.cpp
index fc0b68e57c2..512f24ede7e 100644
--- a/Libraries/LibWeb/XHR/XMLHttpRequest.cpp
+++ b/Libraries/LibWeb/XHR/XMLHttpRequest.cpp
@@ -1029,6 +1029,17 @@ WebIDL::ExceptionOr<void> XMLHttpRequest::send(NullableDocumentOrXMLHttpRequestB
.process_response_consume_body = {},
}));
+ // AD-HOC: Stopping the fetch, as aborting the document does, discards it without a response. Hand this an
+ // aborted network error instead, so that it fires abort as for a canceled fetch.
+ m_fetch_controller->set_stop_steps(GC::create_function(heap(), [this, fetch_controller = m_fetch_controller] {
+ HTML::queue_global_task(HTML::Task::Source::Networking, relevant_global_object(), GC::create_function(heap(), [this, fetch_controller] {
+ if (m_fetch_controller != fetch_controller)
+ return;
+ m_response = Fetch::Infrastructure::Response::aborted_network_error();
+ MUST(handle_errors());
+ }));
+ }));
+
// 11. Let now be the present time.
// 12. Run these steps in parallel:
// 1. Wait until either req’s done flag is set or this’s timeout is not 0 and this’s timeout milliseconds have passed since now.
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index d08af4a4537..2ef9c4e4632 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -504,6 +504,7 @@ Text/input/wpt-import/xhr/send-redirect-post-upload.htm
Text/input/wpt-import/xhr/send-response-upload-event-loadend.htm
Text/input/wpt-import/xhr/send-response-upload-event-loadstart.htm
Text/input/wpt-import/xhr/send-response-upload-event-progress.htm
+Text/input/wpt-import/xhr/abort-after-stop.window.html
Text/input/XML/error-page.html
Text/input/XML/error-page-namespace-error.html
Text/input/base/a-element-target.html
diff --git a/Tests/LibWeb/Text/expected/wpt-import/xhr/abort-after-stop.window.txt b/Tests/LibWeb/Text/expected/wpt-import/xhr/abort-after-stop.window.txt
new file mode 100644
index 00000000000..0e6ecbb3c61
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/xhr/abort-after-stop.window.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass XMLHttpRequest: abort event should fire when stop() method is used
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/expected/wpt-import/xhr/open-after-stop.window.txt b/Tests/LibWeb/Text/expected/wpt-import/xhr/open-after-stop.window.txt
new file mode 100644
index 00000000000..bd05358b0a8
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/xhr/open-after-stop.window.txt
@@ -0,0 +1,6 @@
+Harness status: OK
+
+Found 1 tests
+
+1 Pass
+Pass open() after window.stop()
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/xhr/abort-after-stop.window.html b/Tests/LibWeb/Text/input/wpt-import/xhr/abort-after-stop.window.html
new file mode 100644
index 00000000000..1f9547a39bd
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/xhr/abort-after-stop.window.html
@@ -0,0 +1,8 @@
+<!doctype html>
+<meta charset=utf-8>
+<title>XMLHttpRequest: abort event should fire when stop() method is used</title>
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../xhr/abort-after-stop.window.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/xhr/abort-after-stop.window.js b/Tests/LibWeb/Text/input/wpt-import/xhr/abort-after-stop.window.js
new file mode 100644
index 00000000000..a254648f419
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/xhr/abort-after-stop.window.js
@@ -0,0 +1,22 @@
+// META: title=XMLHttpRequest: abort event should fire when stop() method is used
+
+ var test = async_test();
+ window.onload = test.step_func(function() {
+ var client = new XMLHttpRequest();
+ var abortFired = false;
+ var sync = true;
+ client.onabort = test.step_func(function (e) {
+ assert_false(sync);
+ assert_equals(e.type, 'abort');
+ assert_equals(client.status, 0);
+ abortFired = true;
+ });
+ client.open("GET", "resources/delay.py?ms=3000", true);
+ client.send(null);
+ test.step_timeout(() => {
+ assert_equals(abortFired, true);
+ test.done();
+ }, 200);
+ window.stop();
+ sync = false;
+ });
diff --git a/Tests/LibWeb/Text/input/wpt-import/xhr/open-after-stop.window.html b/Tests/LibWeb/Text/input/wpt-import/xhr/open-after-stop.window.html
new file mode 100644
index 00000000000..731f4c415df
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/xhr/open-after-stop.window.html
@@ -0,0 +1,8 @@
+<!doctype html>
+<meta charset=utf-8>
+
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+
+<div id=log></div>
+<script src="../xhr/open-after-stop.window.js"></script>
diff --git a/Tests/LibWeb/Text/input/wpt-import/xhr/open-after-stop.window.js b/Tests/LibWeb/Text/input/wpt-import/xhr/open-after-stop.window.js
new file mode 100644
index 00000000000..e836a523f86
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/xhr/open-after-stop.window.js
@@ -0,0 +1,43 @@
+// window.stop() below prevents the load event from firing, so wait until it is
+// fired to start the test.
+setup({explicit_done: true });
+
+onload = () => {
+ async_test(function(t) {
+ const client = new XMLHttpRequest();
+
+ const result = [];
+ const expected = [
+ 'readystatechange', 0, 1, // open()
+ ];
+
+ let state = 0;
+
+ client.onreadystatechange = t.step_func(() => {
+ result.push('readystatechange', state, client.readyState);
+ });
+ client.onabort = t.unreached_func("abort should not be fired after window.stop() and open()");
+ client.onloadend = t.unreached_func("loadend should not be fired after window.stop() and open()");
+
+ client.open("GET", "resources/well-formed.xml");
+ assert_equals(client.readyState, 1);
+
+ state = 1;
+ client.send(null);
+ state = 2;
+ window.stop();
+ // Unlike client.abort(), window.stop() does not change readyState
+ // immediately, rather through a task...
+ assert_equals(client.readyState, 1);
+ state = 3;
+ // ... which is then canceled when we open a new request anyway.
+ client.open("GET", "resources/well-formed.xml");
+ assert_equals(client.readyState, 1);
+ assert_array_equals(result, expected);
+
+ // Give the abort and loadend events a chance to fire (erroneously) before
+ // calling this a success.
+ t.step_timeout(t.step_func_done(), 1000);
+ }, "open() after window.stop()");
+ done();
+};
diff --git a/Tests/LibWeb/Text/input/wpt-import/xhr/resources/well-formed.xml b/Tests/LibWeb/Text/input/wpt-import/xhr/resources/well-formed.xml
new file mode 100644
index 00000000000..2f4f1269880
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/xhr/resources/well-formed.xml
@@ -0,0 +1,4 @@
+<html xmlns="http://www.w3.org/1999/xhtml">
+ <p id="n&#49;">1</p>
+ <p xmlns="namespacesarejuststrings" id="n2">2</p>
+</html>