diff options
| author | Georg Bauer <gb@hugo.westfalen.de> | 2005-10-09 10:47:06 +0000 |
|---|---|---|
| committer | Georg Bauer <gb@hugo.westfalen.de> | 2005-10-09 10:47:06 +0000 |
| commit | eb7ebb777cc0f285a2df8e357c30cd49af446e13 (patch) | |
| tree | 3814813895e84bb64834a4174c6b9824079fc5a7 /django/middleware/gzip.py | |
| parent | 4fc6d40d00586610ffabc8bf18b8e62fa12ed9bf (diff) | |
i18n: merged r787:r814 from trunk
git-svn-id: http://code.djangoproject.com/svn/django/branches/i18n@815 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/middleware/gzip.py')
| -rw-r--r-- | django/middleware/gzip.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/django/middleware/gzip.py b/django/middleware/gzip.py new file mode 100644 index 0000000000..201bec2000 --- /dev/null +++ b/django/middleware/gzip.py @@ -0,0 +1,24 @@ +import re +from django.utils.text import compress_string +from django.utils.cache import patch_vary_headers + +re_accepts_gzip = re.compile(r'\bgzip\b') + +class GZipMiddleware: + """ + This middleware compresses content if the browser allows gzip compression. + It sets the Vary header accordingly, so that caches will base their storage + on the Accept-Encoding header. + """ + def process_response(self, request, response): + patch_vary_headers(response, ('Accept-Encoding',)) + if response.has_header('Content-Encoding'): + return response + + ae = request.META.get('HTTP_ACCEPT_ENCODING', '') + if not re_accepts_gzip.search(ae): + return response + + response.content = compress_string(response.content) + response['Content-Encoding'] = 'gzip' + return response |
