[web] set proper Cache-Control header for static files
authorJulien Cristau <julien.cristau@logilab.fr>
Fri, 20 Nov 2015 18:17:08 +0100
changeset 10852 e35d23686d1f
parent 10851 70b21cf8b45c
child 10853 de741492538d
[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
web/test/unittest_views_staticcontrollers.py
web/views/staticcontrollers.py
--- a/web/test/unittest_views_staticcontrollers.py	Fri Nov 20 18:23:17 2015 +0100
+++ b/web/test/unittest_views_staticcontrollers.py	Fri Nov 20 18:17:08 2015 +0100
@@ -70,6 +70,9 @@
         with self._publish_static_files(fname) as req:
             self.assertEqual(200, req.status_out)
             self.assertIn('last-modified', req.headers_out)
+            self.assertIn('expires', req.headers_out)
+            self.assertEqual(req.get_response_header('cache-control'),
+                             {'max-age': 604800})
         next_headers = {
             'if-modified-since': req.get_response_header('last-modified', raw=True),
         }
--- a/web/views/staticcontrollers.py	Fri Nov 20 18:23:17 2015 +0100
+++ b/web/views/staticcontrollers.py	Fri Nov 20 18:17:08 2015 +0100
@@ -66,9 +66,10 @@
         if not debugmode:
             # XXX: Don't provide additional resource information to error responses
             #
-            # the HTTP RFC recommands not going further than 1 year ahead
-            expires = datetime.now() + timedelta(days=6*30)
+            # the HTTP RFC recommends not going further than 1 year ahead
+            expires = datetime.now() + timedelta(seconds=self.max_age(path))
             self._cw.set_header('Expires', generateDateTime(mktime(expires.timetuple())))
+            self._cw.set_header('Cache-Control', 'max-age=%s' % self.max_age(path))
 
         # XXX system call to os.stats could be cached once and for all in
         # production mode (where static files are not expected to change)