1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 10:46:35 +01:00

Fix KIO scheme handler

BUG: 491247

Signed-off-by: Juraj Oravec <jurajoravec@mailo.com>
This commit is contained in:
Juraj Oravec 2024-11-17 00:16:13 +01:00
parent d74c36eafb
commit b456c18bf6

View File

@ -22,10 +22,8 @@
#include <QNetworkReply> #include <QNetworkReply>
#include <QWebEngineUrlRequestJob> #include <QWebEngineUrlRequestJob>
#include <kio_version.h> #include <KIO/StoredTransferJob>
#include <QNetworkAccessManager>
Q_GLOBAL_STATIC_WITH_ARGS(QNetworkAccessManager, s_knam, (nullptr))
KIOSchemeHandler::KIOSchemeHandler(const QString &protocol, QObject *parent) KIOSchemeHandler::KIOSchemeHandler(const QString &protocol, QObject *parent)
: QWebEngineUrlSchemeHandler(parent) : QWebEngineUrlSchemeHandler(parent)
@ -47,19 +45,30 @@ void KIOSchemeHandler::requestStarted(QWebEngineUrlRequestJob *job)
} }
QPointer<QWebEngineUrlRequestJob> jobPtr = job; QPointer<QWebEngineUrlRequestJob> jobPtr = job;
QNetworkReply *reply = s_knam()->get(QNetworkRequest(job->requestUrl()));
connect(reply, &QNetworkReply::finished, this, [=]() { KIO::StoredTransferJob *kioJob = KIO::storedGet(job->requestUrl(), KIO::NoReload,
KIO::HideProgressInfo);
kioJob->setRedirectionHandlingEnabled(false);
connect(kioJob, &KIO::StoredTransferJob::result, this, [=]() {
kioJob->deleteLater();
if (!jobPtr) { if (!jobPtr) {
reply->deleteLater();
return; return;
} }
if (reply->error() != QNetworkReply::NoError) {
reply->deleteLater(); if (kioJob->error() == KJob::NoError) {
qWarning() << "Error:" << reply->errorString(); if (kioJob->redirectUrl().isValid()) {
job->fail(QWebEngineUrlRequestJob::RequestFailed); job->redirect(kioJob->redirectUrl());
} else { } else {
reply->setParent(job); QBuffer *buffer = new QBuffer;
job->reply(reply->header(QNetworkRequest::ContentTypeHeader).toByteArray(), reply); buffer->open(QBuffer::ReadWrite);
buffer->write(kioJob->data());
buffer->seek(0);
connect(buffer, &QIODevice::aboutToClose, buffer, &QObject::deleteLater);
job->reply(kioJob->mimetype().toUtf8(), buffer);
}
}
else {
job->fail(QWebEngineUrlRequestJob::RequestFailed);
} }
}); });
} }