Changeset 1431
- Timestamp:
- 11/16/06 14:59:58
- Files:
-
- trunk/cherrypy/_cpdispatch.py (modified) (1 diff)
- trunk/cherrypy/_cptools.py (modified) (3 diffs)
- trunk/cherrypy/lib/cptools.py (modified) (1 diff)
- trunk/cherrypy/test/test_virtualhost.py (modified) (3 diffs)
- trunk/cherrypy/test/test_xmlrpc.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/_cpdispatch.py
r1417 r1431 309 309 310 310 return handler 311 312 313 def 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 321 def 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 trunk/cherrypy/_cptools.py
r1427 r1431 215 215 216 216 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 allow221 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.request227 228 # Guard against running this method twice.229 if hasattr(request, 'xmlrpc'):230 return231 request.xmlrpc = True232 233 request.error_response = _xmlrpc.on_error234 path_info = request.path_info235 ppath = _xmlrpc.patched_path(path_info)236 if ppath != path_info:237 raise cherrypy.InternalRedirect(ppath)238 239 240 217 class WSGIAppTool(HandlerTool): 241 218 """A tool for running any WSGI middleware/application within CP. … … 333 310 _d.proxy = Tool('before_request_body', cptools.proxy, priority=30) 334 311 _d.response_headers = Tool('on_start_resource', cptools.response_headers) 335 _d.virtual_host = Tool('on_start_resource', cptools.virtual_host, priority=40)336 312 _d.log_tracebacks = Tool('before_error_response', cptools.log_traceback) 337 313 _d.log_headers = Tool('before_error_response', cptools.log_request_headers) … … 346 322 # _sessions.init must be bound after headers are read 347 323 _d.sessions = SessionTool('before_request_body', _sessions.init) 348 _d.xmlrpc = XMLRPCTool()324 _d.xmlrpc = ErrorTool(_xmlrpc.on_error) 349 325 _d.wsgiapp = WSGIAppTool(_wsgiapp.run) 350 326 _d.caching = CachingTool('before_handler', _caching.get, 'caching') trunk/cherrypy/lib/cptools.py
r1427 r1431 272 272 for k in dir(SessionAuth) if not k.startswith("__")]) 273 273 274 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 a283 single website structure as well as to their own "homepage" EG284 285 http://www.mydom1.com -> root286 http://www.mydom2.com -> root/mydom2/287 http://www.mydom3.com -> root/mydom3/288 http://www.mydom4.com -> under construction page289 290 but also to have http://www.mydom1.com/mydom2/ etc to be valid pages in291 their own right.292 """293 request = cherrypy.request294 295 # Guard against running twice.296 if hasattr(request, "virtual_prefix"):297 return298 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 274 307 275 def log_traceback(): trunk/cherrypy/test/test_virtualhost.py
r1428 r1431 4 4 5 5 import cherrypy 6 from cherrypy import _cpdispatch 6 7 7 8 def setup_server(): … … 39 40 root.mydom2 = VHost("Domain 2") 40 41 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 }}) 42 49 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'}) 50 51 51 52 from cherrypy.test import helper … … 77 78 self.assertBody("You sent 'pos'") 78 79 80 # Test that cherrypy.url uses the browser url, not the virtual url 79 81 self.getPage("/url", [('Host', 'www.mydom2.com')]) 80 82 self.assertBody("http://www.mydom2.com/nextpage") trunk/cherrypy/test/test_xmlrpc.py
r1345 r1431 2 2 test.prefer_parent_path() 3 3 import xmlrpclib 4 5 from cherrypy import _cpdispatch 4 6 5 7 … … 58 60 root = Root() 59 61 root.xmlrpc = XmlRpc() 60 cherrypy.tree.mount(root) 62 cherrypy.tree.mount(root, config={'/': { 63 'request.dispatch': _cpdispatch.XMLRPCDispatcher(), 64 }}) 61 65 cherrypy.config.update({'environment': 'test_suite'}) 62 66

