2023-04-17 20:12:27 +00:00
|
|
|
#include "resourceschemehandler.hh"
|
2021-08-28 02:35:23 +00:00
|
|
|
|
2022-12-22 01:08:41 +00:00
|
|
|
ResourceSchemeHandler::ResourceSchemeHandler( ArticleNetworkAccessManager & articleNetMgr, QObject * parent ):
|
|
|
|
QWebEngineUrlSchemeHandler( parent ),
|
|
|
|
mManager( articleNetMgr )
|
|
|
|
{
|
2021-08-28 02:35:23 +00:00
|
|
|
}
|
2022-01-09 01:52:40 +00:00
|
|
|
void ResourceSchemeHandler::requestStarted( QWebEngineUrlRequestJob * requestJob )
|
2021-08-28 02:35:23 +00:00
|
|
|
{
|
|
|
|
QUrl url = requestJob->requestUrl();
|
|
|
|
|
|
|
|
QNetworkRequest request;
|
2022-01-09 03:36:48 +00:00
|
|
|
request.setUrl( url );
|
2022-04-14 14:45:34 +00:00
|
|
|
request.setAttribute( QNetworkRequest::CacheLoadControlAttribute, QNetworkRequest::PreferCache );
|
2022-05-18 23:14:36 +00:00
|
|
|
QNetworkReply * reply = this->mManager.getArticleReply( request );
|
2022-01-08 04:22:41 +00:00
|
|
|
connect( reply, &QNetworkReply::finished, requestJob, [ = ]() {
|
|
|
|
if ( reply->error() == QNetworkReply::ContentNotFoundError ) {
|
|
|
|
requestJob->fail( QWebEngineUrlRequestJob::UrlNotFound );
|
|
|
|
return;
|
|
|
|
}
|
2022-01-09 03:36:48 +00:00
|
|
|
if ( reply->error() != QNetworkReply::NoError ) {
|
|
|
|
qDebug() << "resource handler failed:" << reply->error() << ":" << reply->request().url();
|
|
|
|
requestJob->fail( QWebEngineUrlRequestJob::RequestFailed );
|
|
|
|
return;
|
|
|
|
}
|
2022-01-08 04:22:41 +00:00
|
|
|
QMimeType mineType = db.mimeTypeForUrl( url );
|
|
|
|
QString contentType = mineType.name();
|
|
|
|
// Reply segment
|
|
|
|
requestJob->reply( contentType.toLatin1(), reply );
|
|
|
|
} );
|
2022-03-22 12:29:37 +00:00
|
|
|
connect( requestJob, &QObject::destroyed, reply, &QObject::deleteLater );
|
2021-08-28 02:35:23 +00:00
|
|
|
}
|