Commits

Sylvain Hellegouarch  committed 0c6b2bc

created modified_since function so that applications can check manually if a file has been modified since a given date

  • Participants
  • Parent commits d3ff507

Comments (0)

Files changed (1)

File cherrypy/lib/cptools.py

     def __getattr__(self, key):
         return self.items[key]
 
-
+def modified_since(path, stat=None):
+    """Check whether a file has been modified since the date
+    provided in 'If-Modified-Since'
+    It doesn't check if the file exists or not
+    Return True if has been modified, False otherwise
+    """
+    # serveFile already creates a stat object so let's not
+    # waste our energy to do it again
+    if not stat:
+        try:
+            stat = os.stat(path)
+        except OSError:
+            if cherrypy.config.get('server.log_file_not_found', False):
+                cherrypy.log("    NOT FOUND file: %s" % path, "DEBUG")
+            raise cherrypy.NotFound()
+    
+    response = cherrypy.response
+    strModifTime = httptools.HTTPDate(time.gmtime(stat.st_mtime))
+    if cherrypy.request.headers.has_key('If-Modified-Since'):
+        if cherrypy.request.headers['If-Modified-Since'] == strModifTime:
+            response.status = "304 Not Modified"
+            response.body = None
+            if getattr(cherrypy, "debug", None):
+                cherrypy.log("    Found file (304 Not Modified): %s" % path, "DEBUG")
+            return False
+    response.headers['Last-Modified'] = strModifTime
+    return True
+    
 def serveFile(path, contentType=None, disposition=None, name=None):
     """Set status, headers, and body in order to serve the given file.
     
         contentType = mimetypes.types_map.get(ext, "text/plain")
     response.headers['Content-Type'] = contentType
     
-    strModifTime = httptools.HTTPDate(time.gmtime(stat.st_mtime))
-    if cherrypy.request.headers.has_key('If-Modified-Since'):
-        if cherrypy.request.headers['If-Modified-Since'] == strModifTime:
-            response.status = "304 Not Modified"
-            response.body = None
-            if getattr(cherrypy, "debug", None):
-                cherrypy.log("    Found file (304 Not Modified): %s" % path, "DEBUG")
-            return []
-    response.headers['Last-Modified'] = strModifTime
+    if not modified_since(path, stat):
+        return []
     
     if disposition is not None:
         if name is None: