Commits

Robert Brewer committed 62bf656

Fix for #535 (Apache re-processes Range headers). _cpmodpy now uses a new "ignore_headers(['Range'])" Tool by default.

Comments (0)

Files changed (4)

cherrypy/_cpmodpy.py

         func = getattr(mod, fname)
         func()
     
-    cherrypy.config.update({'log.screen': False})
+    cherrypy.config.update({'log.screen': False,
+                            "tools.ignore_headers.on": True,
+                            "tools.ignore_headers.headers": ['Range'],
+                            })
     
     if cherrypy.engine.state == cherrypy._cpengine.STOPPED:
         cherrypy.engine.start(blocking=False)

cherrypy/_cptools.py

 default_toolbox.expires = Tool('before_finalize', _caching.expires)
 default_toolbox.tidy = Tool('before_finalize', tidy.tidy)
 default_toolbox.nsgmls = Tool('before_finalize', tidy.nsgmls)
+default_toolbox.ignore_headers = Tool('before_request_body', cptools.ignore_headers)
 
 
 del cptools, encoding, static, tidy

cherrypy/lib/cptools.py

             request.remote.ip = xff
 
 
+def ignore_headers(headers=('Range',)):
+    """Delete request headers whose field names are included in 'headers'.
+    
+    This is a useful tool for working behind certain HTTP servers;
+    for example, Apache duplicates the work that CP does for 'Range'
+    headers, and will doubly-truncate the response.
+    """
+    request = cherrypy.request
+    for name in headers:
+        if name in request.headers:
+            del request.headers[name]
+
+
 def response_headers(headers=None):
     """Set headers on the response."""
     for name, value in (headers or []):

cherrypy/test/modpy.py

         
         import cherrypy
         cherrypy.config.update({
-            "log.error.file": os.path.join(curdir, "test.log"),
+            "log.error_file": os.path.join(curdir, "test.log"),
             "environment": "production",
             })
         cherrypy.engine.start(blocking=False)