diff --git a/gddebug.cc b/gddebug.cc index a295c93b..9fce9dd3 100644 --- a/gddebug.cc +++ b/gddebug.cc @@ -53,7 +53,7 @@ va_start(ap, msg); // QTextCodec::setCodecForLocale( utf8Codec ); // } - qDebug()<< QString().vasprintf( msg, ap ); + qDebug().noquote() << QString().vasprintf( msg, ap ); // if( logFilePtr && logFilePtr->isOpen() ) // { diff --git a/globalbroadcaster.cpp b/globalbroadcaster.cpp index 761e2682..885bf89a 100644 --- a/globalbroadcaster.cpp +++ b/globalbroadcaster.cpp @@ -1,5 +1,6 @@ #include "globalbroadcaster.h" #include +#include "utils.hh" Q_GLOBAL_STATIC( GlobalBroadcaster, bdcaster ) GlobalBroadcaster::GlobalBroadcaster( QObject * parent ) : QObject( parent ) @@ -21,6 +22,8 @@ Config::Preferences * GlobalBroadcaster::getPreference() void GlobalBroadcaster::addWhitelist(QString url){ whitelist.push_back(url); + auto baseUrl=::getHostBase(url); + whitelist.push_back(baseUrl); } bool GlobalBroadcaster::existedInWhitelist(QString url){ diff --git a/iframeschemehandler.cpp b/iframeschemehandler.cpp index cc1d6d5e..fc2fd01c 100644 --- a/iframeschemehandler.cpp +++ b/iframeschemehandler.cpp @@ -13,8 +13,10 @@ void IframeSchemeHandler::requestStarted(QWebEngineUrlRequestJob *requestJob) url = QUrl( Utils::Url::queryItemValue( url, "url" ) ); QNetworkRequest request; request.setUrl( url ); + request.setAttribute(QNetworkRequest::RedirectPolicyAttribute,QNetworkRequest::RedirectPolicy::NoLessSafeRedirectPolicy); QNetworkReply * reply = mgr.get( request ); + auto finishAction = [ = ]() -> void { // Handle reply data @@ -22,11 +24,8 @@ void IframeSchemeHandler::requestStarted(QWebEngineUrlRequestJob *requestJob) QByteArray replyData = reply->readAll(); QString articleString; - QTextCodec * codec = QTextCodec::codecForHtml( replyData ); - if( codec ) - articleString = codec->toUnicode( replyData ); - else - articleString = QString::fromUtf8( replyData ); + QTextCodec * codec = QTextCodec::codecForHtml( replyData, QTextCodec::codecForName( "UTF-8" ) ); + articleString = codec->toUnicode( replyData ); // Change links from relative to absolute @@ -86,17 +85,10 @@ void IframeSchemeHandler::requestStarted(QWebEngineUrlRequestJob *requestJob) articleNewString.clear(); } - sptr< Dictionary::DataRequestInstant > response = new Dictionary::DataRequestInstant( true ); + QBuffer * buffer = new QBuffer(requestJob); + buffer->setData(codec->fromUnicode(articleString)); - auto content = articleString.toStdString(); - response->getData().resize( content.size() ); - memcpy( &( response->getData().front() ), content.data(), content.size() ); - - auto contentType="text/html"; - auto newReply = new ArticleResourceReply( this, request, response, contentType ); - - requestJob->reply( contentType, newReply ); - connect( requestJob, &QObject::destroyed, newReply, &QObject::deleteLater ); + requestJob->reply( "text/html;charset=UTF-8", buffer ); }; connect( reply, &QNetworkReply::finished, requestJob, finishAction ); diff --git a/utils.hh b/utils.hh index 891b3c67..9c7699e1 100644 --- a/utils.hh +++ b/utils.hh @@ -55,6 +55,13 @@ inline bool isExternalLink(QUrl const &url) { url.scheme() == "file"; } +inline bool isCssFontImage(QUrl const &url) { + auto fileName = url.fileName(); + auto ext=fileName.mid(fileName.lastIndexOf(".")); + QStringList extensions{".css",".woff",".woff2",".bmp" ,".jpg", ".png", ".tif",".wav", ".ogg", ".oga", ".mp3", ".mp4", ".aac", ".flac",".mid", ".wv ",".ape"} ; + return extensions.indexOf(ext)>-1; +} + inline QString escape( QString const & plain ) { return plain.toHtmlEscaped(); @@ -179,15 +186,20 @@ inline QString getHostBase( QUrl const & url ) { QString host = url.host(); + return getHostBase(host); +} + +inline QString getHostBase( QString const & host ) +{ QStringList domains = host.split( '.' ); int left = domains.size(); - // Skip last <=3-letter domain name + // Skip last <=3-letter domain name if ( left && domains[ left - 1 ].size() <= 3 ) --left; - // Skip another <=3-letter domain name + // Skip another <=3-letter domain name if ( left && domains[ left - 1 ].size() <= 3 ) --left; diff --git a/weburlrequestinterceptor.cpp b/weburlrequestinterceptor.cpp index 6b8faa62..75b14c1f 100644 --- a/weburlrequestinterceptor.cpp +++ b/weburlrequestinterceptor.cpp @@ -11,10 +11,16 @@ WebUrlRequestInterceptor::WebUrlRequestInterceptor(QObject *p) void WebUrlRequestInterceptor::interceptRequest( QWebEngineUrlRequestInfo &info) { if( Utils::isExternalLink( info.requestUrl() ) ) { - if(!GlobalBroadcaster::instance()-> existedInWhitelist(info.requestUrl().host())) + if(GlobalBroadcaster::instance()-> existedInWhitelist(info.requestUrl().host())) { - info.block( true ); + //whitelist url does not block + return; } + if(Utils::isCssFontImage(info.requestUrl())){ + //let throuth the resources file. + return; + } + info.block(true); } if (QWebEngineUrlRequestInfo::NavigationTypeLink == info.navigationType() && info.resourceType() == QWebEngineUrlRequestInfo::ResourceTypeMainFrame) {