From 80749958b7c909644269540863dd78b7435e6a6d Mon Sep 17 00:00:00 2001 From: varjolintu Date: Thu, 30 Aug 2018 13:40:41 +0300 Subject: [PATCH] Fix checking subdomains --- src/browser/BrowserService.cpp | 27 +++++++++++++++++++++++++-- src/browser/BrowserService.h | 1 + 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/browser/BrowserService.cpp b/src/browser/BrowserService.cpp index 086c15062..25c9f21b2 100644 --- a/src/browser/BrowserService.cpp +++ b/src/browser/BrowserService.cpp @@ -388,7 +388,7 @@ QList BrowserService::searchEntries(Database* db, const QString& hostnam return entries; } - for (Entry* entry : EntrySearcher().search(hostname, rootGroup, Qt::CaseInsensitive)) { + for (Entry* entry : EntrySearcher().search(baseDomain(hostname), rootGroup, Qt::CaseInsensitive)) { QString entryUrl = entry->url(); QUrl entryQUrl(entryUrl); QString entryScheme = entryQUrl.scheme(); @@ -401,7 +401,7 @@ QList BrowserService::searchEntries(Database* db, const QString& hostnam } // Filter to match hostname in URL field - if ((!entryUrl.isEmpty() && hostname.contains(entryUrl)) + if ((!entryUrl.isEmpty() && hostname.contains(entryUrl)) || (matchUrlScheme(entryUrl) && hostname.endsWith(entryQUrl.host()))) { entries.append(entry); } @@ -780,6 +780,29 @@ bool BrowserService::removeFirstDomain(QString& hostname) return false; } +/** + * Gets the base domain of URL. + * + * Returns the base domain, e.g. https://another.example.co.uk -> example.co.uk + */ +QString BrowserService::baseDomain(const QString& url) const +{ + QUrl qurl = QUrl::fromUserInput(url); + QString hostname = qurl.host(); + + if (hostname.isEmpty() || !hostname.contains(qurl.topLevelDomain())) { + return {}; + } + + // Remove the top level domain part from the hostname, e.g. https://another.example.co.uk -> https://another.example + hostname.chop(qurl.topLevelDomain().length()); + // Split the URL and select the last part, e.g. https://another.example -> example + QString baseDomain = hostname.split('.').last(); + // Append the top level domain back to the URL, e.g. example -> example.co.uk + baseDomain.append(qurl.topLevelDomain()); + return baseDomain; +} + Database* BrowserService::getDatabase() { if (DatabaseWidget* dbWidget = m_dbTabWidget->currentDatabaseWidget()) { diff --git a/src/browser/BrowserService.h b/src/browser/BrowserService.h index 10c3a786e..56f74bd38 100644 --- a/src/browser/BrowserService.h +++ b/src/browser/BrowserService.h @@ -101,6 +101,7 @@ private: sortPriority(const Entry* entry, const QString& host, const QString& submitUrl, const QString& baseSubmitUrl) const; bool matchUrlScheme(const QString& url); bool removeFirstDomain(QString& hostname); + QString baseDomain(const QString& url) const; Database* getDatabase(); private: