1. cherrypy
  2. CherryPy

Commits

Robert Brewer  committed c225db7

Fix for #528 (Allow request body parsing for methods other than "POST" and "PUT").

  • Participants
  • Parent commits 8491490
  • Branches cherrypy

Comments (0)

Files changed (3)

File _cprequest.py

View file
                 self.hooks.run('on_start_resource')
                 
                 if self.process_request_body:
+                    # Check path-specific methods_with_bodies.
+                    meths = self.config.get("methods_with_bodies", ("POST", "PUT"))
+                    self.process_request_body = self.method in meths
+                    
                     # Prepare the SizeCheckWrapper for the request body
                     mbs = int(self.config.get('server.max_request_body_size',
                                               100 * 1024 * 1024))
             path = "global"
         
         self.method = method
-        self.process_request_body = method in ("POST", "PUT")
-        
         self.path = path
         self.query_string = qs
         self.protocol = proto

File test/test_core.py

View file
 favicon_path = os.path.join(os.getcwd(), localDir, "../favicon.ico")
 
 defined_http_methods = ("OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE",
-                        "TRACE", "CONNECT")
+                        "TRACE", "CONNECT", "PROPFIND")
 
 
 def setup_server():
                           }
             
             def index(self):
-                raise NameError()
+                raise NameError("redirect_test")
             index.exposed = True
         error = Error()
         
         'server.max_request_body_size': 200,
         'server.max_request_header_size': 500,
         })
-    cherrypy.tree.mount(root, conf={'/': {'log_access_file': log_access_file}})
+    appconf = {
+        '/': {'log_access_file': log_access_file},
+        '/method': {'methods_with_bodies': ("POST", "PUT", "PROPFIND")},
+        }
+    cherrypy.tree.mount(root, conf=appconf)
 
 
 #                             Client-side code                             #
                           '=?utf-8?b?4oSrbmdzdHLDtm3ihKtuZ3N0csO2bQ==?=')
     
     def testHTTPMethods(self):
+        helper.webtest.methods_with_bodies = ("POST", "PUT", "PROPFIND")
+        
         # Test that all defined HTTP methods work.
         for m in defined_http_methods:
             self.getPage("/method/", method=m)
         self.assertBody("on top of other things")
         
         # Request a PUT method with a file body
-        h = [("Content-type", "text/plain"),
-             ("Content-Length", "27")]
+        b = "one thing on top of another"
+        h = [("Content-Type", "text/plain"),
+             ("Content-Length", str(len(b)))]
+        self.getPage("/method/request_body", headers=h, method="PUT", body=b)
+        self.assertStatus(200)
+        self.assertBody(b)
         
-        self.getPage("/method/request_body", headers=h, method="PUT",
-                       body="one thing on top of another")
-        self.assertBody("one thing on top of another")
+        # Request a custom method with a request body
+        b = ('<?xml version="1.0" encoding="utf-8" ?>\n\n'
+             '<propfind xmlns="DAV:"><prop><getlastmodified/>'
+             '</prop></propfind>')
+        h = [('Content-Type', 'text/xml'),
+             ('Content-Length', str(len(b)))]
+        self.getPage("/method/request_body", headers=h, method="PROPFIND", body=b)
+        self.assertStatus(200)
+        self.assertBody(b)
         
         # Request a disallowed method
         self.getPage("/method/", method="LINK")

File test/webtest.py

View file
             self._handlewebError(msg)
 
 
+methods_with_bodies = ("POST", "PUT")
 
 def cleanHeaders(headers, method, body, host, port):
     """Return request headers, with required headers added (if missing)."""
     if not found:
         headers.append(("Host", "%s:%s" % (host, port)))
     
-    if method in ("POST", "PUT"):
+    if method in methods_with_bodies:
         # Stick in default type and length headers if not present
         found = False
         for k, v in headers: