Skip to content

Commit 0fa8e47

Browse files
Will Moorevojtajina
Will Moore
authored andcommitted
fix($httpBackend): patch for Firefox bug w/ CORS and response headers
A workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=608735 In FF getAllResponseHeaders() returns null if the request is the result of CORS. Tried to format the code so that when a FF patch is released and gains enough traction it can easily be selected and deleted. Heavily inspired by jQuery's patch for the same bug. This patch falls short of passing through custom headers but covers all of the "simple response headers" in the spec at http://www.w3.org/TR/cors/ This commit should get reverted once Firefox 21 gets out. Closes angular#1468 Conflicts: src/ng/httpBackend.js
1 parent 8043784 commit 0fa8e47

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/ng/httpBackend.js

+24-2
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,30 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
6565
// always async
6666
xhr.onreadystatechange = function() {
6767
if (xhr.readyState == 4) {
68-
completeRequest(
69-
callback, status || xhr.status, xhr.responseText, xhr.getAllResponseHeaders());
68+
var responseHeaders = xhr.getAllResponseHeaders();
69+
70+
// TODO(vojta): remove once Firefox 21 gets released.
71+
// begin: workaround to overcome Firefox CORS http response headers bug
72+
// https://bugzilla.mozilla.org/show_bug.cgi?id=608735
73+
// Firefox already patched in nightly. Should land in Firefox 21.
74+
75+
// CORS "simple response headers" http://www.w3.org/TR/cors/
76+
var value,
77+
simpleHeaders = ["Cache-Control", "Content-Language", "Content-Type",
78+
"Expires", "Last-Modified", "Pragma"];
79+
if (!responseHeaders) {
80+
responseHeaders = "";
81+
forEach(simpleHeaders, function (header) {
82+
var value = xhr.getResponseHeader(header);
83+
if (value) {
84+
responseHeaders += header + ": " + value + "\n";
85+
}
86+
});
87+
}
88+
// end of the workaround.
89+
90+
completeRequest(callback, status || xhr.status, xhr.responseText,
91+
responseHeaders);
7092
}
7193
};
7294

test/ng/httpBackendSpec.js

+3
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,9 @@ describe('$httpBackend', function() {
116116
};
117117

118118
this.getAllResponseHeaders = valueFn('');
119+
// for temporary Firefox CORS workaround
120+
// see https://github.com/angular/angular.js/issues/1468
121+
this.getResponseHeader = valueFn('');
119122
}
120123

121124
callback.andCallFake(function(status, response) {

0 commit comments

Comments
 (0)