Handle URL reconstruction in PEP333 compatible fashion

The Pecan url in wsgi forward scenarios is not correct, as
it does not handle the extra headers injected by the forwarding
handler. Improve this to properly detect the https case in
case the loadbalancer terminates SSL as well and always return
the original's request HOST for consistency.

Change-Id: I3a208abbc6134d1c7be245d35eb4564ef886bd9b
This commit is contained in:
Dirk Mueller 2018-01-29 12:38:34 +01:00
parent bb9da32f0c
commit 4a6f3e9a48
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