Пример #1
0
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);
      }
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);
  }
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);
	}
}