PrismLauncher/launcher/net/Download.cpp

305 lines
9.7 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: GPL-3.0-only
/*
* PolyMC - Minecraft Launcher
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
*
* 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.
*/
#include "Download.h"
#include <QDateTime>
2022-05-08 08:22:50 +01:00
#include <QFileInfo>
2022-05-08 08:22:50 +01:00
#include "ByteArraySink.h"
#include "ChecksumValidator.h"
2022-05-08 08:22:50 +01:00
#include "FileSystem.h"
#include "MetaCacheSink.h"
#include "BuildConfig.h"
2022-05-20 18:56:27 +01:00
#include "Application.h"
namespace Net {
2022-05-08 08:22:50 +01:00
Download::Download() : NetAction()
{
m_state = State::Inactive;
}
auto Download::makeCached(QUrl url, MetaEntryPtr entry, Options options) -> Download::Ptr
{
auto* dl = new Download();
2018-07-15 13:51:05 +01:00
dl->m_url = url;
dl->m_options = options;
auto md5Node = new ChecksumValidator(QCryptographicHash::Md5);
auto cachedNode = new MetaCacheSink(entry, md5Node);
dl->m_sink.reset(cachedNode);
return dl;
}
auto Download::makeByteArray(QUrl url, QByteArray* output, Options options) -> Download::Ptr
{
auto* dl = new Download();
2018-07-15 13:51:05 +01:00
dl->m_url = url;
dl->m_options = options;
dl->m_sink.reset(new ByteArraySink(output));
return dl;
}
auto Download::makeFile(QUrl url, QString path, Options options) -> Download::Ptr
{
auto* dl = new Download();
2018-07-15 13:51:05 +01:00
dl->m_url = url;
dl->m_options = options;
dl->m_sink.reset(new FileSink(path));
return dl;
}
2022-05-08 08:22:50 +01:00
void Download::addValidator(Validator* v)
{
2018-07-15 13:51:05 +01:00
m_sink->addValidator(v);
}
void Download::executeTask()
{
setStatus(tr("Downloading %1").arg(m_url.toString()));
if (getState() == Task::State::AbortedByUser) {
2018-07-15 13:51:05 +01:00
qWarning() << "Attempt to start an aborted Download:" << m_url.toString();
emitAborted();
2018-07-15 13:51:05 +01:00
return;
}
2018-07-15 13:51:05 +01:00
QNetworkRequest request(m_url);
m_state = m_sink->init(request);
switch (m_state) {
case State::Succeeded:
emit succeeded();
2018-07-15 13:51:05 +01:00
qDebug() << "Download cache hit " << m_url.toString();
return;
case State::Running:
2018-07-15 13:51:05 +01:00
qDebug() << "Downloading " << m_url.toString();
break;
case State::Inactive:
case State::Failed:
emitFailed();
2018-07-15 13:51:05 +01:00
return;
case State::AbortedByUser:
emitAborted();
2018-07-15 13:51:05 +01:00
return;
}
2022-06-04 08:33:17 +01:00
request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgent().toUtf8());
if (APPLICATION->currentCapabilities() & Application::SupportsFlame
&& request.url().host().contains("api.curseforge.com")) {
request.setRawHeader("x-api-key", APPLICATION->getFlameAPIKey().toUtf8());
2022-05-08 08:22:50 +01:00
};
2022-05-08 08:22:50 +01:00
QNetworkReply* rep = m_network->get(request);
2018-07-15 13:51:05 +01:00
m_reply.reset(rep);
connect(rep, &QNetworkReply::downloadProgress, this, &Download::downloadProgress);
connect(rep, &QNetworkReply::finished, this, &Download::downloadFinished);
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
connect(rep, SIGNAL(errorOccurred(QNetworkReply::NetworkError)), SLOT(downloadError(QNetworkReply::NetworkError)));
#else
2018-07-15 13:51:05 +01:00
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(downloadError(QNetworkReply::NetworkError)));
#endif
2018-07-15 13:51:05 +01:00
connect(rep, &QNetworkReply::sslErrors, this, &Download::sslErrors);
connect(rep, &QNetworkReply::readyRead, this, &Download::downloadReadyRead);
}
void Download::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
{
setProgress(bytesReceived, bytesTotal);
}
void Download::downloadError(QNetworkReply::NetworkError error)
{
2022-05-08 08:22:50 +01:00
if (error == QNetworkReply::OperationCanceledError) {
2018-07-15 13:51:05 +01:00
qCritical() << "Aborted " << m_url.toString();
m_state = State::AbortedByUser;
2022-05-08 08:22:50 +01:00
} else {
if (m_options & Option::AcceptLocalFiles) {
if (m_sink->hasLocalData()) {
m_state = State::Succeeded;
2018-07-15 13:51:05 +01:00
return;
}
}
// error happened during download.
qCritical() << "Failed " << m_url.toString() << " with reason " << error;
m_state = State::Failed;
2018-07-15 13:51:05 +01:00
}
}
2022-05-08 08:22:50 +01:00
void Download::sslErrors(const QList<QSslError>& errors)
{
2018-07-15 13:51:05 +01:00
int i = 1;
2022-05-08 08:22:50 +01:00
for (auto error : errors) {
2018-07-15 13:51:05 +01:00
qCritical() << "Download" << m_url.toString() << "SSL Error #" << i << " : " << error.errorString();
auto cert = error.certificate();
qCritical() << "Certificate in question:\n" << cert.toText();
i++;
}
}
auto Download::handleRedirect() -> bool
{
2018-07-15 13:51:05 +01:00
QUrl redirect = m_reply->header(QNetworkRequest::LocationHeader).toUrl();
2022-05-08 08:22:50 +01:00
if (!redirect.isValid()) {
if (!m_reply->hasRawHeader("Location")) {
2018-07-15 13:51:05 +01:00
// 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");
2022-05-08 08:22:50 +01:00
if (redirectBA.size() == 0) {
2018-07-15 13:51:05 +01:00
// empty, yet present redirect header? WTF?
return false;
}
QString redirectStr = QString::fromUtf8(redirectBA);
2022-05-08 08:22:50 +01:00
if (redirectStr.startsWith("//")) {
/*
* 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
*/
2018-07-15 13:51:05 +01:00
redirectStr = m_reply->url().scheme() + ":" + redirectStr;
2022-05-08 08:22:50 +01:00
} 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();
}
2018-07-15 13:51:05 +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);
2022-05-08 08:22:50 +01:00
if (!redirect.isValid()) {
2018-07-15 13:51:05 +01:00
qWarning() << "Failed to parse redirect URL:" << redirectStr;
downloadError(QNetworkReply::ProtocolFailure);
return false;
}
qDebug() << "Fixed location header:" << redirect;
2022-05-08 08:22:50 +01:00
} else {
2018-07-15 13:51:05 +01:00
qDebug() << "Location header:" << redirect;
}
2018-07-15 13:51:05 +01:00
m_url = QUrl(redirect.toString());
qDebug() << "Following redirect to " << m_url.toString();
startAction(m_network);
2018-07-15 13:51:05 +01:00
return true;
}
void Download::downloadFinished()
{
2018-07-15 13:51:05 +01:00
// handle HTTP redirection first
2022-05-08 08:22:50 +01:00
if (handleRedirect()) {
2018-07-15 13:51:05 +01:00
qDebug() << "Download redirected:" << m_url.toString();
return;
}
2018-07-15 13:51:05 +01:00
// if the download failed before this point ...
if (m_state == State::Succeeded) // pretend to succeed so we continue processing :)
{
2018-07-15 13:51:05 +01:00
qDebug() << "Download failed but we are allowed to proceed:" << m_url.toString();
m_sink->abort();
m_reply.reset();
emit succeeded();
2018-07-15 13:51:05 +01:00
return;
} else if (m_state == State::Failed) {
2018-07-15 13:51:05 +01:00
qDebug() << "Download failed in previous step:" << m_url.toString();
m_sink->abort();
m_reply.reset();
emit failed("");
2018-07-15 13:51:05 +01:00
return;
} else if (m_state == State::AbortedByUser) {
2018-07-15 13:51:05 +01:00
qDebug() << "Download aborted in previous step:" << m_url.toString();
m_sink->abort();
m_reply.reset();
emit aborted();
2018-07-15 13:51:05 +01:00
return;
}
2018-07-15 13:51:05 +01:00
// make sure we got all the remaining data, if any
auto data = m_reply->readAll();
2022-05-08 08:22:50 +01:00
if (data.size()) {
qDebug() << "Writing extra" << data.size() << "bytes";
m_state = m_sink->write(data);
2018-07-15 13:51:05 +01:00
}
2018-07-15 13:51:05 +01:00
// otherwise, finalize the whole graph
m_state = m_sink->finalize(*m_reply.get());
if (m_state != State::Succeeded) {
2018-07-15 13:51:05 +01:00
qDebug() << "Download failed to finalize:" << m_url.toString();
m_sink->abort();
m_reply.reset();
emit failed("");
2018-07-15 13:51:05 +01:00
return;
}
2018-07-15 13:51:05 +01:00
m_reply.reset();
qDebug() << "Download succeeded:" << m_url.toString();
emit succeeded();
}
void Download::downloadReadyRead()
{
if (m_state == State::Running) {
2018-07-15 13:51:05 +01:00
auto data = m_reply->readAll();
m_state = m_sink->write(data);
if (m_state == State::Failed) {
qCritical() << "Failed to process response chunk";
2018-07-15 13:51:05 +01:00
}
// qDebug() << "Download" << m_url.toString() << "gained" << data.size() << "bytes";
2022-05-08 08:22:50 +01:00
} else {
qCritical() << "Cannot write download data! illegal status " << m_status;
2018-07-15 13:51:05 +01:00
}
}
2022-05-08 08:22:50 +01:00
} // namespace Net
auto Net::Download::abort() -> bool
{
2022-05-08 08:22:50 +01:00
if (m_reply) {
2018-07-15 13:51:05 +01:00
m_reply->abort();
2022-05-08 08:22:50 +01:00
} else {
m_state = State::AbortedByUser;
2018-07-15 13:51:05 +01:00
}
return true;
}