""" This simple module demonstrates how to use the content handler to send gzipped files to the remote browser (if it is supported) To use this handler, simple turn it on via: SnakeContentHandler mod_snake_gzip_content.content_handler You can enable and disable gzipping certain regions of your website by also adding a: SnakeVarSet gzip_content off|on And to change the compression level (6 default), use: SnakeVarSet gzip_level <0-9> XXX -- This module should change once 2.0 supports IO filtering. """ import gzip import mod_snake import string import sys def content_handler(per_dir, per_svr, r): if per_dir.has_key('gzip_content') and per_dir['gzip_content'] == 'off': return mod_snake.DECLINED if not r.headers_in.has_key('Accept-Encoding'): return mod_snake.DECLINED if string.find(r.headers_in['Accept-Encoding'], 'gzip') == -1: return mod_snake.DECLINED try: x = open(r.filename, 'r') data = x.read() r.content_encoding = 'gzip' r.send_http_header() gzip_level = 6 if per_dir.has_key('gzip_level'): try: gzip_level = int(per_dir['gzip_level']) except: pass if gzip_level < 0: gzip_level = 0 elif gzip_level > 9: gzip_level = 9 x = gzip.GzipFile(None, 'w', gzip_level, r.connection) x.write(data) x.close() return mod_snake.OK except IOError: # XXX -- Is this correct? if we don't know anything about it, # simply pass it on? return mod_snake.DECLINED except: # XXX -- Log a traceback return mod_snake.HTTP_INTERNAL_SERVER_ERROR