2013-11-02 17:43:18 +01:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2014-01-11 16:11:42 +01:00
|
|
|
* Copyright (C) 2013-2014 David Rosca <nowrep@gmail.com>
|
2013-11-02 17:43:18 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
|
|
|
#include "adblocksearchtree.h"
|
|
|
|
#include "adblockrule.h"
|
|
|
|
|
|
|
|
AdBlockSearchTree::AdBlockSearchTree()
|
|
|
|
: m_root(new Node)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-11-03 14:29:23 +01:00
|
|
|
AdBlockSearchTree::~AdBlockSearchTree()
|
|
|
|
{
|
|
|
|
deleteNode(m_root);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdBlockSearchTree::clear()
|
|
|
|
{
|
|
|
|
deleteNode(m_root);
|
|
|
|
m_root = new Node;
|
|
|
|
}
|
|
|
|
|
2013-11-02 17:43:18 +01:00
|
|
|
bool AdBlockSearchTree::add(const AdBlockRule* rule)
|
|
|
|
{
|
|
|
|
if (rule->m_type != AdBlockRule::StringContainsMatchRule) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-30 13:43:48 +01:00
|
|
|
const QString filter = rule->m_matchString;
|
2013-11-02 17:43:18 +01:00
|
|
|
int len = filter.size();
|
|
|
|
|
|
|
|
if (len <= 0) {
|
|
|
|
qDebug() << "AdBlockSearchTree: Inserting rule with filter len <= 0!";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Node* node = m_root;
|
|
|
|
|
|
|
|
for (int i = 0; i < len; ++i) {
|
2013-12-30 13:43:48 +01:00
|
|
|
const QChar c = filter.at(i);
|
2013-11-03 14:22:27 +01:00
|
|
|
if (!node->children.contains(c)) {
|
2013-11-02 17:43:18 +01:00
|
|
|
Node* n = new Node;
|
|
|
|
n->c = c;
|
|
|
|
|
2013-11-03 14:22:27 +01:00
|
|
|
node->children[c] = n;
|
2013-11-02 17:43:18 +01:00
|
|
|
}
|
|
|
|
|
2013-11-03 14:22:27 +01:00
|
|
|
node = node->children[c];
|
2013-11-02 17:43:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
node->rule = rule;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-03 14:49:58 +01:00
|
|
|
const AdBlockRule* AdBlockSearchTree::find(const QNetworkRequest &request, const QString &domain, const QString &urlString) const
|
2013-11-02 17:43:18 +01:00
|
|
|
{
|
2013-11-03 14:49:58 +01:00
|
|
|
int len = urlString.size();
|
2013-11-02 17:43:18 +01:00
|
|
|
|
|
|
|
if (len <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-03 14:49:58 +01:00
|
|
|
const QChar* string = urlString.constData();
|
|
|
|
|
2013-11-02 17:43:18 +01:00
|
|
|
for (int i = 0; i < len; ++i) {
|
2013-11-03 14:49:58 +01:00
|
|
|
const AdBlockRule* rule = prefixSearch(request, domain, urlString, string++, len - i);
|
2013-11-02 17:43:18 +01:00
|
|
|
if (rule) {
|
|
|
|
return rule;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-03 14:49:58 +01:00
|
|
|
const AdBlockRule* AdBlockSearchTree::prefixSearch(const QNetworkRequest &request, const QString &domain, const QString &urlString, const QChar* string, int len) const
|
2013-11-02 17:43:18 +01:00
|
|
|
{
|
|
|
|
if (len <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-03 14:49:58 +01:00
|
|
|
QChar c = string[0];
|
2013-11-02 17:43:18 +01:00
|
|
|
|
2013-11-03 14:22:27 +01:00
|
|
|
if (!m_root->children.contains(c)) {
|
2013-11-02 17:43:18 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-03 14:22:27 +01:00
|
|
|
Node* node = m_root->children[c];
|
2013-11-02 17:43:18 +01:00
|
|
|
|
|
|
|
for (int i = 1; i < len; ++i) {
|
2013-12-30 13:43:48 +01:00
|
|
|
const QChar c = (++string)[0];
|
2013-11-02 17:43:18 +01:00
|
|
|
|
|
|
|
if (node->rule && node->rule->networkMatch(request, domain, urlString)) {
|
|
|
|
return node->rule;
|
|
|
|
}
|
|
|
|
|
2013-11-03 14:22:27 +01:00
|
|
|
if (!node->children.contains(c)) {
|
2013-11-02 17:43:18 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-03 14:22:27 +01:00
|
|
|
node = node->children[c];
|
2013-11-02 17:43:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (node->rule && node->rule->networkMatch(request, domain, urlString)) {
|
|
|
|
return node->rule;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AdBlockSearchTree::deleteNode(AdBlockSearchTree::Node* node)
|
|
|
|
{
|
|
|
|
if (!node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-03 14:22:27 +01:00
|
|
|
QHashIterator<QChar, Node*> i(node->children);
|
2013-11-02 17:43:18 +01:00
|
|
|
while (i.hasNext()) {
|
|
|
|
i.next();
|
|
|
|
deleteNode(i.value());
|
|
|
|
}
|
|
|
|
|
|
|
|
delete node;
|
|
|
|
}
|