aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/urlinterceptor.cpp
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2017-12-11 18:16:50 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2017-12-11 18:16:50 +0100
commite25c011d5db32104ccdb3e8949082345efdba805 (patch)
treefb1892f8ae292a088638abaf158d086216106ad2 /src/webengine/urlinterceptor.cpp
parentFixed new window action (diff)
downloadsmolbote-e25c011d5db32104ccdb3e8949082345efdba805.tar.xz
Simple filterlist
Diffstat (limited to 'src/webengine/urlinterceptor.cpp')
-rw-r--r--src/webengine/urlinterceptor.cpp49
1 files changed, 37 insertions, 12 deletions
diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp
index 19bc64f..cbf1c86 100644
--- a/src/webengine/urlinterceptor.cpp
+++ b/src/webengine/urlinterceptor.cpp
@@ -19,31 +19,56 @@
******************************************************************************/
#include "urlinterceptor.h"
-#include "filter/filtercollection.h"
+#include <QFile>
+#include <QTextStream>
-UrlRequestInterceptor::UrlRequestInterceptor(QObject *parent) :
+UrlRequestInterceptor::UrlRequestInterceptor(const QString &path, QObject *parent) :
QWebEngineUrlRequestInterceptor(parent)
{
+#ifdef QT_DEBUG
+ qDebug("Reading request blocklist: %s", qUtf8Printable(path));
+#endif
+
+ int n_rules = 0;
+
+ QFile filter(path);
+ if(filter.open(QIODevice::ReadOnly | QIODevice::Text)) {
+ QTextStream out(&filter);
+ while(!out.atEnd()) {
+ const QString &line = out.readLine();
+ if(!line.startsWith('!')) {
+ FilterRule r(line);
+ // check if valid
+ m_rules.push_back(std::move(r));
+ ++n_rules;
+ }
+ }
+ filter.close();
+ }
+
+ qDebug("Added %i rules", n_rules);
}
void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
{
-#ifdef DEBUG_VERBOSE
- qDebug("%s -[%i]-> %s", qUtf8Printable(info.firstPartyUrl().toString()), info.resourceType(), qUtf8Printable(info.requestUrl().toString()));
-#endif
- for(auto s : m_manager->subscriptions()) {
- bool shouldBlock = s->match(info);
- if(shouldBlock) {
+ for(const FilterRule &test : m_rules) {
+ const QUrl &url = info.requestUrl();
+
+ if(test.shouldBlock(url)) {
info.block(true);
-#ifdef DEBUG_VERBOSE
- qDebug(">> blocked");
+
+#ifdef QT_DEBUG
+ qDebug("blocked [%s] %s", qUtf8Printable(info.firstPartyUrl().toString()), qUtf8Printable(url.toString()));
#endif
return;
}
}
}
-void UrlRequestInterceptor::setSubscription(BlockerManager *manager)
+bool shouldBlock(const QUrl &url, const QString &test)
{
- m_manager = manager;
+ if(url.toString().contains(test)) {
+ return true;
+ }
+ return false;
}