Changeset 2465
- Timestamp:
- 06/24/09 13:55:26
- Files:
-
- trunk/cherrypy/lib/encoding.py (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/lib/encoding.py
r2460 r2465 214 214 215 215 216 def gzip(compress_level=5, mime_types=['text/html', 'text/plain'] ):216 def gzip(compress_level=5, mime_types=['text/html', 'text/plain'], debug=False): 217 217 """Try to gzip the response body if Content-Type in mime_types. 218 218 … … 233 233 if not response.body: 234 234 # Response body is empty (might be a 304 for instance) 235 if debug: 236 cherrypy.log('No response body', context='GZIP') 235 237 return 236 238 … … 238 240 # don't re-zip. 239 241 if getattr(request, "cached", False): 242 if debug: 243 cherrypy.log('Not gzipping cached response', context='GZIP') 240 244 return 241 245 … … 249 253 # information that a different content-coding is meaningful 250 254 # to the client. 255 if debug: 256 cherrypy.log('No Accept-Encoding', context='GZIP') 251 257 return 252 258 … … 254 260 for coding in acceptable: 255 261 if coding.value == 'identity' and coding.qvalue != 0: 262 if debug: 263 cherrypy.log('Non-zero identity qvalue: %r' % coding, 264 context='GZIP') 256 265 return 257 266 if coding.value in ('gzip', 'x-gzip'): 258 267 if coding.qvalue == 0: 268 if debug: 269 cherrypy.log('Zero gzip qvalue: %r' % coding, 270 context='GZIP') 259 271 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 267 288 return 289 290 if debug: 291 cherrypy.log('No acceptable encoding found.', context='GZIP') 268 292 cherrypy.HTTPError(406, "identity, gzip").set_response() 269 293

