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

Commit dd1d189

Browse files
mkawalecjeffbcross
authored andcommittedJun 12, 2014
perf($http): move xsrf cookie check to after cache check in $http
$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
1 parent b32d0f8 commit dd1d189

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed
 

‎src/ng/http.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -602,14 +602,6 @@ function $HttpProvider() {
602602
config.headers = headers;
603603
config.method = uppercase(config.method);
604604

605-
var xsrfValue = urlIsSameOrigin(config.url)
606-
? $browser.cookies()[config.xsrfCookieName || defaults.xsrfCookieName]
607-
: undefined;
608-
if (xsrfValue) {
609-
headers[(config.xsrfHeaderName || defaults.xsrfHeaderName)] = xsrfValue;
610-
}
611-
612-
613605
var serverRequest = function(config) {
614606
headers = config.headers;
615607
var reqData = transformData(config.data, headersGetter(headers), config.transformRequest);
@@ -885,8 +877,17 @@ function $HttpProvider() {
885877
}
886878
}
887879

888-
// if we won't have the response in cache, send the request to the backend
880+
881+
// if we won't have the response in cache, set the xsrf headers and
882+
// send the request to the backend
889883
if (isUndefined(cachedResp)) {
884+
var xsrfValue = urlIsSameOrigin(config.url)
885+
? $browser.cookies()[config.xsrfCookieName || defaults.xsrfCookieName]
886+
: undefined;
887+
if (xsrfValue) {
888+
reqHeaders[(config.xsrfHeaderName || defaults.xsrfHeaderName)] = xsrfValue;
889+
}
890+
890891
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
891892
config.withCredentials, config.responseType);
892893
}

‎test/ng/httpSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,25 @@ describe('$http', function() {
741741

742742
$httpBackend.flush();
743743
}));
744+
745+
it('should check the cache before checking the XSRF cookie', inject(function($browser, $cacheFactory) {
746+
var testCache = $cacheFactory('testCache'),
747+
executionOrder = [];
748+
749+
spyOn($browser, 'cookies').andCallFake(function() {
750+
executionOrder.push('cookies');
751+
return {'XSRF-TOKEN':'foo'};
752+
});
753+
spyOn(testCache, 'get').andCallFake(function() {
754+
executionOrder.push('cache');
755+
});
756+
757+
$httpBackend.expect('GET', '/url', undefined).respond('');
758+
$http({url: '/url', method: 'GET', cache: testCache});
759+
$httpBackend.flush();
760+
761+
expect(executionOrder).toEqual(['cache', 'cookies']);
762+
}));
744763
});
745764

746765

0 commit comments

Comments
 (0)
This repository has been archived.