def _parse_url(url): """ Returns scheme, host, port, path, query. """ scheme, netloc, path, _params, query, _frag = rhnLib.parseUrl(url) host, port = urllib.splitnport(netloc) if (port <= 0): port = None return scheme, host, port, path, query
def split_git_url(url): """Split a Git URL. :param url: Git URL :return: Tuple with host, port, username, path. """ (scheme, netloc, loc, _, _) = urlparse.urlsplit(url) path = urlparse.unquote(loc) if path.startswith("/~"): path = path[1:] (username, hostport) = splituser(netloc) (host, port) = splitnport(hostport, None) return (host, port, username, path)
def parse_host(addr, is_win32=False, tls=False): path = '' port = None host = None # Sensible defaults if not addr and is_win32: return DEFAULT_NPIPE if not addr or addr.strip() == 'unix://': return DEFAULT_UNIX_SOCKET addr = addr.strip() parsed_url = urlparse(addr) proto = parsed_url.scheme if not proto or any([x not in string.ascii_letters + '+' for x in proto]): # https://bugs.python.org/issue754016 parsed_url = urlparse('//' + addr, 'tcp') proto = 'tcp' if proto == 'fd': raise errors.DockerException('fd protocol is not implemented') # These protos are valid aliases for our library but not for the # official spec if proto == 'http' or proto == 'https': tls = proto == 'https' proto = 'tcp' elif proto == 'http+unix': proto = 'unix' if proto not in ('tcp', 'unix', 'npipe', 'ssh'): raise errors.DockerException( "Invalid bind address protocol: {}".format(addr)) if proto == 'tcp' and not parsed_url.netloc: # "tcp://" is exceptionally disallowed by convention; # omitting a hostname for other protocols is fine raise errors.DockerException( 'Invalid bind address format: {}'.format(addr)) if any([ parsed_url.params, parsed_url.query, parsed_url.fragment, parsed_url.password ]): raise errors.DockerException( 'Invalid bind address format: {}'.format(addr)) if parsed_url.path and proto == 'ssh': raise errors.DockerException( 'Invalid bind address format: no path allowed for this protocol:' ' {}'.format(addr)) else: path = parsed_url.path if proto == 'unix' and parsed_url.hostname is not None: # For legacy reasons, we consider unix://path # to be valid and equivalent to unix:///path path = '/'.join((parsed_url.hostname, path)) if proto in ('tcp', 'ssh'): # parsed_url.hostname strips brackets from IPv6 addresses, # which can be problematic hence our use of splitnport() instead. host, port = splitnport(parsed_url.netloc) if port is None or port < 0: if proto != 'ssh': raise errors.DockerException( 'Invalid bind address format: port is required:' ' {}'.format(addr)) port = 22 if not host: host = DEFAULT_HTTP_HOST # Rewrite schemes to fit library internals (requests adapters) if proto == 'tcp': proto = 'http{}'.format('s' if tls else '') elif proto == 'unix': proto = 'http+unix' if proto in ('http+unix', 'npipe'): return "{}://{}".format(proto, path).rstrip('/') return '{0}://{1}:{2}{3}'.format(proto, host, port, path).rstrip('/')