def munge(environ, host_header=None, root_header=None): """Update the environment based on a host header and/or a VHM root. """ vroot_path = [] vhosting = False if host_header is not None: vhosting = True (scheme, netloc, path, query, fragment) = urlsplit(host_header) if ':' in netloc: host, port = netloc.split(':') else: host = netloc port = DEFAULT_PORTS[scheme] environ['wsgi.url_scheme'] = scheme environ['SERVER_NAME'] = host environ['HTTP_HOST'] = "%s:%s" % ( host, port, ) environ['SERVER_PORT'] = port environ['SCRIPT_NAME'] = path environ['repoze.vhm.virtual_host_base'] = '%s:%s' % (host, port) if root_header is not None: vhosting = True environ['repoze.vhm.virtual_root'] = root_header vroot_path = root_header.split('/') if vhosting: server_url = getServerURL(environ) virtual_url_parts = [server_url] script_name = environ['SCRIPT_NAME'] if script_name and script_name != '/': script_name_path = script_name.split('/') if len(script_name_path) > 1: virtual_url_parts += script_name_path[1:] real_path = environ['PATH_INFO'].split('/') if vroot_path: virtual_url_parts += real_path[len(vroot_path):] else: virtual_url_parts += real_path[1:] if virtual_url_parts[-1] == '': virtual_url_parts.pop() # Store the virtual URL environ['repoze.vhm.virtual_url'] = '/'.join(virtual_url_parts)
def munge(environ, host_header=None, root_header=None): """Update the environment based on a host header and/or a VHM root. """ vroot_path = [] vhosting = False if host_header is not None: vhosting = True (scheme, netloc, path, query, fragment) = urlsplit(host_header) if ':' in netloc: host, port = netloc.split(':') else: host = netloc port = DEFAULT_PORTS[scheme] environ['wsgi.url_scheme'] = scheme environ['SERVER_NAME'] = host environ['HTTP_HOST'] = "%s:%s" % (host, port,) environ['SERVER_PORT'] = port environ['SCRIPT_NAME'] = path environ['repoze.vhm.virtual_host_base'] = '%s:%s' % (host, port) if root_header is not None: vhosting = True environ['repoze.vhm.virtual_root'] = root_header vroot_path = root_header.split('/') if vhosting: server_url = getServerURL(environ) virtual_url_parts = [server_url] script_name = environ['SCRIPT_NAME'] if script_name and script_name != '/': script_name_path = script_name.split('/') if len(script_name_path) > 1: virtual_url_parts += script_name_path[1:] real_path = environ['PATH_INFO'].split('/') if vroot_path: virtual_url_parts += real_path[len(vroot_path):] else: virtual_url_parts += real_path[1:] if virtual_url_parts[-1] == '': virtual_url_parts.pop() # Store the virtual URL environ['repoze.vhm.virtual_url'] = '/'.join(virtual_url_parts)
def __call__(self, environ, start_response): scheme = 'HTTPS' in environ and 'https' or 'http' path = environ['PATH_INFO'] vroot_path = [] real_path = [] script_name = '' script_name_path = [] checking_vh_names = False vhosting = False elements = path.split('/') while elements: token = elements.pop(0) if token == 'VirtualHostBase': vhosting = True scheme = elements.pop(0) environ['wsgi.url_scheme'] = scheme host = elements.pop(0) if ':' in host: host, port = host.split(':') else: port = DEFAULT_PORTS[scheme] environ['SERVER_NAME'] = host if port == DEFAULT_PORTS[scheme]: environ['HTTP_HOST'] = host else: environ['HTTP_HOST'] = '%s:%s' % (host, port) environ['SERVER_PORT'] = port environ['repoze.vhm.virtual_host_base'] = '%s:%s' \ % (host, port) elif token == 'VirtualHostRoot': vroot_path = real_path[:] # prefix of vroot if vroot_path and vroot_path != ['']: environ['repoze.vhm.virtual_root'] = '/'.join(vroot_path) else: environ['repoze.vhm.virtual_root'] = '/' checking_vh_names = True elif checking_vh_names: if token.startswith('_vh_'): # capture external subsite script_name_path.append(token[len('_vh_'):]) else: checking_vh_names = False if script_name_path: script_name_path.insert(0, '') script_name = '/'.join(script_name_path) environ['SCRIPT_NAME'] = script_name real_path.append(token) else: real_path.append(token) if not self.conserve_path_infos: environ['PATH_INFO'] = '/'.join(real_path) if vhosting: server_url = getServerURL(environ) virtual_url_parts = [server_url] if script_name_path: virtual_url_parts += script_name_path[1:] if vroot_path: virtual_url_parts += real_path[len(vroot_path):] else: virtual_url_parts += real_path[1:] if virtual_url_parts[-1] == '': virtual_url_parts.pop() # Store the virtual URL. Zope computes ACTUAL_URL from this, # for example. environ['repoze.vhm.virtual_url'] = '/'.join(virtual_url_parts) return self.application(environ, start_response)