Fix URL detection corner cases.
This commit is contained in:
parent
2bf616ff78
commit
71a2ade637
1
doc/TODO
1
doc/TODO
@ -7,7 +7,6 @@ Before 0.1
|
||||
- Website
|
||||
- FAQ
|
||||
- Single-instance and multi-window
|
||||
- Tests for URL detection and fix corner cases
|
||||
- Maybe test under other platforms
|
||||
- Autosave feature (against segfaults/...)
|
||||
- Save tabs feature
|
||||
|
@ -126,6 +126,10 @@ class IsUrlTests(unittest.TestCase):
|
||||
'http://foobar',
|
||||
'localhost:8080',
|
||||
'qutebrowser.org',
|
||||
'127.0.0.1',
|
||||
'::1',
|
||||
'2001:41d0:2:6c11::1',
|
||||
'94.23.233.17',
|
||||
)
|
||||
|
||||
NOT_URLS = (
|
||||
@ -133,6 +137,10 @@ class IsUrlTests(unittest.TestCase):
|
||||
'localhost test',
|
||||
'another . test',
|
||||
'foo',
|
||||
'this is: not an URL',
|
||||
'23.42',
|
||||
'1337',
|
||||
'deadbeef',
|
||||
)
|
||||
|
||||
def test_urls(self, configmock):
|
||||
|
@ -21,6 +21,7 @@
|
||||
|
||||
import re
|
||||
import os.path
|
||||
import ipaddress
|
||||
import urllib.parse
|
||||
|
||||
from PyQt5.QtCore import QUrl
|
||||
@ -78,9 +79,18 @@ def _is_url_naive(urlstr):
|
||||
True if the URL really is a URL, False otherwise.
|
||||
"""
|
||||
url = QUrl.fromUserInput(urlstr)
|
||||
# We don't use url here because fromUserInput appends http://
|
||||
# automatically.
|
||||
if not url.isValid():
|
||||
try:
|
||||
ipaddress.ip_address(urlstr)
|
||||
except ValueError:
|
||||
pass
|
||||
else:
|
||||
# Valid IPv4/IPv6 address
|
||||
return True
|
||||
if re.search(r'^[0-9.]+$', urlstr):
|
||||
# Qt treats things like "23.42" or "1337" as valid URLs which we don't
|
||||
# want to. Note we already filtered *real* valid IPs above.
|
||||
return False
|
||||
elif not url.isValid():
|
||||
return False
|
||||
elif '.' in url.host():
|
||||
return True
|
||||
|
Loading…
Reference in New Issue
Block a user