Changeset 1703
- Timestamp:
- 08/19/07 23:08:27
- Files:
-
- branches/cherrypy-3.0.x/cherrypy/lib/caching.py (modified) (2 diffs)
- branches/cherrypy-3.0.x/cherrypy/test/test_caching.py (modified) (3 diffs)
- branches/cherrypy-3.0.x/cherrypy/test/test_encoding.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/cherrypy-3.0.x/cherrypy/lib/caching.py
r1637 r1703 129 129 if c: 130 130 response = cherrypy.response 131 s, response.headers, b, create_time = cache_data 131 s, h, b, create_time, original_req_headers = cache_data 132 133 # Check 'Vary' selecting headers. If any headers mentioned in "Vary" 134 # differ between the cached and current request, bail out and 135 # let the rest of CP handle the request. This should properly 136 # mimic the behavior of isolated caches as RFC 2616 assumes: 137 # "If the selecting request header fields for the cached entry 138 # do not match the selecting request header fields of the new 139 # request, then the cache MUST NOT use a cached entry to satisfy 140 # the request unless it first relays the new request to the origin 141 # server in a conditional request and the server responds with 142 # 304 (Not Modified), including an entity tag or Content-Location 143 # that indicates the entity to be used. 144 # TODO: can we store multiple variants based on Vary'd headers? 145 for header_element in h.elements('Vary'): 146 key = header_element.value 147 if original_req_headers[key] != request.headers.get(key, 'missing'): 148 request.cached = False 149 request.cacheable = True 150 return False 132 151 133 152 # Add the required Age header 153 response.headers = h 134 154 response.headers["Age"] = str(int(response.time - create_time)) 135 155 … … 158 178 output.append(chunk) 159 179 yield chunk 180 160 181 # Might as well do this here; why cache if the body isn't consumed? 161 182 if response.headers.get('Pragma', None) != 'no-cache': 162 183 # save the cache data 163 184 body = ''.join([chunk for chunk in output]) 185 vary = [he.value for he in 186 cherrypy.response.headers.elements('Vary')] 187 if vary: 188 sel_headers = dict([(k, v) for k, v 189 in cherrypy.request.headers.iteritems() 190 if k in vary]) 191 else: 192 sel_headers = {} 164 193 cherrypy._cache.put((response.status, response.headers or {}, 165 body, response.time ))194 body, response.time, sel_headers)) 166 195 response.body = tee(response.body) 167 196 branches/cherrypy-3.0.x/cherrypy/test/test_caching.py
r1595 r1703 2 2 test.prefer_parent_path() 3 3 4 import gzip 4 5 import os 5 6 curdir = os.path.join(os.getcwd(), os.path.dirname(__file__)) 7 import StringIO 6 8 7 9 import cherrypy … … 59 61 cherrypy.tree.mount(Root()) 60 62 cherrypy.tree.mount(UnCached(), "/expires") 61 cherrypy.config.update({'environment': 'test_suite'}) 63 cherrypy.config.update({'environment': 'test_suite', 64 'tools.gzip.on': 'True'}) 62 65 63 66 … … 90 93 self.getPage("/", method="DELETE") 91 94 self.assertBody('visit #4') 95 92 96 # The previous request should have invalidated the cache, 93 97 # so this request will recalc the response. 98 zbuf = StringIO.StringIO() 99 zfile = gzip.GzipFile(mode='wb', fileobj=zbuf, compresslevel=9) 100 zfile.write("visit #5") 101 zfile.close() 102 103 self.getPage("/", method="GET", headers=[('Accept-Encoding', 'gzip')]) 104 self.assertHeader('Content-Encoding', 'gzip') 105 self.assertInBody(zbuf.getvalue()[:3]) 106 107 # Now check that a second request gets the gzip header and gzipped body 108 self.getPage("/", method="GET", headers=[('Accept-Encoding', 'gzip')]) 109 self.assertHeader('Content-Encoding', 'gzip') 110 self.assertInBody(zbuf.getvalue()[:3]) 111 112 # Now check that a third request that doesn't accept gzip 113 # gets another hit. 94 114 self.getPage("/", method="GET") 95 self.assertBody('visit #5') 115 self.assertNoHeader('Content-Encoding') 116 self.assertBody("visit #6") 96 117 97 118 def testExpiresTool(self): branches/cherrypy-3.0.x/cherrypy/test/test_encoding.py
r1384 r1703 118 118 self.assertInBody(zbuf.getvalue()[:3]) 119 119 self.assertHeader("Vary", "Accept-Encoding") 120 self.assertHeader('Content-Encoding', 'gzip') 120 121 121 122 # Test when gzip is denied.

