From 4eedefc585a81a012a4a1c4efb37f9da6ecde037 Mon Sep 17 00:00:00 2001 From: Xiao YiFang Date: Sat, 15 Oct 2022 13:58:24 +0800 Subject: [PATCH] opt: function refract --- article_netmgr.cc | 2 +- globalbroadcaster.cpp | 8 ++++---- globalbroadcaster.h | 2 +- utils.cc | 8 ++++++++ utils.hh | 17 +++++++---------- weburlrequestinterceptor.cpp | 2 +- 6 files changed, 22 insertions(+), 17 deletions(-) diff --git a/article_netmgr.cc b/article_netmgr.cc index f0f00119..7498b4a9 100644 --- a/article_netmgr.cc +++ b/article_netmgr.cc @@ -200,7 +200,7 @@ QNetworkReply * ArticleNetworkAccessManager::getArticleReply( QNetworkRequest co QUrl refererUrl = QUrl::fromEncoded( referer ); if ( !url.host().endsWith( refererUrl.host() ) && - getHostBaseFromUrl( url ) != getHostBaseFromUrl( refererUrl ) && !url.scheme().startsWith("data") ) + Utils::Url::getHostBaseFromUrl( url ) != Utils::Url::getHostBaseFromUrl( refererUrl ) && !url.scheme().startsWith("data") ) { gdWarning( "Blocking element \"%s\" due to not same domain", url.toEncoded().data() ); diff --git a/globalbroadcaster.cpp b/globalbroadcaster.cpp index f473ca0f..4f84f714 100644 --- a/globalbroadcaster.cpp +++ b/globalbroadcaster.cpp @@ -28,13 +28,13 @@ Config::Preferences * GlobalBroadcaster::getPreference() void GlobalBroadcaster::addWhitelist( QString url ) { - whitelist.push_back( url ); - auto baseUrl = ::getHostBase( url ); - whitelist.push_back( baseUrl ); + whitelist.insert( url ); + auto baseUrl = Utils::Url::getHostBase( url ); + whitelist.insert( baseUrl ); } bool GlobalBroadcaster::existedInWhitelist( QString url ) { - return std::find( whitelist.begin(), whitelist.end(), url ) != whitelist.end(); + return whitelist.contains(url); } // namespace global diff --git a/globalbroadcaster.h b/globalbroadcaster.h index daf627b7..20b44aea 100644 --- a/globalbroadcaster.h +++ b/globalbroadcaster.h @@ -16,7 +16,7 @@ class GlobalBroadcaster : public QObject Q_OBJECT private: Config::Preferences * preference; - std::vector whitelist; + QSet whitelist; public: void setPreference( Config::Preferences * _pre ); diff --git a/utils.cc b/utils.cc index 928becee..37181c4a 100644 --- a/utils.cc +++ b/utils.cc @@ -5,3 +5,11 @@ QString Utils::Path::combine(const QString& path1, const QString& path2) { return QDir::cleanPath(path1 + QDir::separator() + path2); } + +QString Utils::Url::getSchemeAndHost( QUrl const & url ) +{ + auto _url = url.url(); + auto index = _url.indexOf("://"); + auto hostEndIndex = _url.indexOf("/",index+3); + return _url.mid(0,hostEndIndex); +} diff --git a/utils.hh b/utils.hh index e8cf5fe8..d5ce3a82 100644 --- a/utils.hh +++ b/utils.hh @@ -272,16 +272,6 @@ inline bool isAudioUrl( QUrl const & url ) && ( Filetype::isNameOfSound( url.path().toUtf8().data() ) || url.host() == "apifree.forvo.com" ); } -} - -namespace Path{ -QString combine(const QString& path1, const QString& path2); -} - -} - -namespace -{ /// Uses some heuristics to chop off the first domain name from the host name, /// but only if it's not too base. Returns the resulting host name. inline QString getHostBase( QString const & host ) @@ -315,8 +305,15 @@ inline QString getHostBaseFromUrl( QUrl const & url ) return getHostBase( host ); } + +QString getSchemeAndHost( QUrl const & url ); + } +namespace Path{ +QString combine(const QString& path1, const QString& path2); +} +} #endif // UTILS_HH diff --git a/weburlrequestinterceptor.cpp b/weburlrequestinterceptor.cpp index 412532e9..faea2c06 100644 --- a/weburlrequestinterceptor.cpp +++ b/weburlrequestinterceptor.cpp @@ -15,7 +15,7 @@ void WebUrlRequestInterceptor::interceptRequest( QWebEngineUrlRequestInfo &info) if(info.requestUrl().scheme()=="file"){ return; } - auto hostBase = getHostBase( info.requestUrl().host() ); + auto hostBase = Utils::Url::getHostBase( info.requestUrl().host() ); if( GlobalBroadcaster::instance()->existedInWhitelist( hostBase ) ) { //whitelist url does not block