Fix license issues
[sdnc/oam.git] / dgbuilder / dgeflows / node_modules / serve-static / node_modules / send / node_modules / fresh / index.js
1
2 /**
3  * Expose `fresh()`.
4  */
5
6 module.exports = fresh;
7
8 /**
9  * Check freshness of `req` and `res` headers.
10  *
11  * When the cache is "fresh" __true__ is returned,
12  * otherwise __false__ is returned to indicate that
13  * the cache is now stale.
14  *
15  * @param {Object} req
16  * @param {Object} res
17  * @return {Boolean}
18  * @api public
19  */
20
21 function fresh(req, res) {
22   // defaults
23   var etagMatches = true;
24   var notModified = true;
25
26   // fields
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'];
32
33   // unconditional request
34   if (!modifiedSince && !noneMatch) return false;
35
36   // check for no-cache cache request directive
37   if (cc && cc.indexOf('no-cache') !== -1) return false;  
38
39   // parse if-none-match
40   if (noneMatch) noneMatch = noneMatch.split(/ *, */);
41
42   // if-none-match
43   if (noneMatch) etagMatches = ~noneMatch.indexOf(etag) || '*' == noneMatch[0];
44
45   // if-modified-since
46   if (modifiedSince) {
47     modifiedSince = new Date(modifiedSince);
48     lastModified = new Date(lastModified);
49     notModified = lastModified <= modifiedSince;
50   }
51
52   return !! (etagMatches && notModified);
53 }