diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-04-08 14:52:40 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-04-08 14:52:40 +0200 |
commit | badceb8dfa8b54ff6da55e9a2188da53ad1aa8e8 (patch) | |
tree | 5332436d7e74723db680d7430adf3b483e3daaae /src | |
parent | Add FeatureSummary to CMakeLists (diff) | |
download | smolbote-badceb8dfa8b54ff6da55e9a2188da53ad1aa8e8.tar.xz |
Multithreading UrlRequestInterceptor
- Add parse() free function to UrlRequestInterceptor
- hostlists are loaded in parallel via QtConcurrent
Diffstat (limited to 'src')
-rw-r--r-- | src/webengine/urlinterceptor.cpp | 90 | ||||
-rw-r--r-- | src/webengine/urlinterceptor.h | 13 |
2 files changed, 52 insertions, 51 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 diff --git a/src/webengine/urlinterceptor.h b/src/webengine/urlinterceptor.h index b34d9ff..af15f99 100644 --- a/src/webengine/urlinterceptor.h +++ b/src/webengine/urlinterceptor.h @@ -10,6 +10,7 @@ #define URLREQUESTINTERCEPTOR_H #include <QWebEngineUrlRequestInterceptor> +#include <QMutex> class UrlRequestInterceptor : public QWebEngineUrlRequestInterceptor { @@ -20,15 +21,15 @@ public: }; explicit UrlRequestInterceptor(const QString &path, QObject *parent = nullptr); - ~UrlRequestInterceptor(); + ~UrlRequestInterceptor() override; - void interceptRequest(QWebEngineUrlRequestInfo &info); - -public slots: - int parseHostfile(const QString &filename); + void interceptRequest(QWebEngineUrlRequestInfo &info) override; private: - QHash<QString, HostRule *> m_rules; + QHash<QString, HostRule> rules; + QMutex rulesLock; }; +QHash<QString, UrlRequestInterceptor::HostRule> parse(const QString &filename); + #endif // URLREQUESTINTERCEPTOR_H |