Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 6f1050d

Browse files
Gonzalo Ruiz de VillaIgorMinar
Gonzalo Ruiz de Villa
authored andcommitted
fix(httpBackend): should not read response data when request is aborted
When a request is aborted, it makes no sense to read the response headers or text. Also in IE9, trying to read data (either response headers or text) from an aborted request throws an Error c00c023f. Fixes #4913 Closes #4940
1 parent 4d16472 commit 6f1050d

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/ng/httpBackend.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ function $HttpBackendProvider() {
3434
}
3535

3636
function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument, locationProtocol) {
37+
var ABORTED = -1;
38+
3739
// TODO(vojta): fix the signature
3840
return function(method, url, post, callback, headers, timeout, withCredentials, responseType) {
3941
var status;
@@ -69,13 +71,19 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
6971
// always async
7072
xhr.onreadystatechange = function() {
7173
if (xhr.readyState == 4) {
72-
var responseHeaders = xhr.getAllResponseHeaders();
74+
var responseHeaders = null,
75+
response = null;
76+
77+
if(status !== ABORTED) {
78+
responseHeaders = xhr.getAllResponseHeaders();
79+
response = xhr.responseType ? xhr.response : xhr.responseText;
80+
}
7381

7482
// responseText is the old-school way of retrieving response (supported by IE8 & 9)
7583
// response/responseType properties were introduced in XHR Level2 spec (supported by IE10)
7684
completeRequest(callback,
7785
status || xhr.status,
78-
(xhr.responseType ? xhr.response : xhr.responseText),
86+
response,
7987
responseHeaders);
8088
}
8189
};
@@ -99,7 +107,7 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
99107

100108

101109
function timeoutRequest() {
102-
status = -1;
110+
status = ABORTED;
103111
jsonpDone && jsonpDone();
104112
xhr && xhr.abort();
105113
}

test/ng/httpBackendSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,25 @@ describe('$httpBackend', function() {
118118
});
119119
});
120120

121+
it('should not try to read response data when request is aborted', function() {
122+
callback.andCallFake(function(status, response, headers) {
123+
expect(status).toBe(-1);
124+
expect(response).toBe(null);
125+
expect(headers).toBe(null);
126+
});
127+
$backend('GET', '/url', null, callback, {}, 2000);
128+
xhr = MockXhr.$$lastInstance;
129+
spyOn(xhr, 'abort');
130+
131+
fakeTimeout.flush();
132+
expect(xhr.abort).toHaveBeenCalledOnce();
133+
134+
xhr.status = 0;
135+
xhr.readyState = 4;
136+
xhr.onreadystatechange();
137+
expect(callback).toHaveBeenCalledOnce();
138+
});
139+
121140
it('should abort request on timeout', function() {
122141
callback.andCallFake(function(status, response) {
123142
expect(status).toBe(-1);

0 commit comments

Comments
 (0)