2022-05-02 21:26:24 +05:30
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
/*
|
2023-03-02 15:49:26 +05:30
|
|
|
* Prism Launcher - Minecraft Launcher
|
2022-05-02 21:26:24 +05:30
|
|
|
* Copyright (c) 2022 flowln <flowlnlnln@gmail.com>
|
2022-05-27 02:48:54 +05:30
|
|
|
* Copyright (C) 2022 Sefa Eyeoglu <contact@scrumplex.net>
|
2023-03-01 15:24:49 +05:30
|
|
|
* Copyright (C) 2023 TheKodeToad <TheKodeToad@proton.me>
|
2016-05-28 23:24:17 +05:30
|
|
|
*
|
2022-05-02 21:26:24 +05:30
|
|
|
* 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 23:24:17 +05:30
|
|
|
*
|
2022-05-02 21:26:24 +05:30
|
|
|
* 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 23:24:17 +05:30
|
|
|
*
|
2022-05-02 21:26:24 +05:30
|
|
|
* 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 23:24:17 +05:30
|
|
|
*/
|
|
|
|
|
|
|
|
#include "Download.h"
|
2023-03-31 12:20:29 +05:30
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QUrl>
|
2016-05-28 23:24:17 +05:30
|
|
|
|
|
|
|
#include <QDateTime>
|
2022-05-08 12:52:50 +05:30
|
|
|
#include <QFileInfo>
|
2021-11-22 03:51:12 +05:30
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
#include "ByteArraySink.h"
|
2016-05-28 23:24:17 +05:30
|
|
|
#include "ChecksumValidator.h"
|
2022-05-08 12:52:50 +05:30
|
|
|
#include "FileSystem.h"
|
2016-05-28 23:24:17 +05:30
|
|
|
#include "MetaCacheSink.h"
|
|
|
|
|
2021-11-22 03:51:12 +05:30
|
|
|
#include "BuildConfig.h"
|
2022-05-20 23:26:27 +05:30
|
|
|
#include "Application.h"
|
2021-11-22 03:51:12 +05:30
|
|
|
|
2023-03-30 23:52:55 +05:30
|
|
|
Q_LOGGING_CATEGORY(DownloadLogC, "Task.Net.Download")
|
|
|
|
|
2016-05-28 23:24:17 +05:30
|
|
|
namespace Net {
|
|
|
|
|
2023-03-31 12:20:29 +05:30
|
|
|
QString truncateUrlHumanFriendly(QUrl &url, int max_len, bool hard_limit = false)
|
|
|
|
{
|
|
|
|
auto display_options = QUrl::RemoveUserInfo | QUrl::RemoveFragment | QUrl::NormalizePathSegments;
|
|
|
|
auto str_url = url.toDisplayString(display_options);
|
|
|
|
if (str_url.length() <= max_len)
|
|
|
|
return str_url;
|
|
|
|
|
2023-04-01 00:59:59 +05:30
|
|
|
/* this is a PCRE regular expression that splits a URL (given by the display rules above) into 5 capture groups
|
|
|
|
* the scheme (ie https://) is group 1
|
|
|
|
* the host (with trailing /) is group 2
|
|
|
|
* the first part of the path (with trailing /) is group 3
|
|
|
|
* the last part of the path (with leading /) is group 5
|
|
|
|
* the remainder of the URL is in the .* and in group 4
|
|
|
|
*
|
|
|
|
* See: https://regex101.com/r/inHkek/1
|
|
|
|
* for an interactive breakdown
|
|
|
|
*/
|
|
|
|
QRegularExpression re(R"(^([\w]+:\/\/)([\w._-]+\/)([\w._-]+\/)(.*)(\/[^]+[^]+)$)");
|
2023-03-31 12:20:29 +05:30
|
|
|
|
|
|
|
auto url_compact = QString(str_url);
|
2023-04-01 00:59:59 +05:30
|
|
|
url_compact.replace(re, "\\1\\2\\3...\\5");
|
2023-03-31 12:20:29 +05:30
|
|
|
if (url_compact.length() >= max_len) {
|
2023-04-01 00:59:59 +05:30
|
|
|
url_compact = QString(str_url);
|
|
|
|
url_compact.replace(re, "\\1\\2...\\5");
|
2023-03-31 12:20:29 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if ((url_compact.length() >= max_len) && hard_limit) {
|
|
|
|
auto to_remove = url_compact.length() - max_len + 3;
|
|
|
|
url_compact.remove(url_compact.length() - to_remove - 1, to_remove);
|
|
|
|
url_compact.append("...");
|
|
|
|
}
|
|
|
|
|
|
|
|
return url_compact;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-04-28 03:06:11 +05:30
|
|
|
auto Download::makeCached(QUrl url, MetaEntryPtr entry, Options options) -> Download::Ptr
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
2023-01-25 01:22:09 +05:30
|
|
|
auto dl = makeShared<Download>();
|
2018-07-15 18:21:05 +05:30
|
|
|
dl->m_url = url;
|
2023-03-30 23:52:55 +05:30
|
|
|
dl->setObjectName(QString("CACHE:") + url.toString());
|
2018-07-15 18:21:05 +05:30
|
|
|
dl->m_options = options;
|
|
|
|
auto md5Node = new ChecksumValidator(QCryptographicHash::Md5);
|
2022-08-21 21:34:48 +05:30
|
|
|
auto cachedNode = new MetaCacheSink(entry, md5Node, options.testFlag(Option::MakeEternal));
|
2018-07-15 18:21:05 +05:30
|
|
|
dl->m_sink.reset(cachedNode);
|
2021-11-22 03:51:12 +05:30
|
|
|
return dl;
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
2022-04-28 03:06:11 +05:30
|
|
|
auto Download::makeByteArray(QUrl url, QByteArray* output, Options options) -> Download::Ptr
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
2023-01-25 01:22:09 +05:30
|
|
|
auto dl = makeShared<Download>();
|
2018-07-15 18:21:05 +05:30
|
|
|
dl->m_url = url;
|
2023-03-30 23:52:55 +05:30
|
|
|
dl->setObjectName(QString("BYTES:") + url.toString());
|
2018-07-15 18:21:05 +05:30
|
|
|
dl->m_options = options;
|
|
|
|
dl->m_sink.reset(new ByteArraySink(output));
|
2021-11-22 03:51:12 +05:30
|
|
|
return dl;
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
2022-04-28 03:06:11 +05:30
|
|
|
auto Download::makeFile(QUrl url, QString path, Options options) -> Download::Ptr
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
2023-01-25 01:22:09 +05:30
|
|
|
auto dl = makeShared<Download>();
|
2018-07-15 18:21:05 +05:30
|
|
|
dl->m_url = url;
|
2023-03-30 23:52:55 +05:30
|
|
|
dl->setObjectName(QString("FILE:") + url.toString());
|
2018-07-15 18:21:05 +05:30
|
|
|
dl->m_options = options;
|
|
|
|
dl->m_sink.reset(new FileSink(path));
|
2021-11-22 03:51:12 +05:30
|
|
|
return dl;
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
void Download::addValidator(Validator* v)
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
m_sink->addValidator(v);
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
2022-04-22 06:42:14 +05:30
|
|
|
void Download::executeTask()
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
2023-04-01 00:59:59 +05:30
|
|
|
setStatus(tr("Downloading %1").arg(truncateUrlHumanFriendly(m_url, 100)));
|
2022-05-01 19:35:31 +05:30
|
|
|
|
2022-04-22 06:42:14 +05:30
|
|
|
if (getState() == Task::State::AbortedByUser) {
|
2023-03-30 23:52:55 +05:30
|
|
|
qCWarning(DownloadLogC) << getUid().toString() << "Attempt to start an aborted Download:" << m_url.toString();
|
2022-04-27 05:55:42 +05:30
|
|
|
emitAborted();
|
2018-07-15 18:21:05 +05:30
|
|
|
return;
|
|
|
|
}
|
2022-04-22 06:42:14 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
QNetworkRequest request(m_url);
|
2022-04-22 06:42:14 +05:30
|
|
|
m_state = m_sink->init(request);
|
|
|
|
switch (m_state) {
|
|
|
|
case State::Succeeded:
|
2022-04-27 05:55:42 +05:30
|
|
|
emit succeeded();
|
2023-03-30 23:52:55 +05:30
|
|
|
qCDebug(DownloadLogC) << getUid().toString() << "Download cache hit " << m_url.toString();
|
2018-07-15 18:21:05 +05:30
|
|
|
return;
|
2022-04-22 06:42:14 +05:30
|
|
|
case State::Running:
|
2023-03-30 23:52:55 +05:30
|
|
|
qCDebug(DownloadLogC) << getUid().toString() << "Downloading " << m_url.toString();
|
2018-07-15 18:21:05 +05:30
|
|
|
break;
|
2022-04-22 06:42:14 +05:30
|
|
|
case State::Inactive:
|
|
|
|
case State::Failed:
|
2022-04-27 05:55:42 +05:30
|
|
|
emitFailed();
|
2018-07-15 18:21:05 +05:30
|
|
|
return;
|
2022-04-22 06:42:14 +05:30
|
|
|
case State::AbortedByUser:
|
2022-05-01 19:35:31 +05:30
|
|
|
emitAborted();
|
2018-07-15 18:21:05 +05:30
|
|
|
return;
|
|
|
|
}
|
2016-05-28 23:24:17 +05:30
|
|
|
|
2022-06-04 13:03:17 +05:30
|
|
|
request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgent().toUtf8());
|
2023-03-05 23:51:24 +05:30
|
|
|
// TODO remove duplication
|
|
|
|
if (APPLICATION->capabilities() & Application::SupportsFlame && request.url().host() == QUrl(BuildConfig.FLAME_BASE_URL).host()) {
|
2022-05-30 00:59:07 +05:30
|
|
|
request.setRawHeader("x-api-key", APPLICATION->getFlameAPIKey().toUtf8());
|
2023-03-05 23:51:24 +05:30
|
|
|
} else if (request.url().host() == QUrl(BuildConfig.MODRINTH_PROD_URL).host() ||
|
|
|
|
request.url().host() == QUrl(BuildConfig.MODRINTH_STAGING_URL).host()) {
|
2023-03-01 15:24:49 +05:30
|
|
|
QString token = APPLICATION->getModrinthAPIToken();
|
|
|
|
if (!token.isNull())
|
|
|
|
request.setRawHeader("Authorization", token.toUtf8());
|
|
|
|
}
|
2023-03-30 23:52:55 +05:30
|
|
|
|
|
|
|
m_last_progress_time = m_clock.now();
|
|
|
|
m_last_progress_bytes = 0;
|
2016-05-28 23:24:17 +05:30
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
QNetworkReply* rep = m_network->get(request);
|
2023-03-30 23:52:55 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
m_reply.reset(rep);
|
2022-04-27 05:55:42 +05:30
|
|
|
connect(rep, &QNetworkReply::downloadProgress, this, &Download::downloadProgress);
|
|
|
|
connect(rep, &QNetworkReply::finished, this, &Download::downloadFinished);
|
2022-05-17 00:04:07 +05:30
|
|
|
#if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
|
|
|
|
connect(rep, SIGNAL(errorOccurred(QNetworkReply::NetworkError)), SLOT(downloadError(QNetworkReply::NetworkError)));
|
|
|
|
#else
|
2018-07-15 18:21:05 +05:30
|
|
|
connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(downloadError(QNetworkReply::NetworkError)));
|
2022-05-17 00:04:07 +05:30
|
|
|
#endif
|
2018-07-15 18:21:05 +05:30
|
|
|
connect(rep, &QNetworkReply::sslErrors, this, &Download::sslErrors);
|
|
|
|
connect(rep, &QNetworkReply::readyRead, this, &Download::downloadReadyRead);
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void Download::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
|
|
|
{
|
2023-03-30 23:52:55 +05:30
|
|
|
auto now = m_clock.now();
|
|
|
|
auto elapsed = now - m_last_progress_time;
|
|
|
|
auto elapsed_ms = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count();
|
|
|
|
auto bytes_recived_since = bytesReceived - m_last_progress_bytes;
|
2023-03-31 12:20:29 +05:30
|
|
|
if (elapsed_ms > 0) {
|
|
|
|
m_details = humanReadableFileSize(bytes_recived_since / elapsed_ms * 1000) + "/s";
|
|
|
|
} else {
|
|
|
|
m_details = "0 b/s";
|
|
|
|
}
|
2023-03-30 23:52:55 +05:30
|
|
|
|
2022-04-22 06:42:14 +05:30
|
|
|
setProgress(bytesReceived, bytesTotal);
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void Download::downloadError(QNetworkReply::NetworkError error)
|
|
|
|
{
|
2022-05-08 12:52:50 +05:30
|
|
|
if (error == QNetworkReply::OperationCanceledError) {
|
2023-03-30 23:52:55 +05:30
|
|
|
qCCritical(DownloadLogC) << getUid().toString() << "Aborted " << m_url.toString();
|
2022-04-22 06:42:14 +05:30
|
|
|
m_state = State::AbortedByUser;
|
2022-05-08 12:52:50 +05:30
|
|
|
} else {
|
|
|
|
if (m_options & Option::AcceptLocalFiles) {
|
|
|
|
if (m_sink->hasLocalData()) {
|
2022-04-22 06:42:14 +05:30
|
|
|
m_state = State::Succeeded;
|
2018-07-15 18:21:05 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// error happened during download.
|
2023-03-30 23:52:55 +05:30
|
|
|
qCCritical(DownloadLogC) << getUid().toString() << "Failed " << m_url.toString() << " with reason " << error;
|
2022-04-22 06:42:14 +05:30
|
|
|
m_state = State::Failed;
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
void Download::sslErrors(const QList<QSslError>& errors)
|
2017-11-02 03:22:11 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
int i = 1;
|
2022-05-08 12:52:50 +05:30
|
|
|
for (auto error : errors) {
|
2023-03-30 23:52:55 +05:30
|
|
|
qCCritical(DownloadLogC) << getUid().toString() << "Download" << m_url.toString() << "SSL Error #" << i << " : " << error.errorString();
|
2018-07-15 18:21:05 +05:30
|
|
|
auto cert = error.certificate();
|
2023-03-30 23:52:55 +05:30
|
|
|
qCCritical(DownloadLogC) << getUid().toString() << "Certificate in question:\n" << cert.toText();
|
2018-07-15 18:21:05 +05:30
|
|
|
i++;
|
|
|
|
}
|
2017-11-02 03:22:11 +05:30
|
|
|
}
|
|
|
|
|
2022-04-28 03:06:11 +05:30
|
|
|
auto Download::handleRedirect() -> bool
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
QUrl redirect = m_reply->header(QNetworkRequest::LocationHeader).toUrl();
|
2022-05-08 12:52:50 +05:30
|
|
|
if (!redirect.isValid()) {
|
|
|
|
if (!m_reply->hasRawHeader("Location")) {
|
2018-07-15 18:21:05 +05:30
|
|
|
// 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 12:52:50 +05:30
|
|
|
if (redirectBA.size() == 0) {
|
2018-07-15 18:21:05 +05:30
|
|
|
// empty, yet present redirect header? WTF?
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QString redirectStr = QString::fromUtf8(redirectBA);
|
2018-01-21 08:19:54 +05:30
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
if (redirectStr.startsWith("//")) {
|
2018-12-14 07:18:55 +05:30
|
|
|
/*
|
|
|
|
* 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 18:21:05 +05:30
|
|
|
redirectStr = m_reply->url().scheme() + ":" + redirectStr;
|
2022-05-08 12:52:50 +05:30
|
|
|
} else if (redirectStr.startsWith("/")) {
|
2018-12-14 07:18:55 +05:30
|
|
|
/*
|
|
|
|
* 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 08:19:54 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
/*
|
|
|
|
* 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 12:52:50 +05:30
|
|
|
if (!redirect.isValid()) {
|
2023-03-30 23:52:55 +05:30
|
|
|
qCWarning(DownloadLogC) << getUid().toString() << "Failed to parse redirect URL:" << redirectStr;
|
2018-07-15 18:21:05 +05:30
|
|
|
downloadError(QNetworkReply::ProtocolFailure);
|
|
|
|
return false;
|
|
|
|
}
|
2023-03-30 23:52:55 +05:30
|
|
|
qCDebug(DownloadLogC) << getUid().toString() << "Fixed location header:" << redirect;
|
2022-05-08 12:52:50 +05:30
|
|
|
} else {
|
2023-03-30 23:52:55 +05:30
|
|
|
qCDebug(DownloadLogC) << getUid().toString() << "Location header:" << redirect;
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2018-01-21 08:19:54 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
m_url = QUrl(redirect.toString());
|
2023-03-30 23:52:55 +05:30
|
|
|
qCDebug(DownloadLogC) << getUid().toString() << "Following redirect to " << m_url.toString();
|
2022-04-22 06:42:14 +05:30
|
|
|
startAction(m_network);
|
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
return true;
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void Download::downloadFinished()
|
|
|
|
{
|
2018-07-15 18:21:05 +05:30
|
|
|
// handle HTTP redirection first
|
2022-05-08 12:52:50 +05:30
|
|
|
if (handleRedirect()) {
|
2023-03-30 23:52:55 +05:30
|
|
|
qCDebug(DownloadLogC) << getUid().toString() << "Download redirected:" << m_url.toString();
|
2018-07-15 18:21:05 +05:30
|
|
|
return;
|
|
|
|
}
|
2016-05-28 23:24:17 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
// if the download failed before this point ...
|
2022-04-22 06:42:14 +05:30
|
|
|
if (m_state == State::Succeeded) // pretend to succeed so we continue processing :)
|
|
|
|
{
|
2023-03-30 23:52:55 +05:30
|
|
|
qCDebug(DownloadLogC) << getUid().toString() << "Download failed but we are allowed to proceed:" << m_url.toString();
|
2018-07-15 18:21:05 +05:30
|
|
|
m_sink->abort();
|
|
|
|
m_reply.reset();
|
2022-04-27 05:55:42 +05:30
|
|
|
emit succeeded();
|
2018-07-15 18:21:05 +05:30
|
|
|
return;
|
2022-04-22 06:42:14 +05:30
|
|
|
} else if (m_state == State::Failed) {
|
2023-03-30 23:52:55 +05:30
|
|
|
qCDebug(DownloadLogC) << getUid().toString() << "Download failed in previous step:" << m_url.toString();
|
2018-07-15 18:21:05 +05:30
|
|
|
m_sink->abort();
|
|
|
|
m_reply.reset();
|
2022-05-01 19:35:31 +05:30
|
|
|
emit failed("");
|
2018-07-15 18:21:05 +05:30
|
|
|
return;
|
2022-04-22 06:42:14 +05:30
|
|
|
} else if (m_state == State::AbortedByUser) {
|
2023-03-30 23:52:55 +05:30
|
|
|
qCDebug(DownloadLogC) << getUid().toString() << "Download aborted in previous step:" << m_url.toString();
|
2018-07-15 18:21:05 +05:30
|
|
|
m_sink->abort();
|
|
|
|
m_reply.reset();
|
2022-05-01 19:35:31 +05:30
|
|
|
emit aborted();
|
2018-07-15 18:21:05 +05:30
|
|
|
return;
|
|
|
|
}
|
2016-05-28 23:24:17 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
// make sure we got all the remaining data, if any
|
|
|
|
auto data = m_reply->readAll();
|
2022-05-08 12:52:50 +05:30
|
|
|
if (data.size()) {
|
2023-03-30 23:52:55 +05:30
|
|
|
qCDebug(DownloadLogC) << getUid().toString() << "Writing extra" << data.size() << "bytes";
|
2022-04-22 06:42:14 +05:30
|
|
|
m_state = m_sink->write(data);
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2016-05-28 23:24:17 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
// otherwise, finalize the whole graph
|
2022-04-22 06:42:14 +05:30
|
|
|
m_state = m_sink->finalize(*m_reply.get());
|
|
|
|
if (m_state != State::Succeeded) {
|
2023-03-30 23:52:55 +05:30
|
|
|
qCDebug(DownloadLogC) << getUid().toString() << "Download failed to finalize:" << m_url.toString();
|
2018-07-15 18:21:05 +05:30
|
|
|
m_sink->abort();
|
|
|
|
m_reply.reset();
|
2022-05-01 19:35:31 +05:30
|
|
|
emit failed("");
|
2018-07-15 18:21:05 +05:30
|
|
|
return;
|
|
|
|
}
|
2022-04-28 03:06:11 +05:30
|
|
|
|
2018-07-15 18:21:05 +05:30
|
|
|
m_reply.reset();
|
2023-03-30 23:52:55 +05:30
|
|
|
qCDebug(DownloadLogC) << getUid().toString() << "Download succeeded:" << m_url.toString();
|
2022-04-27 05:55:42 +05:30
|
|
|
emit succeeded();
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void Download::downloadReadyRead()
|
|
|
|
{
|
2022-04-22 06:42:14 +05:30
|
|
|
if (m_state == State::Running) {
|
2018-07-15 18:21:05 +05:30
|
|
|
auto data = m_reply->readAll();
|
2022-04-22 06:42:14 +05:30
|
|
|
m_state = m_sink->write(data);
|
|
|
|
if (m_state == State::Failed) {
|
2023-03-30 23:52:55 +05:30
|
|
|
qCCritical(DownloadLogC) << getUid().toString() << "Failed to process response chunk";
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
// qDebug() << "Download" << m_url.toString() << "gained" << data.size() << "bytes";
|
2022-05-08 12:52:50 +05:30
|
|
|
} else {
|
2023-03-30 23:52:55 +05:30
|
|
|
qCCritical(DownloadLogC) << getUid().toString() << "Cannot write download data! illegal status " << m_status;
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
2022-05-08 12:52:50 +05:30
|
|
|
} // namespace Net
|
2016-08-14 06:03:31 +05:30
|
|
|
|
2022-04-28 03:06:11 +05:30
|
|
|
auto Net::Download::abort() -> bool
|
2016-08-14 06:03:31 +05:30
|
|
|
{
|
2022-05-08 12:52:50 +05:30
|
|
|
if (m_reply) {
|
2018-07-15 18:21:05 +05:30
|
|
|
m_reply->abort();
|
2022-05-08 12:52:50 +05:30
|
|
|
} else {
|
2022-04-22 06:42:14 +05:30
|
|
|
m_state = State::AbortedByUser;
|
2018-07-15 18:21:05 +05:30
|
|
|
}
|
|
|
|
return true;
|
2016-08-14 06:03:31 +05:30
|
|
|
}
|