diff options
51 files changed, 823 insertions, 764 deletions
diff --git a/api/dead/src/handlers/WebResourceHandler.cpp b/api/dead/src/handlers/WebResourceHandler.cpp index 757b870a..e4fd74fa 100644 --- a/api/dead/src/handlers/WebResourceHandler.cpp +++ b/api/dead/src/handlers/WebResourceHandler.cpp @@ -1,6 +1,6 @@ #include "WebResourceHandler.h" -#include "net/CacheDownload.h" +#include "net/Download.h" #include "net/HttpMetaCache.h" #include "net/NetJob.h" #include "FileSystem.h" @@ -27,7 +27,7 @@ WebResourceHandler::WebResourceHandler(const QString &url) else { NetJob *job = new NetJob("Icon download"); - job->addNetAction(CacheDownload::make(QUrl(url), entry)); + job->addNetAction(Download::make(QUrl(url), entry)); connect(job, &NetJob::succeeded, this, &WebResourceHandler::succeeded); connect(job, &NetJob::failed, this, [job, this]() {setFailure(job->failReason());}); connect(job, &NetJob::progress, this, &WebResourceHandler::progress); diff --git a/api/logic/CMakeLists.txt b/api/logic/CMakeLists.txt index 2a73566d..1048e091 100644 --- a/api/logic/CMakeLists.txt +++ b/api/logic/CMakeLists.txt @@ -82,21 +82,25 @@ set(PATHMATCHER_SOURCES set(NET_SOURCES # network stuffs + net/ByteArraySink.h + net/ChecksumValidator.h + net/Download.cpp + net/Download.h + net/FileSink.cpp + net/FileSink.h + net/HttpMetaCache.cpp + net/HttpMetaCache.h + net/MetaCacheSink.cpp + net/MetaCacheSink.h net/NetAction.h - net/MD5EtagDownload.h - net/MD5EtagDownload.cpp - net/ByteArrayDownload.h - net/ByteArrayDownload.cpp - net/CacheDownload.h - net/CacheDownload.cpp - net/NetJob.h net/NetJob.cpp - net/HttpMetaCache.h - net/HttpMetaCache.cpp - net/PasteUpload.h + net/NetJob.h net/PasteUpload.cpp - net/URLConstants.h + net/PasteUpload.h + net/Sink.h net/URLConstants.cpp + net/URLConstants.h + net/Validator.h ) # Game launch logic diff --git a/api/logic/minecraft/AssetsUtils.cpp b/api/logic/minecraft/AssetsUtils.cpp index 7a525abe..bb630528 100644 --- a/api/logic/minecraft/AssetsUtils.cpp +++ b/api/logic/minecraft/AssetsUtils.cpp @@ -25,7 +25,9 @@ #include "AssetsUtils.h" #include "FileSystem.h" -#include "net/MD5EtagDownload.h" +#include "net/Download.h" +#include "net/ChecksumValidator.h" + namespace AssetsUtils { @@ -191,7 +193,12 @@ NetActionPtr AssetObject::getDownloadAction() QFileInfo objectFile(getLocalPath()); if ((!objectFile.isFile()) || (objectFile.size() != size)) { - auto objectDL = MD5EtagDownload::make(getUrl(), objectFile.filePath()); + auto objectDL = Net::Download::makeFile(getUrl(), objectFile.filePath()); + if(hash.size()) + { + auto rawHash = QByteArray::fromHex(hash.toLatin1()); + objectDL->addValidator(new Net::ChecksumValidator(QCryptographicHash::Sha1, rawHash)); + } objectDL->m_total_progress = size; return objectDL; } diff --git a/api/logic/minecraft/Library.cpp b/api/logic/minecraft/Library.cpp index 922db84e..584c7ac5 100644 --- a/api/logic/minecraft/Library.cpp +++ b/api/logic/minecraft/Library.cpp @@ -1,5 +1,6 @@ #include "Library.h" -#include <net/CacheDownload.h> +#include <net/Download.h> +#include <net/ChecksumValidator.h> #include <minecraft/forge/ForgeXzDownload.h> #include <Env.h> #include <FileSystem.h> @@ -74,7 +75,7 @@ QList<NetActionPtr> Library::getDownloads(OpSys system, HttpMetaCache * cache, Q bool isLocal = (hint() == "local"); bool isForge = (hint() == "forge-pack-xz"); - auto add_download = [&](QString storage, QString dl) + auto add_download = [&](QString storage, QString url, QString sha1 = QString()) { auto entry = cache->resolveEntry("libraries", storage); if (!entry->isStale()) @@ -95,7 +96,16 @@ QList<NetActionPtr> Library::getDownloads(OpSys system, HttpMetaCache * cache, Q } else { - out.append(CacheDownload::make(dl, entry)); + if(sha1.size()) + { + auto rawSha1 = QByteArray::fromHex(sha1.toLatin1()); + auto dl = Net::Download::makeCached(url, entry); + dl->addValidator(new Net::ChecksumValidator(QCryptographicHash::Sha1, rawSha1)); + out.append(dl); + } + + else + out.append(Net::Download::makeCached(url, entry)); } return true; }; @@ -105,7 +115,7 @@ QList<NetActionPtr> Library::getDownloads(OpSys system, HttpMetaCache * cache, Q if(m_mojangDownloads->artifact) { auto artifact = m_mojangDownloads->artifact; - add_download(artifact->path, artifact->url); + add_download(artifact->path, artifact->url, artifact->sha1); } if(m_nativeClassifiers.contains(system)) { @@ -118,17 +128,17 @@ QList<NetActionPtr> Library::getDownloads(OpSys system, HttpMetaCache * cache, Q nat64Classifier.replace("${arch}", "64"); auto nat32info = m_mojangDownloads->getDownloadInfo(nat32Classifier); if(nat32info) - add_download(nat32info->path, nat32info->url); + add_download(nat32info->path, nat32info->url, nat32info->sha1); auto nat64info = m_mojangDownloads->getDownloadInfo(nat64Classifier); if(nat64info) - add_download(nat64info->path, nat64info->url); + add_download(nat64info->path, nat64info->url, nat64info->sha1); } else { auto info = m_mojangDownloads->getDownloadInfo(nativeClassifier); if(info) { - add_download(info->path, info->url); + add_download(info->path, info->url, info->sha1); } } } diff --git a/api/logic/minecraft/MinecraftVersionList.cpp b/api/logic/minecraft/MinecraftVersionList.cpp index a5cc3a39..4e4eafbc 100644 --- a/api/logic/minecraft/MinecraftVersionList.cpp +++ b/api/logic/minecraft/MinecraftVersionList.cpp @@ -68,6 +68,7 @@ slots: protected: NetJobPtr specificVersionDownloadJob; + QByteArray versionIndexData; std::shared_ptr<MinecraftVersion> updatedVersion; MinecraftVersionList *m_list; }; @@ -410,7 +411,7 @@ MCVListVersionUpdateTask::MCVListVersionUpdateTask(MinecraftVersionList *vlist, void MCVListVersionUpdateTask::executeTask() { auto job = new NetJob("Version index"); - job->addNetAction(ByteArrayDownload::make(QUrl(updatedVersion->getUrl()))); + job->addNetAction(Net::Download::makeByteArray(QUrl(updatedVersion->getUrl()), &versionIndexData)); specificVersionDownloadJob.reset(job); connect(specificVersionDownloadJob.get(), SIGNAL(succeeded()), SLOT(json_downloaded())); connect(specificVersionDownloadJob.get(), SIGNAL(failed(QString)), SIGNAL(failed(QString))); @@ -420,12 +421,11 @@ void MCVListVersionUpdateTask::executeTask() void MCVListVersionUpdateTask::json_downloaded() { - NetActionPtr DlJob = specificVersionDownloadJob->first(); - auto data = std::dynamic_pointer_cast<ByteArrayDownload>(DlJob)->m_data; specificVersionDownloadJob.reset(); QJsonParseError jsonError; - QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError); + QJsonDocument jsonDoc = QJsonDocument::fromJson(versionIndexData, &jsonError); + versionIndexData.clear(); if (jsonError.error != QJsonParseError::NoError) { diff --git a/api/logic/minecraft/forge/ForgeInstaller.cpp b/api/logic/minecraft/forge/ForgeInstaller.cpp index 353328ab..4d004bf5 100644 --- a/api/logic/minecraft/forge/ForgeInstaller.cpp +++ b/api/logic/minecraft/forge/ForgeInstaller.cpp @@ -397,7 +397,7 @@ protected: if (entry->isStale()) { NetJob *fjob = new NetJob("Forge download"); - fjob->addNetAction(CacheDownload::make(forgeVersion->url(), entry)); + fjob->addNetAction(Net::Download::makeCached(forgeVersion->url(), entry)); connect(fjob, &NetJob::progress, this, &Task::setProgress); connect(fjob, &NetJob::status, this, &Task::setStatus); connect(fjob, &NetJob::failed, [this](QString reason) diff --git a/api/logic/minecraft/forge/ForgeVersionList.cpp b/api/logic/minecraft/forge/ForgeVersionList.cpp index de185e5f..fe024780 100644 --- a/api/logic/minecraft/forge/ForgeVersionList.cpp +++ b/api/logic/minecraft/forge/ForgeVersionList.cpp @@ -131,10 +131,8 @@ void ForgeListLoadTask::executeTask() forgeListEntry->setStale(true); gradleForgeListEntry->setStale(true); - job->addNetAction(listDownload = CacheDownload::make(QUrl(URLConstants::FORGE_LEGACY_URL), - forgeListEntry)); - job->addNetAction(gradleListDownload = CacheDownload::make( - QUrl(URLConstants::FORGE_GRADLE_URL), gradleForgeListEntry)); + job->addNetAction(listDownload = Net::Download::makeCached(QUrl(URLConstants::FORGE_LEGACY_URL),forgeListEntry)); + job->addNetAction(gradleListDownload = Net::Download::makeCached(QUrl(URLConstants::FORGE_GRADLE_URL), gradleForgeListEntry)); connect(listDownload.get(), SIGNAL(failed(int)), SLOT(listFailed())); connect(gradleListDownload.get(), SIGNAL(failed(int)), SLOT(gradleListFailed())); @@ -154,15 +152,14 @@ bool ForgeListLoadTask::parseForgeList(QList<BaseVersionPtr> &out) { QByteArray data; { - auto dlJob = listDownload; - auto filename = std::dynamic_pointer_cast<CacheDownload>(dlJob)->getTargetFilepath(); + auto filename = listDownload->getTargetFilepath(); QFile listFile(filename); if (!listFile.open(QIODevice::ReadOnly)) { return false; } data = listFile.readAll(); - dlJob.reset(); + listDownload.reset(); } QJsonParseError jsonError; @@ -266,15 +263,14 @@ bool ForgeListLoadTask::parseForgeGradleList(QList<BaseVersionPtr> &out) QMap<int, std::shared_ptr<ForgeVersion>> lookup; QByteArray data; { - auto dlJob = gradleListDownload; - auto filename = std::dynamic_pointer_cast<CacheDownload>(dlJob)->getTargetFilepath(); + auto filename = gradleListDownload->getTargetFilepath(); QFile listFile(filename); if (!listFile.open(QIODevice::ReadOnly)) { return false; } data = listFile.readAll(); - dlJob.reset(); + gradleListDownload.reset(); } QJsonParseError jsonError; diff --git a/api/logic/minecraft/forge/ForgeVersionList.h b/api/logic/minecraft/forge/ForgeVersionList.h index 62c08b2a..7b30bbb4 100644 --- a/api/logic/minecraft/forge/ForgeVersionList.h +++ b/api/logic/minecraft/forge/ForgeVersionList.h @@ -81,8 +81,8 @@ protected: NetJobPtr listJob; ForgeVersionList *m_list; - CacheDownloadPtr listDownload; - CacheDownloadPtr gradleListDownload; + Net::Download::Ptr listDownload; + Net::Download::Ptr gradleListDownload; private: bool parseForgeList(QList<BaseVersionPtr> &out); diff --git a/api/logic/minecraft/legacy/LegacyUpdate.cpp b/api/logic/minecraft/legacy/LegacyUpdate.cpp index 6539b2d3..a0d1933f 100644 --- a/api/logic/minecraft/legacy/LegacyUpdate.cpp +++ b/api/logic/minecraft/legacy/LegacyUpdate.cpp @@ -114,7 +114,7 @@ void LegacyUpdate::fmllibsStart() auto entry = metacache->resolveEntry("fmllibs", lib.filename); QString urlString = lib.ours ? URLConstants::FMLLIBS_OUR_BASE_URL + lib.filename : URLConstants::FMLLIBS_FORGE_BASE_URL + lib.filename; - dljob->addNetAction(CacheDownload::make(QUrl(urlString), entry)); + dljob->addNetAction(Net::Download::makeCached(QUrl(urlString), entry)); } connect(dljob, &NetJob::succeeded, this, &LegacyUpdate::fmllibsFinished); @@ -372,7 +372,7 @@ void LegacyUpdate::jarStart() auto metacache = ENV.metacache(); auto entry = metacache->resolveEntry("versions", URLConstants::getJarPath(version_id)); - dljob->addNetAction(CacheDownload::make(QUrl(URLConstants::getLegacyJarUrl(version_id)), entry)); + dljob->addNetAction(Net::Download::makeCached(QUrl(URLConstants::getLegacyJarUrl(version_id)), entry)); connect(dljob, SIGNAL(succeeded()), SLOT(jarFinished())); connect(dljob, SIGNAL(failed(QString)), SLOT(jarFailed(QString))); connect(dljob, SIGNAL(progress(qint64, qint64)), SIGNAL(progress(qint64, qint64))); diff --git a/api/logic/minecraft/liteloader/LiteLoaderVersionList.cpp b/api/logic/minecraft/liteloader/LiteLoaderVersionList.cpp index b0c9736a..f8bf095f 100644 --- a/api/logic/minecraft/liteloader/LiteLoaderVersionList.cpp +++ b/api/logic/minecraft/liteloader/LiteLoaderVersionList.cpp @@ -146,8 +146,7 @@ void LLListLoadTask::executeTask() // verify by poking the server. liteloaderEntry->setStale(true); - job->addNetAction(listDownload = CacheDownload::make(QUrl(URLConstants::LITELOADER_URL), - liteloaderEntry)); + job->addNetAction(listDownload = Net::Download::makeCached(QUrl(URLConstants::LITELOADER_URL), liteloaderEntry)); connect(listDownload.get(), SIGNAL(failed(int)), SLOT(listFailed())); @@ -167,8 +166,7 @@ void LLListLoadTask::listDownloaded() { QByteArray data; { - auto dlJob = listDownload; - auto filename = std::dynamic_pointer_cast<CacheDownload>(dlJob)->getTargetFilepath(); + auto filename = listDownload->getTargetFilepath(); QFile listFile(filename); if (!listFile.open(QIODevice::ReadOnly)) { @@ -177,7 +175,7 @@ void LLListLoadTask::listDownloaded() } data = listFile.readAll(); listFile.close(); - dlJob.reset(); + listDownload.reset(); } QJsonParseError jsonError; diff --git a/api/logic/minecraft/liteloader/LiteLoaderVersionList.h b/api/logic/minecraft/liteloader/LiteLoaderVersionList.h index 1dba4b6a..ae8bee92 100644 --- a/api/logic/minecraft/liteloader/LiteLoaderVersionList.h +++ b/api/logic/minecraft/liteloader/LiteLoaderVersionList.h @@ -112,7 +112,7 @@ slots: protected: NetJobPtr listJob; - CacheDownloadPtr listDownload; + Net::Download::Ptr listDownload; LiteLoaderVersionList *m_list; }; diff --git a/api/logic/minecraft/onesix/OneSixUpdate.cpp b/api/logic/minecraft/onesix/OneSixUpdate.cpp index 1c2cd196..d3cd197d 100644 --- a/api/logic/minecraft/onesix/OneSixUpdate.cpp +++ b/api/logic/minecraft/onesix/OneSixUpdate.cpp @@ -31,6 +31,7 @@ #include "minecraft/MinecraftProfile.h" #include "minecraft/Library.h" #include "net/URLConstants.h" +#include "net/ChecksumValidator.h" #include "minecraft/AssetsUtils.h" #include "Exception.h" #include "MMCZip.h" @@ -96,7 +97,13 @@ void OneSixUpdate::assetIndexStart() auto metacache = ENV.metacache(); auto entry = metacache->resolveEntry("asset_indexes", localPath); entry->setStale(true); - job->addNetAction(CacheDownload::make(indexUrl, entry)); + auto hexSha1 = assets->sha1.toLatin1(); + qDebug() << "Asset index SHA1:" << hexSha1; + auto dl = Net::Download::makeCached(indexUrl, entry); + auto rawSha1 = QByteArray::fromHex(assets->sha1.toLatin1()); + dl->addValidator(new Net::ChecksumValidator(QCryptographicHash::Sha1, rawSha1)); + job->addNetAction(dl); + jarlibDownloadJob.reset(job); connect(jarlibDownloadJob.get(), SIGNAL(succeeded()), SLOT(assetIndexFinished())); @@ -180,7 +187,7 @@ void OneSixUpdate::jarlibStart() auto metacache = ENV.metacache(); auto entry = metacache->resolveEntry("versions", localPath); - job->addNetAction(CacheDownload::make(QUrl(urlstr), entry)); + job->addNetAction(Net::Download::makeCached(QUrl(urlstr), entry)); jarlibDownloadJob.reset(job); } @@ -293,7 +300,7 @@ void OneSixUpdate::fmllibsStart() auto entry = metacache->resolveEntry("fmllibs", lib.filename); QString urlString = lib.ours ? URLConstants::FMLLIBS_OUR_BASE_URL + lib.filename : URLConstants::FMLLIBS_FORGE_BASE_URL + lib.filename; - dljob->addNetAction(CacheDownload::make(QUrl(urlString), entry)); + dljob->addNetAction(Net::Download::makeCached(QUrl(urlString), entry)); } connect(dljob, SIGNAL(succeeded()), SLOT(fmllibsFinished())); diff --git a/api/logic/net/ByteArrayDownload.cpp b/api/logic/net/ByteArrayDownload.cpp deleted file mode 100644 index 21990eeb..00000000 --- a/api/logic/net/ByteArrayDownload.cpp +++ /dev/null @@ -1,105 +0,0 @@ -/* Copyright 2013-2015 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 "ByteArrayDownload.h" -#include "Env.h" -#include <QDebug> - -ByteArrayDownload::ByteArrayDownload(QUrl url) : NetAction() -{ - m_url = url; - m_status = Job_NotStarted; -} - -void ByteArrayDownload::start() -{ - qDebug() << "Downloading " << m_url.toString(); - QNetworkRequest request(m_url); - request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)"); - auto worker = ENV.qnam(); - QNetworkReply *rep = worker->get(request); - - m_reply.reset(rep); - connect(rep, SIGNAL(downloadProgress(qint64, qint64)), - SLOT(downloadProgress(qint64, qint64))); - connect(rep, SIGNAL(finished()), SLOT(downloadFinished())); - connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), - SLOT(downloadError(QNetworkReply::NetworkError))); - connect(rep, SIGNAL(readyRead()), SLOT(downloadReadyRead())); -} - -void ByteArrayDownload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) -{ - m_total_progress = bytesTotal; - m_progress = bytesReceived; - emit netActionProgress(m_index_within_job, bytesReceived, bytesTotal); -} - -void ByteArrayDownload::downloadError(QNetworkReply::NetworkError error) -{ - // error happened during download. - qCritical() << "Error getting URL:" << m_url.toString().toLocal8Bit() - << "Network error: " << error; - m_status = Job_Failed; - m_errorString = m_reply->errorString(); -} - -void ByteArrayDownload::downloadFinished() -{ - QVariant redirect = m_reply->header(QNetworkRequest::LocationHeader); - QString redirectURL; - if(redirect.isValid()) - { - redirectURL = redirect.toString(); - } - // FIXME: This is a hack for https://bugreports.qt-project.org/browse/QTBUG-41061 - else if(m_reply->hasRawHeader("Location")) - { - auto data = m_reply->rawHeader("Location"); - if(data.size() > 2 && data[0] == '/' && data[1] == '/') - redirectURL = m_reply->url().scheme() + ":" + data; - } - if (!redirectURL.isEmpty()) - { - m_url = QUrl(redirect.toString()); - qDebug() << "Following redirect to " << m_url.toString(); - start(); - return; - } - - // if the download succeeded - if (m_status != Job_Failed) - { - // nothing went wrong... - m_status = Job_Finished; - m_data = m_reply->readAll(); - m_content_type = m_reply->header(QNetworkRequest::ContentTypeHeader).toString(); - m_reply.reset(); - emit succeeded(m_index_within_job); - return; - } - // else the download failed - else - { - m_reply.reset(); - emit failed(m_index_within_job); - return; - } -} - -void ByteArrayDownload::downloadReadyRead() -{ - // ~_~ -} diff --git a/api/logic/net/ByteArrayDownload.h b/api/logic/net/ByteArrayDownload.h deleted file mode 100644 index e2fc2911..00000000 --- a/api/logic/net/ByteArrayDownload.h +++ /dev/null @@ -1,48 +0,0 @@ -/* Copyright 2013-2015 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 "NetAction.h" - -#include "multimc_logic_export.h" - -typedef std::shared_ptr<class ByteArrayDownload> ByteArrayDownloadPtr; -class MULTIMC_LOGIC_EXPORT ByteArrayDownload : public NetAction -{ - Q_OBJECT -public: - ByteArrayDownload(QUrl url); - static ByteArrayDownloadPtr make(QUrl url) - { - return ByteArrayDownloadPtr(new ByteArrayDownload(url)); - } - virtual ~ByteArrayDownload() {}; -public: - /// if not saving to file, downloaded data is placed here - QByteArray m_data; - - QString m_errorString; - -public -slots: - virtual void start(); - -protected -slots: - void downloadProgress(qint64 bytesReceived, qint64 bytesTotal); - void downloadError(QNetworkReply::NetworkError error); - void downloadFinished(); - void downloadReadyRead(); -}; diff --git a/api/logic/net/ByteArraySink.h b/api/logic/net/ByteArraySink.h new file mode 100644 index 00000000..3deef1ed --- /dev/null +++ b/api/logic/net/ByteArraySink.h @@ -0,0 +1,57 @@ +#pragma once + +#include "Sink.h" + +namespace Net { +/* + * Sink object for downloads that uses an external QByteArray it doesn't own as a target. + */ +class ByteArraySink : public Sink +{ +public: + ByteArraySink(QByteArray *output) + :m_output(output) + { + // nil + }; + + virtual ~ByteArraySink() + { + // nil + } + +public: + JobStatus init(QNetworkRequest & request) override + { + m_output->clear(); + if(initAllValidators(request)) + return Job_InProgress; + return Job_Failed; + }; + + JobStatus write(QByteArray & data) override + { + m_output->append(data); + if(writeAllValidators(data)) + return Job_InProgress; + return Job_Failed; + } + + JobStatus abort() override + { + m_output->clear(); + failAllValidators(); + return Job_Failed; + } + + JobStatus finalize(QNetworkReply &reply) override + { + if(finalizeAllValidators(reply)) + return Job_Finished; + return Job_Failed; + } + +private: + QByteArray * m_output; +}; +} diff --git a/api/logic/net/CacheDownload.cpp b/api/logic/net/CacheDownload.cpp deleted file mode 100644 index 1ac55180..00000000 --- a/api/logic/net/CacheDownload.cpp +++ /dev/null @@ -1,192 +0,0 @@ -/* Copyright 2013-2015 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 "CacheDownload.h" - -#include <QCryptographicHash> -#include <QFileInfo> -#include <QDateTime> -#include <QDebug> -#include "Env.h" -#include <FileSystem.h> - -CacheDownload::CacheDownload(QUrl url, MetaEntryPtr entry) - : NetAction(), md5sum(QCryptographicHash::Md5) -{ - m_url = url; - m_entry = entry; - m_target_path = entry->getFullPath(); - m_status = Job_NotStarted; -} - -void CacheDownload::start() -{ - m_status = Job_InProgress; - if (!m_entry->isStale()) - { - m_status = Job_Finished; - emit succeeded(m_index_within_job); - return; - } - // create a new save file - m_output_file.reset(new QSaveFile(m_target_path)); - - // if there already is a file and md5 checking is in effect and it can be opened - if (!FS::ensureFilePathExists(m_target_path)) - { - qCritical() << "Could not create folder for " + m_target_path; - m_status = Job_Failed; - emit failed(m_index_within_job); - return; - } - if (!m_output_file->open(QIODevice::WriteOnly)) - { - qCritical() << "Could not open " + m_target_path + " for writing"; - m_status = Job_Failed; - emit failed(m_index_within_job); - return; - } - qDebug() << "Downloading " << m_url.toString(); - QNetworkRequest request(m_url); - - // check file consistency first. - QFile current(m_target_path); - if(current.exists() && current.size() != 0) - { - if (m_entry->getRemoteChangedTimestamp().size()) - request.setRawHeader(QString("If-Modified-Since").toLatin1(), - m_entry->getRemoteChangedTimestamp().toLatin1()); - if (m_entry->getETag().size()) - request.setRawHeader(QString("If-None-Match").toLatin1(), m_entry->getETag().toLatin1()); - } - - request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Cached)"); - - auto worker = ENV.qnam(); - QNetworkReply *rep = worker->get(request); - - m_reply.reset(rep); - connect(rep, SIGNAL(downloadProgress(qint64, qint64)), - SLOT(downloadProgress(qint64, qint64))); - connect(rep, SIGNAL(finished()), SLOT(downloadFinished())); - connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), - SLOT(downloadError(QNetworkReply::NetworkError))); - connect(rep, SIGNAL(readyRead()), SLOT(downloadReadyRead())); -} - -void CacheDownload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) -{ - m_total_progress = bytesTotal; - m_progress = bytesReceived; - emit netActionProgress(m_index_within_job, bytesReceived, bytesTotal); -} - -void CacheDownload::downloadError(QNetworkReply::NetworkError error) -{ - // error happened during download. - qCritical() << "Failed " << m_url.toString() << " with reason " << error; - m_status = Job_Failed; -} -void CacheDownload::downloadFinished() -{ - QVariant redirect = m_reply->header(QNetworkRequest::LocationHeader); - QString redirectURL; - if(redirect.isValid()) - { - redirectURL = redirect.toString(); - } - // FIXME: This is a hack for https://bugreports.qt-project.org/browse/QTBUG-41061 - else if(m_reply->hasRawHeader("Location")) - { - auto data = m_reply->rawHeader("Location"); - if(data.size() > 2 && data[0] == '/' && data[1] == '/') - redirectURL = m_reply->url().scheme() + ":" + data; - } - if (!redirectURL.isEmpty()) - { - m_url = QUrl(redirect.toString()); - qDebug() << "Following redirect to " << m_url.toString(); - start(); - return; - } - - // if the download succeeded - if (m_status == Job_Failed) - { - m_output_file->cancelWriting(); - m_reply.reset(); - emit failed(m_index_within_job); - return; - } - - // if we wrote any data to the save file, we try to commit the data to the real file. - if (wroteAnyData) - { - // nothing went wrong... - if (m_output_file->commit()) - { - m_status = Job_Finished; - m_entry->setMD5Sum(md5sum.result().toHex().constData()); - } - else - { - qCritical() << "Failed to commit changes to " << m_target_path; - m_output_file->cancelWriting(); - m_reply.reset(); - m_status = Job_Failed; - emit failed(m_index_within_job); - return; - } - } - else - { - m_status = Job_Finished; - } - - // then get rid of the save file - m_output_file.reset(); - - QFileInfo output_file_info(m_target_path); - - m_entry->setETag(m_reply->rawHeader("ETag").constData()); - if (m_reply->hasRawHeader("Last-Modified")) - { - m_entry->setRemoteChangedTimestamp(m_reply->rawHeader("Last-Modified").constData()); - } - m_entry->setLocalChangedTimestamp(output_file_info.lastModified().toUTC().toMSecsSinceEpoch()); - m_entry->setStale(false); - ENV.metacache()->updateEntry(m_entry); - - m_reply.reset(); - emit succeeded(m_index_within_job); - return; -} - -void CacheDownload::downloadReadyRead() -{ - QByteArray ba = m_reply->readAll(); - md5sum.addData(ba); - if (m_output_file->write(ba) != ba.size()) - { - qCritical() << "Failed writing into " + m_target_path; - m_status = Job_Failed; - m_output_file->cancelWriting(); - m_output_file.reset(); - emit failed(m_index_within_job); - wroteAnyData = false; - return; - } - wroteAnyData = true; -} diff --git a/api/logic/net/ChecksumValidator.h b/api/logic/net/ChecksumValidator.h new file mode 100644 index 00000000..3cd25bc3 --- /dev/null +++ b/api/logic/net/ChecksumValidator.h @@ -0,0 +1,57 @@ +#pragma once + +#include "Validator.h" +#include <QCryptographicHash> +#include <memory> +#include <QFile> + +namespace Net { +class ChecksumValidator: public Validator +{ +public: /* con/des */ + ChecksumValidator(QCryptographicHash::Algorithm algorithm, QByteArray expected = QByteArray()) + :m_checksum(algorithm), m_expected(expected) + { + }; + virtual ~ChecksumValidator() {}; + +public: /* methods */ + bool init(QNetworkRequest &) override + { + m_checksum.reset(); + return true; + } + bool write(QByteArray & data) override + { + m_checksum.addData(data); + this->data.append(data); + return true; + } + bool abort() override + { + return true; + } + bool validate(QNetworkReply &) override + { + if(m_expected.size() && m_expected != hash()) + { + qWarning() << "Checksum mismatch, download is bad."; + return false; + } + return true; + } + QByteArray hash() + { + return m_checksum.result(); + } + void setExpected(QByteArray expected) + { + m_expected = expected; + } + +private: /* data */ + QByteArray data; + QCryptographicHash m_checksum; + QByteArray m_expected; +}; +}
\ No newline at end of file diff --git a/api/logic/net/Download.cpp b/api/logic/net/Download.cpp new file mode 100644 index 00000000..70fe7608 --- /dev/null +++ b/api/logic/net/Download.cpp @@ -0,0 +1,199 @@ +/* Copyright 2013-2015 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 <QFileInfo> +#include <QDateTime> +#include <QDebug> +#include "Env.h" +#include <FileSystem.h> +#include "ChecksumValidator.h" +#include "MetaCacheSink.h" +#include "ByteArraySink.h" + +namespace Net { + +Download::Download():NetAction() +{ + m_status = Job_NotStarted; +} + +Download::Ptr Download::makeCached(QUrl url, MetaEntryPtr entry) +{ + Download * dl = new Download(); + dl->m_url = url; + auto md5Node = new ChecksumValidator(QCryptographicHash::Md5); + auto cachedNode = new MetaCacheSink(entry, md5Node); + dl->m_sink.reset(cachedNode); + dl->m_target_path = entry->getFullPath(); + return std::shared_ptr<Download>(dl); +} + +Download::Ptr Download::makeByteArray(QUrl url, QByteArray *output) +{ + Download * dl = new Download(); + dl->m_url = url; + dl->m_sink.reset(new ByteArraySink(output)); + return std::shared_ptr<Download>(dl); +} + +Download::Ptr Download::makeFile(QUrl url, QString path) +{ + Download * dl = new Download(); + dl->m_url = url; + dl->m_sink.reset(new FileSink(path)); + return std::shared_ptr<Download>(dl); +} + +void Download::addValidator(Validator * v) +{ + m_sink->addValidator(v); +} + +void Download::start() +{ + QNetworkRequest request(m_url); + m_status = m_sink->init(request); + switch(m_status) + { + case Job_Finished: + emit succeeded(m_index_within_job); + qDebug() << "Download cache hit " << m_url.toString(); + return; + case Job_InProgress: + qDebug() << "Downloading " << m_url.toString(); + break; + case Job_NotStarted: + case Job_Failed: + emit failed(m_index_within_job); + return; + } + + request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0"); + + auto worker = ENV.qnam(); + QNetworkReply *rep = worker->get(request); + + m_reply.reset(rep); + connect(rep, SIGNAL(downloadProgress(qint64, qint64)), SLOT(downloadProgress(qint64, qint64))); + connect(rep, SIGNAL(finished()), SLOT(downloadFinished())); + connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(downloadError(QNetworkReply::NetworkError))); + connect(rep, SIGNAL(readyRead()), SLOT(downloadReadyRead())); +} + +void Download::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) +{ + m_total_progress = bytesTotal; + m_progress = bytesReceived; + emit netActionProgress(m_index_within_job, bytesReceived, bytesTotal); +} + +void Download::downloadError(QNetworkReply::NetworkError error) +{ + // error happened during download. + qCritical() << "Failed " << m_url.toString() << " with reason " << error; + m_status = Job_Failed; +} + +bool Download::handleRedirect() +{ + QVariant redirect = m_reply->header(QNetworkRequest::LocationHeader); + QString redirectURL; + if(redirect.isValid()) + { + redirectURL = redirect.toString(); + } + // FIXME: This is a hack for https://bugreports.qt-project.org/browse/QTBUG-41061 + else if(m_reply->hasRawHeader("Location")) + { + auto data = m_reply->rawHeader("Location"); + if(data.size() > 2 && data[0] == '/' && data[1] == '/') + { + redirectURL = m_reply->url().scheme() + ":" + data; + } + } + if (!redirectURL.isEmpty()) + { + m_url = QUrl(redirect.toString()); + qDebug() << "Following redirect to " << m_url.toString(); + start(); + return true; + } + return false; +} + + +void Download::downloadFinished() +{ + // handle HTTP redirection first + if(handleRedirect()) + { + qDebug() << "Download redirected:" << m_url.toString(); + return; + } + + // if the download failed before this point ... + if (m_status == Job_Failed) + { + qDebug() << "Download failed in previous step:" << m_url.toString(); + m_sink->abort(); + m_reply.reset(); + emit failed(m_index_within_job); + return; + } + + // make sure we got all the remaining data, if any + auto data = m_reply->readAll(); + if(data.size()) + { + qDebug() << "Writing extra" << data.size() << "bytes to" << m_target_path; + m_status = m_sink->write(data); + } + + // otherwise, finalize the whole graph + m_status = m_sink->finalize(*m_reply.get()); + if (m_status != Job_Finished) + { + qDebug() << "Download failed to finalize:" << m_url.toString(); + m_sink->abort(); + m_reply.reset(); + emit failed(m_index_within_job); + return; + } + m_reply.reset(); + qDebug() << "Download succeeded:" << m_url.toString(); + emit succeeded(m_index_within_job); +} + +void Download::downloadReadyRead() +{ + if(m_status == Job_InProgress) + { + auto data = m_reply->readAll(); + m_status = m_sink->write(data); + if(m_status == Job_Failed) + { + qCritical() << "Failed to process response chunk for " << m_target_path; + } + // qDebug() << "Download" << m_url.toString() << "gained" << data.size() << "bytes"; + } + else + { + qCritical() << "Cannot write to " << m_target_path << ", illegal status" << m_status; + } +} + +} diff --git a/api/logic/net/CacheDownload.h b/api/logic/net/Download.h index d83b2a0f..8e38dfc9 100644 --- a/api/logic/net/CacheDownload.h +++ b/api/logic/net/Download.h @@ -17,47 +17,50 @@ #include "NetAction.h" #include "HttpMetaCache.h" -#include <QCryptographicHash> -#include <QSaveFile> +#include "Validator.h" +#include "Sink.h" #include "multimc_logic_export.h" - -typedef std::shared_ptr<class CacheDownload> CacheDownloadPtr; -class MULTIMC_LOGIC_EXPORT CacheDownload : public NetAction +namespace Net { +class MULTIMC_LOGIC_EXPORT Download : public NetAction { Q_OBJECT -private: - MetaEntryPtr m_entry; - /// if saving to file, use the one specified in this string - QString m_target_path; - - /// this is the output file, if any - std::unique_ptr<QSaveFile> m_output_file; - /// the hash-as-you-download - QCryptographicHash md5sum; - - bool wroteAnyData = false; +public: /* types */ + typedef std::shared_ptr<class Download> Ptr; +protected: /* con/des */ + explicit Download(); public: - explicit CacheDownload(QUrl url, MetaEntryPtr entry); - static CacheDownloadPtr make(QUrl url, MetaEntryPtr entry) - { - return CacheDownloadPtr(new CacheDownload(url, entry)); - } - virtual ~CacheDownload(){}; + virtual ~Download(){}; + static Download::Ptr makeCached(QUrl url, MetaEntryPtr entry); + static Download::Ptr makeByteArray(QUrl url, QByteArray *output); + static Download::Ptr makeFile(QUrl url, QString path); + +public: /* methods */ + // FIXME: remove this QString getTargetFilepath() { return m_target_path; } -protected -slots: + // FIXME: remove this + void addValidator(Validator * v); + +private: /* methods */ + bool handleRedirect(); + +protected slots: virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal); virtual void downloadError(QNetworkReply::NetworkError error); virtual void downloadFinished(); virtual void downloadReadyRead(); -public -slots: +public slots: virtual void start(); + +private: /* data */ + // FIXME: remove this, it has no business being here. + QString m_target_path; + std::unique_ptr<Sink> m_sink; }; +}
\ No newline at end of file diff --git a/api/logic/net/FileSink.cpp b/api/logic/net/FileSink.cpp new file mode 100644 index 00000000..b4c418a5 --- /dev/null +++ b/api/logic/net/FileSink.cpp @@ -0,0 +1,99 @@ +#include "FileSink.h" +#include <QFile> +#include <QFileInfo> +#include "Env.h" +#include "FileSystem.h" + +namespace Net { + +FileSink::FileSink(QString filename) + :m_filename(filename) +{ + // nil +}; + +FileSink::~FileSink() +{ + // nil +}; + +JobStatus FileSink::init(QNetworkRequest& request) +{ + auto result = initCache(request); + if(result != Job_InProgress) + { + return result; + } + // create a new save file and open it for writing + if (!FS::ensureFilePathExists(m_filename)) + { + qCritical() << "Could not create folder for " + m_filename; + return Job_Failed; + } + m_output_file.reset(new QSaveFile(m_filename)); + if (!m_output_file->open(QIODevice::WriteOnly)) + { + qCritical() << "Could not open " + m_filename + " for writing"; + return Job_Failed; + } + + if(initAllValidators(request)) + return Job_InProgress; + return Job_Failed; +} + +JobStatus FileSink::initCache(QNetworkRequest &) +{ + return Job_InProgress; +} + +JobStatus FileSink::write(QByteArray& data) +{ + if (!writeAllValidators(data) || m_output_file->write(data) != data.size()) + { + qCritical() << "Failed writing into " + m_filename; + m_output_file->cancelWriting(); + m_output_file.reset(); + wroteAnyData = false; + return Job_Failed; + } + wroteAnyData = true; + return Job_InProgress; +} + +JobStatus FileSink::abort() +{ + m_output_file->cancelWriting(); + failAllValidators(); + return Job_Failed; +} + +JobStatus FileSink::finalize(QNetworkReply& reply) +{ + // if we wrote any data to the save file, we try to commit the data to the real file. + if (wroteAnyData) + { + // ask validators for data consistency + // we only do this for actual downloads, not 'your data is still the same' cache hits + if(!finalizeAllValidators(reply)) + return Job_Failed; + // nothing went wrong... + if (!m_output_file->commit()) + { + qCritical() << "Failed to commit changes to " << m_filename; + m_output_file->cancelWriting(); + return Job_Failed; + } + } + // then get rid of the save file + m_output_file.reset(); + + return finalizeCache(reply); +} + +JobStatus FileSink::finalizeCache(QNetworkReply &) +{ + return Job_Finished; +} + +} diff --git a/api/logic/net/FileSink.h b/api/logic/net/FileSink.h new file mode 100644 index 00000000..7adc9c47 --- /dev/null +++ b/api/logic/net/FileSink.h @@ -0,0 +1,27 @@ +#pragma once +#include "Sink.h" +#include <QSaveFile> + +namespace Net { +class FileSink : public Sink +{ +public: /* con/des */ + FileSink(QString filename); + virtual ~FileSink(); + +public: /* methods */ + JobStatus init(QNetworkRequest & request) override; + JobStatus write(QByteArray & data) override; + JobStatus abort() override; + JobStatus finalize(QNetworkReply & reply) override; + +protected: /* methods */ + virtual JobStatus initCache(QNetworkRequest &); + virtual JobStatus finalizeCache(QNetworkReply &reply); + +protected: /* data */ + QString m_filename; + bool wroteAnyData = false; + std::unique_ptr<QSaveFile> m_output_file; +}; +} diff --git a/api/logic/net/MD5EtagDownload.cpp b/api/logic/net/MD5EtagDownload.cpp deleted file mode 100644 index 3b4d5dcd..00000000 --- a/api/logic/net/MD5EtagDownload.cpp +++ /dev/null @@ -1,155 +0,0 @@ -/* Copyright 2013-2015 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 "Env.h" -#include "MD5EtagDownload.h" -#include <FileSystem.h> -#include <QCryptographicHash> -#include <QDebug> - -MD5EtagDownload::MD5EtagDownload(QUrl url, QString target_path) : NetAction() -{ - m_url = url; - m_target_path = target_path; - m_status = Job_NotStarted; -} - -void MD5EtagDownload::start() -{ - QString filename = m_target_path; - m_output_file.setFileName(filename); - // if there already is a file and md5 checking is in effect and it can be opened - if (m_output_file.exists() && m_output_file.open(QIODevice::ReadOnly)) - { - // get the md5 of the local file. - m_local_md5 = - QCryptographicHash::hash(m_output_file.readAll(), QCryptographicHash::Md5) - .toHex() - .constData(); - m_output_file.close(); - // if we are expecting some md5sum, compare it with the local one - if (!m_expected_md5.isEmpty()) - { - // skip if they match - if(m_local_md5 == m_expected_md5) - { - qDebug() << "Skipping " << m_url.toString() << ": md5 match."; - emit succeeded(m_index_within_job); - return; - } - } - else - { - // no expected md5. we use the local md5sum as an ETag - } - } - if (!FS::ensureFilePathExists(filename)) - { - emit failed(m_index_within_job); - return; - } - - QNetworkRequest request(m_url); - - qDebug() << "Downloading " << m_url.toString() << " local MD5: " << m_local_md5; - - if(!m_local_md5.isEmpty()) - { - request.setRawHeader(QString("If-None-Match").toLatin1(), m_local_md5.toLatin1()); - } - if(!m_expected_md5.isEmpty()) - qDebug() << "Expecting " << m_expected_md5; - - request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0 (Uncached)"); - - // Go ahead and try to open the file. - // If we don't do this, empty files won't be created, which breaks the updater. - // Plus, this way, we don't end up starting a download for a file we can't open. - if (!m_output_file.open(QIODevice::WriteOnly)) - { - emit failed(m_index_within_job); - return; - } - - auto worker = ENV.qnam(); - QNetworkReply *rep = worker->get(request); - - m_reply.reset(rep); - connect(rep, SIGNAL(downloadProgress(qint64, qint64)), - SLOT(downloadProgress(qint64, qint64))); - connect(rep, SIGNAL(finished()), SLOT(downloadFinished())); - connect(rep, SIGNAL(error(QNetworkReply::NetworkError)), - SLOT(downloadError(QNetworkReply::NetworkError))); - connect(rep, SIGNAL(readyRead()), SLOT(downloadReadyRead())); -} - -void MD5EtagDownload::downloadProgress(qint64 bytesReceived, qint64 bytesTotal) -{ - m_total_progress = bytesTotal; - m_progress = bytesReceived; - emit netActionProgress(m_index_within_job, bytesReceived, bytesTotal); -} - -void MD5EtagDownload::downloadError(QNetworkReply::NetworkError error) -{ - qCritical() << "Error" << error << ":" << m_reply->errorString() << "while downloading" - << m_reply->url(); - m_status = Job_Failed; -} - -void MD5EtagDownload::downloadFinished() -{ - // if the download succeeded - if (m_status != Job_Failed) - { - // nothing went wrong... - m_status = Job_Finished; - m_output_file.close(); - - // FIXME: compare with the real written data md5sum - // this is just an ETag - qDebug() << "Finished " << m_url.toString() << " got " << m_reply->rawHeader("ETag").constData(); - - m_reply.reset(); - emit succeeded(m_index_within_job); - return; - } - // else the download failed - else - { - m_output_file.close(); - m_output_file.remove(); - m_reply.reset(); - emit failed(m_index_within_job); - return; - } -} - -void MD5EtagDownload::downloadReadyRead() -{ - if (!m_output_file.isOpen()) - { - if (!m_output_file.open(QIODevice::WriteOnly)) - { - /* - * Can't open the file... the job failed - */ - m_reply->abort(); - emit failed(m_index_within_job); - return; - } - } - m_output_file.write(m_reply->readAll()); -} diff --git a/api/logic/net/MD5EtagDownload.h b/api/logic/net/MD5EtagDownload.h deleted file mode 100644 index cd1cb550..00000000 --- a/api/logic/net/MD5EtagDownload.h +++ /dev/null @@ -1,52 +0,0 @@ -/* Copyright 2013-2015 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 "NetAction.h" -#include <QFile> - -typedef std::shared_ptr<class MD5EtagDownload> Md5EtagDownloadPtr; -class MD5EtagDownload : public NetAction -{ - Q_OBJECT -public: - /// the expected md5 checksum. Only set from outside - QString m_expected_md5; - /// the md5 checksum of a file that already exists. - QString m_local_md5; - /// if saving to file, use the one specified in this string - QString m_target_path; - /// this is the output file, if any - QFile m_output_file; - -public: - explicit MD5EtagDownload(QUrl url, QString target_path); - static Md5EtagDownloadPtr make(QUrl url, QString target_path) - { - return Md5EtagDownloadPtr(new MD5EtagDownload(url, target_path)); - } - virtual ~MD5EtagDownload(){}; -protected -slots: - virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal); - virtual void downloadError(QNetworkReply::NetworkError error); - virtual void downloadFinished(); - virtual void downloadReadyRead(); - -public -slots: - virtual void start(); -}; diff --git a/api/logic/net/MetaCacheSink.cpp b/api/logic/net/MetaCacheSink.cpp new file mode 100644 index 00000000..539b4bb3 --- /dev/null +++ b/api/logic/net/MetaCacheSink.cpp @@ -0,0 +1,59 @@ +#include "MetaCacheSink.h" +#include <QFile> +#include <QFileInfo> +#include "Env.h" +#include "FileSystem.h" + +namespace Net { + +MetaCacheSink::MetaCacheSink(MetaEntryPtr entry, ChecksumValidator * md5sum) + :Net::FileSink(entry->getFullPath()), m_entry(entry), m_md5Node(md5sum) +{ + addValidator(md5sum); +}; + +MetaCacheSink::~MetaCacheSink() +{ + // nil +}; + +JobStatus MetaCacheSink::initCache(QNetworkRequest& request) +{ + if (!m_entry->isStale()) + { + return Job_Finished; + } + // check if file exists, if it does, use its information for the request + QFile current(m_filename); + if(current.exists() && current.size() != 0) + { + if (m_entry->getRemoteChangedTimestamp().size()) + { + request.setRawHeader(QString("If-Modified-Since").toLatin1(), m_entry->getRemoteChangedTimestamp().toLatin1()); + } + if (m_entry->getETag().size()) + { + request.setRawHeader(QString("If-None-Match").toLatin1(), m_entry->getETag().toLatin1()); + } + } + return Job_InProgress; +} + +JobStatus MetaCacheSink::finalizeCache(QNetworkReply & reply) +{ + QFileInfo output_file_info(m_filename); + if(wroteAnyData) + { + m_entry->setMD5Sum(m_md5Node->hash().toHex().constData()); + } + m_entry->setETag(reply.rawHeader("ETag").constData()); + if (reply.hasRawHeader("Last-Modified")) + { + m_entry->setRemoteChangedTimestamp(reply.rawHeader("Last-Modified").constData()); + } + m_entry->setLocalChangedTimestamp(output_file_info.lastModified().toUTC().toMSecsSinceEpoch()); + m_entry->setStale(false); + ENV.metacache()->updateEntry(m_entry); + return Job_Finished; +} +} diff --git a/api/logic/net/MetaCacheSink.h b/api/logic/net/MetaCacheSink.h new file mode 100644 index 00000000..98e9bba1 --- /dev/null +++ b/api/logic/net/MetaCacheSink.h @@ -0,0 +1,21 @@ +#pragma once +#include "FileSink.h" +#include "ChecksumValidator.h" +#include "net/HttpMetaCache.h" + +namespace Net { +class MetaCacheSink : public FileSink +{ +public: /* con/des */ + MetaCacheSink(MetaEntryPtr entry, ChecksumValidator * md5sum); + virtual ~MetaCacheSink(); + +protected: /* methods */ + JobStatus initCache(QNetworkRequest & request) override; + JobStatus finalizeCache(QNetworkReply & reply) override; + +private: /* data */ + MetaEntryPtr m_entry; + ChecksumValidator * m_md5Node; +}; +} diff --git a/api/logic/net/NetAction.h b/api/logic/net/NetAction.h index 3c395605..2b3f1b7b 100644 --- a/api/logic/net/NetAction.h +++ b/api/logic/net/NetAction.h @@ -59,9 +59,6 @@ public: /// the network reply unique_qobject_ptr<QNetworkReply> m_reply; - /// the content of the content-type header - QString m_content_type; - /// source URL QUrl m_url; diff --git a/api/logic/net/NetJob.cpp b/api/logic/net/NetJob.cpp index 76c61c35..ca86242a 100644 --- a/api/logic/net/NetJob.cpp +++ b/api/logic/net/NetJob.cpp @@ -14,9 +14,7 @@ */ #include "NetJob.h" -#include "MD5EtagDownload.h" -#include "ByteArrayDownload.h" -#include "CacheDownload.h" +#include "Download.h" #include <QDebug> diff --git a/api/logic/net/NetJob.h b/api/logic/net/NetJob.h index 167fe176..3d16b21f 100644 --- a/api/logic/net/NetJob.h +++ b/api/logic/net/NetJob.h @@ -16,9 +16,7 @@ #pragma once #include <QtNetwork> #include "NetAction.h" -#include "ByteArrayDownload.h" -#include "MD5EtagDownload.h" -#include "CacheDownload.h" +#include "Download.h" #include "HttpMetaCache.h" #include "tasks/Task.h" #include "QObjectPtr.h" diff --git a/api/logic/net/Sink.h b/api/logic/net/Sink.h new file mode 100644 index 00000000..53f2b221 --- /dev/null +++ b/api/logic/net/Sink.h @@ -0,0 +1,70 @@ +#pragma once + +#include "net/NetAction.h" + +#include "multimc_logic_export.h" +#include "Validator.h" + +namespace Net { +class MULTIMC_LOGIC_EXPORT Sink +{ +public: /* con/des */ + Sink() {}; + virtual ~Sink() {}; + +public: /* methods */ + virtual JobStatus init(QNetworkRequest & request) = 0; + virtual JobStatus write(QByteArray & data) = 0; + virtual JobStatus abort() = 0; + virtual JobStatus finalize(QNetworkReply & reply) = 0; + + void addValidator(Validator * validator) + { + if(validator) + { + validators.push_back(std::shared_ptr<Validator>(validator)); + } + } + +protected: /* methods */ + bool finalizeAllValidators(QNetworkReply & reply) + { + for(auto & validator: validators) + { + if(!validator->validate(reply)) + return false; + } + return true; + } + bool failAllValidators() + { + bool success = true; + for(auto & validator: validators) + { + success &= validator->abort(); + } + return success; + } + bool initAllValidators(QNetworkRequest & request) + { + for(auto & validator: validators) + { + if(!validator->init(request)) + return false; + } + return true; + } + bool writeAllValidators(QByteArray & data) + { + for(auto & validator: validators) + { + if(!validator->write(data)) + return false; + } + return true; + } + +protected: /* data */ + std::vector<std::shared_ptr<Validator>> validators; +}; +} diff --git a/api/logic/net/Validator.h b/api/logic/net/Validator.h new file mode 100644 index 00000000..a390ab28 --- /dev/null +++ b/api/logic/net/Validator.h @@ -0,0 +1,20 @@ +#pragma once + +#include "net/NetAction.h" + +#include "multimc_logic_export.h" + +namespace Net { +class MULTIMC_LOGIC_EXPORT Validator +{ +public: /* con/des */ + Validator() {}; + virtual ~Validator() {}; + +public: /* methods */ + virtual bool init(QNetworkRequest & request) = 0; + virtual bool write(QByteArray & data) = 0; + virtual bool abort() = 0; + virtual bool validate(QNetworkReply & reply) = 0; +}; +}
\ No newline at end of file diff --git a/api/logic/news/NewsChecker.cpp b/api/logic/news/NewsChecker.cpp index be4aa1d1..7cc87953 100644 --- a/api/logic/news/NewsChecker.cpp +++ b/api/logic/news/NewsChecker.cpp @@ -37,7 +37,7 @@ void NewsChecker::reloadNews() qDebug() << "Reloading news."; NetJob* job = new NetJob("News RSS Feed"); - job->addNetAction(ByteArrayDownload::make(m_feedUrl)); + job->addNetAction(Net::Download::makeByteArray(m_feedUrl, &newsData)); QObject::connect(job, &NetJob::succeeded, this, &NewsChecker::rssDownloadFinished); QObject::connect(job, &NetJob::failed, this, &NewsChecker::rssDownloadFailed); m_newsNetJob.reset(job); @@ -49,13 +49,7 @@ void NewsChecker::rssDownloadFinished() // Parse the XML file and process the RSS feed entries. qDebug() << "Finished loading RSS feed."; - QByteArray data; - { - ByteArrayDownloadPtr dl = std::dynamic_pointer_cast<ByteArrayDownload>(m_newsNetJob->first()); - data = dl->m_data; - m_newsNetJob.reset(); - } - + m_newsNetJob.reset(); QDomDocument doc; { // Stuff to store error info in. @@ -64,12 +58,14 @@ void NewsChecker::rssDownloadFinished() int errorCol = -1; // Parse the XML. - if (!doc.setContent(data, false, &errorMsg, &errorLine, &errorCol)) + if (!doc.setContent(newsData, false, &errorMsg, &errorLine, &errorCol)) { QString fullErrorMsg = QString("Error parsing RSS feed XML. %s at %d:%d.").arg(errorMsg, errorLine, errorCol); fail(fullErrorMsg); + newsData.clear(); return; } + newsData.clear(); } // If the parsing succeeded, read it. diff --git a/api/logic/news/NewsChecker.h b/api/logic/news/NewsChecker.h index b8b90728..90b2f6d1 100644 --- a/api/logic/news/NewsChecker.h +++ b/api/logic/news/NewsChecker.h @@ -74,7 +74,7 @@ protected slots: void rssDownloadFinished(); void rssDownloadFailed(QString reason); -protected: +protected: /* data */ //! The URL for the RSS feed to fetch. QString m_feedUrl; @@ -87,21 +87,19 @@ protected: //! True if news has been loaded. bool m_loadedNews; + QByteArray newsData; + /*! * Gets the error message that was given last time the news was loaded. * If the last news load succeeded, this will be an empty string. */ QString m_lastLoadError; +protected slots: + /// Emits newsLoaded() and sets m_lastLoadError to empty string. + void succeed(); - /*! - * Emits newsLoaded() and sets m_lastLoadError to empty string. - */ - void Q_SLOT succeed(); - - /*! - * Emits newsLoadingFailed() and sets m_lastLoadError to the given message. - */ - void Q_SLOT fail(const QString& errorMsg); + /// Emits newsLoadingFailed() and sets m_lastLoadError to the given message. + void fail(const QString& errorMsg); }; diff --git a/api/logic/notifications/NotificationChecker.cpp b/api/logic/notifications/NotificationChecker.cpp index ab2570b7..6d006c31 100644 --- a/api/logic/notifications/NotificationChecker.cpp +++ b/api/logic/notifications/NotificationChecker.cpp @@ -6,7 +6,7 @@ #include <QDebug> #include "Env.h" -#include "net/CacheDownload.h" +#include "net/Download.h" NotificationChecker::NotificationChecker(QObject *parent) @@ -55,9 +55,8 @@ void NotificationChecker::checkForNotifications() m_checkJob.reset(new NetJob("Checking for notifications")); auto entry = ENV.metacache()->resolveEntry("root", "notifications.json"); entry->setStale(true); - m_checkJob->addNetAction(m_download = CacheDownload::make(m_notificationsUrl, entry)); - connect(m_download.get(), &CacheDownload::succeeded, this, - &NotificationChecker::downloadSucceeded); + m_checkJob->addNetAction(m_download = Net::Download::makeCached(m_notificationsUrl, entry)); + connect(m_download.get(), &Net::Download::succeeded, this, &NotificationChecker::downloadSucceeded); m_checkJob->start(); } diff --git a/api/logic/notifications/NotificationChecker.h b/api/logic/notifications/NotificationChecker.h index a2d92ab9..c8e831d5 100644 --- a/api/logic/notifications/NotificationChecker.h +++ b/api/logic/notifications/NotificationChecker.h @@ -3,7 +3,7 @@ #include <QObject> #include "net/NetJob.h" -#include "net/CacheDownload.h" +#include "net/Download.h" #include "multimc_logic_export.h" @@ -55,7 +55,7 @@ private: QList<NotificationEntry> m_entries; QUrl m_notificationsUrl; NetJobPtr m_checkJob; - CacheDownloadPtr m_download; + Net::Download::Ptr m_download; QString m_appVersionChannel; QString m_appPlatform; diff --git a/api/logic/status/StatusChecker.cpp b/api/logic/status/StatusChecker.cpp index 13cac037..d09c9976 100644 --- a/api/logic/status/StatusChecker.cpp +++ b/api/logic/status/StatusChecker.cpp @@ -43,7 +43,7 @@ void StatusChecker::reloadStatus() // qDebug() << "Reloading status."; NetJob* job = new NetJob("Status JSON"); - job->addNetAction(ByteArrayDownload::make(URLConstants::MOJANG_STATUS_URL)); + job->addNetAction(Net::Download::makeByteArray(URLConstants::MOJANG_STATUS_URL, &dataSink)); QObject::connect(job, &NetJob::succeeded, this, &StatusChecker::statusDownloadFinished); QObject::connect(job, &NetJob::failed, this, &StatusChecker::statusDownloadFailed); m_statusNetJob.reset(job); @@ -55,15 +55,10 @@ void StatusChecker::statusDownloadFinished() { qDebug() << "Finished loading status JSON."; m_statusEntries.clear(); - QByteArray data; - { - ByteArrayDownloadPtr dl = std::dynamic_pointer_cast<ByteArrayDownload>(m_statusNetJob->first()); - data = dl->m_data; - m_statusNetJob.reset(); - } + m_statusNetJob.reset(); QJsonParseError jsonError; - QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError); + QJsonDocument jsonDoc = QJsonDocument::fromJson(dataSink, &jsonError); if (jsonError.error != QJsonParseError::NoError) { diff --git a/api/logic/status/StatusChecker.h b/api/logic/status/StatusChecker.h index c1a54dba..452248f4 100644 --- a/api/logic/status/StatusChecker.h +++ b/api/logic/status/StatusChecker.h @@ -26,35 +26,35 @@ class MULTIMC_LOGIC_EXPORT StatusChecker : public QObject { Q_OBJECT -public: +public: /* con/des */ StatusChecker(); +public: /* methods */ QString getLastLoadErrorMsg() const; - bool isLoadingStatus() const; - QMap<QString, QString> getStatusEntries() const; - void Q_SLOT reloadStatus(); - -protected: - virtual void timerEvent(QTimerEvent *); - signals: void statusLoading(bool loading); void statusChanged(QMap<QString, QString> newStatus); +public slots: + void reloadStatus(); + +protected: /* methods */ + virtual void timerEvent(QTimerEvent *); + protected slots: void statusDownloadFinished(); void statusDownloadFailed(QString reason); + void succeed(); + void fail(const QString& errorMsg); -protected: +protected: /* data */ QMap<QString, QString> m_prevEntries; QMap<QString, QString> m_statusEntries; NetJobPtr m_statusNetJob; QString m_lastLoadError; - - void Q_SLOT succeed(); - void Q_SLOT fail(const QString& errorMsg); + QByteArray dataSink; }; diff --git a/api/logic/trans/TranslationDownloader.cpp b/api/logic/trans/TranslationDownloader.cpp index ee5c1fd2..61e24c9a 100644 --- a/api/logic/trans/TranslationDownloader.cpp +++ b/api/logic/trans/TranslationDownloader.cpp @@ -1,6 +1,6 @@ #include "TranslationDownloader.h" #include "net/NetJob.h" -#include "net/CacheDownload.h" +#include "net/Download.h" #include "net/URLConstants.h" #include "Env.h" #include <QDebug> @@ -12,7 +12,7 @@ void TranslationDownloader::downloadTranslations() { qDebug() << "Downloading Translations Index..."; m_index_job.reset(new NetJob("Translations Index")); - m_index_task = ByteArrayDownload::make(QUrl("http://files.multimc.org/translations/index")); + m_index_task = Net::Download::makeByteArray(QUrl("http://files.multimc.org/translations/index"), &m_data); m_index_job->addNetAction(m_index_task); connect(m_index_job.get(), &NetJob::failed, this, &TranslationDownloader::indexFailed); connect(m_index_job.get(), &NetJob::succeeded, this, &TranslationDownloader::indexRecieved); @@ -22,17 +22,15 @@ void TranslationDownloader::indexRecieved() { qDebug() << "Got translations index!"; m_dl_job.reset(new NetJob("Translations")); - QList<QByteArray> lines = m_index_task->m_data.split('\n'); + QList<QByteArray> lines = m_data.split('\n'); + m_data.clear(); for (const auto line : lines) { if (!line.isEmpty()) { MetaEntryPtr entry = ENV.metacache()->resolveEntry("translations", "mmc_" + line); entry->setStale(true); - CacheDownloadPtr dl = CacheDownload::make( - QUrl(URLConstants::TRANSLATIONS_BASE_URL + line), - entry); - m_dl_job->addNetAction(dl); + m_dl_job->addNetAction(Net::Download::makeCached(QUrl(URLConstants::TRANSLATIONS_BASE_URL + line), entry)); } } connect(m_dl_job.get(), &NetJob::succeeded, this, &TranslationDownloader::dlGood); diff --git a/api/logic/trans/TranslationDownloader.h b/api/logic/trans/TranslationDownloader.h index e7893805..ad3a648d 100644 --- a/api/logic/trans/TranslationDownloader.h +++ b/api/logic/trans/TranslationDownloader.h @@ -6,8 +6,9 @@ #include <QObject> #include <net/NetJob.h> #include "multimc_logic_export.h" - -class ByteArrayDownload; +namespace Net{ +class Download; +} class NetJob; class MULTIMC_LOGIC_EXPORT TranslationDownloader : public QObject @@ -26,7 +27,8 @@ private slots: void dlGood(); private: - std::shared_ptr<ByteArrayDownload> m_index_task; + std::shared_ptr<Net::Download> m_index_task; NetJobPtr m_dl_job; NetJobPtr m_index_job; + QByteArray m_data; }; diff --git a/api/logic/updater/DownloadTask.cpp b/api/logic/updater/DownloadTask.cpp index 6947e8bf..9bb31695 100644 --- a/api/logic/updater/DownloadTask.cpp +++ b/api/logic/updater/DownloadTask.cpp @@ -43,22 +43,19 @@ void DownloadTask::loadVersionInfo() { setStatus(tr("Loading version information...")); - m_currentVersionFileListDownload.reset(); - m_newVersionFileListDownload.reset(); - NetJob *netJob = new NetJob("Version Info"); // Find the index URL. QUrl newIndexUrl = QUrl(m_status.newRepoUrl).resolved(QString::number(m_status.newVersionId) + ".json"); qDebug() << m_status.newRepoUrl << " turns into " << newIndexUrl; - netJob->addNetAction(m_newVersionFileListDownload = ByteArrayDownload::make(newIndexUrl)); + netJob->addNetAction(m_newVersionFileListDownload = Net::Download::makeByteArray(newIndexUrl, &newVersionFileListData)); // If we have a current version URL, get that one too. if (!m_status.currentRepoUrl.isEmpty()) { QUrl cIndexUrl = QUrl(m_status.currentRepoUrl).resolved(QString::number(m_status.currentVersionId) + ".json"); - netJob->addNetAction(m_currentVersionFileListDownload = ByteArrayDownload::make(cIndexUrl)); + netJob->addNetAction(m_currentVersionFileListDownload = Net::Download::makeByteArray(cIndexUrl, ¤tVersionFileListData)); qDebug() << m_status.currentRepoUrl << " turns into " << cIndexUrl; } @@ -92,7 +89,7 @@ void DownloadTask::processDownloadedVersionInfo() setStatus(tr("Reading file list for new version...")); qDebug() << "Reading file list for new version..."; QString error; - if (!parseVersionInfo(m_newVersionFileListDownload->m_data, m_newVersionFileList, error)) + if (!parseVersionInfo(newVersionFileListData, m_newVersionFileList, error)) { qCritical() << error; emitFailed(error); @@ -106,7 +103,7 @@ void DownloadTask::processDownloadedVersionInfo() qDebug() << "Reading file list for current version..."; // if this fails, it's not a complete loss. QString error; - if(!parseVersionInfo( m_currentVersionFileListDownload->m_data, m_currentVersionFileList, error)) + if(!parseVersionInfo( currentVersionFileListData, m_currentVersionFileList, error)) { qDebug() << error << "This is not a fatal error."; } diff --git a/api/logic/updater/DownloadTask.h b/api/logic/updater/DownloadTask.h index 83b4a142..f6e1288e 100644 --- a/api/logic/updater/DownloadTask.h +++ b/api/logic/updater/DownloadTask.h @@ -64,8 +64,10 @@ protected: void loadVersionInfo(); NetJobPtr m_vinfoNetJob; - ByteArrayDownloadPtr m_currentVersionFileListDownload; - ByteArrayDownloadPtr m_newVersionFileListDownload; + QByteArray currentVersionFileListData; + QByteArray newVersionFileListData; + Net::Download::Ptr m_currentVersionFileListDownload; + Net::Download::Ptr m_newVersionFileListDownload; NetJobPtr m_filesNetJob; diff --git a/api/logic/updater/GoUpdate.cpp b/api/logic/updater/GoUpdate.cpp index 4e465d5c..716048a0 100644 --- a/api/logic/updater/GoUpdate.cpp +++ b/api/logic/updater/GoUpdate.cpp @@ -4,6 +4,9 @@ #include <QFile> #include <FileSystem.h> +#include "net/Download.h" +#include "net/ChecksumValidator.h" + namespace GoUpdate { @@ -189,8 +192,9 @@ bool processFileLists // We need to download the file to the updatefiles folder and add a task // to copy it to its install path. - auto download = MD5EtagDownload::make(source.url, dlPath); - download->m_expected_md5 = entry.md5; + auto download = Net::Download::makeFile(source.url, dlPath); + auto rawMd5 = QByteArray::fromHex(entry.md5.toLatin1()); + download->addValidator(new Net::ChecksumValidator(QCryptographicHash::Md5, rawMd5)); job->addNetAction(download); ops.append(Operation::CopyOp(dlPath, entry.path, entry.mode)); } diff --git a/api/logic/updater/UpdateChecker.cpp b/api/logic/updater/UpdateChecker.cpp index 1cdac916..7dc5823e 100644 --- a/api/logic/updater/UpdateChecker.cpp +++ b/api/logic/updater/UpdateChecker.cpp @@ -28,11 +28,6 @@ UpdateChecker::UpdateChecker(QString channelListUrl, QString currentChannel, int m_channelListUrl = channelListUrl; m_currentChannel = currentChannel; m_currentBuild = currentBuild; - - m_updateChecking = false; - m_chanListLoading = false; - m_checkUpdateWaiting = false; - m_chanListLoaded = false; } QList<UpdateChecker::ChannelListEntry> UpdateChecker::getChannelList() const @@ -93,9 +88,8 @@ void UpdateChecker::checkForUpdate(QString updateChannel, bool notifyNoUpdate) QUrl indexUrl = QUrl(m_newRepoUrl).resolved(QUrl("index.json")); auto job = new NetJob("GoUpdate Repository Index"); - job->addNetAction(ByteArrayDownload::make(indexUrl)); - connect(job, &NetJob::succeeded, [this, notifyNoUpdate]() - { updateCheckFinished(notifyNoUpdate); }); + job->addNetAction(Net::Download::makeByteArray(indexUrl, &indexData)); + connect(job, &NetJob::succeeded, [this, notifyNoUpdate](){ updateCheckFinished(notifyNoUpdate); }); connect(job, &NetJob::failed, this, &UpdateChecker::updateCheckFailed); indexJob.reset(job); job->start(); @@ -106,19 +100,15 @@ void UpdateChecker::updateCheckFinished(bool notifyNoUpdate) qDebug() << "Finished downloading repo index. Checking for new versions."; QJsonParseError jsonError; - QByteArray data; - { - ByteArrayDownloadPtr dl = - std::dynamic_pointer_cast<ByteArrayDownload>(indexJob->first()); - data = dl->m_data; - indexJob.reset(); - } + indexJob.reset(); - QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError); + QJsonDocument jsonDoc = QJsonDocument::fromJson(indexData, &jsonError); + indexData.clear(); if (jsonError.error != QJsonParseError::NoError || !jsonDoc.isObject()) { qCritical() << "Failed to parse GoUpdate repository index. JSON error" << jsonError.errorString() << "at offset" << jsonError.offset; + m_updateChecking = false; return; } @@ -130,6 +120,7 @@ void UpdateChecker::updateCheckFinished(bool notifyNoUpdate) { qCritical() << "Failed to check for updates. API version mismatch. We're using" << API_VERSION << "server has" << apiVersion; + m_updateChecking = false; return; } @@ -165,7 +156,6 @@ void UpdateChecker::updateCheckFinished(bool notifyNoUpdate) { emit noUpdateFound(); } - m_updateChecking = false; } @@ -178,6 +168,12 @@ void UpdateChecker::updateChanList(bool notifyNoUpdate) { qDebug() << "Loading the channel list."; + if (m_chanListLoading) + { + qDebug() << "Ignoring channel list update request. Already grabbing channel list."; + return; + } + if (m_channelListUrl.isEmpty()) { qCritical() << "Failed to update channel list. No channel list URL set." @@ -188,9 +184,8 @@ void UpdateChecker::updateChanList(bool notifyNoUpdate) m_chanListLoading = true; NetJob *job = new NetJob("Update System Channel List"); - job->addNetAction(ByteArrayDownload::make(QUrl(m_channelListUrl))); - connect(job, &NetJob::succeeded, [this, notifyNoUpdate]() - { chanListDownloadFinished(notifyNoUpdate); }); + job->addNetAction(Net::Download::makeByteArray(QUrl(m_channelListUrl), &chanlistData)); + connect(job, &NetJob::succeeded, [this, notifyNoUpdate]() { chanListDownloadFinished(notifyNoUpdate); }); QObject::connect(job, &NetJob::failed, this, &UpdateChecker::chanListDownloadFailed); chanListJob.reset(job); job->start(); @@ -198,21 +193,16 @@ void UpdateChecker::updateChanList(bool notifyNoUpdate) void UpdateChecker::chanListDownloadFinished(bool notifyNoUpdate) { - QByteArray data; - { - ByteArrayDownloadPtr dl = - std::dynamic_pointer_cast<ByteArrayDownload>(chanListJob->first()); - data = dl->m_data; - chanListJob.reset(); - } + chanListJob.reset(); QJsonParseError jsonError; - QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError); + QJsonDocument jsonDoc = QJsonDocument::fromJson(chanlistData, &jsonError); + chanlistData.clear(); if (jsonError.error != QJsonParseError::NoError) { // TODO: Report errors to the user. - qCritical() << "Failed to parse channel list JSON:" << jsonError.errorString() << "at" - << jsonError.offset; + qCritical() << "Failed to parse channel list JSON:" << jsonError.errorString() << "at" << jsonError.offset; + m_chanListLoading = false; return; } @@ -225,6 +215,7 @@ void UpdateChecker::chanListDownloadFinished(bool notifyNoUpdate) qCritical() << "Failed to check for updates. Channel list format version mismatch. We're using" << CHANLIST_FORMAT << "server has" << formatVersion; + m_chanListLoading = false; return; } diff --git a/api/logic/updater/UpdateChecker.h b/api/logic/updater/UpdateChecker.h index c7fad10e..39026b62 100644 --- a/api/logic/updater/UpdateChecker.h +++ b/api/logic/updater/UpdateChecker.h @@ -18,8 +18,6 @@ #include "net/NetJob.h" #include "GoUpdate.h" -#include <QUrl> - #include "multimc_logic_export.h" class MULTIMC_LOGIC_EXPORT UpdateChecker : public QObject @@ -78,7 +76,9 @@ private: friend class UpdateCheckerTest; NetJobPtr indexJob; + QByteArray indexData; NetJobPtr chanListJob; + QByteArray chanlistData; QString m_channelListUrl; @@ -88,24 +88,24 @@ private: * True while the system is checking for updates. * If checkForUpdate is called while this is true, it will be ignored. */ - bool m_updateChecking; + bool m_updateChecking = false; /*! * True if the channel list has loaded. * If this is false, trying to check for updates will call updateChanList first. */ - bool m_chanListLoaded; + bool m_chanListLoaded = false; /*! * Set to true while the channel list is currently loading. */ - bool m_chanListLoading; + bool m_chanListLoading = false; /*! * Set to true when checkForUpdate is called while the channel list isn't loaded. * When the channel list finishes loading, if this is true, the update checker will check for updates. */ - bool m_checkUpdateWaiting; + bool m_checkUpdateWaiting = false; /*! * if m_checkUpdateWaiting, this is the last used update channel diff --git a/api/logic/wonko/tasks/BaseWonkoEntityRemoteLoadTask.cpp b/api/logic/wonko/tasks/BaseWonkoEntityRemoteLoadTask.cpp index 727ec89d..32d2071d 100644 --- a/api/logic/wonko/tasks/BaseWonkoEntityRemoteLoadTask.cpp +++ b/api/logic/wonko/tasks/BaseWonkoEntityRemoteLoadTask.cpp @@ -15,7 +15,7 @@ #include "BaseWonkoEntityRemoteLoadTask.h" -#include "net/CacheDownload.h" +#include "net/Download.h" #include "net/HttpMetaCache.h" #include "net/NetJob.h" #include "wonko/format/WonkoFormat.h" @@ -37,7 +37,7 @@ void BaseWonkoEntityRemoteLoadTask::executeTask() auto entry = ENV.metacache()->resolveEntry("wonko", url().toString()); entry->setStale(true); - m_dl = CacheDownload::make(url(), entry); + m_dl = Net::Download::makeCached(url(), entry); job->addNetAction(m_dl); connect(job, &NetJob::failed, this, &BaseWonkoEntityRemoteLoadTask::emitFailed); connect(job, &NetJob::succeeded, this, &BaseWonkoEntityRemoteLoadTask::networkFinished); diff --git a/api/logic/wonko/tasks/BaseWonkoEntityRemoteLoadTask.h b/api/logic/wonko/tasks/BaseWonkoEntityRemoteLoadTask.h index 91ed6af0..d2bdd5a2 100644 --- a/api/logic/wonko/tasks/BaseWonkoEntityRemoteLoadTask.h +++ b/api/logic/wonko/tasks/BaseWonkoEntityRemoteLoadTask.h @@ -18,6 +18,11 @@ #include "tasks/Task.h" #include <memory> +namespace Net +{ + class Download; +} + class BaseWonkoEntity; class WonkoIndex; class WonkoVersionList; @@ -43,7 +48,7 @@ private: void executeTask() override; BaseWonkoEntity *m_entity; - std::shared_ptr<class CacheDownload> m_dl; + std::shared_ptr<Net::Download> m_dl; }; class WonkoIndexRemoteLoadTask : public BaseWonkoEntityRemoteLoadTask diff --git a/application/MainWindow.cpp b/application/MainWindow.cpp index 39a6a7e9..06d165da 100644 --- a/application/MainWindow.cpp +++ b/application/MainWindow.cpp @@ -59,7 +59,7 @@ #include <SkinUtils.h> #include <net/URLConstants.h> #include <net/NetJob.h> -#include <net/CacheDownload.h> +#include <net/Download.h> #include <news/NewsChecker.h> #include <notifications/NotificationChecker.h> #include <tools/BaseProfiler.h> @@ -519,7 +519,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new MainWindow auto accounts = MMC->accounts(); - QList<CacheDownloadPtr> skin_dls; + QList<Net::Download::Ptr> skin_dls; for (int i = 0; i < accounts->count(); i++) { auto account = accounts->at(i); @@ -531,7 +531,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new MainWindow for (auto profile : account->profiles()) { auto meta = Env::getInstance().metacache()->resolveEntry("skins", profile.id + ".png"); - auto action = CacheDownload::make(QUrl("https://" + URLConstants::SKINS_BASE + profile.id + ".png"), meta); + auto action = Net::Download::makeCached(QUrl("https://" + URLConstants::SKINS_BASE + profile.id + ".png"), meta); skin_dls.append(action); meta->setStale(true); } @@ -1045,9 +1045,8 @@ InstancePtr MainWindow::instanceFromZipPack(QString instName, QString instGroup, const QString path = url.host() + '/' + url.path(); auto entry = ENV.metacache()->resolveEntry("general", path); entry->setStale(true); - CacheDownloadPtr dl = CacheDownload::make(url, entry); NetJob job(tr("Modpack download")); - job.addNetAction(dl); + job.addNetAction(Net::Download::makeCached(url, entry)); // FIXME: possibly causes endless loop problems ProgressDialog dlDialog(this); diff --git a/application/dialogs/AboutDialog.cpp b/application/dialogs/AboutDialog.cpp index fd61adde..a474cfe6 100644 --- a/application/dialogs/AboutDialog.cpp +++ b/application/dialogs/AboutDialog.cpp @@ -109,16 +109,16 @@ AboutDialog::~AboutDialog() void AboutDialog::loadPatronList() { - NetJob* job = new NetJob("Patreon Patron List"); - patronListDownload = ByteArrayDownload::make(QUrl("http://files.multimc.org/patrons.txt")); - job->addNetAction(patronListDownload); - connect(job, &NetJob::succeeded, this, &AboutDialog::patronListLoaded); - job->start(); + netJob.reset(new NetJob("Patreon Patron List")); + netJob->addNetAction(Net::Download::makeByteArray(QUrl("http://files.multimc.org/patrons.txt"), &dataSink)); + connect(netJob.get(), &NetJob::succeeded, this, &AboutDialog::patronListLoaded); + netJob->start(); } void AboutDialog::patronListLoaded() { - QString patronListStr(patronListDownload->m_data); + QString patronListStr(dataSink); + dataSink.clear(); QString html = getCreditsHtml(patronListStr.split("\n", QString::SkipEmptyParts)); ui->creditsText->setHtml(html); } diff --git a/application/dialogs/AboutDialog.h b/application/dialogs/AboutDialog.h index 1885e9c0..5b5874a5 100644 --- a/application/dialogs/AboutDialog.h +++ b/application/dialogs/AboutDialog.h @@ -16,8 +16,7 @@ #pragma once #include <QDialog> - -#include <net/ByteArrayDownload.h> +#include <net/NetJob.h> namespace Ui { @@ -43,5 +42,6 @@ slots: private: Ui::AboutDialog *ui; - ByteArrayDownloadPtr patronListDownload; + NetJobPtr netJob; + QByteArray dataSink; }; diff --git a/application/dialogs/UpdateDialog.cpp b/application/dialogs/UpdateDialog.cpp index 31220e50..6e109bcb 100644 --- a/application/dialogs/UpdateDialog.cpp +++ b/application/dialogs/UpdateDialog.cpp @@ -46,8 +46,7 @@ void UpdateDialog::loadChangelog() url = QString("https://api.github.com/repos/MultiMC/MultiMC5/compare/%1...%2").arg(BuildConfig.GIT_COMMIT, channel); m_changelogType = CHANGELOG_COMMITS; } - changelogDownload = ByteArrayDownload::make(QUrl(url)); - dljob->addNetAction(changelogDownload); + dljob->addNetAction(Net::Download::makeByteArray(QUrl(url), &changelogData)); connect(dljob.get(), &NetJob::succeeded, this, &UpdateDialog::changelogLoaded); connect(dljob.get(), &NetJob::failed, this, &UpdateDialog::changelogFailed); dljob->start(); @@ -201,12 +200,13 @@ void UpdateDialog::changelogLoaded() switch(m_changelogType) { case CHANGELOG_COMMITS: - result = reprocessCommits(changelogDownload->m_data); + result = reprocessCommits(changelogData); break; case CHANGELOG_MARKDOWN: - result = reprocessMarkdown(changelogDownload->m_data); + result = reprocessMarkdown(changelogData); break; } + changelogData.clear(); ui->changelogBrowser->setHtml(result); } diff --git a/application/dialogs/UpdateDialog.h b/application/dialogs/UpdateDialog.h index 403b78ad..f8ecf006 100644 --- a/application/dialogs/UpdateDialog.h +++ b/application/dialogs/UpdateDialog.h @@ -16,7 +16,6 @@ #pragma once #include <QDialog> -#include "net/ByteArrayDownload.h" #include "net/NetJob.h" namespace Ui @@ -60,7 +59,7 @@ public slots: void changelogFailed(QString reason); private: - ByteArrayDownloadPtr changelogDownload; + QByteArray changelogData; NetJobPtr dljob; ChangelogType m_changelogType = CHANGELOG_MARKDOWN; }; diff --git a/application/pages/global/AccountListPage.cpp b/application/pages/global/AccountListPage.cpp index eb3ddff9..f4aa58b0 100644 --- a/application/pages/global/AccountListPage.cpp +++ b/application/pages/global/AccountListPage.cpp @@ -131,8 +131,7 @@ void AccountListPage::addAccount(const QString &errMsg) for (AccountProfile profile : account->profiles()) { auto meta = Env::getInstance().metacache()->resolveEntry("skins", profile.id + ".png"); - auto action = CacheDownload::make( - QUrl("https://" + URLConstants::SKINS_BASE + profile.id + ".png"), meta); + auto action = Net::Download::makeCached(QUrl("https://" + URLConstants::SKINS_BASE + profile.id + ".png"), meta); job->addNetAction(action); meta->setStale(true); } |