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

Changeset 965

Show
Ignore:
Timestamp:
02/13/06 13:50:00
Author:
fumanchu
Message:

Implemented #461 (CPWSGIServer should route multiple apps). This allows you to instantiate a WSGIServer with multiple apps; however, _cpserver still needs to be upgraded to "do that for you". I'll do that in a separate commit.

Files:

Legend:

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

    r959 r965  
    165165    _cpwsgiserver has been designed to not reference CherryPy in any way, 
    166166    so that it can be used in other frameworks and applications. Therefore, 
    167     we wrap it here
     167    we wrap it here, so we can set our own mount points from cherrypy.tree
    168168     
    169169    """ 
     
    171171    RequestHandlerClass = CPHTTPRequest 
    172172     
    173     def __init__(self, app=wsgiApp): 
     173    def __init__(self): 
    174174        conf = cherrypy.config.get 
    175175         
     
    180180            bind_addr = (conf("server.socket_host"), conf("server.socket_port")) 
    181181         
     182        pts = cherrypy.tree.mount_points 
     183        if pts: 
     184            apps = [(base, wsgiApp) for base in pts.keys()] 
     185        else: 
     186            apps = [("", wsgiApp)] 
     187         
    182188        s = _cpwsgiserver.CherryPyWSGIServer 
    183         s.__init__(self, bind_addr, app
     189        s.__init__(self, bind_addr, apps
    184190                   conf("server.thread_pool"), 
    185191                   conf("server.socket_host"), 
  • trunk/cherrypy/_cpwsgiserver.py

    r959 r965  
    6565            qs = "" 
    6666        self.environ["REQUEST_METHOD"] = method 
    67         self.environ["SCRIPT_NAME"] = "" 
    68         self.environ["PATH_INFO"] = path 
     67         
     68        for mount_point, wsgi_app in self.server.mount_points: 
     69            # The mount_points list should be sorted by length, descending. 
     70            if path.startswith(mount_point): 
     71                self.environ["SCRIPT_NAME"] = mount_point 
     72                self.environ["PATH_INFO"] = path[len(mount_point):] 
     73                self.wsgi_app = wsgi_app 
     74                break 
     75        else: 
     76            msg = "Not Found" 
     77            proto = self.environ.get("SERVER_PROTOCOL", "HTTP/1.0") 
     78            self.wfile.write("%s 404 %s\r\n" % (proto, msg)) 
     79            self.wfile.write("Content-Length: %s\r\n\r\n" % len(msg)) 
     80            self.wfile.write(msg) 
     81            self.wfile.flush() 
     82            self.ready = False 
     83            return 
     84         
    6985        self.environ["QUERY_STRING"] = qs 
    7086        self.environ["SERVER_PROTOCOL"] = version 
     
    161177                        request.parse_request() 
    162178                        if request.ready: 
    163                             response = self.server.wsgi_app(request.environ, 
    164                                                             request.start_response) 
     179                            response = request.wsgi_app(request.environ, 
     180                                                        request.start_response) 
    165181                            for line in response: 
    166182                                request.write(line) 
     
    192208        ''' 
    193209        self.requests = Queue.Queue(max) 
    194         self.wsgi_app = wsgi_app 
     210         
     211        if callable(wsgi_app): 
     212            # We've been handed a single wsgi_app, in CP-2.1 style. 
     213            # Assume it's mounted at "". 
     214            self.mount_points = [("", wsgi_app)] 
     215        else: 
     216            # We've been handed a list of (mount_point, wsgi_app) tuples, 
     217            # so that the server can call different wsgi_apps, and also 
     218            # correctly set SCRIPT_NAME. 
     219            self.mount_points = wsgi_app 
     220        self.mount_points.sort() 
     221        self.mount_points.reverse() 
     222         
    195223        self.bind_addr = bind_addr 
    196224        self.numthreads = numthreads or 1 
     
    287315        """Gracefully shutdown a server that is serving forever.""" 
    288316        self.ready = False 
    289         self.socket.close() 
     317        s = getattr(self, "socket", None) 
     318        if s and hasattr(s, "close"): 
     319            s.close() 
    290320         
    291321        # Must shut down threads here so the code that calls 
  • trunk/cherrypy/test/helper.py

    r943 r965  
    6868     
    6969    def __init__(self): 
    70         _cpwsgi.WSGIServer.__init__(self, error_middleware) 
     70        _cpwsgi.WSGIServer.__init__(self) 
     71        self.mount_points = [(base, error_middleware) 
     72                             for base, wsgiapp in self.mount_points] 
    7173 
    7274 

Hosted by WebFaction

Log in as guest/cpguest to create tickets