def set_url_info(args): info = UrlInfo() _result = urlsplit(args._url) for attr in vars(info).keys(): value = getattr(_result, attr, None) if value: setattr(info, attr, value) if info.scheme == 'http' and not _result.port: info.port = 80 # Set the secure arg is the scheme is HTTPS, otherwise do unsecured. info.secure = info.scheme == 'https' if info.netloc: hostname, _ = split_host_and_port(info.netloc) info.host = hostname # ensure stripping port number else: if _result.path: _path = _result.path.split('/', 1) hostname, port = split_host_and_port(_path[0]) info.host = hostname if info.path == _path[0]: info.path = '/' elif len(_path) == 2 and _path[1]: info.path = '/' + _path[1] if port is not None: info.port = port log.debug('Url Info: %s', vars(info)) args.url = info
def set_url_info(args): def split_host_and_port(hostname): if ':' in hostname: host, port = hostname.split(':') return host, int(port) return hostname, None class UrlInfo(object): def __init__(self): self.fragment = None self.host = 'localhost' self.netloc = None self.path = '/' self.port = 443 self.query = None self.scheme = 'https' self.secure = False info = UrlInfo() _result = urlsplit(args._url) for attr in vars(info).keys(): value = getattr(_result, attr, None) if value: setattr(info, attr, value) if info.scheme == 'http' and not _result.port: info.port = 80 # Set the secure arg is the scheme is HTTPS, otherwise do unsecured. info.secure = info.scheme == 'https' if info.netloc: hostname, _ = split_host_and_port(info.netloc) info.host = hostname # ensure stripping port number else: if _result.path: _path = _result.path.split('/', 1) hostname, port = split_host_and_port(_path[0]) info.host = hostname if info.path == _path[0]: info.path = '/' elif len(_path) == 2 and _path[1]: info.path = '/' + _path[1] if port is not None: info.port = port log.debug('Url Info: %s', vars(info)) args.url = info