Merge "Handle URL reconstruction in PEP333 compatible fashion"

This commit is contained in:
Zuul 2018-03-23 00:34:36 +00:00 committed by Gerrit Code Review
commit 3e1c54878d
2 changed files with 10 additions and 2 deletions

View File

@ -78,10 +78,17 @@ def get_base_url_from_request():
"""
if not CONF.host_href and hasattr(pecan.request, 'url'):
p_url = parse.urlsplit(pecan.request.url)
# Pecan does not handle X_FORWARDED_PROTO yet, so we need to
# handle it ourselves. see lp#1445290
scheme = pecan.request.environ.get('HTTP_X_FORWARDED_PROTO', 'http')
# Pecan does not handle url reconstruction according to
# https://www.python.org/dev/peps/pep-0333/#url-reconstruction
netloc = pecan.request.environ.get('HTTP_HOST', p_url.netloc)
# FIXME: implement SERVER_NAME lookup if HTTP_HOST is not set
if p_url.path:
base_url = '%s://%s%s' % (p_url.scheme, p_url.netloc, p_url.path)
base_url = '%s://%s%s' % (scheme, netloc, p_url.path)
else:
base_url = '%s://%s' % (p_url.scheme, p_url.netloc)
base_url = '%s://%s' % (scheme, netloc)
return base_url
else: # when host_href is set or flow is not within wsgi request context
return CONF.host_href

View File

@ -49,6 +49,7 @@ def mock_pecan_request(test_instance, host=None):
mock_req = patcher_obj.start()
test_instance.addCleanup(patcher_obj.stop)
mock_req.url = host
mock_req.environ = os.environ.copy()
@contextmanager