Skip to content

Commit

Permalink
Fix xhr.abort() during XHR readystatechange
Browse files Browse the repository at this point in the history
Closes #1808.
  • Loading branch information
domenic committed Apr 29, 2017
1 parent 270f288 commit e79e9a1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
5 changes: 5 additions & 0 deletions lib/jsdom/living/xmlhttprequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,11 @@ module.exports = function createXMLHttpRequest(window) {
properties.responseXMLCache = null;
readyStateChange(xhr, XMLHttpRequest.HEADERS_RECEIVED);

if (!properties.client) {
// The request was aborted in reaction to the readystatechange event.
return;
}

// Can't use the client since the client gets the post-ungzipping bytes (which can be greater than the
// Content-Length).
response.on("data", chunk => {
Expand Down
5 changes: 3 additions & 2 deletions test/web-platform-tests/to-upstream.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ describe("Local tests in Web Platform Test format (to-upstream)", () => {
"html/webappapis/timers/arguments.html",
"html/webappapis/timers/errors.html",
"html/webappapis/timers/settimeout-setinterval-handles.html",
"XMLHttpRequest/abort-during-readystatechange.html",
"XMLHttpRequest/formdata-constructor.html",
"XMLHttpRequest/formdata-set-blob.html",
"XMLHttpRequest/thrown-error-in-events.html",
"XMLHttpRequest/send-authentication-cors-post.htm"
"XMLHttpRequest/send-authentication-cors-post.htm",
"XMLHttpRequest/thrown-error-in-events.html"
]
.forEach(runWebPlatformTest);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<title>abort() during readystatechange should work fine</title>
<link rel="author" title="Domenic Denicola" href="mailto:d@domenic.me">
<link rel="help" href="https://xhr.spec.whatwg.org/#the-abort()-method">

<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>

<script>
"use strict";

const xhr = new XMLHttpRequest();

// In jsdom's implementation, this would cause a crash, as after firing readystatechange for HEADERS_RECEIVED, it would
// try to manipulate internal state. But that internal state got cleared during abort(). So jsdom needed to be modified
// to check if that internal state had gone away as a result of firing readystatechange, and if so, bail out.

xhr.addEventListener("readystatechange", () => {
if (xhr.readyState === xhr.HEADERS_RECEIVED) {
xhr.abort();
} else if (xhr.readyState === xhr.DONE) {
done();
}
});

xhr.open("GET", "/common/blank.html");
xhr.send();

</script>

0 comments on commit e79e9a1

Please sign in to comment.