From 0772cf8b98387b2b641ae29aeb1b459eef22d794 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Wed, 22 Mar 2017 22:59:07 +0100 Subject: Blocker fixes --- src/blocker/blockersubscription.cpp | 109 ++++++++++++++++++++++++++---------- 1 file changed, 80 insertions(+), 29 deletions(-) (limited to 'src/blocker/blockersubscription.cpp') diff --git a/src/blocker/blockersubscription.cpp b/src/blocker/blockersubscription.cpp index 02def48..86cb0b5 100644 --- a/src/blocker/blockersubscription.cpp +++ b/src/blocker/blockersubscription.cpp @@ -22,35 +22,32 @@ #include "ui_subscriptionform.h" #include "browser.h" -#include #include #include -BlockerSubscription::BlockerSubscription(const QString url, QWidget *parent) : +BlockerSubscription::BlockerSubscription(const QUrl url, QWidget *parent) : QWidget(parent), ui(new Ui::SubscriptionForm) { ui->setupUi(this); - QUrl _url = QUrl::fromUserInput(url); - m_name = _url.fileName(); - - if(!sSettings->value("blocker.path").toString().isEmpty()) { - QString cacheName = sSettings->value("blocker.path").toString() + m_name; - QFile *cache = new QFile(cacheName); - if(cache->exists()) { - load(cache); - } - + m_name = url.fileName(); + m_url = url; + + QString subPath = sSettings->value("blocker.path").toString() + m_name; + qDebug("Adding subscription [%s]", qUtf8Printable(subPath)); + QFile *sub = new QFile(subPath); + if(sub->exists()) { + sub->open(QIODevice::ReadOnly); + load(sub); } else { - - // no cache path specified - pull the subscription - QNetworkRequest request; - request.setUrl(QUrl::fromUserInput(url)); - - QNetworkReply *reply = sNetwork->get(request); - connect(reply, &QNetworkReply::finished, [this, reply]() { - this->load(reply); - }); + if(!url.scheme().startsWith("http")) { + qWarning("Invalid url, subscription cannot be updated"); + sub->deleteLater(); + return; + } + qDebug("Subscription doesn't exist, updating..."); + sub->open(QIODevice::ReadWrite); + update(sub); } } @@ -64,14 +61,69 @@ QString BlockerSubscription::name() const return m_name; } +/** + * Check if a URL request should be blocked or not + * @param info + * @return true if it should be blocked; false otherwise + */ +BlockerSubscription::MatchResult BlockerSubscription::match(QWebEngineUrlRequestInfo &info) +{ + MatchResult result; + + for(BlockerRule* rule : m_urlWhitelist) { + if(rule->match(info)) { + // this request is whitelisted + result.match = true; + result.block = false; + result.pattern = rule->filter(); + return result; + } + } + + // request is not in the whitelist + for(BlockerRule* rule : m_urlBlacklist) { + if(rule->match(info)) { + // this request is blacklisted + result.match = true; + result.block = true; + result.pattern = rule->filter(); + return result; + } + } + + // request matches neither whitelist nor blacklist + result.match = false; + result.block = false; + return result; +} + +void BlockerSubscription::update(QFile *cache) +{ + // no cache path specified - pull the subscription + QNetworkRequest request; + request.setUrl(m_url); + + QNetworkReply *reply = sNetwork->get(request); + connect(reply, &QNetworkReply::readyRead, [this, reply, cache]() { + cache->write(reply->readAll()); + }); + connect(reply, &QNetworkReply::finished, [this, reply, cache]() { + cache->write(reply->readAll()); + cache->flush(); + cache->seek(0); + reply->deleteLater(); + qDebug("Subscription updated: [%s]", qUtf8Printable(this->m_name)); + this->load(cache); + }); +} + void BlockerSubscription::load(QIODevice *dev) { QTextStream subscription(dev); QString header = subscription.readLine(); if(!header.startsWith("[Adblock Plus")) { - qDebug("Invalid format of subscription: %s", qUtf8Printable(m_name)); - return; + qWarning("Invalid format of subscription: %s", qUtf8Printable(m_name)); } // clear all lists @@ -85,24 +137,23 @@ void BlockerSubscription::load(QIODevice *dev) if(line.startsWith('!')) { parseComment(line); } else { - // The line is not a comment - + // The line is not empty or a comment rules++; BlockerRule *rule = new BlockerRule(line, this); if(rule->isValid()) { if(rule->isException()) { m_urlWhitelist.append(rule); - ui->whitelist_listWidget->addItem(rule->toString()); + ui->whitelist_listWidget->addItem(rule->filter()); } else { - ui->blacklist_listWidget->addItem(rule->toString()); + ui->blacklist_listWidget->addItem(rule->filter()); m_urlBlacklist.append(rule); } } } - } - } + } // line.isEmpty + } // subscription.atEnd() qDebug("Loaded %i/%i rules from subscription %s", m_urlBlacklist.count() + m_urlWhitelist.count(), rules, qUtf8Printable(m_name)); dev->deleteLater(); -- cgit v1.2.1