diff options
-rw-r--r-- | CMakeLists.txt | 2 | ||||
-rw-r--r-- | lib/configuration/configuration.cpp | 1 | ||||
-rw-r--r-- | src/browser.cpp | 2 | ||||
-rw-r--r-- | src/webengine/urlinterceptor.cpp | 20 | ||||
-rw-r--r-- | src/webengine/urlinterceptor.h | 9 | ||||
-rw-r--r-- | test/CMakeLists.txt | 4 |
6 files changed, 31 insertions, 7 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 7e09b83..f0f48c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -86,7 +86,7 @@ add_subdirectory(src) if (Tests) enable_testing() find_package(Qt5 COMPONENTS Test REQUIRED) - add_subdirectory(test) + #add_subdirectory(test) endif() message("Version='${VerInfo}' bookmark='${VerBookmark}' commit='${VerCommit}'") diff --git a/lib/configuration/configuration.cpp b/lib/configuration/configuration.cpp index 5da7fb4..70be7b1 100644 --- a/lib/configuration/configuration.cpp +++ b/lib/configuration/configuration.cpp @@ -96,6 +96,7 @@ Configuration::Configuration(QObject *parent) // Filter settings ("filter.path", po::value<std::string>()->default_value(filter_path)) + ("filter.header", po::value<std::vector<std::string>>()) // ("filter.cookies.block.all", po::value<bool>()->default_value(false)) // ("filter.cookies.block.thirdParty", po::value<bool>()->default_value(true)) // ("filter.cookies.path", po::value<std::string>()->default_value("~/.config/smolbote/cookies.d")) 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<DownloadsWidget>(m_config->value<QString>("downloads.path").value()); // url request filter - m_urlFilter = std::make_unique<UrlRequestInterceptor>(m_config->value<QString>("filter.path").value()); + m_urlFilter = std::make_unique<UrlRequestInterceptor>(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 <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); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 29c95ff..ad2ca1f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -8,10 +8,10 @@ macro(create_test testname) autotests.qrc ${ARGN}) - target_include_directories(${testname} PRIVATE ../src) + target_include_directories(${testname} PRIVATE ../lib PRIVATE ../src) target_link_libraries(${testname} Qt5::Test Qt5::Concurrent Qt5::WebEngineWidgets) add_test(NAME smolbote-${testname} COMMAND ${testname}) endmacro() -create_test(HostlistTest ../src/webengine/urlinterceptor.cpp ../src/webengine/urlinterceptor.h)
\ No newline at end of file +create_test(HostlistTest ../src/webengine/urlinterceptor.cpp ../src/webengine/urlinterceptor.h) |