[Zope3-checkins] SVN: Zope3/trunk/src/zope/app/publisher/browser/fileresource.py - isolate the cache-control policy for resources in a separate function

Fred L. Drake, Jr. fdrake at gmail.com
Thu May 12 11:13:53 EDT 2005


Log message for revision 30338:
  - isolate the cache-control policy for resources in a separate function
  - add an Expires: header for older browsers
  

Changed:
  U   Zope3/trunk/src/zope/app/publisher/browser/fileresource.py

-=-
Modified: Zope3/trunk/src/zope/app/publisher/browser/fileresource.py
===================================================================
--- Zope3/trunk/src/zope/app/publisher/browser/fileresource.py	2005-05-12 14:55:31 UTC (rev 30337)
+++ Zope3/trunk/src/zope/app/publisher/browser/fileresource.py	2005-05-12 15:13:53 UTC (rev 30338)
@@ -15,6 +15,9 @@
 
 $Id$
 """
+
+import time
+
 from zope.publisher.interfaces import NotFound
 
 from zope.app.publisher.browser import BrowserView
@@ -89,10 +92,9 @@
         response.setHeader('Content-Type', file.content_type)
         response.setHeader('Last-Modified', file.lmh)
 
-        # Cache for one day
-        response.setHeader('Cache-Control', 'public,max-age=86400')
-        f=open(file.path,'rb')
-        data=f.read()
+        setCacheControl(response)
+        f = open(file.path,'rb')
+        data = f.read()
         f.close()
 
         return data
@@ -102,11 +104,19 @@
         response = self.request.response
         response.setHeader('Content-Type', file.content_type)
         response.setHeader('Last-Modified', file.lmh)
-        # Cache for one day
-        response.setHeader('Cache-Control', 'public,max-age=86400')
+        setCacheControl(response)
         return ''
 
 
+def setCacheControl(response, secs=86400):
+    # Cache for one day by default
+    response.setHeader('Cache-Control', 'public,max-age=%s' % secs)
+    t = time.time() + secs
+    response.setHeader('Expires',
+                       time.strftime("%a, %d %b %Y %H:%M:%S GMT",
+                                     time.gmtime(t)))
+
+
 class FileResourceFactory(object):
 
     def __init__(self, path, checker, name):



More information about the Zope3-Checkins mailing list