Skip to content

Commit

Permalink
Merge branch 'staged' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoyifang committed May 16, 2022
2 parents 964c54f + 7bc0075 commit 99d5093
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 20 deletions.
2 changes: 1 addition & 1 deletion gddebug.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() )
// {
Expand Down
3 changes: 3 additions & 0 deletions globalbroadcaster.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "globalbroadcaster.h"
#include <QGlobalStatic>
#include "utils.hh"

Q_GLOBAL_STATIC( GlobalBroadcaster, bdcaster )
GlobalBroadcaster::GlobalBroadcaster( QObject * parent ) : QObject( parent )
Expand All @@ -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){
Expand Down
Binary file modified icons/playsound_full.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 7 additions & 15 deletions iframeschemehandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@ 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

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

Expand Down Expand Up @@ -86,17 +85,10 @@ void IframeSchemeHandler::requestStarted(QWebEngineUrlRequestJob *requestJob)
articleNewString.clear();
}

sptr< Dictionary::DataRequestInstant > response = new Dictionary::DataRequestInstant( true );

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 );
QBuffer * buffer = new QBuffer(requestJob);
buffer->setData(codec->fromUnicode(articleString));

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 );

Expand Down
16 changes: 14 additions & 2 deletions utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;

Expand Down
10 changes: 8 additions & 2 deletions weburlrequestinterceptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 99d5093

Please sign in to comment.