Rename requests/request filters to interceptors

So we don't collide with the requests library.
This commit is contained in:
Florian Bruhin 2018-12-10 17:35:53 +01:00
parent 7f06b54f25
commit 98543af57b
6 changed files with 29 additions and 29 deletions

View File

@ -19,14 +19,14 @@
"""APIs related to intercepting/blocking requests.""" """APIs related to intercepting/blocking requests."""
from qutebrowser.extensions import requests from qutebrowser.extensions import interceptors
# pylint: disable=unused-import # pylint: disable=unused-import
from qutebrowser.extensions.requests import Request from qutebrowser.extensions.interceptors import Request
def register_filter(reqfilter: requests.RequestFilterType) -> None: def register(interceptor: interceptors.InterceptorType) -> None:
"""Register a request filter. """Register a request interceptor.
Whenever a request happens, the filter gets called with a Request object. Whenever a request happens, the interceptor gets called with a Request object.
""" """
requests.register_filter(reqfilter) interceptors.register(interceptor)

View File

@ -26,7 +26,7 @@ from PyQt5.QtWebEngineCore import (QWebEngineUrlRequestInterceptor,
from qutebrowser.config import config from qutebrowser.config import config
from qutebrowser.browser import shared from qutebrowser.browser import shared
from qutebrowser.utils import utils, log, debug from qutebrowser.utils import utils, log, debug
from qutebrowser.extensions import requests from qutebrowser.extensions import interceptors
class RequestInterceptor(QWebEngineUrlRequestInterceptor): class RequestInterceptor(QWebEngineUrlRequestInterceptor):
@ -84,9 +84,9 @@ class RequestInterceptor(QWebEngineUrlRequestInterceptor):
return return
# FIXME:qtwebengine only block ads for NavigationTypeOther? # FIXME:qtwebengine only block ads for NavigationTypeOther?
request = requests.Request(first_party_url=first_party, request = interceptors.Request(first_party_url=first_party,
request_url=url) request_url=url)
requests.run_filters(request) interceptors.run(request)
if request.is_blocked: if request.is_blocked:
info.block(True) info.block(True)

View File

@ -39,7 +39,7 @@ from PyQt5.QtCore import QUrl
from qutebrowser.browser import downloads from qutebrowser.browser import downloads
from qutebrowser.browser.webkit import webkitelem from qutebrowser.browser.webkit import webkitelem
from qutebrowser.utils import log, objreg, message, usertypes, utils, urlutils from qutebrowser.utils import log, objreg, message, usertypes, utils, urlutils
from qutebrowser.extensions import requests from qutebrowser.extensions import interceptors
@attr.s @attr.s
@ -355,8 +355,8 @@ class _Downloader:
# qute, see the comments/discussion on # qute, see the comments/discussion on
# https://github.com/qutebrowser/qutebrowser/pull/962#discussion_r40256987 # https://github.com/qutebrowser/qutebrowser/pull/962#discussion_r40256987
# and https://github.com/qutebrowser/qutebrowser/issues/1053 # and https://github.com/qutebrowser/qutebrowser/issues/1053
request = requests.Request(first_party_url=None, request_url=url) request = interceptors.Request(first_party_url=None, request_url=url)
requests.run_filters(request) interceptors.run(request)
if request.is_blocked: if request.is_blocked:
log.downloads.debug("Skipping {}, host-blocked".format(url)) log.downloads.debug("Skipping {}, host-blocked".format(url))
# We still need an empty file in the output, QWebView can be pretty # We still need an empty file in the output, QWebView can be pretty

View File

@ -38,7 +38,7 @@ if MYPY:
from qutebrowser.utils import (message, log, usertypes, utils, objreg, from qutebrowser.utils import (message, log, usertypes, utils, objreg,
urlutils, debug) urlutils, debug)
from qutebrowser.browser import shared from qutebrowser.browser import shared
from qutebrowser.extensions import requests from qutebrowser.extensions import interceptors
from qutebrowser.browser.webkit import certificateerror from qutebrowser.browser.webkit import certificateerror
from qutebrowser.browser.webkit.network import (webkitqutescheme, networkreply, from qutebrowser.browser.webkit.network import (webkitqutescheme, networkreply,
filescheme) filescheme)
@ -406,9 +406,9 @@ class NetworkManager(QNetworkAccessManager):
# the webpage shutdown here. # the webpage shutdown here.
current_url = QUrl() current_url = QUrl()
request = requests.Request(first_party_url=current_url, request = interceptors.Request(first_party_url=current_url,
request_url=req.url()) request_url=req.url())
requests.run_filters(request) interceptors.run(request)
if request.is_blocked: if request.is_blocked:
return networkreply.ErrorNetworkReply( return networkreply.ErrorNetworkReply(
req, HOSTBLOCK_ERROR_STRING, QNetworkReply.ContentAccessDenied, req, HOSTBLOCK_ERROR_STRING, QNetworkReply.ContentAccessDenied,

View File

@ -31,7 +31,7 @@ import pathlib
from PyQt5.QtCore import QUrl from PyQt5.QtCore import QUrl
from qutebrowser.api import (cmdutils, hook, config, message, downloads, from qutebrowser.api import (cmdutils, hook, config, message, downloads,
requests, apitypes) interceptor, apitypes)
logger = logging.getLogger('misc') logger = logging.getLogger('misc')
@ -123,7 +123,7 @@ class HostBlocker:
host in self._config_blocked_hosts) and host in self._config_blocked_hosts) and
not _is_whitelisted_url(request_url)) not _is_whitelisted_url(request_url))
def filter_request(self, info: requests.Request) -> None: def filter_request(self, info: interceptor.Request) -> None:
"""Block the given request if necessary.""" """Block the given request if necessary."""
if self._is_blocked(request_url=info.request_url, if self._is_blocked(request_url=info.request_url,
first_party_url=info.first_party_url): first_party_url=info.first_party_url):
@ -344,4 +344,4 @@ def init(context: apitypes.InitContext) -> None:
config_dir=context.config_dir, config_dir=context.config_dir,
has_basedir=context.args.basedir is not None) has_basedir=context.args.basedir is not None)
_host_blocker.read_hosts() _host_blocker.read_hosts()
requests.register_filter(_host_blocker.filter_request) interceptor.register(_host_blocker.filter_request)

View File

@ -17,7 +17,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>. # along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""Infrastructure for filtering requests.""" """Infrastructure for intercepting requests."""
import typing import typing
@ -32,7 +32,7 @@ if MYPY:
@attr.s @attr.s
class Request: class Request:
"""A request which can be blocked.""" """A request which can be intercepted/blocked."""
first_party_url = attr.ib() # type: QUrl first_party_url = attr.ib() # type: QUrl
request_url = attr.ib() # type: QUrl request_url = attr.ib() # type: QUrl
@ -43,16 +43,16 @@ class Request:
self.is_blocked = True self.is_blocked = True
RequestFilterType = typing.Callable[[Request], None] InterceptorType = typing.Callable[[Request], None]
_request_filters = [] # type: typing.List[RequestFilterType] _interceptors = [] # type: typing.List[InterceptorType]
def register_filter(reqfilter: RequestFilterType) -> None: def register(interceptor: InterceptorType) -> None:
_request_filters.append(reqfilter) _interceptors.append(interceptor)
def run_filters(info: Request) -> None: def run(info: Request) -> None:
for reqfilter in _request_filters: for interceptor in _interceptors:
reqfilter(info) interceptor(info)