From adfb6e04d2e95a662a1f06a0cad418836ed1662f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kornel=20Lesi=C5=84ski?= Date: Tue, 31 May 2016 10:34:36 +0100 Subject: [PATCH] Parse request CC --- index.js | 23 ++++++++++++++--------- package.json | 2 +- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index ed03b9d..6be3bc2 100644 --- a/index.js +++ b/index.js @@ -21,18 +21,23 @@ function parseCacheControl(header) { function CachePolicy(req, res, {shared} = {}) { if (!res || !res.headers) { - throw Error("headers missing"); + throw Error("Response headers missing"); + } + if (!req || !req.headers) { + throw Error("Request headers missing"); } this._responseTime = this.now(); this._isShared = shared !== false; this._res = res; - this._cc = parseCacheControl(res.headers['cache-control']); + this._rescc = parseCacheControl(res.headers['cache-control']); + this._req = req; + this._reqcc = parseCacheControl(req.headers['cache-control']); // When the Cache-Control header field is not present in a request, caches MUST consider the no-cache request pragma-directive // as having the same effect as if "Cache-Control: no-cache" were present (see Section 5.2.1). if (!res.headers['cache-control'] && /no-cache/.test(res.headers.pragma)) { - this._cc['no-cache'] = true; + this._rescc['no-cache'] = true; } } @@ -72,13 +77,13 @@ CachePolicy.prototype = { }, maxAge() { - if (this._cc['no-cache'] || this._cc['no-store']) { + if (this._rescc['no-cache'] || this._rescc['no-store']) { return 0; } // Shared responses with cookies are cacheable according to the RFC, but IMHO it'd be unwise to do so by default // so this implementation requires explicit opt-in via public header - if (this._isShared && (this._cc['private'] || (this._res.headers['set-cookie'] && !this._cc['public']))) { + if (this._isShared && (this._rescc['private'] || (this._res.headers['set-cookie'] && !this._rescc['public']))) { return 0; } @@ -89,14 +94,14 @@ CachePolicy.prototype = { if (this._isShared) { // if a response includes the s-maxage directive, a shared cache recipient MUST ignore the Expires field. - if (this._cc['s-maxage']) { - return parseInt(this._cc['s-maxage'], 10); + if (this._rescc['s-maxage']) { + return parseInt(this._rescc['s-maxage'], 10); } } // If a response includes a Cache-Control field with the max-age directive, a recipient MUST ignore the Expires field. - if (this._cc['max-age']) { - return parseInt(this._cc['max-age'], 10); + if (this._rescc['max-age']) { + return parseInt(this._rescc['max-age'], 10); } const dateValue = this.date(); diff --git a/package.json b/package.json index fd411a5..f6778ba 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "http-cache-semantics", - "version": "1.0.0", + "version": "2.0.0", "description": "Parses Cache-Control headers and friends", "main": "index.js", "repository": "https://github.com/pornel/http-cache-semantics.git",