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

Changeset 1670

Show
Ignore:
Timestamp:
06/16/07 19:35:31
Author:
fumanchu
Message:

Fix for #684 (Make it possible to mount virtual host apps separately somewhere below '/'): new VirtualHost? WSGI middleware in _cpwsgi.py.

Files:

Legend:

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

    r1660 r1670  
    77from cherrypy import _cperror, wsgiserver 
    88from cherrypy.lib import http as _http 
     9 
     10 
     11class VirtualHost(object): 
     12    """Select a different WSGI application based on the Host header. 
     13     
     14    This can be useful when running multiple sites within one CP server. 
     15    It allows several domains to point to different applications. For example: 
     16     
     17        root = Root() 
     18        RootApp = cherrypy.Application(root) 
     19        Domain2App = cherrypy.Application(root) 
     20        SecureApp = cherrypy.Application(Secure()) 
     21         
     22        vhost = cherrypy._cpwsgi.VirtualHost(RootApp, 
     23            domains={'www.domain2.example': Domain2App, 
     24                     'www.domain2.example:443': SecureApp, 
     25                     }) 
     26         
     27        cherrypy.tree.graft(vhost) 
     28     
     29    default: required. The default WSGI application. 
     30     
     31    use_x_forwarded_host: if True (the default), any "X-Forwarded-Host" 
     32        request header will be used instead of the "Host" header. This 
     33        is commonly added by HTTP servers (such as Apache) when proxying. 
     34     
     35    domains: a dict of {host header value: application} pairs. 
     36        The incoming "Host" request header is looked up in this dict, 
     37        and, if a match is found, the corresponding WSGI application 
     38        will be called instead of the default. Note that you often need 
     39        separate entries for "example.com" and "www.example.com". 
     40        In addition, "Host" headers may contain the port number. 
     41    """ 
     42     
     43    def __init__(self, default, domains=None, use_x_forwarded_host=True): 
     44        self.default = default 
     45        self.domains = domains or {} 
     46        self.use_x_forwarded_host = use_x_forwarded_host 
     47     
     48    def __call__(self, environ, start_response): 
     49        domain = environ.get('HTTP_HOST', '') 
     50        if self.use_x_forwarded_host: 
     51            domain = environ.get("HTTP_X_FORWARDED_HOST", domain) 
     52         
     53        nextapp = self.domains.get(domain) 
     54        if nextapp is None: 
     55            nextapp = self.default 
     56        return nextapp(environ, start_response) 
    957 
    1058 

Hosted by WebFaction

Log in as guest/cpguest to create tickets