summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorianb <devnull@localhost>2006-11-12 19:00:54 +0000
committerianb <devnull@localhost>2006-11-12 19:00:54 +0000
commit4b5acac85beb579b20baef393762525913e88ba9 (patch)
treebbefd7d61f2684642d53a22ec7b2aec2e0905949
parent2eb140361265ca568b504613d134f4684eb48817 (diff)
downloadpaste-4b5acac85beb579b20baef393762525913e88ba9.tar.gz
Fix for Content-Length in gzip response from Brad Clements. Also gzip test
-rw-r--r--docs/news.txt3
-rw-r--r--paste/gzipper.py5
-rw-r--r--tests/test_gzipper.py18
3 files changed, 26 insertions, 0 deletions
diff --git a/docs/news.txt b/docs/news.txt
index e9f46b0..bbe3c99 100644
--- a/docs/news.txt
+++ b/docs/news.txt
@@ -18,6 +18,9 @@ svn trunk
* Made ``paste.*`` compatible with `py2exe <http://www.py2exe.org/>`_
by adding a ``modulefinder`` call in ``__init__.py``
+* The ``paste.gzipper`` gzipping middleware wasn't changing the
+ Content-Length header properly; thanks to Brad Clements for the fix.
+
1.0
---
diff --git a/paste/gzipper.py b/paste/gzipper.py
index 5ec2efe..48165fe 100644
--- a/paste/gzipper.py
+++ b/paste/gzipper.py
@@ -12,6 +12,7 @@ Gzip-encodes the response.
import gzip
from paste.response import header_value
+from paste.httpheaders import CONTENT_LENGTH
try:
from cStringIO import StringIO
@@ -47,8 +48,10 @@ class GzipResponse(object):
self.compress_level = compress_level
self.buffer = StringIO()
self.compressible = False
+ self.headers = None
def gzip_start_response(self, status, headers, exc_info=None):
+ self.headers = headers
ct = header_value(headers,'content-type')
ce = header_value(headers,'content-encoding')
self.compressible = False
@@ -66,6 +69,8 @@ class GzipResponse(object):
out.seek(0)
s = out.getvalue()
out.close()
+ if self.compressible and self.headers is not None:
+ CONTENT_LENGTH.update(self.headers, str(len(s)))
return [s]
def finish_response(self, app_iter):
diff --git a/tests/test_gzipper.py b/tests/test_gzipper.py
new file mode 100644
index 0000000..0832700
--- /dev/null
+++ b/tests/test_gzipper.py
@@ -0,0 +1,18 @@
+from paste.fixture import TestApp
+from paste.gzipper import middleware
+import gzip, cStringIO
+
+def simple_app(environ, start_response):
+ start_response('200 OK', [('content-type', 'text/plain')])
+ return 'this is a test'
+
+wsgi_app = middleware(simple_app)
+app = TestApp(wsgi_app)
+
+def test_gzip():
+ res = app.get(
+ '/', extra_environ=dict(HTTP_ACCEPT_ENCODING='gzip'))
+ assert int(res.header('content-length')) == len(res.body)
+ assert res.body != 'this is a test'
+ actual = gzip.GzipFile(fileobj=cStringIO.StringIO(res.body)).read()
+ assert actual == 'this is a test'