def _is_blocked(self, request_url: QUrl, first_party_url: QUrl = None) -> bool: """Check whether the given request is blocked.""" if not self.enabled: return False if first_party_url is not None and not first_party_url.isValid(): first_party_url = None qtutils.ensure_valid(request_url) if not config.get("content.blocking.enabled", url=first_party_url): return False if blockutils.is_whitelisted_url(request_url): return False host = request_url.host() if config.get("content.blocking.hosts.block_subdomains"): return any(hostname in self._blocked_hosts or hostname in self._config_blocked_hosts for hostname in urlutils.widened_hostnames(host)) else: return (host in self._blocked_hosts or host in self._config_blocked_hosts)
def get_for_url(self, url: QUrl = None, *, fallback: bool = True) -> Any: """Get a config value, falling back when needed. This first tries to find a value matching the URL (if given). If there's no match: With fallback=True, the global/default setting is returned. With fallback=False, usertypes.UNSET is returned. """ self._check_pattern_support(url) if url is None: return self._get_fallback(fallback) qtutils.ensure_valid(url) candidates: List[ScopedValue] = [] # Urls trailing with '.' are equivalent to non-trailing types. # urlutils strips them, so in order to match we will need to as well. widened_hosts = urlutils.widened_hostnames(url.host().rstrip('.')) # We must check the 'None' key as well, in case any patterns that # did not have a domain match. for host in itertools.chain(widened_hosts, [None]): host_set = self._domain_map.get(host, ()) for scoped in host_set: if scoped.pattern is not None and scoped.pattern.matches(url): candidates.append(scoped) if candidates: scoped = max(candidates, key=operator.attrgetter('pattern_id')) return scoped.value if not fallback: return usertypes.UNSET return self._get_fallback(fallback)
def get_for_url(self, url: QUrl = None, *, fallback: bool = True) -> typing.Any: """Get a config value, falling back when needed. This first tries to find a value matching the URL (if given). If there's no match: With fallback=True, the global/default setting is returned. With fallback=False, UNSET is returned. """ self._check_pattern_support(url) if url is None: return self._get_fallback(fallback) candidates = [] # type: typing.List[ScopedValue] widened_hosts = urlutils.widened_hostnames(url.host()) # We must check the 'None' key as well, in case any patterns that # did not have a domain match. for host in itertools.chain(widened_hosts, [None]): host_set = self._domain_map.get(host, ()) for scoped in host_set: if scoped.pattern is not None and scoped.pattern.matches(url): candidates.append(scoped) if candidates: scoped = max(candidates, key=operator.attrgetter('pattern_id')) return scoped.value if not fallback: return UNSET return self._get_fallback(fallback)
def test_bench_widen_hostnames(self, hostname, benchmark): benchmark(lambda: list(urlutils.widened_hostnames(hostname)))
def test_widen_hostnames(self, hostname, expected): assert list(urlutils.widened_hostnames(hostname)) == expected