SVN: r24226 - trunk/quixote

Neil Schemenauer <nascheme-fVcApmY9cLvQ3/1i3zOLAti2O/[email protected]> Thu, 13 May 2004 11:36:35 -0400
Newsgroups gmane.comp.web.quixote.cvs
Message-ID <[email protected]>
Author: nascheme
Date: 2004-05-13 11:36:35 -0400 (Thu, 13 May 2004)
New Revision: 24226

Modified:
   trunk/quixote/publish.py
Log:
Add filter_output() hook (as suggested by Thomas Guettler).


Modified: trunk/quixote/publish.py
===================================================================
--- trunk/quixote/publish.py	2004-05-13 15:03:53 UTC (rev 24225)
+++ trunk/quixote/publish.py	2004-05-13 15:36:35 UTC (rev 24226)
@@ -473,21 +473,22 @@
 
         return output
 
-    _gzip_header = ("\037\213" # magic
+    _GZIP_HEADER = ("\037\213" # magic
                     "\010" # compression method
                     "\000" # flags
                     "\000\000\000\000" # time, who cares?
                     "\002"
                     "\377")
 
+    _GZIP_THRESHOLD = 200 # responses smaller than this are not compressed
+
     def compress_output (self, request, output):
-
         encoding = request.get_encoding(["gzip", "x-gzip"])
         n = len(output)
-        if n > 200 and encoding:
+        if n > self._GZIP_THRESHOLD and encoding:
             co = zlib.compressobj(6, zlib.DEFLATED, -zlib.MAX_WBITS,
                                   zlib.DEF_MEM_LEVEL, 0)
-            chunks = [self._gzip_header,
+            chunks = [self._GZIP_HEADER,
                       co.compress(output),
                       co.flush(),
                       struct.pack("<ll", zlib.crc32(output), len(output))]
@@ -497,6 +498,16 @@
             request.response.set_header("Content-Encoding", encoding)
         return output
 
+    def filter_output(self, request, output):
+        """Hook for post processing the output.  Subclasses may wish to
+        override (e.g. check HTML syntax).
+        """
+        if (output and
+                self.config.compress_pages and
+                not isinstance(output, Stream)):
+            output = self.compress_output(request, str(output))
+        return output
+    
     def process_request (self, request, env):
         """process_request(request : HTTPRequest, env : dict) : string
 
@@ -514,13 +525,8 @@
         except:
             # Some other exception, generate error messages to the logs, etc.
             output = self.finish_failed_request(request)
+        output = self.filter_output(request, output)
         self.log_request(request)
-
-        if (output and
-                self.config.compress_pages and
-                not isinstance(output, Stream)):
-            output = self.compress_output(request, str(output))
-
         return output
 
     def publish (self, stdin, stdout, stderr, env):