2021-08-14 07:25:10 +00:00
|
|
|
/* This file is (c) 2008-2012 Konstantin Isakov <ikm@goldendict.org>
|
2009-01-28 20:55:45 +00:00
|
|
|
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
|
|
|
|
|
2009-04-27 12:25:15 +00:00
|
|
|
#include <stdint.h>
|
2013-05-31 04:20:25 +00:00
|
|
|
#include <QUrl>
|
2009-01-28 20:55:45 +00:00
|
|
|
#include "article_netmgr.hh"
|
2013-11-16 18:34:09 +00:00
|
|
|
#include "gddebug.hh"
|
2021-11-27 07:17:33 +00:00
|
|
|
#include "utils.hh"
|
2021-08-05 06:57:22 +00:00
|
|
|
#include <QNetworkAccessManager>
|
2023-04-17 21:56:55 +00:00
|
|
|
#include "globalbroadcaster.hh"
|
2013-05-30 13:24:21 +00:00
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
using std::string;
|
|
|
|
|
2017-03-23 15:03:17 +00:00
|
|
|
// AllowFrameReply
|
|
|
|
|
|
|
|
AllowFrameReply::AllowFrameReply( QNetworkReply * _reply ):
|
|
|
|
baseReply( _reply )
|
|
|
|
{
|
|
|
|
// Set base data
|
|
|
|
|
|
|
|
setOperation( baseReply->operation() );
|
|
|
|
setRequest( baseReply->request() );
|
|
|
|
setUrl( baseReply->url() );
|
|
|
|
|
2024-10-26 11:35:27 +00:00
|
|
|
QList< QByteArray > rawHeaders = baseReply->rawHeaderList();
|
|
|
|
for ( const auto & header : rawHeaders ) {
|
|
|
|
if ( header.toLower() != "x-frame-options" )
|
|
|
|
setRawHeader( header, baseReply->rawHeader( header ) );
|
|
|
|
}
|
2017-03-23 15:03:17 +00:00
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( baseReply, &QNetworkReply::errorOccurred, this, &AllowFrameReply::applyError );
|
2017-03-24 14:05:22 +00:00
|
|
|
|
2017-03-23 15:03:17 +00:00
|
|
|
connect( baseReply, &QIODevice::readyRead, this, &QIODevice::readyRead );
|
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
// Redirect QNetworkReply signals
|
2017-03-23 15:03:17 +00:00
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( baseReply, &QNetworkReply::finished, this, &QNetworkReply::finished );
|
2017-03-23 15:03:17 +00:00
|
|
|
|
|
|
|
setOpenMode( QIODevice::ReadOnly );
|
|
|
|
}
|
|
|
|
|
|
|
|
qint64 AllowFrameReply::bytesAvailable() const
|
|
|
|
{
|
|
|
|
return baseReply->bytesAvailable();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AllowFrameReply::applyError( QNetworkReply::NetworkError code )
|
2023-07-20 08:02:22 +00:00
|
|
|
{
|
2017-03-23 15:03:17 +00:00
|
|
|
setError( code, baseReply->errorString() );
|
2022-01-08 13:16:22 +00:00
|
|
|
emit errorOccurred( code );
|
2023-07-20 08:02:22 +00:00
|
|
|
}
|
|
|
|
|
2017-03-24 14:05:22 +00:00
|
|
|
qint64 AllowFrameReply::readData( char * data, qint64 maxSize )
|
|
|
|
{
|
2022-05-13 16:00:23 +00:00
|
|
|
auto bytesAvailable = baseReply->bytesAvailable();
|
|
|
|
qint64 size = qMin( maxSize, bytesAvailable );
|
|
|
|
baseReply->read( data, size );
|
2017-03-24 14:05:22 +00:00
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2022-05-18 23:14:36 +00:00
|
|
|
QNetworkReply * ArticleNetworkAccessManager::getArticleReply( QNetworkRequest const & req )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2023-03-26 06:46:27 +00:00
|
|
|
if ( req.url().scheme() == "qrcx" ) {
|
2023-10-08 13:50:17 +00:00
|
|
|
// Do not support qrcx which is the custom define protocol.
|
|
|
|
return new BlockedNetworkReply( this );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-08 13:50:17 +00:00
|
|
|
auto op = GetOperation;
|
|
|
|
|
|
|
|
QUrl url = req.url();
|
2021-12-27 13:15:13 +00:00
|
|
|
QMimeType mineType = db.mimeTypeForUrl( url );
|
|
|
|
QString contentType = mineType.name();
|
|
|
|
|
2021-09-24 13:08:54 +00:00
|
|
|
if ( req.url().scheme() == "gdlookup" ) {
|
|
|
|
QString path = url.path();
|
2023-10-28 05:25:10 +00:00
|
|
|
if ( path.size() > 1 ) {
|
2022-11-19 06:12:31 +00:00
|
|
|
url.setPath( "" );
|
|
|
|
|
|
|
|
Utils::Url::addQueryItem( url, "word", path.mid( 1 ) );
|
2022-11-24 12:34:21 +00:00
|
|
|
Utils::Url::addQueryItem( url, "group", QString::number( GlobalBroadcaster::instance()->currentGroupId ) );
|
2021-09-24 13:08:54 +00:00
|
|
|
}
|
2023-07-20 08:02:22 +00:00
|
|
|
}
|
2021-09-24 13:08:54 +00:00
|
|
|
|
2023-10-15 23:37:10 +00:00
|
|
|
auto dr = getResource( url, contentType );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
if ( dr.get() ) {
|
|
|
|
return new ArticleResourceReply( this, req, dr, contentType );
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2022-01-11 14:20:38 +00:00
|
|
|
|
|
|
|
//dr.get() can be null. code continue to execute.
|
|
|
|
|
|
|
|
//can not match dictionary in the above code,means the url must be external links.
|
|
|
|
//if not external url,can be blocked from here. no need to continue execute the following code.
|
|
|
|
//such as bres://upload.wikimedia.... etc .
|
2022-05-13 16:00:23 +00:00
|
|
|
if ( !Utils::isExternalLink( url ) ) {
|
2023-10-15 23:37:10 +00:00
|
|
|
gdWarning( R"(Blocking element "%s" as built-in link )", req.url().toEncoded().data() );
|
2022-01-11 14:20:38 +00:00
|
|
|
return new BlockedNetworkReply( this );
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-08-31 12:18:08 +00:00
|
|
|
// Check the Referer. If the user has opted-in to block elements from external
|
|
|
|
// pages, we block them.
|
|
|
|
|
|
|
|
if ( disallowContentFromOtherSites && req.hasRawHeader( "Referer" ) ) {
|
|
|
|
QByteArray referer = req.rawHeader( "Referer" );
|
|
|
|
|
2013-05-31 04:20:25 +00:00
|
|
|
QUrl refererUrl = QUrl::fromEncoded( referer );
|
2009-08-31 12:18:08 +00:00
|
|
|
|
2022-05-13 16:00:23 +00:00
|
|
|
if ( !url.host().endsWith( refererUrl.host() )
|
2022-10-15 05:58:24 +00:00
|
|
|
&& Utils::Url::getHostBaseFromUrl( url ) != Utils::Url::getHostBaseFromUrl( refererUrl )
|
|
|
|
&& !url.scheme().startsWith( "data" ) ) {
|
2023-10-15 23:37:10 +00:00
|
|
|
gdWarning( R"(Blocking element "%s" due to not same domain)", url.toEncoded().data() );
|
2009-08-31 12:18:08 +00:00
|
|
|
|
|
|
|
return new BlockedNetworkReply( this );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-13 20:21:33 +00:00
|
|
|
if ( req.url().scheme() == "file" ) {
|
|
|
|
// Check file presence and adjust path if necessary
|
|
|
|
QString fileName = req.url().toLocalFile();
|
2023-10-15 23:37:10 +00:00
|
|
|
if ( req.url().host().isEmpty() && ArticleMaker::adjustFilePath( fileName ) ) {
|
2013-05-31 04:20:25 +00:00
|
|
|
QUrl newUrl( req.url() );
|
2019-08-05 14:51:57 +00:00
|
|
|
QUrl localUrl = QUrl::fromLocalFile( fileName );
|
|
|
|
|
|
|
|
newUrl.setHost( localUrl.host() );
|
2021-11-27 07:17:33 +00:00
|
|
|
newUrl.setPath( Utils::Url::ensureLeadingSlash( localUrl.path() ) );
|
2012-12-13 20:21:33 +00:00
|
|
|
|
|
|
|
QNetworkRequest newReq( req );
|
|
|
|
newReq.setUrl( newUrl );
|
|
|
|
|
2022-05-18 23:14:36 +00:00
|
|
|
return QNetworkAccessManager::createRequest( op, newReq, nullptr );
|
2012-12-13 20:21:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-08 12:20:12 +00:00
|
|
|
// spoof User-Agent
|
2022-05-13 16:00:23 +00:00
|
|
|
QNetworkRequest newReq;
|
|
|
|
newReq.setUrl( url );
|
|
|
|
newReq.setAttribute( QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy );
|
|
|
|
if ( hideGoldenDictHeader && url.scheme().startsWith( "http", Qt::CaseInsensitive ) ) {
|
2022-01-08 13:16:22 +00:00
|
|
|
newReq.setRawHeader( "User-Agent", req.rawHeader( "User-Agent" ).replace( qApp->applicationName().toUtf8(), "" ) );
|
2016-06-25 12:37:08 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 23:14:36 +00:00
|
|
|
QNetworkReply * reply = QNetworkAccessManager::createRequest( op, newReq, nullptr );
|
2016-06-25 12:37:08 +00:00
|
|
|
|
2022-05-13 16:00:23 +00:00
|
|
|
if ( url.scheme() == "https" ) {
|
2022-12-15 07:11:09 +00:00
|
|
|
#ifndef QT_NO_SSL
|
2016-06-25 12:37:08 +00:00
|
|
|
connect( reply, SIGNAL( sslErrors( QList< QSslError > ) ), reply, SLOT( ignoreSslErrors() ) );
|
|
|
|
#endif
|
2013-04-08 12:20:12 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 23:14:36 +00:00
|
|
|
return new AllowFrameReply( reply );
|
2021-08-05 06:57:22 +00:00
|
|
|
}
|
|
|
|
|
2023-10-07 12:49:38 +00:00
|
|
|
string ArticleNetworkAccessManager::getHtml( ResourceType resourceType )
|
|
|
|
{
|
|
|
|
switch ( resourceType ) {
|
|
|
|
case ResourceType::UNTITLE:
|
2023-10-10 13:27:48 +00:00
|
|
|
return articleMaker.makeUntitleHtml();
|
2023-10-07 12:49:38 +00:00
|
|
|
case ResourceType::WELCOME:
|
2023-10-10 13:27:48 +00:00
|
|
|
return articleMaker.makeWelcomeHtml();
|
2023-10-10 13:17:34 +00:00
|
|
|
case ResourceType::BLANK:
|
|
|
|
return articleMaker.makeBlankHtml();
|
2023-10-07 12:49:38 +00:00
|
|
|
default:
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
sptr< Dictionary::DataRequest > ArticleNetworkAccessManager::getResource( QUrl const & url, QString & contentType )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2022-10-22 05:42:17 +00:00
|
|
|
qDebug() << "getResource:" << url.toString();
|
|
|
|
qDebug() << "scheme:" << url.scheme();
|
|
|
|
qDebug() << "host:" << url.host();
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
if ( url.scheme() == "gdlookup" ) {
|
2014-05-23 17:43:44 +00:00
|
|
|
if ( !url.host().isEmpty() && url.host() != "localhost" ) {
|
|
|
|
// Strange request - ignore it
|
2022-11-29 03:54:31 +00:00
|
|
|
return std::make_shared< Dictionary::DataRequestInstant >( false );
|
2014-05-23 17:43:44 +00:00
|
|
|
}
|
|
|
|
|
2009-05-11 15:33:57 +00:00
|
|
|
contentType = "text/html";
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
if ( Utils::Url::queryItemValue( url, "blank" ) == "1" ) {
|
2009-05-11 15:33:57 +00:00
|
|
|
return articleMaker.makeEmptyPage();
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2009-05-11 15:33:57 +00:00
|
|
|
|
2023-05-28 16:01:54 +00:00
|
|
|
QString word = Utils::Url::queryItemValue( url, "word" ).trimmed();
|
2009-04-10 12:48:40 +00:00
|
|
|
|
2021-06-10 16:13:11 +00:00
|
|
|
bool groupIsValid = false;
|
2021-11-27 07:17:33 +00:00
|
|
|
unsigned group = Utils::Url::queryItemValue( url, "group" ).toUInt( &groupIsValid );
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
QString dictIDs = Utils::Url::queryItemValue( url, "dictionaries" );
|
2014-04-16 16:18:28 +00:00
|
|
|
if ( !dictIDs.isEmpty() ) {
|
|
|
|
// Individual dictionaries set from full-text search
|
|
|
|
QStringList dictIDList = dictIDs.split( "," );
|
2023-05-28 16:01:54 +00:00
|
|
|
return articleMaker.makeDefinitionFor( word, group, QMap< QString, QString >(), QSet< QString >(), dictIDList );
|
2014-04-16 16:18:28 +00:00
|
|
|
}
|
|
|
|
|
2009-09-23 18:44:38 +00:00
|
|
|
// See if we have some dictionaries muted
|
|
|
|
|
2022-01-08 13:16:22 +00:00
|
|
|
QStringList mutedDictLists = Utils::Url::queryItemValue( url, "muted" ).split( ',' );
|
|
|
|
QSet< QString > mutedDicts( mutedDictLists.begin(), mutedDictLists.end() );
|
2009-09-23 18:44:38 +00:00
|
|
|
|
2009-05-29 19:48:50 +00:00
|
|
|
// Unpack contexts
|
|
|
|
|
2023-10-15 23:37:10 +00:00
|
|
|
QString const contextsEncoded = Utils::Url::queryItemValue( url, "contexts" );
|
|
|
|
QMap< QString, QString > const contexts = Utils::str2map( contextsEncoded );
|
2009-05-29 19:48:50 +00:00
|
|
|
|
2018-06-13 16:00:42 +00:00
|
|
|
// See for ignore diacritics
|
|
|
|
|
2021-11-27 07:17:33 +00:00
|
|
|
bool ignoreDiacritics = Utils::Url::queryItemValue( url, "ignore_diacritics" ) == "1";
|
2018-06-13 16:00:42 +00:00
|
|
|
|
2023-05-28 16:01:54 +00:00
|
|
|
if ( groupIsValid && !word.isEmpty() ) { // Require group and phrase to be passed
|
|
|
|
return articleMaker.makeDefinitionFor( word, group, contexts, mutedDicts, QStringList(), ignoreDiacritics );
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2013-06-22 16:36:25 +00:00
|
|
|
if ( ( url.scheme() == "bres" || url.scheme() == "gdau" || url.scheme() == "gdvideo" || url.scheme() == "gico" )
|
2009-01-28 20:55:45 +00:00
|
|
|
&& url.path().size() ) {
|
|
|
|
|
2023-10-06 01:13:55 +00:00
|
|
|
|
|
|
|
QMimeType mineType = db.mimeTypeForUrl( url );
|
|
|
|
contentType = mineType.name();
|
2009-01-28 20:55:45 +00:00
|
|
|
string id = url.host().toStdString();
|
|
|
|
|
2024-10-22 06:15:17 +00:00
|
|
|
for ( const auto & dictionary : dictionaries ) {
|
|
|
|
if ( dictionary->getId() == id ) {
|
|
|
|
if ( url.scheme() == "gico" ) {
|
|
|
|
QByteArray bytes;
|
|
|
|
QBuffer buffer( &bytes );
|
|
|
|
buffer.open( QIODevice::WriteOnly );
|
|
|
|
dictionary->getIcon().pixmap( 64 ).save( &buffer, "PNG" );
|
|
|
|
buffer.close();
|
|
|
|
sptr< Dictionary::DataRequestInstant > ico = std::make_shared< Dictionary::DataRequestInstant >( true );
|
|
|
|
ico->getData().resize( bytes.size() );
|
|
|
|
memcpy( &( ico->getData().front() ), bytes.data(), bytes.size() );
|
|
|
|
return ico;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return dictionary->getResource( Utils::Url::path( url ).mid( 1 ).toUtf8().data() );
|
|
|
|
}
|
|
|
|
catch ( std::exception & e ) {
|
|
|
|
gdWarning( "getResource request error (%s) in \"%s\"\n", e.what(), dictionary->getName().c_str() );
|
|
|
|
return {};
|
2012-01-28 03:07:23 +00:00
|
|
|
}
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2023-10-15 23:37:10 +00:00
|
|
|
return {};
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ArticleResourceReply::ArticleResourceReply( QObject * parent,
|
2009-03-26 19:00:08 +00:00
|
|
|
QNetworkRequest const & netReq,
|
|
|
|
sptr< Dictionary::DataRequest > const & req_,
|
2009-01-28 20:55:45 +00:00
|
|
|
QString const & contentType ):
|
2009-03-26 19:00:08 +00:00
|
|
|
QNetworkReply( parent ),
|
|
|
|
req( req_ ),
|
|
|
|
alreadyRead( 0 )
|
2009-01-28 20:55:45 +00:00
|
|
|
{
|
2009-03-26 19:00:08 +00:00
|
|
|
setRequest( netReq );
|
2009-01-28 20:55:45 +00:00
|
|
|
setOpenMode( ReadOnly );
|
2022-01-10 12:17:22 +00:00
|
|
|
setUrl( netReq.url() );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
|
|
|
if ( contentType.size() ) {
|
|
|
|
setHeader( QNetworkRequest::ContentTypeHeader, contentType );
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( req.get(), &Dictionary::Request::updated, this, &ArticleResourceReply::reqUpdated );
|
|
|
|
|
|
|
|
connect( req.get(), &Dictionary::Request::finished, this, &ArticleResourceReply::reqFinished );
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( req->isFinished() || req->dataSize() > 0 ) {
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( this,
|
2023-03-26 06:46:27 +00:00
|
|
|
&ArticleResourceReply::readyReadSignal,
|
|
|
|
this,
|
|
|
|
&ArticleResourceReply::readyReadSlot,
|
|
|
|
Qt::QueuedConnection );
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( this,
|
2023-03-26 06:46:27 +00:00
|
|
|
&ArticleResourceReply::finishedSignal,
|
|
|
|
this,
|
|
|
|
&ArticleResourceReply::finishedSlot,
|
|
|
|
Qt::QueuedConnection );
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
emit readyReadSignal();
|
|
|
|
|
|
|
|
if ( req->isFinished() ) {
|
|
|
|
emit finishedSignal();
|
2014-05-10 21:02:31 +00:00
|
|
|
GD_DPRINTF( "In-place finish.\n" );
|
2009-03-26 19:00:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-03-28 15:59:39 +00:00
|
|
|
ArticleResourceReply::~ArticleResourceReply()
|
|
|
|
{
|
|
|
|
req->cancel();
|
|
|
|
}
|
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
void ArticleResourceReply::reqUpdated()
|
|
|
|
{
|
|
|
|
emit readyRead();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleResourceReply::reqFinished()
|
|
|
|
{
|
|
|
|
emit readyRead();
|
|
|
|
finishedSlot();
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qint64 ArticleResourceReply::bytesAvailable() const
|
|
|
|
{
|
2023-10-15 23:37:10 +00:00
|
|
|
qint64 const avail = req->dataSize();
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( avail < 0 ) {
|
|
|
|
return 0;
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2023-10-15 23:37:10 +00:00
|
|
|
qint64 const availBytes = avail - alreadyRead + QNetworkReply::bytesAvailable();
|
2023-10-08 13:40:18 +00:00
|
|
|
if ( availBytes == 0 && !req->isFinished() ) {
|
|
|
|
return 10240;
|
2023-09-10 11:02:31 +00:00
|
|
|
}
|
|
|
|
|
2023-10-08 13:40:18 +00:00
|
|
|
return availBytes;
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2023-09-10 11:02:31 +00:00
|
|
|
|
2023-03-29 03:19:11 +00:00
|
|
|
bool ArticleResourceReply::atEnd() const
|
|
|
|
{
|
2023-09-10 11:02:31 +00:00
|
|
|
return req->isFinished() && bytesAvailable() == 0;
|
2023-03-29 03:19:11 +00:00
|
|
|
}
|
|
|
|
|
2009-01-28 20:55:45 +00:00
|
|
|
qint64 ArticleResourceReply::readData( char * out, qint64 maxSize )
|
|
|
|
{
|
2013-08-04 19:19:57 +00:00
|
|
|
// From the doc: "This function might be called with a maxSize of 0,
|
|
|
|
// which can be used to perform post-reading operations".
|
|
|
|
if ( maxSize == 0 ) {
|
|
|
|
return 0;
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2013-08-04 19:19:57 +00:00
|
|
|
|
2023-10-15 23:37:10 +00:00
|
|
|
bool const finished = req->isFinished();
|
2023-07-20 08:02:22 +00:00
|
|
|
|
2023-10-15 23:37:10 +00:00
|
|
|
qint64 const avail = req->dataSize();
|
2009-03-26 19:00:08 +00:00
|
|
|
|
|
|
|
if ( avail < 0 ) {
|
|
|
|
return finished ? -1 : 0;
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2009-03-26 19:00:08 +00:00
|
|
|
|
2023-10-15 23:37:10 +00:00
|
|
|
qint64 const left = avail - alreadyRead;
|
2023-03-29 03:19:11 +00:00
|
|
|
|
2023-10-15 23:37:10 +00:00
|
|
|
qint64 const toRead = maxSize < left ? maxSize : left;
|
2023-09-10 11:02:31 +00:00
|
|
|
if ( !toRead && finished ) {
|
|
|
|
return -1;
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2024-10-23 13:37:44 +00:00
|
|
|
if ( toRead == 0 ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
GD_DPRINTF( "====reading %lld of (%lld) bytes, %lld bytes readed . Finish status: %d",
|
|
|
|
toRead,
|
|
|
|
avail,
|
|
|
|
alreadyRead,
|
|
|
|
finished );
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2013-09-19 19:43:16 +00:00
|
|
|
try {
|
|
|
|
req->getDataSlice( alreadyRead, toRead, out );
|
|
|
|
}
|
|
|
|
catch ( std::exception & e ) {
|
2022-06-01 15:40:00 +00:00
|
|
|
qWarning( "getDataSlice error: %s", e.what() );
|
2013-09-19 19:43:16 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
alreadyRead += toRead;
|
2009-01-28 20:55:45 +00:00
|
|
|
|
2009-03-26 19:00:08 +00:00
|
|
|
if ( !toRead && finished ) {
|
2009-01-28 20:55:45 +00:00
|
|
|
return -1;
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
else {
|
|
|
|
return toRead;
|
2024-10-10 07:13:23 +00:00
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleResourceReply::readyReadSlot()
|
|
|
|
{
|
2021-12-27 13:15:13 +00:00
|
|
|
emit readyRead();
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ArticleResourceReply::finishedSlot()
|
|
|
|
{
|
2022-01-08 04:22:41 +00:00
|
|
|
if ( req->dataSize() < 0 ) {
|
2022-01-08 13:16:22 +00:00
|
|
|
emit errorOccurred( ContentNotFoundError );
|
2022-01-08 04:22:41 +00:00
|
|
|
setError( ContentNotFoundError, "content not found" );
|
|
|
|
}
|
2022-01-09 06:30:06 +00:00
|
|
|
//prevent sent multi times.
|
|
|
|
if ( !finishSignalSent.loadAcquire() ) {
|
|
|
|
finishSignalSent.ref();
|
2023-03-26 06:46:27 +00:00
|
|
|
setFinished( true );
|
2022-01-09 06:30:06 +00:00
|
|
|
emit finished();
|
|
|
|
}
|
2009-01-28 20:55:45 +00:00
|
|
|
}
|
|
|
|
|
2009-08-31 12:18:08 +00:00
|
|
|
BlockedNetworkReply::BlockedNetworkReply( QObject * parent ):
|
|
|
|
QNetworkReply( parent )
|
|
|
|
{
|
|
|
|
setError( QNetworkReply::ContentOperationNotPermittedError, "Content Blocked" );
|
|
|
|
|
2022-12-26 02:08:17 +00:00
|
|
|
connect( this, &BlockedNetworkReply::finishedSignal, this, &BlockedNetworkReply::finishedSlot, Qt::QueuedConnection );
|
2009-08-31 12:18:08 +00:00
|
|
|
|
|
|
|
emit finishedSignal(); // This way we call readyRead()/finished() sometime later
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void BlockedNetworkReply::finishedSlot()
|
|
|
|
{
|
|
|
|
emit readyRead();
|
2023-03-26 06:46:27 +00:00
|
|
|
setFinished( true );
|
2009-08-31 12:18:08 +00:00
|
|
|
emit finished();
|
|
|
|
}
|
2021-08-05 06:57:22 +00:00
|
|
|
|
2022-12-22 01:08:41 +00:00
|
|
|
LocalSchemeHandler::LocalSchemeHandler( ArticleNetworkAccessManager & articleNetMgr, QObject * parent ):
|
|
|
|
QWebEngineUrlSchemeHandler( parent ),
|
|
|
|
mManager( articleNetMgr )
|
|
|
|
{
|
2021-08-14 07:25:10 +00:00
|
|
|
}
|
2021-10-03 11:28:26 +00:00
|
|
|
|
2021-10-02 12:48:49 +00:00
|
|
|
void LocalSchemeHandler::requestStarted( QWebEngineUrlRequestJob * requestJob )
|
2021-08-14 07:25:10 +00:00
|
|
|
{
|
2023-10-15 23:37:10 +00:00
|
|
|
QUrl const url = requestJob->requestUrl();
|
2022-05-13 16:00:23 +00:00
|
|
|
QNetworkRequest request;
|
|
|
|
request.setUrl( url );
|
2021-09-24 12:29:13 +00:00
|
|
|
|
2022-05-27 15:10:50 +00:00
|
|
|
//all the url reached here must be either gdlookup or bword scheme.
|
2023-10-15 23:37:10 +00:00
|
|
|
auto [ schemeValid, word ] = Utils::Url::getQueryWord( url );
|
2022-05-27 15:10:50 +00:00
|
|
|
// or the condition can be (!queryWord.first || word.isEmpty())
|
|
|
|
// ( queryWord.first && word.isEmpty() ) is only part of the above condition.
|
2023-10-15 23:37:10 +00:00
|
|
|
if ( schemeValid && word.isEmpty() ) {
|
2022-05-27 15:10:50 +00:00
|
|
|
// invalid gdlookup url.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-05-18 23:14:36 +00:00
|
|
|
QNetworkReply * reply = this->mManager.getArticleReply( request );
|
2023-03-26 06:46:27 +00:00
|
|
|
requestJob->reply( "text/html", reply );
|
2022-05-13 16:00:23 +00:00
|
|
|
connect( requestJob, &QObject::destroyed, reply, &QObject::deleteLater );
|
2021-08-14 07:25:10 +00:00
|
|
|
}
|