aboutsummaryrefslogtreecommitdiff
path: root/src/webengine/urlinterceptor.cpp
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2018-04-08 14:52:40 +0200
committerAqua-sama <aqua@iserlohn-fortress.net>2018-04-08 14:52:40 +0200
commitbadceb8dfa8b54ff6da55e9a2188da53ad1aa8e8 (patch)
tree5332436d7e74723db680d7430adf3b483e3daaae /src/webengine/urlinterceptor.cpp
parentAdd FeatureSummary to CMakeLists (diff)
downloadsmolbote-badceb8dfa8b54ff6da55e9a2188da53ad1aa8e8.tar.xz
Multithreading UrlRequestInterceptor
- Add parse() free function to UrlRequestInterceptor - hostlists are loaded in parallel via QtConcurrent
Diffstat (limited to 'src/webengine/urlinterceptor.cpp')
-rw-r--r--src/webengine/urlinterceptor.cpp90
1 files changed, 45 insertions, 45 deletions
diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp
index 91919ea..e88e5b7 100644
--- a/src/webengine/urlinterceptor.cpp
+++ b/src/webengine/urlinterceptor.cpp
@@ -9,76 +9,76 @@
#include "urlinterceptor.h"
#include <QDir>
#include <QTextStream>
+#include <QtConcurrent>
UrlRequestInterceptor::UrlRequestInterceptor(const QString &path, QObject *parent)
: QWebEngineUrlRequestInterceptor(parent)
{
-#ifdef QT_DEBUG
- qDebug("Reading request blocklist: %s", qUtf8Printable(path));
-#endif
-
QDir hostsD(path);
const QStringList hostFiles = hostsD.entryList(QDir::Files);
for(const QString &file : hostFiles) {
- qDebug("Parsing hosts.d/%s: %i", qUtf8Printable(file), parseHostfile(hostsD.absoluteFilePath(file)));
- }
+ const QString absPath = hostsD.absoluteFilePath(file);
+ QtConcurrent::run([this, absPath]() {
+ auto r = parse(absPath);
+#ifdef QT_DEBUG
+ qDebug("Parsed %i rules from %s", r.count(), qUtf8Printable(absPath));
+#endif
- qDebug("Total number of rules: %i", m_rules.count());
+ rulesLock.lock();
+ for(const auto &k : r.keys()) {
+ if(rules.contains(k)) {
+ //
+ } else {
+ rules.insert(k, r.value(k));
+ }
+ }
+ rulesLock.unlock();
+ });
+ }
}
-UrlRequestInterceptor::~UrlRequestInterceptor()
-{
- qDeleteAll(m_rules);
-}
+UrlRequestInterceptor::~UrlRequestInterceptor() = default;
void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info)
{
- if(m_rules.contains(info.requestUrl().host())) {
- if(m_rules.value(info.requestUrl().host())->isBlocking) {
- info.block(true);
- }
+ rulesLock.lock();
+ if(rules.contains(info.requestUrl().host())) {
+ info.block(rules.value(info.requestUrl().host()).isBlocking);
}
+ rulesLock.unlock();
}
-int UrlRequestInterceptor::parseHostfile(const QString &filename)
+QHash<QString, UrlRequestInterceptor::HostRule> parse(const QString &filename)
{
- int numRules = 0;
- QFile file(filename);
+ QHash<QString, UrlRequestInterceptor::HostRule> rules;
- // try to open the file
- if(file.open(QIODevice::ReadOnly | QIODevice::Text)) {
+ QFile hostfile(filename);
+ if(hostfile.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();
+ QTextStream hostfile_stream(&hostfile);
+ while(!hostfile_stream.atEnd()) {
+ // read line and remove any whitespace at the end
+ const QString &line = hostfile_stream.readLine().trimmed();
// skip comments and empty lines
- if(!line.startsWith('#') && !line.isEmpty()) {
- // format is <redirect> <host>
- //0.0.0.0 host
- QStringList parts = line.split(' ');
- QString redirect = parts.at(0);
- QString host = parts.at(1);
+ // everything else should be a rule
+ // format is <redirect> <host>
+ // 0.0.0.0 hostname
+ const QStringList &parts = line.split(' ');
+ const QString &redirect = parts.at(0);
- if(m_rules.contains(host)) {
- qWarning("Duplicate rule %s", qUtf8Printable(line));
- }
-
- if(redirect == "0.0.0.0") {
- HostRule *rule = new HostRule;
- rule->isBlocking = true;
- m_rules.insert(host, rule);
-
- ++numRules;
- } else {
- qDebug("Ignoring rule %s", qUtf8Printable(line));
+ for(const QString &host : parts.mid(1)) {
+ if(!rules.contains(host)) {
+ UrlRequestInterceptor::HostRule rule{};
+ rule.isBlocking = redirect == "0.0.0.0";
+ rules.insert(host, rule);
}
}
}
- // close once we're done with it
- file.close();
+ hostfile.close();
}
- return numRules;
-}
+
+ return rules;
+}; \ No newline at end of file