Check QUrl objects for validity

This commit is contained in:
Florian Bruhin 2014-06-20 23:57:52 +02:00
parent 16758354e8
commit 0dc2ecef46
7 changed files with 31 additions and 17 deletions

View File

@ -276,7 +276,7 @@ following guidelines:
* Convert a string to a QUrl object as early as possible, i.e. directly after
the user did enter it.
- Use `utils.url.fuzzy_url` if the URL is entered by the user somewhere.
- Be sure you handle `utils.url.SearchEngineError` and display an error
- Be sure you handle `utils.url.FuzzyError` and display an error
message to the user.
* Convert a `QUrl` object to a string as late as possible, e.g. before
displaying it to the user.
@ -288,6 +288,7 @@ displaying it to the user.
* Name a string URL something like `urlstr`, and a `QUrl` something like `url`.
* Mention in the docstring whether your function needs a URL string or a
`QUrl`.
* Call QUrl.isValid() and take appropriate action if not.
Style conventions

View File

@ -294,7 +294,7 @@ class Application(QApplication):
self._opened_urls.append(cmd)
try:
url = urlutils.fuzzy_url(cmd)
except urlutils.SearchEngineError as e:
except urlutils.FuzzyUrlError as e:
message.error("Error in startup argument '{}': {}".format(
cmd, e))
else:
@ -305,7 +305,7 @@ class Application(QApplication):
for urlstr in self.config.get('general', 'startpage'):
try:
url = urlutils.fuzzy_url(urlstr)
except urlutils.SearchEngineError as e:
except urlutils.FuzzyUrlError as e:
message.error("Error when opening startpage: {}".format(e))
else:
self.mainwindow.tabs.tabopen(url)

View File

@ -162,7 +162,7 @@ class CommandDispatcher:
tab = self._tabs.cntwidget(count)
try:
url = urlutils.fuzzy_url(urlstr)
except urlutils.SearchEngineError as e:
except urlutils.FuzzyUrlError as e:
raise CommandError(e)
if tab is None:
if count is None:
@ -434,7 +434,7 @@ class CommandDispatcher:
"""Open a new tab with a given url."""
try:
url = urlutils.fuzzy_url(urlstr)
except urlutils.SearchEngineError as e:
except urlutils.FuzzyUrlError as e:
raise CommandError(e)
self._tabs.tabopen(url, background=False)
@ -443,7 +443,7 @@ class CommandDispatcher:
"""Open a new tab in background."""
try:
url = urlutils.fuzzy_url(urlstr)
except urlutils.SearchEngineError as e:
except urlutils.FuzzyUrlError as e:
raise CommandError(e)
self._tabs.tabopen(url, background=True)
@ -518,7 +518,7 @@ class CommandDispatcher:
log.misc.debug("Clipboard contained: '{}'".format(text))
try:
url = urlutils.fuzzy_url(text)
except urlutils.SearchEngineError as e:
except urlutils.FuzzyUrlError as e:
raise CommandError(e)
if tab:
self._tabs.tabopen(url)
@ -640,7 +640,10 @@ class CommandDispatcher:
def quickmark_load(self, name):
"""Load a quickmark."""
urlstr = quickmarks.get(name)
self._tabs.currentWidget().openurl(QUrl(urlstr))
url = QUrl(urlstr)
if not url.isValid():
raise CommandError("Invalid URL {}".format(urlstr))
self._tabs.currentWidget().openurl(url)
@cmdutils.register(instance='mainwindow.tabs.cmd')
def quickmark_load_tab(self, name):

View File

@ -400,7 +400,7 @@ class HintManager(QObject):
raise CommandError("No {} links found!".format(
"prev" if prev else "forward"))
url = self._resolve_url(elem, baseurl)
if url is None:
if url is None or not url.isValid():
raise CommandError("No {} links found!".format(
"prev" if prev else "forward"))
self.openurl.emit(url, newtab)

View File

@ -97,4 +97,8 @@ def get(name):
if name not in marks:
raise CommandError("Quickmark '{}' does not exist!".format(name))
urlstr = marks[name]
return QUrl(urlstr)
url = QUrl(urlstr)
if not url.isValid():
raise CommandError("Invalid URL for quickmark {}: {}".format(
name, urlstr))
return url

View File

@ -129,7 +129,7 @@ class SearchUrlTests(TestCase):
def test_engine_wrong(self):
"""Test with wrong search engine."""
with self.assertRaises(urlutils.SearchEngineError):
with self.assertRaises(urlutils.FuzzyUrlError):
_ = urlutils._get_search_url('!blub testfoo')
def tearDown(self):

View File

@ -43,7 +43,7 @@ def _get_search_url(txt):
The search URL as a QUrl.
Raise:
SearchEngineError if there is no template or no search term was found.
FuzzyUrlError if there is no template or no search term was found.
"""
logger.debug("Finding search engine for '{}'".format(txt))
r = re.compile(r'(^|\s+)!(\w+)($|\s+)')
@ -53,7 +53,7 @@ def _get_search_url(txt):
try:
template = config.get('searchengines', engine)
except config.NoOptionError:
raise SearchEngineError("Search engine {} not found!".format(
raise FuzzyUrlError("Search engine {} not found!".format(
engine))
term = r.sub('', txt)
logger.debug("engine {}, term '{}'".format(engine, term))
@ -62,7 +62,7 @@ def _get_search_url(txt):
term = txt
logger.debug("engine: default, term '{}'".format(txt))
if not term:
raise SearchEngineError("No search term given")
raise FuzzyUrlError("No search term given")
return QUrl.fromUserInput(template.format(urllib.parse.quote(term)))
@ -79,7 +79,9 @@ def _is_url_naive(urlstr):
url = QUrl.fromUserInput(urlstr)
# We don't use url here because fromUserInput appends http://
# automatically.
if QUrl(urlstr).scheme() in schemes:
if not url.isValid():
return False
elif QUrl(urlstr).scheme() in schemes:
return True
elif '.' in url.host():
return True
@ -98,6 +100,8 @@ def _is_url_dns(url):
Return:
True if the URL really is a URL, False otherwise.
"""
if not url.isValid():
return False
host = url.host()
logger.debug("DNS request for {}".format(host))
if not host:
@ -136,6 +140,8 @@ def is_special_url(url):
Args:
url: The URL as QUrl.
"""
if not url.isValid():
return False
special_schemes = ('about', 'qute', 'file')
return url.scheme() in special_schemes
@ -184,8 +190,8 @@ def is_url(urlstr):
raise ValueError("Invalid autosearch value")
class SearchEngineError(Exception):
class FuzzyUrlError(Exception):
"""Exception raised when a search engine wasn't found."""
"""Exception raised by fuzzy_url on problems."""
pass