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

Ticket #600: dispwrappers.patch

  • _cpdispatch.py

    old new  
    308308                merge(app.config[curpath]) 
    309309         
    310310        return handler 
     311 
     312 
     313def XMLRPCDispatcher(next_dispatcher=Dispatcher()): 
     314    from cherrypy.lib import xmlrpc 
     315    def xmlrpc_dispatch(path_info): 
     316        path_info = xmlrpc.patched_path(path_info) 
     317        return next_dispatcher(path_info) 
     318    return xmlrpc_dispatch 
     319 
     320 
     321def VirtualHost(next_dispatcher=Dispatcher(), use_x_forwarded_host=True, **domains): 
     322    """Select a different handler based on the Host header. 
     323     
     324    Useful when running multiple sites within one CP server. 
     325     
     326    From http://groups.google.com/group/cherrypy-users/browse_thread/thread/f393540fe278e54d: 
     327     
     328    For various reasons I need several domains to point to different parts of a 
     329    single website structure as well as to their own "homepage"   EG 
     330     
     331    http://www.mydom1.com  ->  root 
     332    http://www.mydom2.com  ->  root/mydom2/ 
     333    http://www.mydom3.com  ->  root/mydom3/ 
     334    http://www.mydom4.com  ->  under construction page 
     335     
     336    but also to have  http://www.mydom1.com/mydom2/  etc to be valid pages in 
     337    their own right. 
     338    """ 
     339    from cherrypy.lib import http 
     340    def vhost_dispatch(path_info): 
     341        header = cherrypy.request.headers.get 
     342         
     343        domain = header('Host', '') 
     344        if use_x_forwarded_host: 
     345            domain = header("X-Forwarded-Host", domain) 
     346         
     347        prefix = domains.get(domain, "") 
     348        if prefix: 
     349            path_info = http.urljoin(prefix, path_info) 
     350         
     351        return next_dispatcher(path_info) 
     352    return vhost_dispatch 
     353 
  • _cptools.py

    old new  
    214214    index = __call__ 
    215215 
    216216 
    217 class XMLRPCTool(object): 
    218     """Tool for using XMLRPC over HTTP. 
    219      
    220     Python's None value cannot be used in standard XML-RPC; to allow 
    221     using it via an extension, provide a true value for allow_none. 
    222     """ 
    223      
    224     def _setup(self): 
    225         """Hook this tool into cherrypy.request.""" 
    226         request = cherrypy.request 
    227          
    228         # Guard against running this method twice. 
    229         if hasattr(request, 'xmlrpc'): 
    230             return 
    231         request.xmlrpc = True 
    232          
    233         request.error_response = _xmlrpc.on_error 
    234         path_info = request.path_info 
    235         ppath = _xmlrpc.patched_path(path_info) 
    236         if ppath != path_info: 
    237             raise cherrypy.InternalRedirect(ppath) 
    238  
    239  
    240217class WSGIAppTool(HandlerTool): 
    241218    """A tool for running any WSGI middleware/application within CP. 
    242219     
     
    332309_d.session_auth = SessionAuthTool(cptools.session_auth) 
    333310_d.proxy = Tool('before_request_body', cptools.proxy, priority=30) 
    334311_d.response_headers = Tool('on_start_resource', cptools.response_headers) 
    335 _d.virtual_host = Tool('on_start_resource', cptools.virtual_host, priority=40) 
    336312_d.log_tracebacks = Tool('before_error_response', cptools.log_traceback) 
    337313_d.log_headers = Tool('before_error_response', cptools.log_request_headers) 
    338314_d.err_redirect = ErrorTool(cptools.redirect) 
     
    345321_d.staticfile = HandlerTool(static.staticfile) 
    346322# _sessions.init must be bound after headers are read 
    347323_d.sessions = SessionTool('before_request_body', _sessions.init) 
    348 _d.xmlrpc = XMLRPCTool(
     324_d.xmlrpc = ErrorTool(_xmlrpc.on_error
    349325_d.wsgiapp = WSGIAppTool(_wsgiapp.run) 
    350326_d.caching = CachingTool('before_handler', _caching.get, 'caching') 
    351327_d.expires = Tool('before_finalize', _caching.expires) 
  • lib/cptools.py

    old new  
    272272                 for k in dir(SessionAuth) if not k.startswith("__")]) 
    273273 
    274274 
    275 def virtual_host(use_x_forwarded_host=True, **domains): 
    276     """Redirect internally based on the Host header. 
    277      
    278     Useful when running multiple sites within one CP server. 
    279      
    280     From http://groups.google.com/group/cherrypy-users/browse_thread/thread/f393540fe278e54d: 
    281      
    282     For various reasons I need several domains to point to different parts of a 
    283     single website structure as well as to their own "homepage"   EG 
    284      
    285     http://www.mydom1.com  ->  root 
    286     http://www.mydom2.com  ->  root/mydom2/ 
    287     http://www.mydom3.com  ->  root/mydom3/ 
    288     http://www.mydom4.com  ->  under construction page 
    289      
    290     but also to have  http://www.mydom1.com/mydom2/  etc to be valid pages in 
    291     their own right. 
    292     """ 
    293     request = cherrypy.request 
    294      
    295     # Guard against running twice. 
    296     if hasattr(request, "virtual_prefix"): 
    297         return 
    298      
    299     domain = request.headers.get('Host', '') 
    300     if use_x_forwarded_host: 
    301         domain = request.headers.get("X-Forwarded-Host", domain) 
    302      
    303     request.virtual_prefix = prefix = domains.get(domain, "") 
    304     if prefix: 
    305         raise cherrypy.InternalRedirect(_http.urljoin(prefix, request.path_info)) 
    306  
    307275def log_traceback(): 
    308276    """Write the last error's traceback to the cherrypy error log.""" 
    309277    from cherrypy import _cperror 
  • test/test_virtualhost.py

    old new  
    33test.prefer_parent_path() 
    44 
    55import cherrypy 
     6from cherrypy import _cpdispatch 
    67 
    78def setup_server(): 
    89    class Root: 
     
    3839    root = Root() 
    3940    root.mydom2 = VHost("Domain 2") 
    4041    root.mydom3 = VHost("Domain 3") 
    41     cherrypy.tree.mount(root) 
     42    cherrypy.tree.mount(root, config={'/': { 
     43        'request.dispatch': _cpdispatch.VirtualHost( 
     44            **{'www.mydom2.com': '/mydom2', 
     45               'www.mydom3.com': '/mydom3', 
     46               'www.mydom4.com': '/dom4', 
     47               }), 
     48        }}) 
    4249     
    43     cherrypy.config.update({ 
    44         'environment': 'test_suite', 
    45         'tools.virtual_host.on': True, 
    46         'tools.virtual_host.www.mydom2.com': '/mydom2', 
    47         'tools.virtual_host.www.mydom3.com': '/mydom3', 
    48         'tools.virtual_host.www.mydom4.com': '/dom4', 
    49         }) 
     50    cherrypy.config.update({'environment': 'test_suite'}) 
    5051 
    5152from cherrypy.test import helper 
    5253 
     
    7677        self.getPage("/vmethod/pos", [('Host', 'www.mydom3.com')]) 
    7778        self.assertBody("You sent 'pos'") 
    7879         
     80        # Test that cherrypy.url uses the browser url, not the virtual url 
    7981        self.getPage("/url", [('Host', 'www.mydom2.com')]) 
    8082        self.assertBody("http://www.mydom2.com/nextpage") 
    8183 
  • test/test_xmlrpc.py

    old new  
    22test.prefer_parent_path() 
    33import xmlrpclib 
    44 
     5from cherrypy import _cpdispatch 
    56 
     7 
    68def setup_server(): 
    79    import cherrypy 
    810    from cherrypy import _cptools 
     
    5759 
    5860    root = Root() 
    5961    root.xmlrpc = XmlRpc() 
    60     cherrypy.tree.mount(root) 
     62    cherrypy.tree.mount(root, config={'/': { 
     63        'request.dispatch': _cpdispatch.XMLRPCDispatcher(), 
     64        }}) 
    6165    cherrypy.config.update({'environment': 'test_suite'}) 
    6266 
    6367 

Hosted by WebFaction

Log in as guest/cpguest to create tickets