[r7407]: branches / 1.5 / turbogears / dispatchers.py  Maximize  Restore  History

Download this file

50 lines (38 with data), 1.7 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Standard TurboGears request dispatchers for CherryPy."""
__all__ = ['VirtualPathDispatcher']
from cherrypy import NotFound, dispatch, request
class VirtualPathDispatcher(object):
"""Dispatcher that makes CherryPy ignorant of a URL root path.
That is, you can mount your app so the URI "/users/~rdel/myapp/" maps to
the root object "/".
Note that this can not be done by a hook since they are run too late.
"""
def __init__(self, next_dispatcher=None, webpath=''):
self.next_dispatcher = next_dispatcher or dispatch.Dispatcher()
webpath = webpath.strip('/')
if webpath:
webpath = '/' + webpath
self.webpath = webpath
def __call__(self, path_info):
"""Determine the relevant path info by stripping off prefixes.
Strips webpath and request.script_name from request.path_info.
"""
webpath = self.webpath
try:
webpath += request.script_name.rstrip('/')
except AttributeError:
pass
if webpath:
try:
if path_info.startswith(webpath + '/'):
request.path_info = path_info = path_info[len(webpath):]
else:
if not request.prev and not request.wsgi_environ[
'HTTP_X_FORWARDED_SERVER']:
# check for webpath only if not forwarded
raise KeyError
except (AttributeError, KeyError):
request.handler = NotFound()
else:
request.script_name = webpath
return self.next_dispatcher(path_info)