diff options
-rw-r--r-- | cherrypy/lib/encoding.py | 13 | ||||
-rw-r--r-- | cherrypy/test/test_caching.py | 21 |
2 files changed, 28 insertions, 6 deletions
diff --git a/cherrypy/lib/encoding.py b/cherrypy/lib/encoding.py index fe2ca74d..410a2dca 100644 --- a/cherrypy/lib/encoding.py +++ b/cherrypy/lib/encoding.py @@ -221,6 +221,9 @@ def gzip(compress_level=5, mime_types=['text/html', 'text/plain']): * The 'identity' value is given with a qvalue > 0. """ response = cherrypy.response + + set_vary_header(response, "Accept-Encoding") + if not response.body: # Response body is empty (might be a 304 for instance) return @@ -249,8 +252,7 @@ def gzip(compress_level=5, mime_types=['text/html', 'text/plain']): if coding.qvalue == 0: return if ct in mime_types: - set_vary_header(response) - + # Return a generator that compresses the page response.headers['Content-Encoding'] = 'gzip' response.body = compress(response.body, compress_level) if response.headers.has_key("Content-Length"): @@ -259,10 +261,9 @@ def gzip(compress_level=5, mime_types=['text/html', 'text/plain']): return cherrypy.HTTPError(406, "identity, gzip").set_response() -def set_vary_header(response): - # Return a generator that compresses the page +def set_vary_header(response, header_name): varies = response.headers.get("Vary", "") varies = [x.strip() for x in varies.split(",") if x.strip()] - if "Accept-Encoding" not in varies: - varies.append("Accept-Encoding") + if header_name not in varies: + varies.append(header_name) response.headers['Vary'] = ", ".join(varies) diff --git a/cherrypy/test/test_caching.py b/cherrypy/test/test_caching.py index efa8637e..9360f5f4 100644 --- a/cherrypy/test/test_caching.py +++ b/cherrypy/test/test_caching.py @@ -33,6 +33,18 @@ def setup_server(): cherrypy.response.headers['Last-Modified'] = http.HTTPDate() return gif_bytes a_gif.exposed = True + + class VaryHeaderCachingServer: + + _cp_config = {'tools.caching.on': True, + 'tools.response_headers.on': True, + 'tools.response_headers.headers': [('Vary', 'Content-Encoding')], + } + + def index(self): + msg = "Always Coca-cola" + return msg + index.exposed = True class UnCached(object): _cp_config = {'tools.expires.on': True, @@ -70,6 +82,7 @@ def setup_server(): cherrypy.tree.mount(Root()) cherrypy.tree.mount(UnCached(), "/expires") + cherrypy.tree.mount(VaryHeaderCachingServer(), "/varying_headers") cherrypy.config.update({'tools.gzip.on': True}) @@ -92,6 +105,8 @@ class CacheTest(helper.CPWebCase): # POST, PUT, DELETE should not be cached. self.getPage("/", method="POST") self.assertBody('visit #2') + # Because gzip is turned on, the Vary header should always Vary for content-encoding + self.assertHeader('Vary', 'Content-Encoding') # The previous request should have invalidated the cache, # so this request will recalc the response. self.getPage("/", method="GET") @@ -106,6 +121,7 @@ class CacheTest(helper.CPWebCase): # so this request will recalc the response. self.getPage("/", method="GET", headers=[('Accept-Encoding', 'gzip')]) self.assertHeader('Content-Encoding', 'gzip') + self.assertHeader('Vary') self.assertEqual(cherrypy.lib.encoding.decompress(self.body), "visit #5") # Now check that a second request gets the gzip header and gzipped body @@ -121,6 +137,11 @@ class CacheTest(helper.CPWebCase): self.assertNoHeader('Content-Encoding') self.assertBody('visit #6') + def testVaryHeader(self): + self.getPage("/varying_headers/") + self.assertStatus("200 OK") + self.assertHeader('Vary', 'Our-Varying-Header') + def testExpiresTool(self): # test setting an expires header |