opt: function refract

This commit is contained in:
Xiao YiFang 2022-10-15 13:58:24 +08:00
parent 2619e7be97
commit 4eedefc585
6 changed files with 22 additions and 17 deletions

View file

@ -200,7 +200,7 @@ QNetworkReply * ArticleNetworkAccessManager::getArticleReply( QNetworkRequest co
QUrl refererUrl = QUrl::fromEncoded( referer ); QUrl refererUrl = QUrl::fromEncoded( referer );
if ( !url.host().endsWith( refererUrl.host() ) && 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() ); gdWarning( "Blocking element \"%s\" due to not same domain", url.toEncoded().data() );

View file

@ -28,13 +28,13 @@ Config::Preferences * GlobalBroadcaster::getPreference()
void GlobalBroadcaster::addWhitelist( QString url ) void GlobalBroadcaster::addWhitelist( QString url )
{ {
whitelist.push_back( url ); whitelist.insert( url );
auto baseUrl = ::getHostBase( url ); auto baseUrl = Utils::Url::getHostBase( url );
whitelist.push_back( baseUrl ); whitelist.insert( baseUrl );
} }
bool GlobalBroadcaster::existedInWhitelist( QString url ) bool GlobalBroadcaster::existedInWhitelist( QString url )
{ {
return std::find( whitelist.begin(), whitelist.end(), url ) != whitelist.end(); return whitelist.contains(url);
} }
// namespace global // namespace global

View file

@ -16,7 +16,7 @@ class GlobalBroadcaster : public QObject
Q_OBJECT Q_OBJECT
private: private:
Config::Preferences * preference; Config::Preferences * preference;
std::vector<QString> whitelist; QSet<QString> whitelist;
public: public:
void setPreference( Config::Preferences * _pre ); void setPreference( Config::Preferences * _pre );

View file

@ -5,3 +5,11 @@ QString Utils::Path::combine(const QString& path1, const QString& path2)
{ {
return QDir::cleanPath(path1 + QDir::separator() + 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);
}

View file

@ -272,16 +272,6 @@ inline bool isAudioUrl( QUrl const & url )
&& ( Filetype::isNameOfSound( url.path().toUtf8().data() ) || url.host() == "apifree.forvo.com" ); && ( 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, /// 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. /// but only if it's not too base. Returns the resulting host name.
inline QString getHostBase( QString const & host ) inline QString getHostBase( QString const & host )
@ -315,8 +305,15 @@ inline QString getHostBaseFromUrl( QUrl const & url )
return getHostBase( host ); return getHostBase( host );
} }
QString getSchemeAndHost( QUrl const & url );
} }
namespace Path{
QString combine(const QString& path1, const QString& path2);
}
}
#endif // UTILS_HH #endif // UTILS_HH

View file

@ -15,7 +15,7 @@ void WebUrlRequestInterceptor::interceptRequest( QWebEngineUrlRequestInfo &info)
if(info.requestUrl().scheme()=="file"){ if(info.requestUrl().scheme()=="file"){
return; return;
} }
auto hostBase = getHostBase( info.requestUrl().host() ); auto hostBase = Utils::Url::getHostBase( info.requestUrl().host() );
if( GlobalBroadcaster::instance()->existedInWhitelist( hostBase ) ) if( GlobalBroadcaster::instance()->existedInWhitelist( hostBase ) )
{ {
//whitelist url does not block //whitelist url does not block