2022-06-01 21:03:51 +01:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
2023-03-02 10:19:26 +00:00
|
|
|
* Prism Launcher - Minecraft Launcher
|
2022-06-01 21:03:51 +01:00
|
|
|
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2023-03-01 09:54:49 +00:00
|
|
|
* Copyright (C) 2023 TheKodeToad <TheKodeToad@proton.me>
|
2023-04-03 03:40:32 +01:00
|
|
|
* Copyright (C) 2023 Rachel Powers <508861+Ryex@users.noreply.github.com>
|
2022-06-01 21:03:51 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2022-05-28 20:53:12 +01:00
|
|
|
|
|
|
|
#include "Upload.h"
|
|
|
|
|
2023-06-25 20:36:27 +01:00
|
|
|
#include <memory>
|
2022-05-28 20:53:12 +01:00
|
|
|
#include <utility>
|
|
|
|
#include "BuildConfig.h"
|
2023-06-25 20:02:46 +01:00
|
|
|
#include "ByteArraySink.h"
|
|
|
|
|
|
|
|
#if defined(LAUNCHER_APPLICATION)
|
2022-05-28 20:53:12 +01:00
|
|
|
#include "Application.h"
|
2023-06-25 20:02:46 +01:00
|
|
|
#endif
|
2022-05-28 20:53:12 +01:00
|
|
|
|
2023-04-03 03:43:43 +01:00
|
|
|
#include "net/Logging.h"
|
2023-04-01 03:25:01 +01:00
|
|
|
|
2022-05-28 20:53:12 +01:00
|
|
|
namespace Net {
|
|
|
|
|
2023-06-25 20:02:46 +01:00
|
|
|
bool Upload::abort()
|
|
|
|
{
|
|
|
|
if (m_reply) {
|
|
|
|
m_reply->abort();
|
|
|
|
} else {
|
|
|
|
m_state = State::AbortedByUser;
|
2022-06-07 00:16:13 +01:00
|
|
|
}
|
2023-06-25 20:02:46 +01:00
|
|
|
return true;
|
|
|
|
}
|
2022-06-07 00:16:13 +01:00
|
|
|
|
2023-06-25 20:02:46 +01:00
|
|
|
void Upload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
|
|
|
{
|
|
|
|
setProgress(bytesReceived, bytesTotal);
|
|
|
|
}
|
2022-05-28 20:53:12 +01:00
|
|
|
|
2023-06-25 20:02:46 +01:00
|
|
|
void Upload::downloadError(QNetworkReply::NetworkError error)
|
|
|
|
{
|
|
|
|
if (error == QNetworkReply::OperationCanceledError) {
|
|
|
|
qCCritical(taskUploadLogC) << getUid().toString() << "Aborted " << m_url.toString();
|
|
|
|
m_state = State::AbortedByUser;
|
|
|
|
} else {
|
|
|
|
// error happened during download.
|
|
|
|
qCCritical(taskUploadLogC) << getUid().toString() << "Failed " << m_url.toString() << " with reason " << error;
|
|
|
|
m_state = State::Failed;
|
2022-05-28 20:53:12 +01:00
|
|
|
}
|
2023-06-25 20:02:46 +01:00
|
|
|
}
|
2022-05-28 20:53:12 +01:00
|
|
|
|
2023-06-25 20:02:46 +01:00
|
|
|
void Upload::sslErrors(const QList<QSslError>& errors)
|
|
|
|
{
|
|
|
|
int i = 1;
|
|
|
|
for (const auto& error : errors) {
|
|
|
|
qCCritical(taskUploadLogC) << getUid().toString() << "Upload" << m_url.toString() << "SSL Error #" << i << " : "
|
|
|
|
<< error.errorString();
|
|
|
|
auto cert = error.certificate();
|
|
|
|
qCCritical(taskUploadLogC) << getUid().toString() << "Certificate in question:\n" << cert.toText();
|
|
|
|
i++;
|
2022-05-28 20:53:12 +01:00
|
|
|
}
|
2023-06-25 20:02:46 +01:00
|
|
|
}
|
2022-05-28 20:53:12 +01:00
|
|
|
|
2023-06-25 20:02:46 +01:00
|
|
|
bool Upload::handleRedirect()
|
|
|
|
{
|
|
|
|
QUrl redirect = m_reply->header(QNetworkRequest::LocationHeader).toUrl();
|
|
|
|
if (!redirect.isValid()) {
|
|
|
|
if (!m_reply->hasRawHeader("Location")) {
|
|
|
|
// no redirect -> it's fine to continue
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// there is a Location header, but it's not correct. we need to apply some workarounds...
|
|
|
|
QByteArray redirectBA = m_reply->rawHeader("Location");
|
|
|
|
if (redirectBA.size() == 0) {
|
|
|
|
// empty, yet present redirect header? WTF?
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QString redirectStr = QString::fromUtf8(redirectBA);
|
2022-05-28 20:53:12 +01:00
|
|
|
|
2023-06-25 20:02:46 +01:00
|
|
|
if (redirectStr.startsWith("//")) {
|
2022-05-28 20:53:12 +01:00
|
|
|
/*
|
2023-06-25 20:02:46 +01:00
|
|
|
* IF the URL begins with //, we need to insert the URL scheme.
|
|
|
|
* See: https://bugreports.qt.io/browse/QTBUG-41061
|
|
|
|
* See: http://tools.ietf.org/html/rfc3986#section-4.2
|
2022-05-28 20:53:12 +01:00
|
|
|
*/
|
2023-06-25 20:02:46 +01:00
|
|
|
redirectStr = m_reply->url().scheme() + ":" + redirectStr;
|
|
|
|
} else if (redirectStr.startsWith("/")) {
|
|
|
|
/*
|
|
|
|
* IF the URL begins with /, we need to process it as a relative URL
|
|
|
|
*/
|
|
|
|
auto url = m_reply->url();
|
|
|
|
url.setPath(redirectStr, QUrl::TolerantMode);
|
|
|
|
redirectStr = url.toString();
|
2022-05-28 20:53:12 +01:00
|
|
|
}
|
|
|
|
|
2023-06-25 20:02:46 +01:00
|
|
|
/*
|
|
|
|
* Next, make sure the URL is parsed in tolerant mode. Qt doesn't parse the location header in tolerant mode, which causes issues.
|
|
|
|
* FIXME: report Qt bug for this
|
|
|
|
*/
|
|
|
|
redirect = QUrl(redirectStr, QUrl::TolerantMode);
|
|
|
|
if (!redirect.isValid()) {
|
|
|
|
qCWarning(taskUploadLogC) << getUid().toString() << "Failed to parse redirect URL:" << redirectStr;
|
|
|
|
downloadError(QNetworkReply::ProtocolFailure);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
qCDebug(taskUploadLogC) << getUid().toString() << "Fixed location header:" << redirect;
|
|
|
|
} else {
|
|
|
|
qCDebug(taskUploadLogC) << getUid().toString() << "Location header:" << redirect;
|
2022-05-28 20:53:12 +01:00
|
|
|
}
|
|
|
|
|
2023-06-25 20:02:46 +01:00
|
|
|
m_url = QUrl(redirect.toString());
|
|
|
|
qCDebug(taskUploadLogC) << getUid().toString() << "Following redirect to " << m_url.toString();
|
|
|
|
startAction(m_network);
|
|
|
|
return true;
|
|
|
|
}
|
2022-05-28 20:53:12 +01:00
|
|
|
|
2023-06-25 20:02:46 +01:00
|
|
|
void Upload::downloadFinished()
|
|
|
|
{
|
|
|
|
// handle HTTP redirection first
|
|
|
|
// very unlikely for post requests, still can happen
|
|
|
|
if (handleRedirect()) {
|
|
|
|
qCDebug(taskUploadLogC) << getUid().toString() << "Upload redirected:" << m_url.toString();
|
|
|
|
return;
|
|
|
|
}
|
2022-05-28 20:53:12 +01:00
|
|
|
|
2023-06-25 20:02:46 +01:00
|
|
|
// if the download failed before this point ...
|
|
|
|
if (m_state == State::Succeeded) {
|
|
|
|
qCDebug(taskUploadLogC) << getUid().toString() << "Upload failed but we are allowed to proceed:" << m_url.toString();
|
|
|
|
m_sink->abort();
|
|
|
|
m_reply.reset();
|
|
|
|
emitSucceeded();
|
|
|
|
return;
|
|
|
|
} else if (m_state == State::Failed) {
|
|
|
|
qCDebug(taskUploadLogC) << getUid().toString() << "Upload failed in previous step:" << m_url.toString();
|
|
|
|
m_sink->abort();
|
|
|
|
m_reply.reset();
|
|
|
|
emitFailed("");
|
|
|
|
return;
|
|
|
|
} else if (m_state == State::AbortedByUser) {
|
|
|
|
qCDebug(taskUploadLogC) << getUid().toString() << "Upload aborted in previous step:" << m_url.toString();
|
|
|
|
m_sink->abort();
|
|
|
|
m_reply.reset();
|
|
|
|
emitAborted();
|
|
|
|
return;
|
|
|
|
}
|
2022-05-28 20:53:12 +01:00
|
|
|
|
2023-06-25 20:02:46 +01:00
|
|
|
// make sure we got all the remaining data, if any
|
|
|
|
auto data = m_reply->readAll();
|
|
|
|
if (data.size()) {
|
|
|
|
qCDebug(taskUploadLogC) << getUid().toString() << "Writing extra" << data.size() << "bytes";
|
|
|
|
m_state = m_sink->write(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise, finalize the whole graph
|
|
|
|
m_state = m_sink->finalize(*m_reply.get());
|
|
|
|
if (m_state != State::Succeeded) {
|
|
|
|
qCDebug(taskUploadLogC) << getUid().toString() << "Upload failed to finalize:" << m_url.toString();
|
|
|
|
m_sink->abort();
|
2022-05-28 20:53:12 +01:00
|
|
|
m_reply.reset();
|
2023-06-25 20:02:46 +01:00
|
|
|
emitFailed("");
|
|
|
|
return;
|
2022-05-28 20:53:12 +01:00
|
|
|
}
|
2023-06-25 20:02:46 +01:00
|
|
|
m_reply.reset();
|
|
|
|
qCDebug(taskUploadLogC) << getUid().toString() << "Upload succeeded:" << m_url.toString();
|
|
|
|
emitSucceeded();
|
|
|
|
}
|
2022-05-28 20:53:12 +01:00
|
|
|
|
2023-06-25 20:02:46 +01:00
|
|
|
void Upload::downloadReadyRead()
|
|
|
|
{
|
|
|
|
if (m_state == State::Running) {
|
|
|
|
auto data = m_reply->readAll();
|
|
|
|
m_state = m_sink->write(data);
|
2022-05-28 20:53:12 +01:00
|
|
|
}
|
2023-06-25 20:02:46 +01:00
|
|
|
}
|
2022-05-28 20:53:12 +01:00
|
|
|
|
2023-06-25 20:02:46 +01:00
|
|
|
void Upload::executeTask()
|
|
|
|
{
|
|
|
|
setStatus(tr("Uploading %1").arg(m_url.toString()));
|
2022-05-28 20:53:12 +01:00
|
|
|
|
2023-06-25 20:02:46 +01:00
|
|
|
if (m_state == State::AbortedByUser) {
|
|
|
|
qCWarning(taskUploadLogC) << getUid().toString() << "Attempt to start an aborted Upload:" << m_url.toString();
|
|
|
|
emitAborted();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QNetworkRequest request(m_url);
|
|
|
|
m_state = m_sink->init(request);
|
|
|
|
switch (m_state) {
|
|
|
|
case State::Succeeded:
|
|
|
|
emitSucceeded();
|
|
|
|
qCDebug(taskUploadLogC) << getUid().toString() << "Upload cache hit " << m_url.toString();
|
2022-05-28 20:53:12 +01:00
|
|
|
return;
|
2023-06-25 20:02:46 +01:00
|
|
|
case State::Running:
|
|
|
|
qCDebug(taskUploadLogC) << getUid().toString() << "Uploading " << m_url.toString();
|
|
|
|
break;
|
|
|
|
case State::Inactive:
|
|
|
|
case State::Failed:
|
|
|
|
emitFailed("");
|
|
|
|
return;
|
|
|
|
case State::AbortedByUser:
|
|
|
|
emitAborted();
|
|
|
|
return;
|
|
|
|
}
|
2022-05-28 20:53:12 +01:00
|
|
|
|
2023-06-25 20:02:46 +01:00
|
|
|
#if defined(LAUNCHER_APPLICATION)
|
|
|
|
auto user_agent = APPLICATION->getUserAgent();
|
|
|
|
#else
|
|
|
|
auto user_agent = BuildConfig.USER_AGENT;
|
|
|
|
#endif
|
|
|
|
request.setHeader(QNetworkRequest::UserAgentHeader, user_agent.toUtf8());
|
|
|
|
|
|
|
|
for (auto& header_proxy : m_headerProxies) {
|
|
|
|
header_proxy->writeHeaders(request);
|
|
|
|
}
|
2023-03-01 09:54:49 +00:00
|
|
|
|
2023-06-25 20:02:46 +01:00
|
|
|
// TODO other types of post requests ?
|
|
|
|
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
|
|
QNetworkReply* rep = m_network->post(request, m_post_data);
|
2022-05-28 20:53:12 +01:00
|
|
|
|
2023-06-25 20:02:46 +01:00
|
|
|
m_reply.reset(rep);
|
|
|
|
connect(rep, &QNetworkReply::downloadProgress, this, &Upload::downloadProgress);
|
|
|
|
connect(rep, &QNetworkReply::finished, this, &Upload::downloadFinished);
|
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) // QNetworkReply::errorOccurred added in 5.15
|
|
|
|
connect(rep, &QNetworkReply::errorOccurred, this, &Upload::downloadError);
|
2023-04-18 01:51:34 +01:00
|
|
|
#else
|
2023-06-25 20:02:46 +01:00
|
|
|
connect(rep, QOverload<QNetworkReply::NetworkError>::of(&QNetworkReply::error), this, &Upload::downloadError);
|
2023-04-18 01:51:34 +01:00
|
|
|
#endif
|
2023-06-25 20:02:46 +01:00
|
|
|
connect(rep, &QNetworkReply::sslErrors, this, &Upload::sslErrors);
|
|
|
|
connect(rep, &QNetworkReply::readyRead, this, &Upload::downloadReadyRead);
|
|
|
|
}
|
2022-05-28 20:53:12 +01:00
|
|
|
|
2023-06-15 20:59:41 +01:00
|
|
|
Upload::Ptr Upload::makeByteArray(QUrl url, std::shared_ptr<QByteArray> output, QByteArray m_post_data)
|
2023-06-25 20:02:46 +01:00
|
|
|
{
|
|
|
|
auto up = makeShared<Upload>();
|
|
|
|
up->m_url = std::move(url);
|
|
|
|
up->m_sink.reset(new ByteArraySink(output));
|
|
|
|
up->m_post_data = std::move(m_post_data);
|
|
|
|
return up;
|
|
|
|
}
|
|
|
|
} // namespace Net
|