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

Changeset 1555

Show
Ignore:
Timestamp:
12/22/06 04:01:20
Author:
fumanchu
Message:

wsgiserver optimization: use socket.sendall instead of wfile (since we were flushing regularly anyway).

Files:

Legend:

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

    r1554 r1555  
    104104        self.connection = connection 
    105105        self.rfile = self.connection.rfile 
    106         self.wfile = self.connection.wfile 
     106        self.sendall = self.connection.sendall 
    107107        self.environ = connection.environ.copy() 
    108108         
     
    220220         
    221221        # then all the http headers 
    222         headers = rfc822.Message(self.rfile
     222        headers = rfc822.Message(self.rfile, seekable=0
    223223        self.environ.update(self.parse_headers(headers)) 
    224224         
     
    333333         
    334334        # Grab any trailer headers 
    335         headers = rfc822.Message(self.rfile
     335        headers = rfc822.Message(self.rfile, seekable=0
    336336        self.environ.update(self.parse_headers(headers)) 
    337337         
     
    343343    def respond(self): 
    344344        """Call the appropriate WSGI app and write its iterable output.""" 
    345         wfile = self.wfile 
    346345        response = self.wsgi_app(self.environ, self.start_response) 
    347346        try: 
     
    356355            self.send_headers() 
    357356        if self.chunked_write: 
    358             wfile.write("0\r\n\r\n") 
    359             wfile.flush() 
     357            self.sendall("0\r\n\r\n") 
    360358     
    361359    def simple_response(self, status, msg=""): 
    362360        """Write a simple response back to the client.""" 
    363361        status = str(status) 
    364         wfile = self.wfile 
    365         wfile.write("%s %s\r\n" % (self.connection.server.protocol, status)) 
    366         wfile.write("Content-Length: %s\r\n" % len(msg)) 
     362        buf = ["%s %s\r\n" % (self.connection.server.protocol, status), 
     363               "Content-Length: %s\r\n" % len(msg)] 
    367364         
    368365        if status[:3] == "413" and self.response_protocol == 'HTTP/1.1': 
    369366            # Request Entity Too Large 
    370367            self.close_connection = True 
    371             wfile.write("Connection: close\r\n") 
    372          
    373         wfile.write("\r\n") 
     368            buf.append("Connection: close\r\n") 
     369         
     370        buf.append("\r\n") 
    374371        if msg: 
    375             wfile.write(msg) 
    376         wfile.flush(
     372            buf.append(msg) 
     373        self.sendall("".join(buf)
    377374     
    378375    def start_response(self, status, headers, exc_info = None): 
     
    400397            self.sent_headers = True 
    401398            self.send_headers() 
    402         wfile = self.wfile 
    403399        if self.chunked_write: 
    404             wfile.write(hex(len(chunk))[2:]) 
    405             wfile.write("\r\n") 
    406             wfile.write(chunk) 
    407             wfile.write("\r\n") 
     400            buf = [hex(len(chunk))[2:], 
     401                   "\r\n", chunk, "\r\n"] 
     402            self.sendall("".join(buf)) 
    408403        else: 
    409             wfile.write(chunk) 
    410         wfile.flush() 
     404            self.sendall(chunk) 
    411405     
    412406    def send_headers(self): 
     
    437431         
    438432        server = self.connection.server 
    439         wfile = self.wfile 
    440433         
    441434        if "server" not in hkeys: 
    442435            self.outheaders.append(("Server", server.version)) 
    443436         
    444         wfile.write(server.protocol + " " + self.status + "\r\n") 
     437        buf = [server.protocol, " ", self.status, "\r\n"] 
    445438        try: 
    446439            for k, v in self.outheaders: 
    447                 wfile.write(k + ": " + v + "\r\n") 
     440                buf.append(k + ": " + v + "\r\n") 
    448441        except TypeError: 
    449442            if not isinstance(k, str): 
     
    453446            else: 
    454447                raise 
    455         wfile.write("\r\n") 
    456         wfile.flush(
     448        buf.append("\r\n") 
     449        self.sendall("".join(buf)
    457450 
    458451 
     
    521514    environ: a WSGI environ template. This will be copied for each request. 
    522515    rfile: a fileobject for reading from the socket. 
    523     wfile: a fileobject for writing to the socket. 
     516    sendall: a function for writing (+ flush) to the socket. 
    524517    """ 
    525518     
    526519    rbufsize = -1 
    527     wbufsize = -1 
    528520    RequestHandlerClass = HTTPRequest 
    529521    environ = {"wsgi.version": (1, 0), 
     
    547539            self.rfile = SSL_fileobject(sock, "r", self.rbufsize) 
    548540            self.rfile.ssl_timeout = timeout 
    549             self.wfile = SSL_fileobject(sock, "w", self.wbufsize) 
    550             self.wfile.ssl_timeout = timeout 
     541            self.sendall = _ssl_wrap_method(sock.sendall) 
    551542            self.environ["wsgi.url_scheme"] = "https" 
    552543            self.environ["HTTPS"] = "on" 
     
    555546                self.environ.update(sslenv) 
    556547        else: 
    557             self.rfile = self.socket.makefile("r", self.rbufsize) 
    558             self.wfile = self.socket.makefile("w", self.wbufsize) 
     548            self.rfile = sock.makefile("r", self.rbufsize) 
     549            self.sendall = sock.sendall 
    559550         
    560551        self.environ.update({"wsgi.input": self.rfile, 
     
    605596        """Close the socket underlying this connection.""" 
    606597        self.rfile.close() 
    607         self.wfile.close() 
    608598        self.socket.close() 
    609599 

Hosted by WebFaction

Log in as guest/cpguest to create tickets