aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/urlinterceptor.cpp
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2017-12-23 16:45:31 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2017-12-23 16:45:31 +0100
commitf7ed63179fa4f99322d6c7716e17466ec4e3e4ce (patch)
treef187d1f73300f1ac0882a4be569d567bb4ed29f7 /src/webengine/urlinterceptor.cpp
parentRemoved SingleApplication::SessionParam (diff)
downloadsmolbote-f7ed63179fa4f99322d6c7716e17466ec4e3e4ce.tar.xz
Request filter now properly takes hostlists
- hostslist directory is set in browser.filterPath
Diffstat (limited to 'src/webengine/urlinterceptor.cpp')
-rw-r--r--src/webengine/urlinterceptor.cpp67
1 files changed, 41 insertions, 26 deletions
diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp
index 363d3ba..9598f36 100644
--- a/src/webengine/urlinterceptor.cpp
+++ b/src/webengine/urlinterceptor.cpp
@@ -7,6 +7,7 @@
*/
#include "urlinterceptor.h"
+#include <QDir>
#include <QFile>
#include <QTextStream>
@@ -17,46 +18,60 @@ UrlRequestInterceptor::UrlRequestInterceptor(const QString &path, QObject *paren
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();
+ QDir hostsD(path);
+ for(const QString &file : hostsD.entryList(QDir::Files)) {
+ qDebug("Parsing hosts.d/%s: %i", qUtf8Printable(file), parseHostfile(hostsD.absoluteFilePath(file)));
}
+}
- qDebug("Added %i rules", n_rules);
+UrlRequestInterceptor::~UrlRequestInterceptor()
+{
+ for(HostRule *r : m_rules) {
+ delete r;
+ }
}
void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
{
- for(const FilterRule &test : m_rules) {
- const QUrl &url = info.requestUrl();
+//#ifdef QT_DEBUG
+// qDebug("%s -> %s", qUtf8Printable(info.firstPartyUrl().toString()), qUtf8Printable(info.requestUrl().toString()));
+//#endif
- if(test.shouldBlock(url)) {
+ for(HostRule *test : m_rules) {
+ if(test->shouldBlock(info)) {
info.block(true);
-#ifdef QT_DEBUG
- qDebug("blocked [%s] %s", qUtf8Printable(info.firstPartyUrl().toString()), qUtf8Printable(url.toString()));
-#endif
+//#ifdef QT_DEBUG
+// qDebug("blocked on pattern %s", qUtf8Printable(test->pattern()));
+//#endif
return;
}
}
}
-bool shouldBlock(const QUrl &url, const QString &test)
+int UrlRequestInterceptor::parseHostfile(const QString &filename)
{
- if(url.toString().contains(test)) {
- return true;
+ int numRules = 0;
+ QFile file(filename);
+
+ // try to open the file
+ if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
+
+ // with a QTextStream we can read lines without getting linebreaks at the end
+ QTextStream out(&file);
+ while(!out.atEnd()) {
+ const QString &line = out.readLine().trimmed();
+
+ // skip comments and empty lines
+ if(!line.startsWith('#') && !line.isEmpty()) {
+ HostRule *r = new HostRule(line);
+ m_rules.push_back(r);
+ ++numRules;
+ }
+ }
+
+ // close once we're done with it
+ file.close();
}
- return false;
+ return numRules;
}