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

Changeset 1968

Show
Ignore:
Timestamp:
05/24/08 13:23:57
Author:
fumanchu
Message:

Test and fix for #819 (Request body not consumed on error when max_request_body_size is 0).

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/test/test_conn.py

    r1959 r1968  
    5454         
    5555        def upload(self): 
     56            if not cherrypy.request.method == 'POST': 
     57                raise AssertionError("'POST' != request.method %r" % 
     58                                     cherrypy.request.method) 
    5659            return ("thanks for '%s' (%s)" % 
    5760                    (cherrypy.request.body.read(), 
     
    276279            self.body = response.read() 
    277280            self.assertBody(pov) 
     281            conn.close() 
    278282        finally: 
    279283            if old_timeout is not None: 
     
    368372        self.assertStatus(200) 
    369373        self.assertBody("thanks for 'I am a small file' (text/plain)") 
     374        conn.close() 
    370375     
    371376    def test_readall_or_close(self): 
     
    381386            self.HTTP_CONN = httplib.HTTPConnection 
    382387         
    383         self.persistent = True 
    384         conn = self.HTTP_CONN 
    385          
    386         # Get a POST page with an error 
    387         conn.putrequest("POST", "/err_before_read", skip_host=True) 
    388         conn.putheader("Host", self.HOST) 
    389         conn.putheader("Content-Type", "text/plain") 
    390         conn.putheader("Content-Length", "1000") 
    391         conn.putheader("Expect", "100-continue") 
    392         conn.endheaders() 
    393         response = conn.response_class(conn.sock, method="POST") 
    394          
    395         # ...assert and then skip the 100 response 
    396         version, status, reason = response._read_status() 
    397         self.assertEqual(status, 100) 
    398         while True: 
    399             skip = response.fp.readline().strip() 
    400             if not skip: 
    401                 break 
    402          
    403         # ...send the body 
    404         conn.send("x" * 1000) 
    405          
    406         # ...get the final response 
    407         response.begin() 
    408         self.status, self.headers, self.body = webtest.shb(response) 
    409         self.assertStatus(500) 
    410          
    411         # Now try a working page with an Expect header... 
    412         conn._output('POST /upload HTTP/1.1') 
    413         conn._output("Host: %s" % self.HOST) 
    414         conn._output("Content-Type: text/plain") 
    415         conn._output("Content-Length: 17") 
    416         conn._output("Expect: 100-continue") 
    417         conn._send_output() 
    418         response = conn.response_class(conn.sock, method="POST") 
    419          
    420         # ...assert and then skip the 100 response 
    421         version, status, reason = response._read_status() 
    422         self.assertEqual(status, 100) 
    423         while True: 
    424             skip = response.fp.readline().strip() 
    425             if not skip: 
    426                 break 
    427          
    428         # ...send the body 
    429         conn.send("I am a small file") 
    430          
    431         # ...get the final response 
    432         response.begin() 
    433         self.status, self.headers, self.body = webtest.shb(response) 
    434         self.assertStatus(200) 
    435         self.assertBody("thanks for 'I am a small file' (text/plain)") 
     388        # Test a max of 0 (the default) and then reset to what it was above. 
     389        old_max = cherrypy.server.max_request_body_size 
     390        for new_max in (0, old_max): 
     391            cherrypy.server.max_request_body_size = new_max 
     392             
     393            self.persistent = True 
     394            conn = self.HTTP_CONN 
     395             
     396            # Get a POST page with an error 
     397            conn.putrequest("POST", "/err_before_read", skip_host=True) 
     398            conn.putheader("Host", self.HOST) 
     399            conn.putheader("Content-Type", "text/plain") 
     400            conn.putheader("Content-Length", "1000") 
     401            conn.putheader("Expect", "100-continue") 
     402            conn.endheaders() 
     403            response = conn.response_class(conn.sock, method="POST") 
     404             
     405            # ...assert and then skip the 100 response 
     406            version, status, reason = response._read_status() 
     407            self.assertEqual(status, 100) 
     408            while True: 
     409                skip = response.fp.readline().strip() 
     410                if not skip: 
     411                    break 
     412             
     413            # ...send the body 
     414            conn.send("x" * 1000) 
     415             
     416            # ...get the final response 
     417            response.begin() 
     418            self.status, self.headers, self.body = webtest.shb(response) 
     419            self.assertStatus(500) 
     420             
     421            # Now try a working page with an Expect header... 
     422            conn._output('POST /upload HTTP/1.1') 
     423            conn._output("Host: %s" % self.HOST) 
     424            conn._output("Content-Type: text/plain") 
     425            conn._output("Content-Length: 17") 
     426            conn._output("Expect: 100-continue") 
     427            conn._send_output() 
     428            response = conn.response_class(conn.sock, method="POST") 
     429             
     430            # ...assert and then skip the 100 response 
     431            version, status, reason = response._read_status() 
     432            self.assertEqual(status, 100) 
     433            while True: 
     434                skip = response.fp.readline().strip() 
     435                if not skip: 
     436                    break 
     437             
     438            # ...send the body 
     439            conn.send("I am a small file") 
     440             
     441            # ...get the final response 
     442            response.begin() 
     443            self.status, self.headers, self.body = webtest.shb(response) 
     444            self.assertStatus(200) 
     445            self.assertBody("thanks for 'I am a small file' (text/plain)") 
     446            conn.close() 
    436447     
    437448    def test_No_Message_Body(self): 
     
    444455        # Set our HTTP_CONN to an instance so it persists between requests. 
    445456        self.persistent = True 
    446         conn = self.HTTP_CONN 
    447457         
    448458        # Make the first request and assert there's no "Connection: close". 
     
    516526        self.assertStatus(413) 
    517527        self.assertBody("") 
     528        conn.close() 
    518529     
    519530    def test_HTTP10(self): 
     
    545556        # Apache, for example, may emit a Connection header even for HTTP/1.0 
    546557##        self.assertNoHeader("Connection") 
    547  
     558     
    548559    def test_598(self): 
    549560        remote_data_conn = urllib.urlopen('%s://%s:%s/one_megabyte_of_a/' % 
     
    557568            remaining -= len(received_data) 
    558569        
    559         self.assertTrue(received_data)  
    560         self.assertEqual(remaining, 0)  
     570        self.assertTrue(received_data) 
     571        self.assertEqual(remaining, 0) 
     572        remote_data_conn.close() 
     573 
    561574 
    562575if __name__ == "__main__": 
  • trunk/cherrypy/wsgiserver/__init__.py

    r1961 r1968  
    529529        else: 
    530530            cl = int(self.environ.get("CONTENT_LENGTH", 0)) 
    531             self.rfile.maxlen = min(cl, self.max_request_body_size) 
     531            if self.max_request_body_size: 
     532                self.rfile.maxlen = min(cl, self.max_request_body_size) 
     533            else: 
     534                self.rfile.maxlen = cl 
    532535        self.rfile.bytes_read = 0 
    533536         

Hosted by WebFaction

Log in as guest/cpguest to create tickets