diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-10-18 14:40:32 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2018-10-18 14:40:32 +0200 |
commit | 9ab6f6d363f873f0799982c3c0872d38a656ee84 (patch) | |
tree | bf7fba76f36be8d6560fa5914404ae4fb44711ad /lib/urlfilter/domain.cpp | |
parent | buildconfig: don't overwrite install prefix by default (diff) | |
download | smolbote-9ab6f6d363f873f0799982c3c0872d38a656ee84.tar.xz |
urlfilter: add Domain class
(#6): Add domain matcher class
Diffstat (limited to 'lib/urlfilter/domain.cpp')
-rw-r--r-- | lib/urlfilter/domain.cpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/lib/urlfilter/domain.cpp b/lib/urlfilter/domain.cpp new file mode 100644 index 0000000..3686210 --- /dev/null +++ b/lib/urlfilter/domain.cpp @@ -0,0 +1,61 @@ +/* + * 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: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote + * + * SPDX-License-Identifier: GPL-3.0 + */ + +#include "domain.h" + +Domain::Domain(const QString& domain) + : m_domain(domain) + , m_hash(qHash(domain, 0)) +{ +} + +Domain::Domain(Domain && other) + : m_domain(std::move(other.m_domain)) + , m_hash(std::move(other.m_hash)) +{ +} + +Domain & Domain::operator=(Domain && other) +{ + m_domain = std::move(other.m_domain); + m_hash = other.m_hash; + return *this; +} + +bool Domain::matches(const QUrl& url) const +{ + const QString domain = url.host(); + + // domain and filter are the same + if(domain == m_domain) { + return true; + } + + // domain cannot be matched if it doesn't end with filter + // ex. example2.com isn't matched by example.com + if(!domain.endsWith(m_domain)) { + return false; + } + + // match with subdomains + // ex. subdomain.example.com is matched by example.com + int index = domain.indexOf(m_domain); + + // match if (domain ends with filter) && (filter has been found) and (character before filter is '.') + return index > 0 && domain[index - 1] == QLatin1Char('.'); +} + +bool Domain::matchesExactly(uint hash) const +{ + return (m_hash == hash); +} + +QString Domain::host() const +{ + return m_domain; +} |