Changeset 1576
- Timestamp:
- 12/28/06 16:16:54
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/cherrypy-2.x/cherrypy/filters/cachefilter.py
r1524 r1576 118 118 119 119 request = cherrypy.request 120 response = cherrypy.response 120 121 121 122 # POST, PUT, DELETE should invalidate (delete) the cached copy. … … 130 131 # found a hit! check the if-modified-since request header 131 132 expirationTime, lastModified, obj = cacheData 132 s, h, b = obj133 s, h, b, create_time = obj 133 134 modifiedSince = request.headers.get('If-Modified-Since', None) 134 135 if modifiedSince is not None and modifiedSince == lastModified: 135 136 cherrypy._cache.totNonModified += 1 136 cherrypy.response.status = "304 Not Modified"137 ct = h.get( "Content-Type")137 response.status = "304 Not Modified" 138 ct = h.get('Content-Type', None) 138 139 if ct: 139 cherrypy.response.header_list["Content-Type"] = ct140 cherrypy.response.body = None140 response.headers['Content-Type'] = ct 141 response.body = None 141 142 else: 142 143 # serve it & get out from the request 143 144 response = cherrypy.response 144 response.status, response.header_list, response.body = s, h, b 145 raise cherrypy.RequestHandled() 145 response.status, response.headers, response.body = s, h, b 146 response.headers['Age'] = str(int(time.time() - create_time)) 147 request.execute_main = False 146 148 else: 147 149 request.cacheable = True … … 169 171 # save the cache data 170 172 body = ''.join([chunk for chunk in response._cachefilter_tee]) 173 create_time = time.time() 171 174 cherrypy._cache.put(lastModified, (response.status, 172 response.header_list, 173 body)) 175 response.headers, 176 body, 177 create_time)) 174 178 175 179 branches/cherrypy-2.x/cherrypy/test/test_cache_filter.py
r1506 r1576 4 4 import cherrypy 5 5 from cherrypy.filters.cachefilter import expires 6 from cherrypy.lib.httptools import HTTPDate 6 7 7 8 … … 16 17 return msg 17 18 index.exposed = True 18 19 20 def textplain(self): 21 cherrypy.response.headers['Content-type'] = 'text/plain' 22 cherrypy.response.headers['Last-Modified'] = HTTPDate() 23 return self.index() 24 textplain.exposed = True 25 19 26 class UnCached(object): 20 27 … … 60 67 61 68 def testCaching(self): 69 elapsed = 0.0 62 70 for trial in xrange(10): 63 71 self.getPage("/") 64 # The response should be the same every time! 72 # The response should be the same every time, 73 # except for the Age response header. 65 74 self.assertBody('visit #1') 75 if trial != 0: 76 age = int(self.assertHeader("Age")) 77 self.assert_(age >= elapsed) 78 elapsed = age 66 79 67 80 # POST, PUT, DELETE should not be cached. … … 81 94 self.getPage("/", method="GET") 82 95 self.assertBody('visit #5') 83 96 97 # make sure that custom set Content-types get passed through on 304s 98 self.getPage("/textplain") 99 self.assertHeader("Content-type", "text/plain") 100 self.assertStatus("200 OK") 101 self.assertBody('visit #6') 102 date = self.assertHeader("Last-Modified") 103 self.getPage("/textplain", [("If-Modified-Since", date)]) 104 self.assertHeader("Content-type", "text/plain") 105 self.assertStatus("304 Not Modified") 106 84 107 def testExpiresTool(self): 85 108

