Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 929

Show
Ignore:
Timestamp:
01/20/06 11:08:00
Author:
lawouach
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/lib/cptools.py

    r909 r929  
    6161        return self.items[key] 
    6262 
    63  
     63def modified_since(path, stat=None): 
     64    """Check whether a file has been modified since the date 
     65    provided in 'If-Modified-Since' 
     66    It doesn't check if the file exists or not 
     67    Return True if has been modified, False otherwise 
     68    """ 
     69    # serveFile already creates a stat object so let's not 
     70    # waste our energy to do it again 
     71    if not stat: 
     72        try: 
     73            stat = os.stat(path) 
     74        except OSError: 
     75            if cherrypy.config.get('server.log_file_not_found', False): 
     76                cherrypy.log("    NOT FOUND file: %s" % path, "DEBUG") 
     77            raise cherrypy.NotFound() 
     78     
     79    response = cherrypy.response 
     80    strModifTime = httptools.HTTPDate(time.gmtime(stat.st_mtime)) 
     81    if cherrypy.request.headers.has_key('If-Modified-Since'): 
     82        if cherrypy.request.headers['If-Modified-Since'] == strModifTime: 
     83            response.status = "304 Not Modified" 
     84            response.body = None 
     85            if getattr(cherrypy, "debug", None): 
     86                cherrypy.log("    Found file (304 Not Modified): %s" % path, "DEBUG") 
     87            return False 
     88    response.headers['Last-Modified'] = strModifTime 
     89    return True 
     90     
    6491def serveFile(path, contentType=None, disposition=None, name=None): 
    6592    """Set status, headers, and body in order to serve the given file. 
     
    104131    response.headers['Content-Type'] = contentType 
    105132     
    106     strModifTime = httptools.HTTPDate(time.gmtime(stat.st_mtime)) 
    107     if cherrypy.request.headers.has_key('If-Modified-Since'): 
    108         if cherrypy.request.headers['If-Modified-Since'] == strModifTime: 
    109             response.status = "304 Not Modified" 
    110             response.body = None 
    111             if getattr(cherrypy, "debug", None): 
    112                 cherrypy.log("    Found file (304 Not Modified): %s" % path, "DEBUG") 
    113             return [] 
    114     response.headers['Last-Modified'] = strModifTime 
     133    if not modified_since(path, stat): 
     134        return [] 
    115135     
    116136    if disposition is not None: 

Hosted by WebFaction

Log in as guest/cpguest to create tickets