From a6bd5a227ecccba76d2486c5a4d6408770e1bcec Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 5 Jul 2018 12:13:08 +0200 Subject: Add filter.header --- src/browser.cpp | 2 +- src/webengine/urlinterceptor.cpp | 20 ++++++++++++++++++-- src/webengine/urlinterceptor.h | 9 ++++++++- 3 files changed, 27 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/browser.cpp b/src/browser.cpp index f056525..29eaea5 100644 --- a/src/browser.cpp +++ b/src/browser.cpp @@ -102,7 +102,7 @@ void Browser::setup(const QString &defaultProfile) // downloads m_downloads = std::make_unique(m_config->value("downloads.path").value()); // url request filter - m_urlFilter = std::make_unique(m_config->value("filter.path").value()); + m_urlFilter = std::make_unique(m_config); // cookie request filter // load profiles diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp index 4fc23e0..70d7701 100644 --- a/src/webengine/urlinterceptor.cpp +++ b/src/webengine/urlinterceptor.cpp @@ -9,11 +9,13 @@ #include "urlinterceptor.h" #include #include +#include +#include -UrlRequestInterceptor::UrlRequestInterceptor(const QString &path, QObject *parent) +UrlRequestInterceptor::UrlRequestInterceptor(const std::unique_ptr &config, QObject *parent) : QWebEngineUrlRequestInterceptor(parent) { - QDir hostsD(path); + QDir hostsD(config->value("filter.path").value()); const QStringList hostFiles = hostsD.entryList(QDir::Files); for(const QString &file : hostFiles) { const QString absPath = hostsD.absoluteFilePath(file); @@ -24,10 +26,24 @@ UrlRequestInterceptor::UrlRequestInterceptor(const QString &path, QObject *paren rules.unite(r); } + + const auto header = config->value>("filter.header"); + if(header) { + for(const std::string &h : header.value()) { + std::vector s; + boost::split(s, h, boost::is_any_of(":=")); + auto pair = std::make_pair(s.at(0), s.at(1)); + m_headers.emplace_back(pair); + } + } } void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) { + for(const Header &header : m_headers) { + info.setHttpHeader(QByteArray::fromStdString(header.first), QByteArray::fromStdString(header.second)); + } + if(rules.contains(info.requestUrl().host())) { info.block(rules.value(info.requestUrl().host()).isBlocking); } diff --git a/src/webengine/urlinterceptor.h b/src/webengine/urlinterceptor.h index 2a5c50d..2f91e30 100644 --- a/src/webengine/urlinterceptor.h +++ b/src/webengine/urlinterceptor.h @@ -10,7 +10,13 @@ #define URLREQUESTINTERCEPTOR_H #include +#include +#include +#include +typedef std::pair Header; + +class Configuration; class UrlRequestInterceptor : public QWebEngineUrlRequestInterceptor { Q_OBJECT @@ -19,13 +25,14 @@ public: bool isBlocking; }; - explicit UrlRequestInterceptor(const QString &path, QObject *parent = nullptr); + explicit UrlRequestInterceptor(const std::unique_ptr &config, QObject *parent = nullptr); ~UrlRequestInterceptor() = default; void interceptRequest(QWebEngineUrlRequestInfo &info) override; private: QHash rules; + std::vector
m_headers; }; QHash parse(const QString &filename); -- cgit v1.2.1