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

Commit

Permalink
fix($httpBackend): patch for Firefox bug w/ CORS and response headers
Browse files Browse the repository at this point in the history
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 #1468
  • Loading branch information
Will Moore authored and vojtajina committed Feb 15, 2013
1 parent 37e8b12 commit e19b04c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,30 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
// always async
xhr.onreadystatechange = function() {
if (xhr.readyState == 4) {
var responseHeaders = xhr.getAllResponseHeaders();

// TODO(vojta): remove once Firefox 21 gets released.
// begin: workaround to overcome Firefox CORS http response headers bug
// https://bugzilla.mozilla.org/show_bug.cgi?id=608735
// Firefox already patched in nightly. Should land in Firefox 21.

// CORS "simple response headers" http://www.w3.org/TR/cors/
var value,
simpleHeaders = ["Cache-Control", "Content-Language", "Content-Type",
"Expires", "Last-Modified", "Pragma"];
if (!responseHeaders) {
responseHeaders = "";
forEach(simpleHeaders, function (header) {
var value = xhr.getResponseHeader(header);
if (value) {
responseHeaders += header + ": " + value + "\n";
}
});
}
// end of the workaround.

completeRequest(callback, status || xhr.status, xhr.response || xhr.responseText,
xhr.getAllResponseHeaders());
responseHeaders);
}
};

Expand Down
3 changes: 3 additions & 0 deletions test/ng/httpBackendSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ describe('$httpBackend', function() {
};

this.getAllResponseHeaders = valueFn('');
// for temporary Firefox CORS workaround
// see https://github.com/angular/angular.js/issues/1468
this.getResponseHeader = valueFn('');
}

callback.andCallFake(function(status, response) {
Expand Down

3 comments on commit e19b04c

@yehosef
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FF 23 is released, can you revert this commit and instructed in the comment?

@bfanger
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FF 24 is out, but this is added in the AngularJS 1.2.0-rc.2 release?

@petebacondarwin
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bfanger or @yehosef - would you like to make a PR for this revert? I am not sure what our current FF browser support commitment is...

Please sign in to comment.