/django/middleware/gzip.py

https://code.google.com/p/mango-py/ · Python · 38 lines · 22 code · 8 blank · 8 comment · 7 complexity · 736e15cec955c7c22f73c69b28d706b5 MD5 · raw file

  1. import re
  2. from django.utils.text import compress_string
  3. from django.utils.cache import patch_vary_headers
  4. re_accepts_gzip = re.compile(r'\bgzip\b')
  5. class GZipMiddleware(object):
  6. """
  7. This middleware compresses content if the browser allows gzip compression.
  8. It sets the Vary header accordingly, so that caches will base their storage
  9. on the Accept-Encoding header.
  10. """
  11. def process_response(self, request, response):
  12. # It's not worth compressing non-OK or really short responses.
  13. if response.status_code != 200 or len(response.content) < 200:
  14. return response
  15. patch_vary_headers(response, ('Accept-Encoding',))
  16. # Avoid gzipping if we've already got a content-encoding.
  17. if response.has_header('Content-Encoding'):
  18. return response
  19. # MSIE have issues with gzipped respones of various content types.
  20. if "msie" in request.META.get('HTTP_USER_AGENT', '').lower():
  21. ctype = response.get('Content-Type', '').lower()
  22. if not ctype.startswith("text/") or "javascript" in ctype:
  23. return response
  24. ae = request.META.get('HTTP_ACCEPT_ENCODING', '')
  25. if not re_accepts_gzip.search(ae):
  26. return response
  27. response.content = compress_string(response.content)
  28. response['Content-Encoding'] = 'gzip'
  29. response['Content-Length'] = str(len(response.content))
  30. return response