opt: refactor code (#1243)

* opt: refactor code

* opt: refactor str2map method

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
xiaoyifang 2023-10-16 07:37:10 +08:00 committed by GitHub
parent 5de4690b2d
commit 564d3ddffc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 30 deletions

View file

@ -118,7 +118,7 @@ QNetworkReply * ArticleNetworkAccessManager::getArticleReply( QNetworkRequest co
} }
} }
sptr< Dictionary::DataRequest > dr = getResource( url, contentType ); auto dr = getResource( url, contentType );
if ( dr.get() ) if ( dr.get() )
return new ArticleResourceReply( this, req, dr, contentType ); return new ArticleResourceReply( this, req, dr, contentType );
@ -129,7 +129,7 @@ QNetworkReply * ArticleNetworkAccessManager::getArticleReply( QNetworkRequest co
//if not external url,can be blocked from here. no need to continue execute the following code. //if not external url,can be blocked from here. no need to continue execute the following code.
//such as bres://upload.wikimedia.... etc . //such as bres://upload.wikimedia.... etc .
if ( !Utils::isExternalLink( url ) ) { if ( !Utils::isExternalLink( url ) ) {
gdWarning( "Blocking element \"%s\" as built-in link ", req.url().toEncoded().data() ); gdWarning( R"(Blocking element "%s" as built-in link )", req.url().toEncoded().data() );
return new BlockedNetworkReply( this ); return new BlockedNetworkReply( this );
} }
@ -144,7 +144,7 @@ QNetworkReply * ArticleNetworkAccessManager::getArticleReply( QNetworkRequest co
if ( !url.host().endsWith( refererUrl.host() ) if ( !url.host().endsWith( refererUrl.host() )
&& Utils::Url::getHostBaseFromUrl( url ) != Utils::Url::getHostBaseFromUrl( refererUrl ) && Utils::Url::getHostBaseFromUrl( url ) != Utils::Url::getHostBaseFromUrl( refererUrl )
&& !url.scheme().startsWith( "data" ) ) { && !url.scheme().startsWith( "data" ) ) {
gdWarning( "Blocking element \"%s\" due to not same domain", url.toEncoded().data() ); gdWarning( R"(Blocking element "%s" due to not same domain)", url.toEncoded().data() );
return new BlockedNetworkReply( this ); return new BlockedNetworkReply( this );
} }
@ -153,7 +153,7 @@ QNetworkReply * ArticleNetworkAccessManager::getArticleReply( QNetworkRequest co
if ( req.url().scheme() == "file" ) { if ( req.url().scheme() == "file" ) {
// Check file presence and adjust path if necessary // Check file presence and adjust path if necessary
QString fileName = req.url().toLocalFile(); QString fileName = req.url().toLocalFile();
if ( req.url().host().isEmpty() && articleMaker.adjustFilePath( fileName ) ) { if ( req.url().host().isEmpty() && ArticleMaker::adjustFilePath( fileName ) ) {
QUrl newUrl( req.url() ); QUrl newUrl( req.url() );
QUrl localUrl = QUrl::fromLocalFile( fileName ); QUrl localUrl = QUrl::fromLocalFile( fileName );
@ -236,21 +236,8 @@ sptr< Dictionary::DataRequest > ArticleNetworkAccessManager::getResource( QUrl c
// Unpack contexts // Unpack contexts
QMap< QString, QString > contexts; QString const contextsEncoded = Utils::Url::queryItemValue( url, "contexts" );
QMap< QString, QString > const contexts = Utils::str2map( contextsEncoded );
QString contextsEncoded = Utils::Url::queryItemValue( url, "contexts" );
if ( contextsEncoded.size() ) {
QByteArray ba = QByteArray::fromBase64( contextsEncoded.toLatin1() );
QBuffer buf( &ba );
buf.open( QBuffer::ReadOnly );
QDataStream stream( &buf );
stream >> contexts;
}
// See for ignore diacritics // See for ignore diacritics
@ -289,13 +276,13 @@ sptr< Dictionary::DataRequest > ArticleNetworkAccessManager::getResource( QUrl c
} }
catch ( std::exception & e ) { catch ( std::exception & e ) {
gdWarning( "getResource request error (%s) in \"%s\"\n", e.what(), dictionary->getName().c_str() ); gdWarning( "getResource request error (%s) in \"%s\"\n", e.what(), dictionary->getName().c_str() );
return sptr< Dictionary::DataRequest >(); return {};
} }
} }
} }
} }
return sptr< Dictionary::DataRequest >(); return {};
} }
ArticleResourceReply::ArticleResourceReply( QObject * parent, ArticleResourceReply::ArticleResourceReply( QObject * parent,
@ -356,12 +343,12 @@ void ArticleResourceReply::reqFinished()
qint64 ArticleResourceReply::bytesAvailable() const qint64 ArticleResourceReply::bytesAvailable() const
{ {
qint64 avail = req->dataSize(); qint64 const avail = req->dataSize();
if ( avail < 0 ) if ( avail < 0 )
return 0; return 0;
qint64 availBytes = avail - alreadyRead + QNetworkReply::bytesAvailable(); qint64 const availBytes = avail - alreadyRead + QNetworkReply::bytesAvailable();
if ( availBytes == 0 && !req->isFinished() ) { if ( availBytes == 0 && !req->isFinished() ) {
return 10240; return 10240;
} }
@ -382,17 +369,17 @@ qint64 ArticleResourceReply::readData( char * out, qint64 maxSize )
if ( maxSize == 0 ) if ( maxSize == 0 )
return 0; return 0;
bool finished = req->isFinished(); bool const finished = req->isFinished();
qint64 avail = req->dataSize(); qint64 const avail = req->dataSize();
if ( avail < 0 ) if ( avail < 0 )
return finished ? -1 : 0; return finished ? -1 : 0;
qint64 left = avail - alreadyRead; qint64 const left = avail - alreadyRead;
qint64 toRead = maxSize < left ? maxSize : left; qint64 const toRead = maxSize < left ? maxSize : left;
if ( !toRead && finished ) if ( !toRead && finished )
return -1; return -1;
GD_DPRINTF( "====reading %d of (%lld) bytes . Finished: %d", (int)toRead, avail, finished ); GD_DPRINTF( "====reading %d of (%lld) bytes . Finished: %d", (int)toRead, avail, finished );
@ -457,15 +444,15 @@ LocalSchemeHandler::LocalSchemeHandler( ArticleNetworkAccessManager & articleNet
void LocalSchemeHandler::requestStarted( QWebEngineUrlRequestJob * requestJob ) void LocalSchemeHandler::requestStarted( QWebEngineUrlRequestJob * requestJob )
{ {
QUrl url = requestJob->requestUrl(); QUrl const url = requestJob->requestUrl();
QNetworkRequest request; QNetworkRequest request;
request.setUrl( url ); request.setUrl( url );
//all the url reached here must be either gdlookup or bword scheme. //all the url reached here must be either gdlookup or bword scheme.
auto [ valid, word ] = Utils::Url::getQueryWord( url ); auto [ schemeValid, word ] = Utils::Url::getQueryWord( url );
// or the condition can be (!queryWord.first || word.isEmpty()) // or the condition can be (!queryWord.first || word.isEmpty())
// ( queryWord.first && word.isEmpty() ) is only part of the above condition. // ( queryWord.first && word.isEmpty() ) is only part of the above condition.
if ( valid && word.isEmpty() ) { if ( schemeValid && word.isEmpty() ) {
// invalid gdlookup url. // invalid gdlookup url.
return; return;
} }

View file

@ -7,8 +7,23 @@
#ifdef _MSC_VER #ifdef _MSC_VER
#include <stub_msvc.h> #include <stub_msvc.h>
#endif #endif
#include <QBuffer>
using std::string; using std::string;
namespace Utils { namespace Utils {
QMap< QString, QString > str2map( const QString & contextsEncoded )
{
QMap< QString, QString > contexts;
if ( contextsEncoded.size() ) {
QByteArray ba = QByteArray::fromBase64( contextsEncoded.toLatin1() );
QBuffer buf( &ba );
buf.open( QBuffer::ReadOnly );
QDataStream stream( &buf );
stream >> contexts;
}
return contexts;
}
//some str has \0 in the middle of the string. return the string before the \0 //some str has \0 in the middle of the string. return the string before the \0
std::string c_string( const QString & str ) std::string c_string( const QString & str )
{ {

View file

@ -17,6 +17,8 @@
using std::string; using std::string;
namespace Utils { namespace Utils {
QMap< QString, QString > str2map( const QString & contextsEncoded );
inline bool isCJKChar( ushort ch ) inline bool isCJKChar( ushort ch )
{ {
if ( ( ch >= 0x3400 && ch <= 0x9FFF ) || ( ch >= 0xF900 && ch <= 0xFAFF ) || ( ch >= 0xD800 && ch <= 0xDFFF ) ) if ( ( ch >= 0x3400 && ch <= 0x9FFF ) || ( ch >= 0xF900 && ch <= 0xFAFF ) || ( ch >= 0xD800 && ch <= 0xDFFF ) )