Move whitelist host code to networkmanager

This means no :adblock-update after modifying the whitelist.
This commit is contained in:
Daniel 2015-09-16 17:10:03 +02:00
parent 523e071a97
commit fc806525a2
2 changed files with 20 additions and 20 deletions

View File

@ -24,7 +24,6 @@ import os.path
import functools import functools
import posixpath import posixpath
import zipfile import zipfile
import fnmatch
from qutebrowser.config import config from qutebrowser.config import config
from qutebrowser.utils import objreg, standarddir, log, message from qutebrowser.utils import objreg, standarddir, log, message
@ -60,22 +59,6 @@ def get_fileobj(byte_io):
return io.TextIOWrapper(byte_io, encoding='utf-8') return io.TextIOWrapper(byte_io, encoding='utf-8')
def is_whitelisted_domain(host):
"""Check if the given host is on the adblock whitelist.
Args:
host: The host as given by the adblocker as string.
"""
whitelist = config.get('content', 'host-blocking-whitelist')
if whitelist is None:
return False
for pattern in whitelist:
if fnmatch.fnmatch(host, pattern.lower()):
return True
return False
class FakeDownload: class FakeDownload:
"""A download stub to use on_download_finished with local files.""" """A download stub to use on_download_finished with local files."""
@ -205,8 +188,7 @@ class HostBlocker:
else: else:
error_count += 1 error_count += 1
continue continue
if (host not in self.WHITELISTED if host not in self.WHITELISTED:
and not is_whitelisted_domain(host)):
self.blocked_hosts.add(host) self.blocked_hosts.add(host)
log.misc.debug("{}: read {} lines".format(byte_io.name, line_count)) log.misc.debug("{}: read {} lines".format(byte_io.name, line_count))
if error_count > 0: if error_count > 0:

View File

@ -20,6 +20,7 @@
"""Our own QNetworkAccessManager.""" """Our own QNetworkAccessManager."""
import collections import collections
import fnmatch
from PyQt5.QtCore import (pyqtSlot, pyqtSignal, PYQT_VERSION, QCoreApplication, from PyQt5.QtCore import (pyqtSlot, pyqtSignal, PYQT_VERSION, QCoreApplication,
QUrl, QByteArray) QUrl, QByteArray)
@ -49,6 +50,22 @@ def init():
QSslSocket.setDefaultCiphers(good_ciphers) QSslSocket.setDefaultCiphers(good_ciphers)
def is_whitelisted_domain(host):
"""Check if the given host is on the adblock whitelist.
Args:
host: The host as given by the adblocker as string.
"""
whitelist = config.get('content', 'host-blocking-whitelist')
if whitelist is None:
return False
for pattern in whitelist:
if fnmatch.fnmatch(host, pattern.lower()):
return True
return False
class SslError(QSslError): class SslError(QSslError):
"""A QSslError subclass which provides __hash__ on Qt < 5.4.""" """A QSslError subclass which provides __hash__ on Qt < 5.4."""
@ -347,7 +364,8 @@ class NetworkManager(QNetworkAccessManager):
host_blocker = objreg.get('host-blocker') host_blocker = objreg.get('host-blocker')
if (op == QNetworkAccessManager.GetOperation and if (op == QNetworkAccessManager.GetOperation and
req.url().host() in host_blocker.blocked_hosts and req.url().host() in host_blocker.blocked_hosts and
config.get('content', 'host-blocking-enabled')): config.get('content', 'host-blocking-enabled') and
not is_whitelisted_domain(req.url().host())):
log.webview.info("Request to {} blocked by host blocker.".format( log.webview.info("Request to {} blocked by host blocker.".format(
req.url().host())) req.url().host()))
return networkreply.ErrorNetworkReply( return networkreply.ErrorNetworkReply(