diff options
Diffstat (limited to 'src/webengine')
-rw-r--r-- | src/webengine/urlinterceptor.cpp | 20 | ||||
-rw-r--r-- | src/webengine/urlinterceptor.h | 9 |
2 files changed, 26 insertions, 3 deletions
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 <QDir> #include <QTextStream> +#include <configuration/configuration.h> +#include <boost/algorithm/string.hpp> -UrlRequestInterceptor::UrlRequestInterceptor(const QString &path, QObject *parent) +UrlRequestInterceptor::UrlRequestInterceptor(const std::unique_ptr<Configuration> &config, QObject *parent) : QWebEngineUrlRequestInterceptor(parent) { - QDir hostsD(path); + QDir hostsD(config->value<QString>("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<std::vector<std::string>>("filter.header"); + if(header) { + for(const std::string &h : header.value()) { + std::vector<std::string> 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 <QWebEngineUrlRequestInterceptor> +#include <memory> +#include <QVector> +#include <QByteArray> +typedef std::pair<std::string, std::string> 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<Configuration> &config, QObject *parent = nullptr); ~UrlRequestInterceptor() = default; void interceptRequest(QWebEngineUrlRequestInfo &info) override; private: QHash<QString, HostRule> rules; + std::vector<Header> m_headers; }; QHash<QString, UrlRequestInterceptor::HostRule> parse(const QString &filename); |