Package omeroweb :: Package webgateway :: Module middleware
[hide private]
[frames] | no frames]

Source Code for Module omeroweb.webgateway.middleware

 1  import re 
 2   
 3  from django.utils.text import compress_string 
 4  from django.utils.cache import patch_vary_headers 
 5   
 6  re_accepts_gzip = re.compile(r'\bgzip\b') 
 7   
8 -class GZipMiddleware(object):
9 """ 10 This middleware compresses content if the browser allows gzip compression. 11 It sets the Vary header accordingly, so that caches will base their storage 12 on the Accept-Encoding header. 13 """
14 - def process_response(self, request, response):
15 # It's not worth compressing non-OK or really short responses. 16 # omeroweb: the tradeoff for less than 8192k of uncompressed text is not worth it most of the times. 17 if response.status_code != 200 or len(response.content) < 8192: 18 return response 19 20 # Avoid gzipping if we've already got a content-encoding. 21 if response.has_header('Content-Encoding'): 22 return response 23 24 # omeroweb: we don't want to compress everything, so doing an opt-in approach 25 ctype = response.get('Content-Type', '').lower() 26 if not "javascript" in ctype and not "text" in ctype: 27 return response 28 29 patch_vary_headers(response, ('Accept-Encoding',)) 30 31 # Avoid gzipping if we've already got a content-encoding. 32 if response.has_header('Content-Encoding'): 33 return response 34 35 # Older versions of IE have issues with gzipped pages containing either 36 # Javascript and PDF. 37 if "msie" in request.META.get('HTTP_USER_AGENT', '').lower(): 38 ctype = response.get('Content-Type', '').lower() 39 if "javascript" in ctype or ctype == "application/pdf": 40 return response 41 42 ae = request.META.get('HTTP_ACCEPT_ENCODING', '') 43 if not re_accepts_gzip.search(ae): 44 return response 45 46 response.content = compress_string(response.content) 47 response['Content-Encoding'] = 'gzip' 48 response['Content-Length'] = str(len(response.content)) 49 return response
50