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

Changeset 1406

Show
Ignore:
Timestamp:
10/21/06 22:36:38
Author:
fumanchu
Message:

Fix for #588 (CherryPyWSGIServer sends Connection: close during 204 and 304 responses).

Files:

Legend:

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

    r1400 r1406  
    607607        if self.stream: 
    608608            headers.pop('Content-Length', None) 
     609        elif code < 200 or code in (204, 304): 
     610            # "All 1xx (informational), 204 (no content), 
     611            # and 304 (not modified) responses MUST NOT 
     612            # include a message-body." 
     613            headers.pop('Content-Length', None) 
     614            self.body = "" 
    609615        else: 
    610616            # Responses which are not streamed should have a Content-Length, 
    611617            # but allow user code to set Content-Length if desired. 
    612             if (dict.get(headers, 'Content-Length') is None 
    613                     and code not in (304,)): 
     618            if dict.get(headers, 'Content-Length') is None: 
    614619                content = self.collapse_body() 
    615620                dict.__setitem__(headers, 'Content-Length', len(content)) 
  • trunk/cherrypy/_cpwsgiserver.py

    r1388 r1406  
    320320        hkeys = [key.lower() for (key,value) in self.outheaders] 
    321321         
     322        status = int(self.status[:3]) 
    322323        if (self.response_protocol == 'HTTP/1.1' 
    323324            and (# Request Entity Too Large. Close conn to avoid garbage. 
    324                 self.status[:3] == "413" 
     325                status == 413 
    325326                # No Content-Length. Close conn to determine transfer-length. 
    326                 or "content-length" not in hkeys)): 
     327                or ("content-length" not in hkeys and 
     328                    # "All 1xx (informational), 204 (no content), 
     329                    # and 304 (not modified) responses MUST NOT 
     330                    # include a message-body." 
     331                    status >= 200 and status not in (204, 304)))): 
    327332            if "connection" not in hkeys: 
    328333                self.outheaders.append(("Connection", "close")) 
  • trunk/cherrypy/test/test_conn.py

    r1367 r1406  
    3838                     cherrypy.request.headers['Content-Type'])) 
    3939        upload.exposed = True 
     40         
     41        def custom(self, response_code): 
     42            cherrypy.response.status = response_code 
     43            return "Code = %s" % response_code 
     44        custom.exposed = True 
    4045     
    4146    cherrypy.tree.mount(Root()) 
     
    292297        self.assertBody("thanks for 'I am a small file' (text/plain)") 
    293298     
     299    def test_No_Message_Body(self): 
     300        if cherrypy.server.protocol_version != "HTTP/1.1": 
     301            print "skipped ", 
     302            return 
     303         
     304        self.PROTOCOL = "HTTP/1.1" 
     305         
     306        # Set our HTTP_CONN to an instance so it persists between requests. 
     307        if self.scheme == "https": 
     308            self.HTTP_CONN = httplib.HTTPSConnection(self.HOST, self.PORT) 
     309        else: 
     310            self.HTTP_CONN = httplib.HTTPConnection(self.HOST, self.PORT) 
     311        # Don't automatically re-connect 
     312        self.HTTP_CONN.auto_open = False 
     313        self.HTTP_CONN.connect() 
     314         
     315        # Make the first request and assert there's no "Connection: close". 
     316        self.getPage("/") 
     317        self.assertStatus('200 OK') 
     318        self.assertBody(pov) 
     319        self.assertNoHeader("Connection") 
     320         
     321        # Make a 204 request on the same connection. 
     322        self.getPage("/custom/204") 
     323        self.assertStatus(204) 
     324        self.assertNoHeader("Content-Length") 
     325        self.assertBody("") 
     326        self.assertNoHeader("Connection") 
     327         
     328        # Make a 304 request on the same connection. 
     329        self.getPage("/custom/304") 
     330        self.assertStatus(304) 
     331        self.assertNoHeader("Content-Length") 
     332        self.assertBody("") 
     333        self.assertNoHeader("Connection") 
     334     
    294335    def test_Chunked_Encoding(self): 
    295336        if cherrypy.server.protocol_version != "HTTP/1.1": 

Hosted by WebFaction

Log in as guest/cpguest to create tickets