Commit df07a42e authored by Brian Lloyd's avatar Brian Lloyd

Merged Brad Clements compression refinements.

parent acac2b72
...@@ -12,8 +12,8 @@ ...@@ -12,8 +12,8 @@
############################################################################## ##############################################################################
'''CGI Response Output formatter '''CGI Response Output formatter
$Id: HTTPResponse.py,v 1.63 2002/06/14 15:52:49 Brian Exp $''' $Id: HTTPResponse.py,v 1.64 2002/06/20 15:17:07 Brian Exp $'''
__version__='$Revision: 1.63 $'[11:-2] __version__='$Revision: 1.64 $'[11:-2]
import types, os, sys, re import types, os, sys, re
import zlib, struct import zlib, struct
...@@ -145,7 +145,12 @@ class HTTPResponse(BaseResponse): ...@@ -145,7 +145,12 @@ class HTTPResponse(BaseResponse):
realm='Zope' realm='Zope'
_error_format='text/html' _error_format='text/html'
_locked_status = 0 _locked_status = 0
use_HTTP_content_compression = 0 # indicate if setBody should content-compress output
# Indicate if setBody should content-compress output.
# 0 - no compression
# 1 - compress if accept-encoding ok
# 2 - ignore accept-encoding (i.e. force)
use_HTTP_content_compression = 0
def __init__(self,body='',status=200,headers=None, def __init__(self,body='',status=200,headers=None,
stdout=sys.stdout, stderr=sys.stderr,): stdout=sys.stdout, stderr=sys.stderr,):
...@@ -331,7 +336,14 @@ class HTTPResponse(BaseResponse): ...@@ -331,7 +336,14 @@ class HTTPResponse(BaseResponse):
self.body = z self.body = z
self.setHeader('content-length', newlen) self.setHeader('content-length', newlen)
self.setHeader('content-encoding','gzip') self.setHeader('content-encoding','gzip')
if self.use_HTTP_content_compression == 1:
# use_HTTP_content_compression == 1 if force was
# NOT used in enableHTTPCompression().
# If we forced it, then Accept-Encoding
# was ignored anyway, so cache should not
# vary on it. Otherwise if not forced, cache should
# respect Accept-Encoding client header
self.appendHeader('Vary','Accept-Encoding')
return self return self
def enableHTTPCompression(self,REQUEST={},force=0,disable=0,query=0): def enableHTTPCompression(self,REQUEST={},force=0,disable=0,query=0):
...@@ -342,7 +354,8 @@ class HTTPResponse(BaseResponse): ...@@ -342,7 +354,8 @@ class HTTPResponse(BaseResponse):
disable -- set true to disable compression disable -- set true to disable compression
query -- just return if compression has been previously requested query -- just return if compression has been previously requested
returns -- 1 if compression will be performed, 0 otherwise returns -- 1 if compression will be attempted, 2 if compression
is forced, 0 if no compression
The HTTP specification allows for transfer encoding and content The HTTP specification allows for transfer encoding and content
encoding. Unfortunately many web browsers still do not support encoding. Unfortunately many web browsers still do not support
...@@ -377,9 +390,10 @@ class HTTPResponse(BaseResponse): ...@@ -377,9 +390,10 @@ class HTTPResponse(BaseResponse):
self.use_HTTP_content_compression = 0 self.use_HTTP_content_compression = 0
elif force or (REQUEST.get('HTTP_ACCEPT_ENCODING','').find('gzip') != -1): elif force or (REQUEST.get('HTTP_ACCEPT_ENCODING','').find('gzip') != -1):
if force:
self.use_HTTP_content_compression = 2
else:
self.use_HTTP_content_compression = 1 self.use_HTTP_content_compression = 1
if not force:
self.appendHeader('Vary', 'Accept-Encoding')
return self.use_HTTP_content_compression return self.use_HTTP_content_compression
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment