Removed AuthRequest and NetAction

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97
2024-05-13 23:25:08 +03:00
parent f15981d786
commit 3336f8107c
50 changed files with 381 additions and 743 deletions

View File

@@ -21,7 +21,6 @@
#include "ByteArraySink.h"
#include "ChecksumValidator.h"
#include "MetaCacheSink.h"
#include "net/NetAction.h"
namespace Net {

View File

@@ -19,9 +19,6 @@
#include "net/ApiUpload.h"
#include "ByteArraySink.h"
#include "ChecksumValidator.h"
#include "MetaCacheSink.h"
#include "net/NetAction.h"
namespace Net {

View File

@@ -47,8 +47,6 @@
#include "ChecksumValidator.h"
#include "MetaCacheSink.h"
#include "net/NetAction.h"
namespace Net {
#if defined(LAUNCHER_APPLICATION)

View File

@@ -1,100 +0,0 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
* Copyright (C) 2023 Rachel Powers <508861+Ryex@users.noreply.github.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* This file incorporates work covered by the following copyright and
* permission notice:
*
* Copyright 2013-2021 MultiMC Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <QNetworkReply>
#include <QUrl>
#include "QObjectPtr.h"
#include "tasks/Task.h"
#include "HeaderProxy.h"
class NetAction : public Task {
Q_OBJECT
protected:
explicit NetAction() : Task() {}
public:
using Ptr = shared_qobject_ptr<NetAction>;
virtual ~NetAction() = default;
QUrl url() { return m_url; }
void setNetwork(shared_qobject_ptr<QNetworkAccessManager> network) { m_network = network; }
void addHeaderProxy(Net::HeaderProxy* proxy) { m_headerProxies.push_back(std::shared_ptr<Net::HeaderProxy>(proxy)); }
virtual void init() = 0;
protected slots:
virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) = 0;
virtual void downloadError(QNetworkReply::NetworkError error) = 0;
virtual void downloadFinished() = 0;
virtual void downloadReadyRead() = 0;
virtual void sslErrors(const QList<QSslError>& errors)
{
int i = 1;
for (auto error : errors) {
qCritical() << "Network SSL Error #" << i << " : " << error.errorString();
auto cert = error.certificate();
qCritical() << "Certificate in question:\n" << cert.toText();
i++;
}
}
public slots:
void startAction(shared_qobject_ptr<QNetworkAccessManager> network)
{
m_network = network;
executeTask();
}
protected:
void executeTask() override {}
public:
shared_qobject_ptr<QNetworkAccessManager> m_network;
/// the network reply
unique_qobject_ptr<QNetworkReply> m_reply;
/// source URL
QUrl m_url;
std::vector<std::shared_ptr<Net::HeaderProxy>> m_headerProxies;
};

View File

@@ -36,6 +36,7 @@
*/
#include "NetJob.h"
#include "net/NetRequest.h"
#include "tasks/ConcurrentTask.h"
#if defined(LAUNCHER_APPLICATION)
#include "Application.h"
@@ -48,7 +49,7 @@ NetJob::NetJob(QString job_name, shared_qobject_ptr<QNetworkAccessManager> netwo
#endif
}
auto NetJob::addNetAction(NetAction::Ptr action) -> bool
auto NetJob::addNetAction(Net::NetRequest::Ptr action) -> bool
{
action->setNetwork(m_network);
@@ -111,11 +112,11 @@ auto NetJob::abort() -> bool
return fullyAborted;
}
auto NetJob::getFailedActions() -> QList<NetAction*>
auto NetJob::getFailedActions() -> QList<Net::NetRequest*>
{
QList<NetAction*> failed;
QList<Net::NetRequest*> failed;
for (auto index : m_failed) {
failed.push_back(dynamic_cast<NetAction*>(index.get()));
failed.push_back(dynamic_cast<Net::NetRequest*>(index.get()));
}
return failed;
}
@@ -124,7 +125,7 @@ auto NetJob::getFailedFiles() -> QList<QString>
{
QList<QString> failed;
for (auto index : m_failed) {
failed.append(static_cast<NetAction*>(index.get())->url().toString());
failed.append(static_cast<Net::NetRequest*>(index.get())->url().toString());
}
return failed;
}

View File

@@ -39,7 +39,7 @@
#include <QtNetwork>
#include <QObject>
#include "NetAction.h"
#include "net/NetRequest.h"
#include "tasks/ConcurrentTask.h"
// Those are included so that they are also included by anyone using NetJob
@@ -58,9 +58,9 @@ class NetJob : public ConcurrentTask {
auto size() const -> int;
auto canAbort() const -> bool override;
auto addNetAction(NetAction::Ptr action) -> bool;
auto addNetAction(Net::NetRequest::Ptr action) -> bool;
auto getFailedActions() -> QList<NetAction*>;
auto getFailedActions() -> QList<Net::NetRequest*>;
auto getFailedFiles() -> QList<QString>;
public slots:

View File

@@ -37,6 +37,7 @@
*/
#include "NetRequest.h"
#include <qnetworkreply.h>
#include <QUrl>
#include <QDateTime>
@@ -48,8 +49,6 @@
#endif
#include "BuildConfig.h"
#include "net/NetAction.h"
#include "MMCTime.h"
#include "StringUtils.h"
@@ -105,20 +104,18 @@ void NetRequest::executeTask()
for (auto& header_proxy : m_headerProxies) {
header_proxy->writeHeaders(request);
}
// TODO remove duplication
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
request.setTransferTimeout();
#endif
request.setTransferTimeout(QNetworkRequest::DefaultTransferTimeoutConstant);
m_last_progress_time = m_clock.now();
m_last_progress_bytes = 0;
QNetworkReply* rep = getReply(request);
auto rep = getReply(request);
if (rep == nullptr) // it failed
return;
m_reply.reset(rep);
connect(rep, &QNetworkReply::downloadProgress, this, &NetRequest::downloadProgress);
connect(rep, &QNetworkReply::uploadProgress, this, &NetRequest::onProgress);
connect(rep, &QNetworkReply::downloadProgress, this, &NetRequest::onProgress);
connect(rep, &QNetworkReply::finished, this, &NetRequest::downloadFinished);
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) // QNetworkReply::errorOccurred added in 5.15
connect(rep, &QNetworkReply::errorOccurred, this, &NetRequest::downloadError);
@@ -129,7 +126,7 @@ void NetRequest::executeTask()
connect(rep, &QNetworkReply::readyRead, this, &NetRequest::downloadReadyRead);
}
void NetRequest::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
void NetRequest::onProgress(qint64 bytesReceived, qint64 bytesTotal)
{
auto now = m_clock.now();
auto elapsed = now - m_last_progress_time;
@@ -237,7 +234,7 @@ auto NetRequest::handleRedirect() -> bool
m_url = QUrl(redirect.toString());
qCDebug(logCat) << getUid().toString() << "Following redirect to " << m_url.toString();
startAction(m_network);
executeTask();
return true;
}
@@ -334,4 +331,23 @@ auto NetRequest::abort() -> bool
return true;
}
int NetRequest::replyStatusCode() const
{
return m_reply ? m_reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() : -1;
}
QNetworkReply::NetworkError NetRequest::error() const
{
return m_reply ? m_reply->error() : QNetworkReply::NoError;
}
QUrl NetRequest::url() const
{
return m_url;
}
QString NetRequest::errorString() const
{
return m_reply ? m_reply->errorString() : "";
}
} // namespace Net

View File

@@ -39,20 +39,23 @@
#pragma once
#include <qloggingcategory.h>
#include <QNetworkReply>
#include <QUrl>
#include <chrono>
#include "NetAction.h"
#include "HeaderProxy.h"
#include "Sink.h"
#include "Validator.h"
#include "QObjectPtr.h"
#include "net/Logging.h"
#include "tasks/Task.h"
namespace Net {
class NetRequest : public NetAction {
class NetRequest : public Task {
Q_OBJECT
protected:
explicit NetRequest() : NetAction() {}
explicit NetRequest() : Task() {}
public:
using Ptr = shared_qobject_ptr<class NetRequest>;
@@ -61,26 +64,30 @@ class NetRequest : public NetAction {
public:
~NetRequest() override = default;
void init() override {}
public:
void addValidator(Validator* v);
auto abort() -> bool override;
auto canAbort() const -> bool override { return true; }
void setNetwork(shared_qobject_ptr<QNetworkAccessManager> network) { m_network = network; }
void addHeaderProxy(Net::HeaderProxy* proxy) { m_headerProxies.push_back(std::shared_ptr<Net::HeaderProxy>(proxy)); }
virtual void init() {}
QUrl url() const;
int replyStatusCode() const;
QNetworkReply::NetworkError error() const;
QString errorString() const;
private:
auto handleRedirect() -> bool;
virtual QNetworkReply* getReply(QNetworkRequest&) = 0;
protected slots:
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) override;
void downloadError(QNetworkReply::NetworkError error) override;
void sslErrors(const QList<QSslError>& errors) override;
void downloadFinished() override;
void downloadReadyRead() override;
public slots:
void onProgress(qint64 bytesReceived, qint64 bytesTotal);
void downloadError(QNetworkReply::NetworkError error);
void sslErrors(const QList<QSslError>& errors);
void downloadFinished();
void downloadReadyRead();
void executeTask() override;
protected:
@@ -93,6 +100,15 @@ class NetRequest : public NetAction {
std::chrono::steady_clock m_clock;
std::chrono::time_point<std::chrono::steady_clock> m_last_progress_time;
qint64 m_last_progress_bytes;
shared_qobject_ptr<QNetworkAccessManager> m_network;
/// the network reply
unique_qobject_ptr<QNetworkReply> m_reply;
/// source URL
QUrl m_url;
std::vector<std::shared_ptr<Net::HeaderProxy>> m_headerProxies;
};
} // namespace Net

View File

@@ -35,9 +35,8 @@
#pragma once
#include "net/NetAction.h"
#include "Validator.h"
#include "tasks/Task.h"
namespace Net {
class Sink {

View File

@@ -34,7 +34,7 @@
#pragma once
#include "net/NetAction.h"
#include <QNetworkReply>
namespace Net {
class Validator {