SVN: r25537 - trunk/quixote

Neil Schemenauer <nascheme-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]> Thu, 4 Nov 2004 18:47:05 -0500
Newsgroups gmane.comp.web.quixote.cvs
Message-ID <[email protected]>
Author: nascheme
Date: 2004-11-04 18:46:35 -0500 (Thu, 04 Nov 2004)
New Revision: 25537

Modified:
   trunk/quixote/http_response.py
Log:
Don't bother trying to compress content that is already likely to be
compressed.


Modified: trunk/quixote/http_response.py
===================================================================
--- trunk/quixote/http_response.py	2004-11-04 23:30:44 UTC (rev 25536)
+++ trunk/quixote/http_response.py	2004-11-04 23:46:35 UTC (rev 25537)
@@ -6,6 +6,7 @@
 """
 
 import time
+from sets import Set
 try:
     import zlib
 except ImportError:
@@ -70,6 +71,16 @@
                 "\002"
                 "\377")
 
+_GZIP_EXCLUDE = Set(["application/pdf",
+                     "application/zip",
+                     "audio/mpeg",
+                     "image/gif",
+                     "image/jpeg",
+                     "image/png",
+                     "video/mpeg",
+                     "video/quicktime",
+                     "video/x-msvideo",
+                     ])
 
 class HTTPResponse:
     """
@@ -210,7 +221,7 @@
             return chunk # non-ASCII chars are okay
         else:
             return chunk.encode(self.charset)
-            
+
     def _compress_body(self, body):
         """(body: str) -> str
         """
@@ -224,7 +235,7 @@
         compressed_body = "".join(chunks)
         ratio = float(n) / len(compressed_body)
         #print "gzip original size %d, ratio %.1f" % (n, ratio)
-        if ratio > 1.1:
+        if ratio > 1.0:
             self.set_header("Content-Encoding", "gzip")
             return compressed_body
         else:
@@ -238,7 +249,7 @@
         """
         if not isinstance(body, Stream):
             body = self._encode_chunk(stringify(body))
-            if compress:
+            if compress and self.content_type not in _GZIP_EXCLUDE:
                 body = self._compress_body(body)
         self.body = body