Skip to content

Commit e19b04c

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
1 parent 37e8b12 commit e19b04c

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/ng/httpBackend.js

Lines changed: 23 additions & 1 deletion
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+
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+
6890
completeRequest(callback, status || xhr.status, xhr.response || xhr.responseText,
69-
xhr.getAllResponseHeaders());
91+
responseHeaders);
7092
}
7193
};
7294

test/ng/httpBackendSpec.js

Lines changed: 3 additions & 0 deletions
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)