From 5e939a9a86f42318f1e92419cec23f49ec5b76f6 Mon Sep 17 00:00:00 2001 From: Chris McDonough Date: Sun, 21 Jun 2009 19:54:16 +0000 Subject: - Make Routes mapper responsible for doing magic to fix up PATH_INFO and SCRIPT_NAME when a ``path_info`` key exists in the matchdict. This used to be done in the traverser, which made no sense. --- repoze/bfg/urldispatch.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'repoze/bfg/urldispatch.py') diff --git a/repoze/bfg/urldispatch.py b/repoze/bfg/urldispatch.py index bfeae3333..dcd4af208 100644 --- a/repoze/bfg/urldispatch.py +++ b/repoze/bfg/urldispatch.py @@ -1,3 +1,5 @@ +import re + from routes import Mapper from routes import request_config @@ -45,6 +47,21 @@ class RoutesRootFactory(Mapper): environ['wsgiorg.routing_args'] = ((), args) environ['bfg.routes.route'] = route environ['bfg.routes.matchdict'] = args + # this is stolen from routes.middleware; if the route map + # has a *path_info capture, use it to influence the path + # info and script_name of the generated environment + if 'path_info' in args: + if not 'SCRIPT_NAME' in environ: + environ['SCRIPT_NAME'] = '' + oldpath = environ['PATH_INFO'] + newpath = args['path_info'] or '' + environ['PATH_INFO'] = newpath + if not environ['PATH_INFO'].startswith('/'): + environ['PATH_INFO'] = '/' + environ['PATH_INFO'] + pattern = r'^(.*?)/' + re.escape(newpath) + '$' + environ['SCRIPT_NAME'] += re.sub(pattern, r'\1', oldpath) + if environ['SCRIPT_NAME'].endswith('/'): + environ['SCRIPT_NAME'] = environ['SCRIPT_NAME'][:-1] factory = route._factory or self.default_root_factory return factory(environ) -- cgit v1.2.3