void ApiWebEngineRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo& request) { const ApiInfo& apiInfo = ApiInfo::instance(); request.setHttpHeader("User-Agent", apiInfo.userAgent); request.setHttpHeader(apiInfo.clientIdHeader, apiInfo.clientId); request.setHttpHeader(apiInfo.apiKeyHeader, apiInfo.apiKey); }
bool AdBlockRule::networkMatch(const QWebEngineUrlRequestInfo &request, const QString &domain, const QString &encodedUrl) const { if (m_type == CssRule || !m_isEnabled || m_isInternalDisabled) { return false; } bool matched = stringMatch(domain, encodedUrl); if (matched) { // Check domain restrictions if (hasOption(DomainRestrictedOption) && !matchDomain(request.firstPartyUrl().host())) { return false; } // Check third-party restriction if (hasOption(ThirdPartyOption) && !matchThirdParty(request)) { return false; } // Check object restrictions if (hasOption(ObjectOption) && !matchObject(request)) { return false; } // Check subdocument restriction if (hasOption(SubdocumentOption) && !matchSubdocument(request)) { return false; } // Check xmlhttprequest restriction if (hasOption(XMLHttpRequestOption) && !matchXmlHttpRequest(request)) { return false; } // Check image restriction if (hasOption(ImageOption) && !matchImage(request)) { return false; } // Check script restriction if (hasOption(ScriptOption) && !matchScript(request)) { return false; } // Check stylesheet restriction if (hasOption(StyleSheetOption) && !matchStyleSheet(request)) { return false; } // Check object-subrequest restriction if (hasOption(ObjectSubrequestOption) && !matchObjectSubrequest(request)) { return false; } } return matched; }
bool AdBlockManager::block(QWebEngineUrlRequestInfo &request) { QMutexLocker locker(&m_mutex); if (!isEnabled()) { return false; } #ifdef ADBLOCK_DEBUG QElapsedTimer timer; timer.start(); #endif const QString urlString = request.requestUrl().toEncoded().toLower(); const QString urlDomain = request.requestUrl().host().toLower(); const QString urlScheme = request.requestUrl().scheme().toLower(); if (!canRunOnScheme(urlScheme) || !canBeBlocked(request.firstPartyUrl())) { return false; } bool res = false; const AdBlockRule* blockedRule = m_matcher->match(request, urlDomain, urlString); if (blockedRule) { res = true; if (request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) { QUrl url(QSL("qupzilla:adblock")); QUrlQuery query; query.addQueryItem(QSL("rule"), blockedRule->filter()); query.addQueryItem(QSL("subscription"), blockedRule->subscription()->title()); url.setQuery(query); request.redirect(url); } else { request.block(true); } #ifdef ADBLOCK_DEBUG qDebug() << "BLOCKED: " << timer.elapsed() << blockedRule->filter() << request.requestUrl(); #endif } #ifdef ADBLOCK_DEBUG qDebug() << timer.elapsed() << request.requestUrl(); #endif return res; }
void NetworkUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo& info) { if (m_sendDNT) { info.setHttpHeader(QByteArrayLiteral("DNT"), QByteArrayLiteral("1")); } // NOTE: Here we can add custom headers for each webengine request, for example "User-Agent". foreach (UrlInterceptor* interceptor, m_interceptors) { interceptor->interceptRequest(info); }
bool NetworkUrlInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) { bool result = false; if (m_sendDNT) { result = true; info.setExtraHeader(QByteArrayLiteral("DNT"), QByteArrayLiteral("1")); } foreach (UrlInterceptor *interceptor, m_interceptors) { if (interceptor->interceptRequest(info)) result = true; } return result; }
bool AdBlockManager::block(QWebEngineUrlRequestInfo &request, QString &ruleFilter, QString &ruleSubscription) { QMutexLocker locker(&m_mutex); if (!isEnabled()) { return false; } #ifdef ADBLOCK_DEBUG QElapsedTimer timer; timer.start(); #endif const QString urlString = request.requestUrl().toEncoded().toLower(); const QString urlDomain = request.requestUrl().host().toLower(); const QString urlScheme = request.requestUrl().scheme().toLower(); if (!canRunOnScheme(urlScheme) || !canBeBlocked(request.firstPartyUrl())) { return false; } const AdBlockRule* blockedRule = m_matcher->match(request, urlDomain, urlString); if (blockedRule) { ruleFilter = blockedRule->filter(); ruleSubscription = blockedRule->subscription()->title(); #ifdef ADBLOCK_DEBUG qDebug() << "BLOCKED: " << timer.elapsed() << blockedRule->filter() << request.requestUrl(); #endif } #ifdef ADBLOCK_DEBUG qDebug() << timer.elapsed() << request.requestUrl(); #endif return blockedRule; }
void QtWebEngineUrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &request) { if (!m_areImagesEnabled && request.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeImage) { request.block(true); return; } if (!m_contentBlockingProfiles.contains(request.firstPartyUrl().host())) { if (SettingsManager::getOption(SettingsManager::ContentBlocking_EnableContentBlockingOption, request.firstPartyUrl()).toBool()) { m_contentBlockingProfiles[request.firstPartyUrl().host()] = ContentBlockingManager::getProfileList(SettingsManager::getOption(SettingsManager::ContentBlocking_ProfilesOption, request.firstPartyUrl()).toStringList()); } else { m_contentBlockingProfiles[request.firstPartyUrl().host()] = QVector<int>(); } } const QVector<int> contentBlockingProfiles(m_contentBlockingProfiles.value(request.firstPartyUrl().host())); if (contentBlockingProfiles.isEmpty()) { const NetworkManagerFactory::DoNotTrackPolicy doNotTrackPolicy(NetworkManagerFactory::getDoNotTrackPolicy()); if (doNotTrackPolicy != NetworkManagerFactory::SkipTrackPolicy) { request.setHttpHeader(QStringLiteral("DNT").toLatin1(), ((doNotTrackPolicy == NetworkManagerFactory::DoNotAllowToTrackPolicy) ? QStringLiteral("1") : QStringLiteral("0")).toLatin1()); } return; } NetworkManager::ResourceType resourceType(NetworkManager::OtherType); bool storeBlockedUrl(true); switch (request.resourceType()) { case QWebEngineUrlRequestInfo::ResourceTypeMainFrame: resourceType = NetworkManager::MainFrameType; break; case QWebEngineUrlRequestInfo::ResourceTypeSubFrame: resourceType = NetworkManager::SubFrameType; break; case QWebEngineUrlRequestInfo::ResourceTypeStylesheet: resourceType = NetworkManager::StyleSheetType; storeBlockedUrl = false; break; case QWebEngineUrlRequestInfo::ResourceTypeScript: resourceType = NetworkManager::ScriptType; storeBlockedUrl = false; break; case QWebEngineUrlRequestInfo::ResourceTypeImage: resourceType = NetworkManager::ImageType; break; case QWebEngineUrlRequestInfo::ResourceTypeObject: case QWebEngineUrlRequestInfo::ResourceTypeMedia: resourceType = NetworkManager::ObjectType; break; #if QT_VERSION >= 0x050700 case QWebEngineUrlRequestInfo::ResourceTypePluginResource: resourceType = NetworkManager::ObjectSubrequestType; storeBlockedUrl = false; break; #endif case QWebEngineUrlRequestInfo::ResourceTypeXhr: resourceType = NetworkManager::XmlHttpRequestType; break; default: break; } const ContentBlockingManager::CheckResult result(ContentBlockingManager::checkUrl(contentBlockingProfiles, request.firstPartyUrl(), request.requestUrl(), resourceType)); if (result.isBlocked) { if (storeBlockedUrl && !m_blockedElements.value(request.firstPartyUrl().host()).contains(request.requestUrl().url())) { m_blockedElements[request.firstPartyUrl().host()].append(request.requestUrl().url()); } Console::addMessage(QCoreApplication::translate("main", "Request blocked with rule: %1").arg(result.rule), Console::NetworkCategory, Console::LogLevel, request.requestUrl().toString(), -1); request.block(true); } }
void QtWebEngineUrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &request) { if (!m_contentBlockingProfiles.contains(request.firstPartyUrl().host())) { m_contentBlockingProfiles[request.firstPartyUrl().host()] = ContentBlockingManager::getProfileList(SettingsManager::getValue(QLatin1String("Content/BlockingProfiles"), request.firstPartyUrl()).toStringList()); } const QVector<int> contentBlockingProfiles(m_contentBlockingProfiles.value(request.firstPartyUrl().host())); if (contentBlockingProfiles.isEmpty()) { const NetworkManagerFactory::DoNotTrackPolicy doNotTrackPolicy(NetworkManagerFactory::getDoNotTrackPolicy()); if (doNotTrackPolicy != NetworkManagerFactory::SkipTrackPolicy) { request.setHttpHeader(QStringLiteral("DNT").toLatin1(), ((doNotTrackPolicy == NetworkManagerFactory::DoNotAllowToTrackPolicy) ? QStringLiteral("1") : QStringLiteral("0")).toLatin1()); } return; } ContentBlockingManager::ResourceType resourceType(ContentBlockingManager::OtherType); switch (request.resourceType()) { case QWebEngineUrlRequestInfo::ResourceTypeMainFrame: resourceType = ContentBlockingManager::MainFrameType; break; case QWebEngineUrlRequestInfo::ResourceTypeSubFrame: resourceType = ContentBlockingManager::SubFrameType; break; case QWebEngineUrlRequestInfo::ResourceTypeStylesheet: resourceType = ContentBlockingManager::StyleSheetType; break; case QWebEngineUrlRequestInfo::ResourceTypeScript: resourceType = ContentBlockingManager::ScriptType; break; case QWebEngineUrlRequestInfo::ResourceTypeImage: resourceType = ContentBlockingManager::ImageType; break; case QWebEngineUrlRequestInfo::ResourceTypeObject: case QWebEngineUrlRequestInfo::ResourceTypeMedia: resourceType = ContentBlockingManager::ObjectType; break; case QWebEngineUrlRequestInfo::ResourceTypeXhr: resourceType = ContentBlockingManager::XmlHttpRequestType; break; default: break; } const ContentBlockingManager::CheckResult result(ContentBlockingManager::checkUrl(contentBlockingProfiles, request.firstPartyUrl(), request.requestUrl(), resourceType)); if (result.isBlocked) { Console::addMessage(QCoreApplication::translate("main", "Blocked request"), Otter::NetworkMessageCategory, LogMessageLevel, request.requestUrl().toString(), -1); request.block(true); } }
void RequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo& info) { QUrl url = info.requestUrl(); qDebug() << "Request URL in interceptRequest: " << url; emit redirectUrlCapture(url); }