From 4cbf8607e94732cf4318451d397e0d416c0080b7 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Tue, 26 Dec 2017 16:29:21 +0100 Subject: UrlRequestInterceptor fixes - Using QHash to store HostRule's, so lookup should be faster - HostRule is now a struct --- src/webengine/urlinterceptor.cpp | 41 +++++++++++++++++++++++----------------- 1 file changed, 24 insertions(+), 17 deletions(-) (limited to 'src/webengine/urlinterceptor.cpp') diff --git a/src/webengine/urlinterceptor.cpp b/src/webengine/urlinterceptor.cpp index 2f3f04f..175643e 100644 --- a/src/webengine/urlinterceptor.cpp +++ b/src/webengine/urlinterceptor.cpp @@ -22,29 +22,20 @@ UrlRequestInterceptor::UrlRequestInterceptor(const QString &path, QObject *paren for(const QString &file : hostsD.entryList(QDir::Files)) { qDebug("Parsing hosts.d/%s: %i", qUtf8Printable(file), parseHostfile(hostsD.absoluteFilePath(file))); } + + qDebug("Total number of rules: %i", m_rules.count()); } UrlRequestInterceptor::~UrlRequestInterceptor() { - for(HostRule *r : m_rules) { - delete r; - } + qDeleteAll(m_rules); } void UrlRequestInterceptor::interceptRequest(QWebEngineUrlRequestInfo &info) { -//#ifdef QT_DEBUG -// qDebug("%s -> %s", qUtf8Printable(info.firstPartyUrl().toString()), qUtf8Printable(info.requestUrl().toString())); -//#endif - - for(HostRule *test : m_rules) { - if(test->shouldBlock(info)) { + if(m_rules.contains(info.requestUrl().host())) { + if(m_rules.value(info.requestUrl().host())->isBlocking) { info.block(true); - -//#ifdef QT_DEBUG -// qDebug("blocked on pattern %s", qUtf8Printable(test->pattern())); -//#endif - return; } } } @@ -64,9 +55,25 @@ int UrlRequestInterceptor::parseHostfile(const QString &filename) // skip comments and empty lines if(!line.startsWith('#') && !line.isEmpty()) { - HostRule *r = new HostRule(line); - m_rules.push_back(r); - ++numRules; + // format is + //0.0.0.0 host + QStringList parts = line.split(' '); + QString redirect = parts.at(0); + QString host = parts.at(1); + + 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)); + } } } -- cgit v1.2.1