From badceb8dfa8b54ff6da55e9a2188da53ad1aa8e8 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Sun, 8 Apr 2018 14:52:40 +0200 Subject: Multithreading UrlRequestInterceptor - Add parse() free function to UrlRequestInterceptor - hostlists are loaded in parallel via QtConcurrent --- src/webengine/urlinterceptor.cpp | 90 ++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 45 deletions(-) (limited to 'src/webengine/urlinterceptor.cpp') 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 #include +#include 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 parse(const QString &filename) { - int numRules = 0; - QFile file(filename); + QHash 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 - //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 + // 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 -- cgit v1.2.1