Check QUrl objects for validity
This commit is contained in:
parent
16758354e8
commit
0dc2ecef46
@ -276,7 +276,7 @@ following guidelines:
|
|||||||
* Convert a string to a QUrl object as early as possible, i.e. directly after
|
* Convert a string to a QUrl object as early as possible, i.e. directly after
|
||||||
the user did enter it.
|
the user did enter it.
|
||||||
- Use `utils.url.fuzzy_url` if the URL is entered by the user somewhere.
|
- 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.
|
message to the user.
|
||||||
* Convert a `QUrl` object to a string as late as possible, e.g. before
|
* Convert a `QUrl` object to a string as late as possible, e.g. before
|
||||||
displaying it to the user.
|
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`.
|
* 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
|
* Mention in the docstring whether your function needs a URL string or a
|
||||||
`QUrl`.
|
`QUrl`.
|
||||||
|
* Call QUrl.isValid() and take appropriate action if not.
|
||||||
|
|
||||||
|
|
||||||
Style conventions
|
Style conventions
|
||||||
|
@ -294,7 +294,7 @@ class Application(QApplication):
|
|||||||
self._opened_urls.append(cmd)
|
self._opened_urls.append(cmd)
|
||||||
try:
|
try:
|
||||||
url = urlutils.fuzzy_url(cmd)
|
url = urlutils.fuzzy_url(cmd)
|
||||||
except urlutils.SearchEngineError as e:
|
except urlutils.FuzzyUrlError as e:
|
||||||
message.error("Error in startup argument '{}': {}".format(
|
message.error("Error in startup argument '{}': {}".format(
|
||||||
cmd, e))
|
cmd, e))
|
||||||
else:
|
else:
|
||||||
@ -305,7 +305,7 @@ class Application(QApplication):
|
|||||||
for urlstr in self.config.get('general', 'startpage'):
|
for urlstr in self.config.get('general', 'startpage'):
|
||||||
try:
|
try:
|
||||||
url = urlutils.fuzzy_url(urlstr)
|
url = urlutils.fuzzy_url(urlstr)
|
||||||
except urlutils.SearchEngineError as e:
|
except urlutils.FuzzyUrlError as e:
|
||||||
message.error("Error when opening startpage: {}".format(e))
|
message.error("Error when opening startpage: {}".format(e))
|
||||||
else:
|
else:
|
||||||
self.mainwindow.tabs.tabopen(url)
|
self.mainwindow.tabs.tabopen(url)
|
||||||
|
@ -162,7 +162,7 @@ class CommandDispatcher:
|
|||||||
tab = self._tabs.cntwidget(count)
|
tab = self._tabs.cntwidget(count)
|
||||||
try:
|
try:
|
||||||
url = urlutils.fuzzy_url(urlstr)
|
url = urlutils.fuzzy_url(urlstr)
|
||||||
except urlutils.SearchEngineError as e:
|
except urlutils.FuzzyUrlError as e:
|
||||||
raise CommandError(e)
|
raise CommandError(e)
|
||||||
if tab is None:
|
if tab is None:
|
||||||
if count is None:
|
if count is None:
|
||||||
@ -434,7 +434,7 @@ class CommandDispatcher:
|
|||||||
"""Open a new tab with a given url."""
|
"""Open a new tab with a given url."""
|
||||||
try:
|
try:
|
||||||
url = urlutils.fuzzy_url(urlstr)
|
url = urlutils.fuzzy_url(urlstr)
|
||||||
except urlutils.SearchEngineError as e:
|
except urlutils.FuzzyUrlError as e:
|
||||||
raise CommandError(e)
|
raise CommandError(e)
|
||||||
self._tabs.tabopen(url, background=False)
|
self._tabs.tabopen(url, background=False)
|
||||||
|
|
||||||
@ -443,7 +443,7 @@ class CommandDispatcher:
|
|||||||
"""Open a new tab in background."""
|
"""Open a new tab in background."""
|
||||||
try:
|
try:
|
||||||
url = urlutils.fuzzy_url(urlstr)
|
url = urlutils.fuzzy_url(urlstr)
|
||||||
except urlutils.SearchEngineError as e:
|
except urlutils.FuzzyUrlError as e:
|
||||||
raise CommandError(e)
|
raise CommandError(e)
|
||||||
self._tabs.tabopen(url, background=True)
|
self._tabs.tabopen(url, background=True)
|
||||||
|
|
||||||
@ -518,7 +518,7 @@ class CommandDispatcher:
|
|||||||
log.misc.debug("Clipboard contained: '{}'".format(text))
|
log.misc.debug("Clipboard contained: '{}'".format(text))
|
||||||
try:
|
try:
|
||||||
url = urlutils.fuzzy_url(text)
|
url = urlutils.fuzzy_url(text)
|
||||||
except urlutils.SearchEngineError as e:
|
except urlutils.FuzzyUrlError as e:
|
||||||
raise CommandError(e)
|
raise CommandError(e)
|
||||||
if tab:
|
if tab:
|
||||||
self._tabs.tabopen(url)
|
self._tabs.tabopen(url)
|
||||||
@ -640,7 +640,10 @@ class CommandDispatcher:
|
|||||||
def quickmark_load(self, name):
|
def quickmark_load(self, name):
|
||||||
"""Load a quickmark."""
|
"""Load a quickmark."""
|
||||||
urlstr = quickmarks.get(name)
|
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')
|
@cmdutils.register(instance='mainwindow.tabs.cmd')
|
||||||
def quickmark_load_tab(self, name):
|
def quickmark_load_tab(self, name):
|
||||||
|
@ -400,7 +400,7 @@ class HintManager(QObject):
|
|||||||
raise CommandError("No {} links found!".format(
|
raise CommandError("No {} links found!".format(
|
||||||
"prev" if prev else "forward"))
|
"prev" if prev else "forward"))
|
||||||
url = self._resolve_url(elem, baseurl)
|
url = self._resolve_url(elem, baseurl)
|
||||||
if url is None:
|
if url is None or not url.isValid():
|
||||||
raise CommandError("No {} links found!".format(
|
raise CommandError("No {} links found!".format(
|
||||||
"prev" if prev else "forward"))
|
"prev" if prev else "forward"))
|
||||||
self.openurl.emit(url, newtab)
|
self.openurl.emit(url, newtab)
|
||||||
|
@ -97,4 +97,8 @@ def get(name):
|
|||||||
if name not in marks:
|
if name not in marks:
|
||||||
raise CommandError("Quickmark '{}' does not exist!".format(name))
|
raise CommandError("Quickmark '{}' does not exist!".format(name))
|
||||||
urlstr = marks[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
|
||||||
|
@ -129,7 +129,7 @@ class SearchUrlTests(TestCase):
|
|||||||
|
|
||||||
def test_engine_wrong(self):
|
def test_engine_wrong(self):
|
||||||
"""Test with wrong search engine."""
|
"""Test with wrong search engine."""
|
||||||
with self.assertRaises(urlutils.SearchEngineError):
|
with self.assertRaises(urlutils.FuzzyUrlError):
|
||||||
_ = urlutils._get_search_url('!blub testfoo')
|
_ = urlutils._get_search_url('!blub testfoo')
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
|
@ -43,7 +43,7 @@ def _get_search_url(txt):
|
|||||||
The search URL as a QUrl.
|
The search URL as a QUrl.
|
||||||
|
|
||||||
Raise:
|
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))
|
logger.debug("Finding search engine for '{}'".format(txt))
|
||||||
r = re.compile(r'(^|\s+)!(\w+)($|\s+)')
|
r = re.compile(r'(^|\s+)!(\w+)($|\s+)')
|
||||||
@ -53,7 +53,7 @@ def _get_search_url(txt):
|
|||||||
try:
|
try:
|
||||||
template = config.get('searchengines', engine)
|
template = config.get('searchengines', engine)
|
||||||
except config.NoOptionError:
|
except config.NoOptionError:
|
||||||
raise SearchEngineError("Search engine {} not found!".format(
|
raise FuzzyUrlError("Search engine {} not found!".format(
|
||||||
engine))
|
engine))
|
||||||
term = r.sub('', txt)
|
term = r.sub('', txt)
|
||||||
logger.debug("engine {}, term '{}'".format(engine, term))
|
logger.debug("engine {}, term '{}'".format(engine, term))
|
||||||
@ -62,7 +62,7 @@ def _get_search_url(txt):
|
|||||||
term = txt
|
term = txt
|
||||||
logger.debug("engine: default, term '{}'".format(txt))
|
logger.debug("engine: default, term '{}'".format(txt))
|
||||||
if not term:
|
if not term:
|
||||||
raise SearchEngineError("No search term given")
|
raise FuzzyUrlError("No search term given")
|
||||||
return QUrl.fromUserInput(template.format(urllib.parse.quote(term)))
|
return QUrl.fromUserInput(template.format(urllib.parse.quote(term)))
|
||||||
|
|
||||||
|
|
||||||
@ -79,7 +79,9 @@ def _is_url_naive(urlstr):
|
|||||||
url = QUrl.fromUserInput(urlstr)
|
url = QUrl.fromUserInput(urlstr)
|
||||||
# We don't use url here because fromUserInput appends http://
|
# We don't use url here because fromUserInput appends http://
|
||||||
# automatically.
|
# automatically.
|
||||||
if QUrl(urlstr).scheme() in schemes:
|
if not url.isValid():
|
||||||
|
return False
|
||||||
|
elif QUrl(urlstr).scheme() in schemes:
|
||||||
return True
|
return True
|
||||||
elif '.' in url.host():
|
elif '.' in url.host():
|
||||||
return True
|
return True
|
||||||
@ -98,6 +100,8 @@ def _is_url_dns(url):
|
|||||||
Return:
|
Return:
|
||||||
True if the URL really is a URL, False otherwise.
|
True if the URL really is a URL, False otherwise.
|
||||||
"""
|
"""
|
||||||
|
if not url.isValid():
|
||||||
|
return False
|
||||||
host = url.host()
|
host = url.host()
|
||||||
logger.debug("DNS request for {}".format(host))
|
logger.debug("DNS request for {}".format(host))
|
||||||
if not host:
|
if not host:
|
||||||
@ -136,6 +140,8 @@ def is_special_url(url):
|
|||||||
Args:
|
Args:
|
||||||
url: The URL as QUrl.
|
url: The URL as QUrl.
|
||||||
"""
|
"""
|
||||||
|
if not url.isValid():
|
||||||
|
return False
|
||||||
special_schemes = ('about', 'qute', 'file')
|
special_schemes = ('about', 'qute', 'file')
|
||||||
return url.scheme() in special_schemes
|
return url.scheme() in special_schemes
|
||||||
|
|
||||||
@ -184,8 +190,8 @@ def is_url(urlstr):
|
|||||||
raise ValueError("Invalid autosearch value")
|
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
|
pass
|
||||||
|
Loading…
Reference in New Issue
Block a user