diff --git a/src/article_netmgr.cc b/src/article_netmgr.cc index d023bd71..6b18e835 100644 --- a/src/article_netmgr.cc +++ b/src/article_netmgr.cc @@ -260,9 +260,10 @@ sptr< Dictionary::DataRequest > ArticleNetworkAccessManager::getResource( QUrl c if ( ( url.scheme() == "bres" || url.scheme() == "gdau" || url.scheme() == "gdvideo" || url.scheme() == "gico" ) && url.path().size() ) { - //GD_DPRINTF( "Get %s\n", req.url().host().toLocal8Bit().data() ); - //GD_DPRINTF( "Get %s\n", req.url().path().toLocal8Bit().data() ); + + QMimeType mineType = db.mimeTypeForUrl( url ); + contentType = mineType.name(); string id = url.host().toStdString(); bool search = ( id == "search" ); diff --git a/src/resourceschemehandler.cc b/src/resourceschemehandler.cc index 1e97543e..a4cd2ec6 100644 --- a/src/resourceschemehandler.cc +++ b/src/resourceschemehandler.cc @@ -7,26 +7,46 @@ ResourceSchemeHandler::ResourceSchemeHandler( ArticleNetworkAccessManager & arti } void ResourceSchemeHandler::requestStarted( QWebEngineUrlRequestJob * requestJob ) { - QUrl url = requestJob->requestUrl(); - - QNetworkRequest request; - request.setUrl( url ); - request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache ); - QNetworkReply * reply = this->mManager.getArticleReply( request ); - connect( reply, &QNetworkReply::finished, requestJob, [ = ]() { - if ( reply->error() == QNetworkReply::ContentNotFoundError ) { - requestJob->fail( QWebEngineUrlRequestJob::UrlNotFound ); - return; - } - if ( reply->error() != QNetworkReply::NoError ) { - qDebug() << "resource handler failed:" << reply->error() << ":" << reply->request().url(); - requestJob->fail( QWebEngineUrlRequestJob::RequestFailed ); - return; - } - QMimeType mineType = db.mimeTypeForUrl( url ); - QString contentType = mineType.name(); - // Reply segment - requestJob->reply( contentType.toLatin1(), reply ); - } ); - connect( requestJob, &QObject::destroyed, reply, &QObject::deleteLater ); + const QUrl url = requestJob->requestUrl(); + QString content_type; + const QMimeType mineType = db.mimeTypeForUrl( url ); + const sptr< Dictionary::DataRequest > reply = this->mManager.getResource( url, content_type ); + content_type = mineType.name(); + if ( reply->isFinished() ) { + replyJob( reply, requestJob, content_type ); + } + else + connect( reply.get(), &Dictionary::DataRequest::finished, requestJob, [ = ]() { + replyJob( reply, requestJob, content_type ); + } ); +} + + +void ResourceSchemeHandler::replyJob( sptr< Dictionary::DataRequest > reply, + QWebEngineUrlRequestJob * requestJob, + QString content_type ) +{ + if ( !reply.get() ) { + requestJob->fail( QWebEngineUrlRequestJob::UrlNotFound ); + return; + } + const auto & data = reply->getFullData(); + if ( data.empty() ) { + requestJob->fail( QWebEngineUrlRequestJob::UrlNotFound ); + return; + } + QByteArray * ba = new QByteArray( data.data(), data.size() ); + QBuffer * buffer = new QBuffer( ba ); + buffer->open( QBuffer::ReadOnly ); + buffer->seek( 0 ); + + // Reply segment + requestJob->reply( content_type.toLatin1(), buffer ); + + connect( requestJob, &QObject::destroyed, buffer, [ = ]() { + buffer->close(); + ba->clear(); + delete ba; + buffer->deleteLater(); + } ); } diff --git a/src/resourceschemehandler.hh b/src/resourceschemehandler.hh index 1f8cf248..3df002ca 100644 --- a/src/resourceschemehandler.hh +++ b/src/resourceschemehandler.hh @@ -12,6 +12,7 @@ public: void requestStarted( QWebEngineUrlRequestJob * requestJob ); protected: + void replyJob( sptr< Dictionary::DataRequest > reply, QWebEngineUrlRequestJob * requestJob, QString content_type ); private: ArticleNetworkAccessManager & mManager;