aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAqua-sama <aqua@iserlohn-fortress.net>2017-12-26 16:29:21 +0100
committerAqua-sama <aqua@iserlohn-fortress.net>2017-12-26 16:29:21 +0100
commit4cbf8607e94732cf4318451d397e0d416c0080b7 (patch)
tree6c37e9380186f1d60f592ae9131a3605d42941e0
parentSettingsDialog patches (diff)
downloadsmolbote-4cbf8607e94732cf4318451d397e0d416c0080b7.tar.xz
UrlRequestInterceptor fixes
- Using QHash to store HostRule's, so lookup should be faster - HostRule is now a struct
-rw-r--r--BUGS.md3
-rw-r--r--smolbote.qbs2
-rw-r--r--src/webengine/hostrule.cpp47
-rw-r--r--src/webengine/hostrule.h31
-rw-r--r--src/webengine/urlinterceptor.cpp41
-rw-r--r--src/webengine/urlinterceptor.h9
6 files changed, 31 insertions, 102 deletions
diff --git a/BUGS.md b/BUGS.md
index a14c19e..a3e3239 100644
--- a/BUGS.md
+++ b/BUGS.md
@@ -23,8 +23,7 @@ List of things to do before 1.0 release
### Request filter
- How are multiple IPs per hostname to be treated?
-- match host names against QHash
-- review code
+- Multiple hostnames on a line get ignored after the first one
### No Script
- there's already a profile setting to disable scripts
diff --git a/smolbote.qbs b/smolbote.qbs
index e9f112f..406874d 100644
--- a/smolbote.qbs
+++ b/smolbote.qbs
@@ -119,8 +119,6 @@ Project {
Group {
name: "Request Filter"
files: [
- "src/webengine/hostrule.cpp",
- "src/webengine/hostrule.h",
"src/webengine/urlinterceptor.cpp",
"src/webengine/urlinterceptor.h",
]
diff --git a/src/webengine/hostrule.cpp b/src/webengine/hostrule.cpp
deleted file mode 100644
index 1605b78..0000000
--- a/src/webengine/hostrule.cpp
+++ /dev/null
@@ -1,47 +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 "hostrule.h"
-
-HostRule::HostRule(const QString &line)
-{
- valid = parse(line);
-}
-
-QString HostRule::pattern() const
-{
- return domain;
-}
-
-bool HostRule::shouldBlock(const QWebEngineUrlRequestInfo &info) const
-{
- if(info.requestUrl().host().contains(domain)) {
- return true;
- }
- return false;
-}
-
-bool HostRule::parse(const QString &line)
-{
- QStringList parts = line.split(' ');
- if(parts.first() == "0.0.0.0") {
- blocking = true;
- } else {
- qDebug("Skipping rule: '%s'", qUtf8Printable(line));
- }
-
- domain = parts.last();
-
-#ifdef QT_DEBUG
- if(domain.isEmpty()) {
- qWarning("error parsing %s", qUtf8Printable(line));
- }
-#endif
-
- return true;
-}
diff --git a/src/webengine/hostrule.h b/src/webengine/hostrule.h
deleted file mode 100644
index d1289a0..0000000
--- a/src/webengine/hostrule.h
+++ /dev/null
@@ -1,31 +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 HOSTRULE_H
-#define HOSTRULE_H
-
-#include <QString>
-#include <QWebEngineUrlRequestInfo>
-
-class HostRule
-{
-public:
- HostRule(const QString &line);
- QString pattern() const;
- bool shouldBlock(const QWebEngineUrlRequestInfo &info) const;
-
-private:
- bool valid = false;
-
- bool parse(const QString &line);
-
- QString domain;
- bool blocking;
-};
-
-#endif // HOSTRULE_H
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 <redirect> <host>
+ //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));
+ }
}
}
diff --git a/src/webengine/urlinterceptor.h b/src/webengine/urlinterceptor.h
index 95e96e2..af0bf35 100644
--- a/src/webengine/urlinterceptor.h
+++ b/src/webengine/urlinterceptor.h
@@ -10,13 +10,16 @@
#define URLREQUESTINTERCEPTOR_H
#include <QWebEngineUrlRequestInterceptor>
-#include <vector>
-#include "hostrule.h"
class UrlRequestInterceptor : public QWebEngineUrlRequestInterceptor
{
Q_OBJECT
public:
+
+ struct HostRule {
+ bool isBlocking;
+ };
+
explicit UrlRequestInterceptor(const QString &path, QObject *parent = nullptr);
~UrlRequestInterceptor();
@@ -26,7 +29,7 @@ public slots:
int parseHostfile(const QString &filename);
private:
- std::vector<HostRule*> m_rules;
+ QHash<QString, HostRule*> m_rules;
};
#endif // URLREQUESTINTERCEPTOR_H