From ad332e5ca9503bba2e14cc8c3ca675eed56d72bc Mon Sep 17 00:00:00 2001 From: Kevin Christopher Henry Date: Tue, 11 Oct 2016 03:42:00 -0400 Subject: Refs #19705 -- Made GZipMiddleware make ETags weak. Django's conditional request processing can now produce 304 Not Modified responses for content that is subject to compression. --- django/middleware/gzip.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'django') diff --git a/django/middleware/gzip.py b/django/middleware/gzip.py index 642e3188d3..d6e2134f17 100644 --- a/django/middleware/gzip.py +++ b/django/middleware/gzip.py @@ -41,8 +41,12 @@ class GZipMiddleware(MiddlewareMixin): response.content = compressed_content response['Content-Length'] = str(len(response.content)) - if response.has_header('ETag'): - response['ETag'] = re.sub('"$', ';gzip"', response['ETag']) + # If there is a strong ETag, make it weak to fulfill the requirements + # of RFC 7232 section-2.1 while also allowing conditional request + # matches on ETags. + etag = response.get('ETag') + if etag and etag.startswith('"'): + response['ETag'] = 'W/' + etag response['Content-Encoding'] = 'gzip' return response -- cgit v1.3