Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 2465

Show
Ignore:
Timestamp:
06/24/09 13:55:26
Author:
fumanchu
Message:

New 'debug' arg to lib.gzip.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/lib/encoding.py

    r2460 r2465  
    214214 
    215215 
    216 def gzip(compress_level=5, mime_types=['text/html', 'text/plain']): 
     216def gzip(compress_level=5, mime_types=['text/html', 'text/plain'], debug=False): 
    217217    """Try to gzip the response body if Content-Type in mime_types. 
    218218     
     
    233233    if not response.body: 
    234234        # Response body is empty (might be a 304 for instance) 
     235        if debug: 
     236            cherrypy.log('No response body', context='GZIP') 
    235237        return 
    236238     
     
    238240    # don't re-zip. 
    239241    if getattr(request, "cached", False): 
     242        if debug: 
     243            cherrypy.log('Not gzipping cached response', context='GZIP') 
    240244        return 
    241245     
     
    249253        # information that a different content-coding is meaningful 
    250254        # to the client. 
     255        if debug: 
     256            cherrypy.log('No Accept-Encoding', context='GZIP') 
    251257        return 
    252258     
     
    254260    for coding in acceptable: 
    255261        if coding.value == 'identity' and coding.qvalue != 0: 
     262            if debug: 
     263                cherrypy.log('Non-zero identity qvalue: %r' % coding, 
     264                             context='GZIP') 
    256265            return 
    257266        if coding.value in ('gzip', 'x-gzip'): 
    258267            if coding.qvalue == 0: 
     268                if debug: 
     269                    cherrypy.log('Zero gzip qvalue: %r' % coding, 
     270                                 context='GZIP') 
    259271                return 
    260             if ct in mime_types: 
    261                 # Return a generator that compresses the page 
    262                 response.headers['Content-Encoding'] = 'gzip' 
    263                 response.body = compress(response.body, compress_level) 
    264                 if "Content-Length" in response.headers: 
    265                     # Delete Content-Length header so finalize() recalcs it. 
    266                     del response.headers["Content-Length"] 
     272             
     273            if ct not in mime_types: 
     274                if debug: 
     275                    cherrypy.log('Content-Type %r not in mime_types %r' % 
     276                                 (ct, mime_types), context='GZIP') 
     277                return 
     278             
     279            if debug: 
     280                cherrypy.log('Gzipping', context='GZIP') 
     281            # Return a generator that compresses the page 
     282            response.headers['Content-Encoding'] = 'gzip' 
     283            response.body = compress(response.body, compress_level) 
     284            if "Content-Length" in response.headers: 
     285                # Delete Content-Length header so finalize() recalcs it. 
     286                del response.headers["Content-Length"] 
     287             
    267288            return 
     289     
     290    if debug: 
     291        cherrypy.log('No acceptable encoding found.', context='GZIP') 
    268292    cherrypy.HTTPError(406, "identity, gzip").set_response() 
    269293 

Hosted by WebFaction

Log in as guest/cpguest to create tickets