[web] set proper Cache-Control header for static files

  • set Cache-Control max-age to match Expires
  • use the max_age method (defaults to 1 week) instead of hardcoded 6 months
authorJulien Cristau <julien.cristau@logilab.fr>
changesete35d23686d1f
branchdefault
phasepublic
hiddenno
parent revision#70b21cf8b45c [web] use a redirect for /data/foo requests that don't use the instance md5 hash
child revision#de741492538d [dataimport] backport massive store from dataio cube
files modified by this revision
web/test/unittest_views_staticcontrollers.py
web/views/staticcontrollers.py
# HG changeset patch
# User Julien Cristau <julien.cristau@logilab.fr>
# Date 1448039828 -3600
# Fri Nov 20 18:17:08 2015 +0100
# Node ID e35d23686d1f501f1ad49b9de7037d1d57e9a48e
# Parent 70b21cf8b45cbc5aa6ed400fcdd3d6ce9146a284
[web] set proper Cache-Control header for static files

- set Cache-Control max-age to match Expires
- use the max_age method (defaults to 1 week) instead of hardcoded 6
months

diff --git a/web/test/unittest_views_staticcontrollers.py b/web/test/unittest_views_staticcontrollers.py
@@ -68,10 +68,13 @@
1 
2      def _check_datafile_ok(self, fname):
3          with self._publish_static_files(fname) as req:
4              self.assertEqual(200, req.status_out)
5              self.assertIn('last-modified', req.headers_out)
6 +            self.assertIn('expires', req.headers_out)
7 +            self.assertEqual(req.get_response_header('cache-control'),
8 +                             {'max-age': 604800})
9          next_headers = {
10              'if-modified-since': req.get_response_header('last-modified', raw=True),
11          }
12          with self._publish_static_files(fname, next_headers) as req:
13              self.assertEqual(304, req.status_out)
diff --git a/web/views/staticcontrollers.py b/web/views/staticcontrollers.py
@@ -64,13 +64,14 @@
14          if not osp.isfile(path):
15              raise NotFound()
16          if not debugmode:
17              # XXX: Don't provide additional resource information to error responses
18              #
19 -            # the HTTP RFC recommands not going further than 1 year ahead
20 -            expires = datetime.now() + timedelta(days=6*30)
21 +            # the HTTP RFC recommends not going further than 1 year ahead
22 +            expires = datetime.now() + timedelta(seconds=self.max_age(path))
23              self._cw.set_header('Expires', generateDateTime(mktime(expires.timetuple())))
24 +            self._cw.set_header('Cache-Control', 'max-age=%s' % self.max_age(path))
25 
26          # XXX system call to os.stats could be cached once and for all in
27          # production mode (where static files are not expected to change)
28          #
29          # Note that: we do a osp.isdir + osp.isfile before and a potential