def base_url(path=''): url = CONF['public_endpoint'] if url: substitutions = dict( itertools.chain(CONF.items(), CONF.eventlet_server.items())) url = url % substitutions elif flask.request.environ: url = wsgiref.util.application_uri(flask.request.environ) # remove version from the URL as it may be part of SCRIPT_NAME but # it should not be part of base URL url = re.sub(r'/v(3|(2\.0))/*$', '', url) # now remove the standard port url = utils.remove_standard_port(url) else: # if we don't have enough information to come up with a base URL, # then fall back to localhost. This should never happen in # production environment. url = 'http://localhost:%d' % CONF.eventlet_server.public_port if path: # Cleanup leading /v3 if needed. path = path.rstrip('/').lstrip('/') if path.startswith('v3'): path = path[2:].lstrip('/') url = url.rstrip('/') url = '/'.join([p for p in (url, 'v3', path) if p]) return url