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

Changeset 1549

Show
Ignore:
Timestamp:
12/21/06 14:46:12
Author:
fumanchu
Message:

wsgiserver: comments, gave HTTPRequest a 'wfile' attr, and now ignore only a single leading CRLF.

Files:

Legend:

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

    r1546 r1549  
    108108        self.connection = connection 
    109109        self.rfile = self.connection.rfile 
     110        self.wfile = self.connection.wfile 
    110111        self.environ = connection.environ.copy() 
    111112         
     
    127128        # (although your TCP stack might suffer for it: cf Apache's history 
    128129        # with FIN_WAIT_2). 
    129         while True: 
     130        request_line = self.rfile.readline() 
     131        if not request_line: 
     132            # Force self.ready = False so the connection will close. 
     133            self.ready = False 
     134            return 
     135         
     136        if request_line == "\r\n": 
     137            # RFC 2616 sec 4.1: "...if the server is reading the protocol 
     138            # stream at the beginning of a message and receives a CRLF 
     139            # first, it should ignore the CRLF." 
     140            # But only ignore one leading line! else we enable a DoS. 
    130141            request_line = self.rfile.readline() 
    131142            if not request_line: 
    132                 # Force self.ready = False so the connection will close. 
    133143                self.ready = False 
    134144                return 
    135             elif request_line == "\r\n": 
    136                 # "...if the server is reading the protocol stream at the 
    137                 # beginning of a message and receives a CRLF first, it 
    138                 # should ignore the CRLF." RFC 2616 sec 4.1 
    139                 pass 
    140             else: 
    141                 break 
    142145         
    143146        server = self.connection.server 
     
    333336                return 
    334337         
     338        # Grab any trailer headers 
    335339        headers = mimetools.Message(self.rfile) 
    336340        self.environ.update(self.parse_headers(headers)) 
     341         
    337342        data.seek(0) 
    338343        self.environ["wsgi.input"] = data 
     
    342347    def respond(self): 
    343348        """Call the appropriate WSGI app and write its iterable output.""" 
    344         wfile = self.connection.wfile 
     349        wfile = self.wfile 
    345350        response = self.wsgi_app(self.environ, self.start_response) 
    346351        try: 
     
    361366        """Write a simple response back to the client.""" 
    362367        status = str(status) 
    363         wfile = self.connection.wfile 
     368        wfile = self.wfile 
    364369        wfile.write("%s %s\r\n" % (self.connection.server.protocol, status)) 
    365370        wfile.write("Content-Length: %s\r\n" % len(msg)) 
     
    399404            self.sent_headers = True 
    400405            self.send_headers() 
    401         wfile = self.connection.wfile 
     406        wfile = self.wfile 
    402407        if self.chunked_write: 
    403408            wfile.write(hex(len(chunk))[2:]) 
     
    437442         
    438443        server = self.connection.server 
    439         wfile = self.connection.wfile 
     444        wfile = self.wfile 
    440445         
    441446        if "server" not in hkeys: 
     
    465470                return method(self, *args, **kwargs) 
    466471            except (SSL.WantReadError, SSL.WantWriteError): 
    467                 # Sleep and try again 
     472                # Sleep and try again. This is dangerous, because it means 
     473                # the rest of the stack has no way of differentiating 
     474                # between a "new handshake" error and "client dropped". 
     475                # Note this isn't an endless loop: there's a timeout below. 
    468476                time.sleep(self.ssl_retry) 
    469477            except SSL.SysCallError, e: 

Hosted by WebFaction

Log in as guest/cpguest to create tickets