urlutils: Raise exception on errors in host_tuple.

This commit is contained in:
Florian Bruhin 2015-05-20 13:24:48 +02:00
parent 6f904759b5
commit 269676318b
2 changed files with 37 additions and 7 deletions

View File

@ -195,10 +195,20 @@ class NetworkManager(QNetworkAccessManager):
errors = [SslError(e) for e in errors]
ssl_strict = config.get('network', 'ssl-strict')
if ssl_strict == 'ask':
host_tpl = urlutils.host_tuple(reply.url())
if set(errors).issubset(self._accepted_ssl_errors[host_tpl]):
try:
host_tpl = urlutils.host_tuple(reply.url())
except ValueError:
host_tpl = None
is_accepted = False
is_rejected = False
else:
is_accepted = set(errors).issubset(
self._accepted_ssl_errors[host_tpl])
is_rejected = set(errors).issubset(
self._rejected_ssl_errors[host_tpl])
if is_accepted:
reply.ignoreSslErrors()
elif set(errors).issubset(self._rejected_ssl_errors[host_tpl]):
elif is_rejected:
pass
else:
err_string = '\n'.join('- ' + err.errorString() for err in
@ -208,9 +218,11 @@ class NetworkManager(QNetworkAccessManager):
owner=reply)
if answer:
reply.ignoreSslErrors()
self._accepted_ssl_errors[host_tpl] += errors
d = self._accepted_ssl_errors
else:
self._rejected_ssl_errors[host_tpl] += errors
d = self._rejected_ssl_errors
if host_tpl is not None:
d[host_tpl] += errors
elif ssl_strict:
pass
else:

View File

@ -350,11 +350,29 @@ def filename_from_url(url):
def host_tuple(url):
"""Get a (scheme, host, port) tuple.
"""Get a (scheme, host, port) tuple from a QUrl.
This is suitable to identify a connection, e.g. for SSL errors.
"""
return (url.scheme(), url.host(), url.port())
if not url.isValid():
raise ValueError(get_errstring(url))
scheme, host, port = url.scheme(), url.host(), url.port()
assert scheme
if not host:
raise ValueError("Got URL {} without host.".format(
url.toDisplayString()))
if port == -1:
port_mapping = {
'http': 80,
'https': 443,
'ftp': 21,
}
try:
port = port_mapping[scheme]
except KeyError:
raise ValueError("Got URL {} with unknown port.".format(
url.toDisplayString()))
return scheme, host, port
def get_errstring(url, base="Invalid URL"):