| 63 | | |
|---|
| | 63 | def 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 | |
|---|
| 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 [] |
|---|