summaryrefslogtreecommitdiff
path: root/node_modules/http-basic/lib/cache-utils.js
blob: 3f76b0f1f121d9622321f55be222286f8f067235 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
'use strict';

exports.isMatch = function (requestHeaders, cachedResponse) {
  if (cachedResponse.headers['vary'] && cachedResponse.requestHeaders) {
    return cachedResponse.headers['vary'].split(',').map(function (header) { return header.trim().toLowerCase(); }).every(function (header) {
      return requestHeaders[header] === cachedResponse.requestHeaders[header];
    });
  } else {
    return true;
  }
};
exports.isExpired = function (cachedResponse) {
  var match
  if (cachedResponse.headers['cache-control'] && (match = /^public\, *max\-age\=(\d+)$/.exec(cachedResponse.headers['cache-control']))) {
    var time = (Date.now() - cachedResponse.requestTimestamp) / 1000;
    if ((+match[1]) > time) {
      return false;
    }
  }
  if (cachedResponse.statusCode === 301 || cachedResponse.statusCode === 308) return false;
  return true;
};
exports.canCache = function (res) {
  if (res.headers['etag']) return true;
  if (/^public\, *max\-age\=(\d+)$/.test(res.headers['cache-control'])) return true;
  if (res.statusCode === 301 || res.statusCode === 308) return true;

  return false;
};