Fix URL detection corner cases.

This commit is contained in:
Florian Bruhin 2014-09-02 06:53:52 +02:00
parent 2bf616ff78
commit 71a2ade637
3 changed files with 21 additions and 4 deletions

View File

@ -7,7 +7,6 @@ Before 0.1
- Website - Website
- FAQ - FAQ
- Single-instance and multi-window - Single-instance and multi-window
- Tests for URL detection and fix corner cases
- Maybe test under other platforms - Maybe test under other platforms
- Autosave feature (against segfaults/...) - Autosave feature (against segfaults/...)
- Save tabs feature - Save tabs feature

View File

@ -126,6 +126,10 @@ class IsUrlTests(unittest.TestCase):
'http://foobar', 'http://foobar',
'localhost:8080', 'localhost:8080',
'qutebrowser.org', 'qutebrowser.org',
'127.0.0.1',
'::1',
'2001:41d0:2:6c11::1',
'94.23.233.17',
) )
NOT_URLS = ( NOT_URLS = (
@ -133,6 +137,10 @@ class IsUrlTests(unittest.TestCase):
'localhost test', 'localhost test',
'another . test', 'another . test',
'foo', 'foo',
'this is: not an URL',
'23.42',
'1337',
'deadbeef',
) )
def test_urls(self, configmock): def test_urls(self, configmock):

View File

@ -21,6 +21,7 @@
import re import re
import os.path import os.path
import ipaddress
import urllib.parse import urllib.parse
from PyQt5.QtCore import QUrl from PyQt5.QtCore import QUrl
@ -78,9 +79,18 @@ def _is_url_naive(urlstr):
True if the URL really is a URL, False otherwise. True if the URL really is a URL, False otherwise.
""" """
url = QUrl.fromUserInput(urlstr) url = QUrl.fromUserInput(urlstr)
# We don't use url here because fromUserInput appends http:// try:
# automatically. ipaddress.ip_address(urlstr)
if not url.isValid(): 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 return False
elif '.' in url.host(): elif '.' in url.host():
return True return True