From d9b116be959f95f8e3a70ec80a7d530996ada838 Mon Sep 17 00:00:00 2001 From: Will Moore Date: Fri, 18 Jan 2013 14:09:19 -0800 Subject: [PATCH 1/2] feat(httpBackend) Patch for Firefox bug w/ CORS and resonse 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/ Closes #1468 --- src/ng/httpBackend.js | 20 +++++++++++++++++++- test/ng/httpBackendSpec.js | 2 ++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/ng/httpBackend.js b/src/ng/httpBackend.js index bca46ee1a939..edd156f657f4 100644 --- a/src/ng/httpBackend.js +++ b/src/ng/httpBackend.js @@ -65,11 +65,29 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument, // always async xhr.onreadystatechange = function() { if (xhr.readyState == 4) { + var responseHeaders = xhr.getAllResponseHeaders(); + // begin: workaround to overcome firefox CORS http response headers bug + // https://bugzilla.mozilla.org/show_bug.cgi?id=608735 + // 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 workaround. completeRequest(callback, status || xhr.status, xhr.response || xhr.responseText, - xhr.getAllResponseHeaders()); + responseHeaders); } }; + if (withCredentials) { xhr.withCredentials = true; } diff --git a/test/ng/httpBackendSpec.js b/test/ng/httpBackendSpec.js index a491ae269870..24c97e98405f 100644 --- a/test/ng/httpBackendSpec.js +++ b/test/ng/httpBackendSpec.js @@ -116,6 +116,8 @@ describe('$httpBackend', function() { }; this.getAllResponseHeaders = valueFn(''); + // for temporary FF CORS workaround + this.getResponseHeader = valueFn(''); } callback.andCallFake(function(status, response) { From 9695c7806662dc35a5faa4545fc0faa99e60d7e2 Mon Sep 17 00:00:00 2001 From: Will Moore Date: Wed, 13 Feb 2013 12:53:53 -0800 Subject: [PATCH 2/2] feat(httpBackend) Patch for Firefox bug w/ CORS and response headers Adding a note about Firefox fix version and cleaned up formatting. Closes #1468 --- src/ng/httpBackend.js | 6 +++++- test/ng/httpBackendSpec.js | 3 ++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/ng/httpBackend.js b/src/ng/httpBackend.js index edd156f657f4..50a2d188106e 100644 --- a/src/ng/httpBackend.js +++ b/src/ng/httpBackend.js @@ -66,8 +66,11 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument, xhr.onreadystatechange = function() { if (xhr.readyState == 4) { var responseHeaders = xhr.getAllResponseHeaders(); - // begin: workaround to overcome firefox CORS http response headers bug + + // 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", @@ -82,6 +85,7 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument, }); } // end workaround. + completeRequest(callback, status || xhr.status, xhr.response || xhr.responseText, responseHeaders); } diff --git a/test/ng/httpBackendSpec.js b/test/ng/httpBackendSpec.js index 24c97e98405f..76d343905156 100644 --- a/test/ng/httpBackendSpec.js +++ b/test/ng/httpBackendSpec.js @@ -116,7 +116,8 @@ describe('$httpBackend', function() { }; this.getAllResponseHeaders = valueFn(''); - // for temporary FF CORS workaround + // for temporary Firefox CORS workaround + // see https://github.com/angular/angular.js/issues/1468 this.getResponseHeader = valueFn(''); }