aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2017-12-23 16:45:31 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2017-12-23 16:45:31 +0100
commitf7ed63179fa4f99322d6c7716e17466ec4e3e4ce (patch)
treef187d1f73300f1ac0882a4be569d567bb4ed29f7 /lib
parentRemoved SingleApplication::SessionParam (diff)
downloadsmolbote-f7ed63179fa4f99322d6c7716e17466ec4e3e4ce.tar.xz
Request filter now properly takes hostlists
- hostslist directory is set in browser.filterPath
Diffstat (limited to 'lib')
-rw-r--r--lib/adblock/adblock.qbs37
-rw-r--r--lib/adblock/filterrule.cpp90
-rw-r--r--lib/adblock/filterrule.h50
-rw-r--r--lib/adblock/test/filtertest.cpp71
-rw-r--r--lib/adblock/test/filtertest.h36
-rw-r--r--lib/adblock/test/main.cpp12
6 files changed, 0 insertions, 296 deletions
diff --git a/lib/adblock/adblock.qbs b/lib/adblock/adblock.qbs
deleted file mode 100644
index e4df779..0000000
--- a/lib/adblock/adblock.qbs
+++ /dev/null
@@ -1,37 +0,0 @@
-import qbs 1.0
-
-Project {
- name: "Filter"
-
- StaticLibrary {
- name: "adblock"
-
- Depends { name: "cpp" }
- Depends {
- name: "Qt"
- submodules: ["core"]
- }
-
- files: [
- "filterrule.h",
- "filterrule.cpp",
- ]
- }
-
- CppApplication {
- name: "adblock-test"
-
- Depends {
- name: "Qt"
- submodules: ["core", "test"]
- }
-
- Depends { name: "adblock" }
-
- files: [
- "test/main.cpp",
- "test/filtertest.cpp",
- "test/filtertest.h",
- ]
- }
-}
diff --git a/lib/adblock/filterrule.cpp b/lib/adblock/filterrule.cpp
deleted file mode 100644
index 79ded6d..0000000
--- a/lib/adblock/filterrule.cpp
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * This file is part of smolbote. It's copyrighted by the contributors recorded
- * in the version control history of the file, available from its original
- * location: git://neueland.iserlohn-fortress.net/smolbote.git
- *
- * SPDX-License-Identifier: GPL-3.0
- */
-
-#include "filterrule.h"
-#include <QUrl>
-
-/* TODO
- * - split this class into two: FilterRule that only deals with matching, and AdBlockRule, which only deals with parsing
- */
-
-/* The AdBlock standard is an incredible mess, vaguely explaiend on:
- * - https://adblockplus.org/filters
- * - https://adblockplus.org/filter-cheatsheet
- */
-
-FilterRule::FilterRule(const QString &line)
-{
- valid = parse(line);
-}
-
-FilterRule::~FilterRule()
-{
-}
-
-bool FilterRule::isValid() const
-{
- return valid;
-}
-
-bool FilterRule::isException() const
-{
- return exception;
-}
-
-bool FilterRule::shouldBlock(const QUrl &requestUrl) const
-{
- QRegularExpressionMatch match = rule.match(requestUrl.toString());
- return match.hasMatch();
-}
-
-bool FilterRule::parse(const QString &line)
-{
- // skip for comments and empty rules
- if(line.startsWith('!') || line.trimmed().isEmpty()) {
- return false;
- }
-
- // make a copy of the pattern so we can snap off the parts we've already parsed
- QString pattern = line;
-
- if(pattern.startsWith("@@")) {
- exception = true;
- pattern = pattern.mid(2); // remove @@
- }
-
- rule.setPattern(createRegExpPattern(pattern));
- return true;
-}
-
-QString createRegExpPattern(const QString &line)
-{
- QString pattern = line;
-
- // replace . (any character) with \. (a dot)
- pattern.replace('.', "\\.");
-
- // translate adblock special characters into regex
- // replace wildcard (*) with '.*' (zero or more of any element)
- pattern.replace('*', ".*");
-
- // replace separator (^) with '($|\?|\/)' (end of string, or ?, or /, or :number)
- pattern.replace('^', "($|\\?|\\/|:\\d+)");
-
- // replace || with ^\w+://([\w,\d,\.]+)?
- pattern.replace("||", "^\\w+://([\\w,\\d,\\.]+)?");
-
- if(pattern.startsWith('|') && pattern.endsWith('|')) {
- // replace | at start with ^ (start of string)
- pattern.replace(0, 1, '^');
- // replace | at end with $ (end of string)
- pattern.replace('|', '$');
- }
-
- return pattern;
-}
diff --git a/lib/adblock/filterrule.h b/lib/adblock/filterrule.h
deleted file mode 100644
index 084bd8c..0000000
--- a/lib/adblock/filterrule.h
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * This file is part of smolbote. It's copyrighted by the contributors recorded
- * in the version control history of the file, available from its original
- * location: git://neueland.iserlohn-fortress.net/smolbote.git
- *
- * SPDX-License-Identifier: GPL-3.0
- */
-
-#ifndef FILTERRULE_H
-#define FILTERRULE_H
-
-#include <QRegularExpression>
-
-class QUrl;
-class FilterRule
-{
-public:
-
- FilterRule(const QString &line);
-
- // delete the copy constructor and assignment operator
- FilterRule(const FilterRule&) = delete;
- FilterRule& operator=(const FilterRule&) = delete;
-
- // move constructor
- FilterRule(FilterRule&& other) {
- valid = other.valid;
- exception = other.exception;
- rule = other.rule;
- }
-
- ~FilterRule();
-
- bool isValid() const;
- bool isException() const;
- bool shouldBlock(const QUrl &requestUrl) const;
-
-private:
- bool parse(const QString &line);
-
- bool valid;
- bool exception;
-
- QRegularExpression rule;
-
-};
-
-QString createRegExpPattern(const QString &line);
-
-#endif // FILTERRULE_H
diff --git a/lib/adblock/test/filtertest.cpp b/lib/adblock/test/filtertest.cpp
deleted file mode 100644
index 76e607b..0000000
--- a/lib/adblock/test/filtertest.cpp
+++ /dev/null
@@ -1,71 +0,0 @@
-/*
- * This file is part of smolbote. It's copyrighted by the contributors recorded
- * in the version control history of the file, available from its original
- * location: git://neueland.iserlohn-fortress.net/smolbote.git
- *
- * SPDX-License-Identifier: GPL-3.0
- */
-
-#include "filtertest.h"
-#include <QtTest/QtTest>
-#include "../filterrule.h"
-#include <QUrl>
-
-FilterTest::FilterTest(QObject *parent) : QObject(parent)
-{
-}
-
-void FilterTest::initTestCase()
-{
- addressRule = new FilterRule("/banner/*/img^");
- QVERIFY(addressRule->isValid() == true);
- QVERIFY(addressRule->isException() == false);
-
- domainRule = new FilterRule("||ads.example.com^");
- QVERIFY(domainRule->isValid() == true);
- QVERIFY(domainRule->isException() == false);
-
- exactAddressRule = new FilterRule("|http://example.com/|");
- QVERIFY(exactAddressRule->isValid() == true);
- QVERIFY(exactAddressRule->isException() == false);
-}
-
-void FilterTest::testAddressBlock()
-{
- // This rule blocks:
- QVERIFY(addressRule->shouldBlock(QUrl("http://example.com/banner/foo/img")) == true);
- QVERIFY(addressRule->shouldBlock(QUrl("http://example.com/banner/foo/bar/img?param")) == true);
- QVERIFY(addressRule->shouldBlock(QUrl("http://example.com/banner//img/foo")) == true);
-
- // This rule doesn't block:
- QVERIFY(addressRule->shouldBlock(QUrl("http://example.com/banner/img")) == false);
- QVERIFY(addressRule->shouldBlock(QUrl("http://example.com/banner/foo/imgraph")) == false);
- QVERIFY(addressRule->shouldBlock(QUrl("http://example.com/banner/foo/img.gif")) == false);
-}
-
-void FilterTest::testDomainBlock()
-{
- // This rule blocks:
- QVERIFY(domainRule->shouldBlock(QUrl("http://ads.example.com/foo.gif")) == true);
- QVERIFY(domainRule->shouldBlock(QUrl("http://server1.ads.example.com/foo.gif")) == true);
- QVERIFY(domainRule->shouldBlock(QUrl("https://ads.example.com:8000/")) == true);
-
- // This rule doesn't block:
- QVERIFY(domainRule->shouldBlock(QUrl("http://ads.example.com.ua/foo.gif")) == false);
- QVERIFY(domainRule->shouldBlock(QUrl("http://example.com/redirect/http://ads.example.com/")) == false);
-}
-
-void FilterTest::testExactAddressBlock()
-{
- // This rule blocks:
- QVERIFY(exactAddressRule->shouldBlock(QUrl("http://example.com/")) == true);
-
- // This rule doesn't block:
- QVERIFY(exactAddressRule->shouldBlock(QUrl("http://example.com/foo.gif")) == false);
- QVERIFY(exactAddressRule->shouldBlock(QUrl("http://example.info/redirect/http://example.com/")) == false);
-}
-
-void FilterTest::cleanupTestCase()
-{
- delete addressRule;
-}
diff --git a/lib/adblock/test/filtertest.h b/lib/adblock/test/filtertest.h
deleted file mode 100644
index f91c3dd..0000000
--- a/lib/adblock/test/filtertest.h
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * This file is part of smolbote. It's copyrighted by the contributors recorded
- * in the version control history of the file, available from its original
- * location: git://neueland.iserlohn-fortress.net/smolbote.git
- *
- * SPDX-License-Identifier: GPL-3.0
- */
-
-#ifndef FILTERTEST_H
-#define FILTERTEST_H
-
-#include <QObject>
-
-class FilterRule;
-class FilterTest : public QObject
-{
- Q_OBJECT
-public:
- explicit FilterTest(QObject *parent = nullptr);
-
-signals:
-
-private slots:
- void initTestCase();
- void testAddressBlock();
- void testDomainBlock();
- void testExactAddressBlock();
- void cleanupTestCase();
-
-private:
- FilterRule *addressRule;
- FilterRule *domainRule;
- FilterRule *exactAddressRule;
-};
-
-#endif // FILTERTEST_H
diff --git a/lib/adblock/test/main.cpp b/lib/adblock/test/main.cpp
deleted file mode 100644
index b8c9dca..0000000
--- a/lib/adblock/test/main.cpp
+++ /dev/null
@@ -1,12 +0,0 @@
-/*
- * This file is part of smolbote. It's copyrighted by the contributors recorded
- * in the version control history of the file, available from its original
- * location: git://neueland.iserlohn-fortress.net/smolbote.git
- *
- * SPDX-License-Identifier: GPL-3.0
- */
-
-#include <QtTest/QtTest>
-#include "filtertest.h"
-
-QTEST_APPLESS_MAIN(FilterTest)