6 module.exports = fresh;
9 * Check freshness of `req` and `res` headers.
11 * When the cache is "fresh" __true__ is returned,
12 * otherwise __false__ is returned to indicate that
13 * the cache is now stale.
21 function fresh(req, res) {
23 var etagMatches = true;
24 var notModified = true;
27 var modifiedSince = req['if-modified-since'];
28 var noneMatch = req['if-none-match'];
29 var lastModified = res['last-modified'];
30 var etag = res['etag'];
31 var cc = req['cache-control'];
33 // unconditional request
34 if (!modifiedSince && !noneMatch) return false;
36 // check for no-cache cache request directive
37 if (cc && cc.indexOf('no-cache') !== -1) return false;
39 // parse if-none-match
40 if (noneMatch) noneMatch = noneMatch.split(/ *, */);
43 if (noneMatch) etagMatches = ~noneMatch.indexOf(etag) || '*' == noneMatch[0];
47 modifiedSince = new Date(modifiedSince);
48 lastModified = new Date(lastModified);
49 notModified = lastModified <= modifiedSince;
52 return !! (etagMatches && notModified);