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