diff options
Diffstat (limited to 'qutebrowser/utils/urlutils.py')
-rw-r--r-- | qutebrowser/utils/urlutils.py | 81 |
1 files changed, 40 insertions, 41 deletions
diff --git a/qutebrowser/utils/urlutils.py b/qutebrowser/utils/urlutils.py index 58862925e..c652d29f7 100644 --- a/qutebrowser/utils/urlutils.py +++ b/qutebrowser/utils/urlutils.py @@ -28,8 +28,7 @@ import urllib.parse import mimetypes from typing import Optional, Tuple, Union, Iterable -from qutebrowser.qt.core import QUrl -from qutebrowser.qt.network import QHostInfo, QHostAddress, QNetworkProxy +from qutebrowser.qt import network, core from qutebrowser.api import cmdutils from qutebrowser.config import config @@ -65,7 +64,7 @@ class InvalidUrlError(Error): """Error raised if a function got an invalid URL.""" - def __init__(self, url: QUrl) -> None: + def __init__(self, url: core.QUrl) -> None: if url.isValid(): raise ValueError("Got valid URL {}!".format(url.toDisplayString())) self.url = url @@ -107,7 +106,7 @@ def _parse_search_term(s: str) -> Tuple[Optional[str], Optional[str]]: return (engine, term) -def _get_search_url(txt: str) -> QUrl: +def _get_search_url(txt: str) -> core.QUrl: """Get a search engine URL for a text. Args: @@ -128,9 +127,9 @@ def _get_search_url(txt: str) -> QUrl: unquoted=term, quoted=quoted_term, semiquoted=semiquoted_term) - url = QUrl.fromUserInput(evaluated) + url = core.QUrl.fromUserInput(evaluated) else: - url = QUrl.fromUserInput(config.val.url.searchengines[engine]) + url = core.QUrl.fromUserInput(config.val.url.searchengines[engine]) url.setPath(None) # type: ignore[arg-type] url.setFragment(None) # type: ignore[arg-type] url.setQuery(None) # type: ignore[call-overload] @@ -147,7 +146,7 @@ def _is_url_naive(urlstr: str) -> bool: Return: True if the URL really is a URL, False otherwise. """ - url = QUrl.fromUserInput(urlstr) + url = core.QUrl.fromUserInput(urlstr) assert url.isValid() host = url.host() @@ -172,11 +171,11 @@ def _is_url_dns(urlstr: str) -> bool: Return: True if the URL really is a URL, False otherwise. """ - url = QUrl.fromUserInput(urlstr) + url = core.QUrl.fromUserInput(urlstr) assert url.isValid() if (utils.raises(ValueError, ipaddress.ip_address, urlstr) and - not QHostAddress(urlstr).isNull()): + not network.QHostAddress(urlstr).isNull()): log.url.debug("Bogus IP URL -> False") # Qt treats things like "23.42" or "1337" or "0xDEAD" as valid URLs # which we don't want to. @@ -187,7 +186,7 @@ def _is_url_dns(urlstr: str) -> bool: log.url.debug("URL has no host -> False") return False log.url.debug("Doing DNS request for {}".format(host)) - info = QHostInfo.fromName(host) + info = network.QHostInfo.fromName(host) return not info.error() @@ -195,7 +194,7 @@ def fuzzy_url(urlstr: str, cwd: str = None, relative: bool = False, do_search: bool = True, - force_search: bool = False) -> QUrl: + force_search: bool = False) -> core.QUrl: """Get a QUrl based on a user input which is URL or search term. Args: @@ -214,24 +213,24 @@ def fuzzy_url(urlstr: str, check_exists=True) if not force_search and path is not None: - url = QUrl.fromLocalFile(path) + url = core.QUrl.fromLocalFile(path) elif force_search or (do_search and not is_url(urlstr)): # probably a search term log.url.debug("URL is a fuzzy search term") try: url = _get_search_url(urlstr) except ValueError: # invalid search engine - url = QUrl.fromUserInput(urlstr) + url = core.QUrl.fromUserInput(urlstr) else: # probably an address log.url.debug("URL is a fuzzy address") - url = QUrl.fromUserInput(urlstr) + url = core.QUrl.fromUserInput(urlstr) log.url.debug("Converting fuzzy term {!r} to URL -> {}".format( urlstr, url.toDisplayString())) ensure_valid(url) return url -def _has_explicit_scheme(url: QUrl) -> bool: +def _has_explicit_scheme(url: core.QUrl) -> bool: """Check if a url has an explicit scheme given. Args: @@ -246,7 +245,7 @@ def _has_explicit_scheme(url: QUrl) -> bool: not url.path().startswith(':')) -def is_special_url(url: QUrl) -> bool: +def is_special_url(url: core.QUrl) -> bool: """Return True if url is an about:... or other special URL. Args: @@ -273,8 +272,8 @@ def is_url(urlstr: str) -> bool: urlstr, autosearch)) urlstr = urlstr.strip() - qurl = QUrl(urlstr) - qurl_userinput = QUrl.fromUserInput(urlstr) + qurl = core.QUrl(urlstr) + qurl_userinput = core.QUrl.fromUserInput(urlstr) if autosearch == 'never': # no autosearch, so everything is a URL unless it has an explicit @@ -320,12 +319,12 @@ def is_url(urlstr: str) -> bool: return url -def ensure_valid(url: QUrl) -> None: +def ensure_valid(url: core.QUrl) -> None: if not url.isValid(): raise InvalidUrlError(url) -def invalid_url_error(url: QUrl, action: str) -> None: +def invalid_url_error(url: core.QUrl, action: str) -> None: """Display an error message for a URL. Args: @@ -340,7 +339,7 @@ def invalid_url_error(url: QUrl, action: str) -> None: message.error(errstring) -def raise_cmdexc_if_invalid(url: QUrl) -> None: +def raise_cmdexc_if_invalid(url: core.QUrl) -> None: """Check if the given QUrl is invalid, and if so, raise a CommandError.""" try: ensure_valid(url) @@ -396,7 +395,7 @@ def get_path_if_valid(pathstr: str, return path -def filename_from_url(url: QUrl, fallback: str = None) -> Optional[str]: +def filename_from_url(url: core.QUrl, fallback: str = None) -> Optional[str]: """Get a suitable filename from a URL. Args: @@ -429,7 +428,7 @@ def filename_from_url(url: QUrl, fallback: str = None) -> Optional[str]: HostTupleType = Tuple[str, str, int] -def host_tuple(url: QUrl) -> HostTupleType: +def host_tuple(url: core.QUrl) -> HostTupleType: """Get a (scheme, host, port) tuple from a QUrl. This is suitable to identify a connection, e.g. for SSL errors. @@ -454,7 +453,7 @@ def host_tuple(url: QUrl) -> HostTupleType: return scheme, host, port -def get_errstring(url: QUrl, base: str = "Invalid URL") -> str: +def get_errstring(url: core.QUrl, base: str = "Invalid URL") -> str: """Get an error string for a URL. Args: @@ -471,7 +470,7 @@ def get_errstring(url: QUrl, base: str = "Invalid URL") -> str: return base -def same_domain(url1: QUrl, url2: QUrl) -> bool: +def same_domain(url1: core.QUrl, url2: core.QUrl) -> bool: """Check if url1 and url2 belong to the same website. This will use a "public suffix list" to determine what a "top level domain" @@ -519,7 +518,7 @@ def same_domain(url1: QUrl, url2: QUrl) -> bool: return domain1 == domain2 -def encoded_url(url: QUrl) -> str: +def encoded_url(url: core.QUrl) -> str: """Return the fully encoded url as string. Args: @@ -534,19 +533,19 @@ def file_url(path: str) -> str: Arguments: path: The absolute path to the local file """ - url = QUrl.fromLocalFile(path) - return url.toString(QUrl.ComponentFormattingOption.FullyEncoded) # type: ignore[arg-type] + url = core.QUrl.fromLocalFile(path) + return url.toString(core.QUrl.ComponentFormattingOption.FullyEncoded) # type: ignore[arg-type] -def data_url(mimetype: str, data: bytes) -> QUrl: +def data_url(mimetype: str, data: bytes) -> core.QUrl: """Get a data: QUrl for the given data.""" b64 = base64.b64encode(data).decode('ascii') - url = QUrl('data:{};base64,{}'.format(mimetype, b64)) + url = core.QUrl('data:{};base64,{}'.format(mimetype, b64)) qtutils.ensure_valid(url) return url -def safe_display_string(qurl: QUrl) -> str: +def safe_display_string(qurl: core.QUrl) -> str: """Get a IDN-homograph phishing safe form of the given QUrl. If we're dealing with a Punycode-encoded URL, this prepends the hostname in @@ -557,11 +556,11 @@ def safe_display_string(qurl: QUrl) -> str: """ ensure_valid(qurl) - host = qurl.host(QUrl.ComponentFormattingOption.FullyEncoded) + host = qurl.host(core.QUrl.ComponentFormattingOption.FullyEncoded) assert '..' not in host, qurl # https://bugreports.qt.io/browse/QTBUG-60364 for part in host.split('.'): - url_host = qurl.host(QUrl.ComponentFormattingOption.FullyDecoded) + url_host = qurl.host(core.QUrl.ComponentFormattingOption.FullyDecoded) if part.startswith('xn--') and host != url_host: return '({}) {}'.format(host, qurl.toDisplayString()) @@ -576,7 +575,7 @@ class InvalidProxyTypeError(Exception): super().__init__("Invalid proxy type {}!".format(typ)) -def proxy_from_url(url: QUrl) -> Union[QNetworkProxy, pac.PACFetcher]: +def proxy_from_url(url: core.QUrl) -> Union[network.QNetworkProxy, pac.PACFetcher]: """Create a QNetworkProxy from QUrl and a proxy type. Args: @@ -594,15 +593,15 @@ def proxy_from_url(url: QUrl) -> Union[QNetworkProxy, pac.PACFetcher]: return fetcher types = { - 'http': QNetworkProxy.ProxyType.HttpProxy, - 'socks': QNetworkProxy.ProxyType.Socks5Proxy, - 'socks5': QNetworkProxy.ProxyType.Socks5Proxy, - 'direct': QNetworkProxy.ProxyType.NoProxy, + 'http': network.QNetworkProxy.ProxyType.HttpProxy, + 'socks': network.QNetworkProxy.ProxyType.Socks5Proxy, + 'socks5': network.QNetworkProxy.ProxyType.Socks5Proxy, + 'direct': network.QNetworkProxy.ProxyType.NoProxy, } if scheme not in types: raise InvalidProxyTypeError(scheme) - proxy = QNetworkProxy(types[scheme], url.host()) + proxy = network.QNetworkProxy(types[scheme], url.host()) if url.port() != -1: proxy.setPort(url.port()) @@ -613,7 +612,7 @@ def proxy_from_url(url: QUrl) -> Union[QNetworkProxy, pac.PACFetcher]: return proxy -def parse_javascript_url(url: QUrl) -> str: +def parse_javascript_url(url: core.QUrl) -> str: """Get JavaScript source from the given URL. See https://wiki.whatwg.org/wiki/URL_schemes#javascript:_URLs @@ -626,7 +625,7 @@ def parse_javascript_url(url: QUrl) -> str: raise Error("URL contains unexpected components: {}" .format(url.authority())) - urlstr = url.toString(QUrl.ComponentFormattingOption.FullyEncoded) # type: ignore[arg-type] + urlstr = url.toString(core.QUrl.ComponentFormattingOption.FullyEncoded) # type: ignore[arg-type] urlstr = urllib.parse.unquote(urlstr) code = urlstr[len('javascript:'):] |