You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
4.0 KiB

4 years ago
  1. import calendar
  2. import time
  3. from email.utils import formatdate, parsedate, parsedate_tz
  4. from datetime import datetime, timedelta
  5. TIME_FMT = "%a, %d %b %Y %H:%M:%S GMT"
  6. def expire_after(delta, date=None):
  7. date = date or datetime.utcnow()
  8. return date + delta
  9. def datetime_to_header(dt):
  10. return formatdate(calendar.timegm(dt.timetuple()))
  11. class BaseHeuristic(object):
  12. def warning(self, response):
  13. """
  14. Return a valid 1xx warning header value describing the cache
  15. adjustments.
  16. The response is provided too allow warnings like 113
  17. http://tools.ietf.org/html/rfc7234#section-5.5.4 where we need
  18. to explicitly say response is over 24 hours old.
  19. """
  20. return '110 - "Response is Stale"'
  21. def update_headers(self, response):
  22. """Update the response headers with any new headers.
  23. NOTE: This SHOULD always include some Warning header to
  24. signify that the response was cached by the client, not
  25. by way of the provided headers.
  26. """
  27. return {}
  28. def apply(self, response):
  29. updated_headers = self.update_headers(response)
  30. if updated_headers:
  31. response.headers.update(updated_headers)
  32. warning_header_value = self.warning(response)
  33. if warning_header_value is not None:
  34. response.headers.update({"Warning": warning_header_value})
  35. return response
  36. class OneDayCache(BaseHeuristic):
  37. """
  38. Cache the response by providing an expires 1 day in the
  39. future.
  40. """
  41. def update_headers(self, response):
  42. headers = {}
  43. if "expires" not in response.headers:
  44. date = parsedate(response.headers["date"])
  45. expires = expire_after(timedelta(days=1), date=datetime(*date[:6]))
  46. headers["expires"] = datetime_to_header(expires)
  47. headers["cache-control"] = "public"
  48. return headers
  49. class ExpiresAfter(BaseHeuristic):
  50. """
  51. Cache **all** requests for a defined time period.
  52. """
  53. def __init__(self, **kw):
  54. self.delta = timedelta(**kw)
  55. def update_headers(self, response):
  56. expires = expire_after(self.delta)
  57. return {"expires": datetime_to_header(expires), "cache-control": "public"}
  58. def warning(self, response):
  59. tmpl = "110 - Automatically cached for %s. Response might be stale"
  60. return tmpl % self.delta
  61. class LastModified(BaseHeuristic):
  62. """
  63. If there is no Expires header already, fall back on Last-Modified
  64. using the heuristic from
  65. http://tools.ietf.org/html/rfc7234#section-4.2.2
  66. to calculate a reasonable value.
  67. Firefox also does something like this per
  68. https://developer.mozilla.org/en-US/docs/Web/HTTP/Caching_FAQ
  69. http://lxr.mozilla.org/mozilla-release/source/netwerk/protocol/http/nsHttpResponseHead.cpp#397
  70. Unlike mozilla we limit this to 24-hr.
  71. """
  72. cacheable_by_default_statuses = {
  73. 200, 203, 204, 206, 300, 301, 404, 405, 410, 414, 501
  74. }
  75. def update_headers(self, resp):
  76. headers = resp.headers
  77. if "expires" in headers:
  78. return {}
  79. if "cache-control" in headers and headers["cache-control"] != "public":
  80. return {}
  81. if resp.status not in self.cacheable_by_default_statuses:
  82. return {}
  83. if "date" not in headers or "last-modified" not in headers:
  84. return {}
  85. date = calendar.timegm(parsedate_tz(headers["date"]))
  86. last_modified = parsedate(headers["last-modified"])
  87. if date is None or last_modified is None:
  88. return {}
  89. now = time.time()
  90. current_age = max(0, now - date)
  91. delta = date - calendar.timegm(last_modified)
  92. freshness_lifetime = max(0, min(delta / 10, 24 * 3600))
  93. if freshness_lifetime <= current_age:
  94. return {}
  95. expires = date + freshness_lifetime
  96. return {"expires": time.strftime(TIME_FMT, time.gmtime(expires))}
  97. def warning(self, resp):
  98. return None