Make sure QUrl::fromUserInput is valid in is_url.

Fixes #460.

Without this fix, it's possible for URLs to be valid according to is_url, but
not according to QUrl::fromUserInput, e.g. "http:foo:0". This caused an
exception later because fuzzy_url runs qtutils.ensure_valid.
This commit is contained in:
Florian Bruhin 2015-01-09 20:10:39 +01:00
parent 3eac528716
commit 354018efcd
2 changed files with 7 additions and 5 deletions

View File

@ -157,6 +157,7 @@ class IsUrlTests(unittest.TestCase):
'1337', '1337',
'deadbeef', 'deadbeef',
'31c3', '31c3',
'http:foo:0',
) )
def test_urls(self, configmock): def test_urls(self, configmock):

View File

@ -205,25 +205,26 @@ def is_url(urlstr):
if _has_explicit_scheme(qurl): if _has_explicit_scheme(qurl):
# URLs with explicit schemes are always URLs # URLs with explicit schemes are always URLs
log.url.debug("Contains explicit scheme") log.url.debug("Contains explicit scheme")
return True url = True
elif ' ' in urlstr: elif ' ' in urlstr:
# A URL will never contain a space # A URL will never contain a space
log.url.debug("Contains space -> no URL") log.url.debug("Contains space -> no URL")
return False url = False
elif is_special_url(qurl): elif is_special_url(qurl):
# Special URLs are always URLs, even with autosearch=False # Special URLs are always URLs, even with autosearch=False
log.url.debug("Is an special URL.") log.url.debug("Is an special URL.")
return True url = True
elif autosearch == 'dns': elif autosearch == 'dns':
log.url.debug("Checking via DNS") log.url.debug("Checking via DNS")
# We want to use qurl_from_user_input here, as the user might enter # We want to use qurl_from_user_input here, as the user might enter
# "foo.de" and that should be treated as URL here. # "foo.de" and that should be treated as URL here.
return _is_url_dns(qurl_from_user_input(urlstr)) url = _is_url_dns(qurl_from_user_input(urlstr))
elif autosearch == 'naive': elif autosearch == 'naive':
log.url.debug("Checking via naive check") log.url.debug("Checking via naive check")
return _is_url_naive(urlstr) url = _is_url_naive(urlstr)
else: else:
raise ValueError("Invalid autosearch value") raise ValueError("Invalid autosearch value")
return url and QUrl.fromUserInput(urlstr).isValid()
def qurl_from_user_input(urlstr): def qurl_from_user_input(urlstr):