From 2c2fac2d9763137795cac524db2593de9cba70ac Mon Sep 17 00:00:00 2001 From: Kornel Date: Thu, 13 Feb 2020 22:13:34 +0000 Subject: [PATCH] Drop trustServerDate --- index.js | 27 ++++----------------------- package.json | 2 +- test/okhttptest.js | 10 +++++----- 3 files changed, 10 insertions(+), 29 deletions(-) diff --git a/index.js b/index.js index 14a0cf0..f3ff441 100644 --- a/index.js +++ b/index.js @@ -87,7 +87,6 @@ module.exports = class CachePolicy { cacheHeuristic, immutableMinTimeToLive, ignoreCargoCult, - trustServerDate, _fromObject, } = {} ) { @@ -103,8 +102,6 @@ module.exports = class CachePolicy { this._responseTime = this.now(); this._isShared = shared !== false; - this._trustServerDate = - undefined !== trustServerDate ? trustServerDate : true; this._cacheHeuristic = undefined !== cacheHeuristic ? cacheHeuristic : 0.1; // 10% matches IE this._immutableMinTtl = @@ -331,24 +328,13 @@ module.exports = class CachePolicy { } /** - * Value of the Date response header or current time if Date was demed invalid + * Value of the Date response header or current time if Date was invalid * @return timestamp */ date() { - if (this._trustServerDate) { - return this._serverDate(); - } - return this._responseTime; - } - - _serverDate() { const serverDate = Date.parse(this._resHeaders.date); if (isFinite(serverDate)) { - const maxClockDrift = 8 * 3600 * 1000; - const clockDrift = Math.abs(this._responseTime - serverDate); - if (clockDrift < maxClockDrift) { - return serverDate; - } + return serverDate; } return this._responseTime; } @@ -360,11 +346,7 @@ module.exports = class CachePolicy { * @return Number */ age() { - let age = Math.max(0, (this._responseTime - this.date()) / 1000); - if (this._resHeaders.age) { - let ageValue = this._ageValue(); - if (ageValue > age) age = ageValue; - } + let age = this._ageValue(); const residentTime = (this.now() - this._responseTime) / 1000; return age + residentTime; @@ -419,7 +401,7 @@ module.exports = class CachePolicy { const defaultMinTtl = this._rescc.immutable ? this._immutableMinTtl : 0; - const serverDate = this._serverDate(); + const serverDate = this.date(); if (this._resHeaders.expires) { const expires = Date.parse(this._resHeaders.expires); // A cache recipient MUST interpret invalid date formats, especially the value "0", as representing a time in the past (i.e., "already expired"). @@ -644,7 +626,6 @@ module.exports = class CachePolicy { shared: this._isShared, cacheHeuristic: this._cacheHeuristic, immutableMinTimeToLive: this._immutableMinTtl, - trustServerDate: this._trustServerDate, }), modified: false, matches: true, diff --git a/package.json b/package.json index df86242..897798d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "http-cache-semantics", - "version": "4.0.4", + "version": "4.1.0", "description": "Parses Cache-Control and other headers. Helps building correct HTTP caches and proxies", "repository": "https://github.com/kornelski/http-cache-semantics.git", "main": "index.js", diff --git a/test/okhttptest.js b/test/okhttptest.js index 5c65b28..3ffcdb7 100644 --- a/test/okhttptest.js +++ b/test/okhttptest.js @@ -157,7 +157,7 @@ describe('okhttp tests', function() { { shared: false } ); - assert(cache.stale()); + assert(!cache.stale()); }); it('max age preferred over lower shared max age', function() { @@ -180,7 +180,7 @@ describe('okhttp tests', function() { { headers: {} }, { headers: { - date: formatDate(-3, 60), + age: 360, 'cache-control': 's-maxage=60, max-age=180', }, }, @@ -262,7 +262,7 @@ describe('okhttp tests', function() { { headers: { 'last-modified': formatDate(-2, 3600), - date: formatDate(-1, 60), + age: 60, expires: formatDate(1, 3600), }, }, @@ -325,7 +325,7 @@ describe('okhttp tests', function() { { headers: { 'cache-control': 'max-age=120', - date: formatDate(-4, 60), + age: 4*60, }, }, { shared: false } @@ -364,7 +364,7 @@ describe('okhttp tests', function() { { headers: { 'cache-control': 'max-age=120, must-revalidate', - date: formatDate(-4, 60), + age: 360, }, }, { shared: false }