From 9ab6f6d363f873f0799982c3c0872d38a656ee84 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Thu, 18 Oct 2018 14:40:32 +0200 Subject: urlfilter: add Domain class (#6): Add domain matcher class --- lib/urlfilter/domain.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 lib/urlfilter/domain.cpp (limited to 'lib/urlfilter/domain.cpp') 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; +} -- cgit v1.2.1