From 382f1a14866d8401f989043f909cfcf133fc44f8 Mon Sep 17 00:00:00 2001 From: Jakub Hampl Date: Tue, 4 Nov 2014 19:22:52 +0000 Subject: [PATCH 1/2] fix($http): won't parse single space response Rails sends a single space response instead of empty headers, this change will not attempt to parse the response --- src/ng/http.js | 2 +- test/ng/httpSpec.js | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/ng/http.js b/src/ng/http.js index bd07e5d504e6..08940e7bb7f3 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -782,7 +782,7 @@ function $HttpProvider() { function transformResponse(response) { // make a copy since the response must be cacheable var resp = extend({}, response); - if (!response.data) { + if (!response.data || response.data === ' ') { resp.data = response.data; } else { resp.data = transformData(response.data, response.headers, config.transformResponse); diff --git a/test/ng/httpSpec.js b/test/ng/httpSpec.js index db79c8d9a6d1..e4e7f86d28bb 100644 --- a/test/ng/httpSpec.js +++ b/test/ng/httpSpec.js @@ -1133,6 +1133,17 @@ describe('$http', function() { expect(callback.mostRecentCall.args[0]).toEqual(''); }); + it('should not attempt to deserialize json for a blank response whose header contains application/json', function() { + //per http spec for Content-Type, HEAD request should return a Content-Type header + //set to what the content type would have been if a get was sent + $httpBackend.expect('GET', '/url').respond(' ', {'Content-Type': 'application/json'}); + $http({method: 'GET', url: '/url'}).success(callback); + $httpBackend.flush(); + + expect(callback).toHaveBeenCalledOnce(); + expect(callback.mostRecentCall.args[0]).toEqual(' '); + }); + it('should not deserialize tpl beginning with ng expression', function() { $httpBackend.expect('GET', '/url').respond('{{some}}'); $http.get('/url').success(callback); From 9f4c1c23a885a02d009d6bd77a61f8380bec805a Mon Sep 17 00:00:00 2001 From: Jakub Hampl Date: Tue, 11 Nov 2014 15:21:52 +0000 Subject: [PATCH 2/2] fixup! fix($http): won't parse single space response --- src/ng/http.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ng/http.js b/src/ng/http.js index 08940e7bb7f3..4a0a70d1f4a5 100644 --- a/src/ng/http.js +++ b/src/ng/http.js @@ -11,7 +11,7 @@ function defaultHttpResponseTransform(data, headers) { // strip json vulnerability protection prefix data = data.replace(JSON_PROTECTION_PREFIX, ''); var contentType = headers('Content-Type'); - if ((contentType && contentType.indexOf(APPLICATION_JSON) === 0) || + if ((contentType && contentType.indexOf(APPLICATION_JSON) === 0 && data.trim()) || (JSON_START.test(data) && JSON_END.test(data))) { data = fromJson(data); } @@ -782,7 +782,7 @@ function $HttpProvider() { function transformResponse(response) { // make a copy since the response must be cacheable var resp = extend({}, response); - if (!response.data || response.data === ' ') { + if (!response.data) { resp.data = response.data; } else { resp.data = transformData(response.data, response.headers, config.transformResponse);