mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-19 13:05:57 +08:00
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
This commit is contained in:
committed by
Jeff Cross
parent
b32d0f8649
commit
dd1d189ee7
@@ -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);
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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']);
|
||||
}));
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user