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

Commit

Permalink
perf($http): move xsrf cookie check to after cache check in $http
Browse files Browse the repository at this point in the history
$http was previously checking cookies to find an xsrf-token prior to checking
the cache. This caused a performance penalty of about 2ms, which can be very
significant when loading hundreds of template instances on a page.

Fixes #7717
  • Loading branch information
mkawalec authored and jeffbcross committed Jun 12, 2014
1 parent b32d0f8 commit dd1d189
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
19 changes: 10 additions & 9 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,14 +602,6 @@ function $HttpProvider() {
config.headers = headers;
config.method = uppercase(config.method);

var xsrfValue = urlIsSameOrigin(config.url)
? $browser.cookies()[config.xsrfCookieName || defaults.xsrfCookieName]
: undefined;
if (xsrfValue) {
headers[(config.xsrfHeaderName || defaults.xsrfHeaderName)] = xsrfValue;
}


var serverRequest = function(config) {
headers = config.headers;
var reqData = transformData(config.data, headersGetter(headers), config.transformRequest);
Expand Down Expand Up @@ -885,8 +877,17 @@ function $HttpProvider() {
}
}

// if we won't have the response in cache, send the request to the backend

// if we won't have the response in cache, set the xsrf headers and
// send the request to the backend
if (isUndefined(cachedResp)) {
var xsrfValue = urlIsSameOrigin(config.url)
? $browser.cookies()[config.xsrfCookieName || defaults.xsrfCookieName]
: undefined;
if (xsrfValue) {
reqHeaders[(config.xsrfHeaderName || defaults.xsrfHeaderName)] = xsrfValue;
}

$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
config.withCredentials, config.responseType);
}
Expand Down
19 changes: 19 additions & 0 deletions test/ng/httpSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,25 @@ describe('$http', function() {

$httpBackend.flush();
}));

it('should check the cache before checking the XSRF cookie', inject(function($browser, $cacheFactory) {
var testCache = $cacheFactory('testCache'),
executionOrder = [];

spyOn($browser, 'cookies').andCallFake(function() {
executionOrder.push('cookies');
return {'XSRF-TOKEN':'foo'};
});
spyOn(testCache, 'get').andCallFake(function() {
executionOrder.push('cache');
});

$httpBackend.expect('GET', '/url', undefined).respond('');
$http({url: '/url', method: 'GET', cache: testCache});
$httpBackend.flush();

expect(executionOrder).toEqual(['cache', 'cookies']);
}));
});


Expand Down

0 comments on commit dd1d189

Please sign in to comment.