Skip to content

Commit d0e5f21

Browse files
committed
fix: Send Vary: Accept-Encoding on any data that could be compressed
This allows upstream caches to return the correct data even when compression is not in use. It is not sent if the content would never be compressed. I.E. size < 100 bytes, dynamic compression disabled, file would not benefit from compression (img/jpeg, img/png). Fix setVary to add header to vary list only if it's not already there. Found by redbot.org testing. References: https://www.stackpath.com/blog/accept-encoding-vary-important/
1 parent 2e793cb commit d0e5f21

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,9 @@ Fixed:
161161
rather than reporting a TypeError. (John Rouillard)
162162
- Make Last-Modified header use GMT not -0000 timezone. Fix error
163163
reported by redbot testing. (John Rouillard)
164+
- Send Vary: Accept-Encoding on any file that could be compressed
165+
even if the file is not encoded/compressed. Found by Redbot
166+
testing. (John Rouillard)
164167

165168
Features:
166169

roundup/cgi/client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,7 +2355,8 @@ def setVary(self, header):
23552355
'''Vary header will include the new header. This will append
23562356
if Vary exists.'''
23572357

2358-
if ('Vary' in self.additional_headers):
2358+
if ('Vary' in self.additional_headers and
2359+
header not in self.additional_headers['Vary']):
23592360
self.additional_headers['Vary'] += ", %s" % header
23602361
else:
23612362
self.additional_headers['Vary'] = header
@@ -2373,6 +2374,7 @@ def compress_encode(self, byte_content, quality=4):
23732374
# abort if already encoded (e.g. served from
23742375
# precompressed file or cache on disk)
23752376
if ('Content-Encoding' in self.additional_headers):
2377+
# Vary: 'Accept-Encoding' is set when Content-encoding set
23762378
return byte_content
23772379

23782380
# abort if file-type already compressed
@@ -2381,6 +2383,8 @@ def compress_encode(self, byte_content, quality=4):
23812383
self.precompressed_mime_types):
23822384
return byte_content
23832385

2386+
self.setVary('Accept-Encoding')
2387+
23842388
encoder = None
23852389
# return same content if unable to compress
23862390
new_content = byte_content
@@ -2419,7 +2423,6 @@ def compress_encode(self, byte_content, quality=4):
24192423
# and add Content-Encoding and Vary header.
24202424
self.additional_headers['Content-Length'] = str(len(new_content))
24212425
self.additional_headers['Content-Encoding'] = encoder
2422-
self.setVary('Accept-Encoding')
24232426
try:
24242427
current_etag = self.additional_headers['ETag']
24252428
except KeyError:

0 commit comments

Comments
 (0)