NOISSUE refactor *Download into more, smaller pieces

* Download is now Download.
* Download uses Sink subclasses to process various events.
* Validators can be used to further customize the Sink behaviour.
This commit is contained in:
Petr Mrázek 2016-05-28 19:54:17 +02:00
parent a750f6e63c
commit a1abbd9e05
51 changed files with 824 additions and 765 deletions

View File

@ -1,6 +1,6 @@
#include "WebResourceHandler.h" #include "WebResourceHandler.h"
#include "net/CacheDownload.h" #include "net/Download.h"
#include "net/HttpMetaCache.h" #include "net/HttpMetaCache.h"
#include "net/NetJob.h" #include "net/NetJob.h"
#include "FileSystem.h" #include "FileSystem.h"
@ -27,7 +27,7 @@ WebResourceHandler::WebResourceHandler(const QString &url)
else else
{ {
NetJob *job = new NetJob("Icon download"); 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::succeeded, this, &WebResourceHandler::succeeded);
connect(job, &NetJob::failed, this, [job, this]() {setFailure(job->failReason());}); connect(job, &NetJob::failed, this, [job, this]() {setFailure(job->failReason());});
connect(job, &NetJob::progress, this, &WebResourceHandler::progress); connect(job, &NetJob::progress, this, &WebResourceHandler::progress);

View File

@ -82,21 +82,25 @@ set(PATHMATCHER_SOURCES
set(NET_SOURCES set(NET_SOURCES
# network stuffs # network stuffs
net/NetAction.h net/ByteArraySink.h
net/MD5EtagDownload.h net/ChecksumValidator.h
net/MD5EtagDownload.cpp net/Download.cpp
net/ByteArrayDownload.h net/Download.h
net/ByteArrayDownload.cpp net/FileSink.cpp
net/CacheDownload.h net/FileSink.h
net/CacheDownload.cpp
net/NetJob.h
net/NetJob.cpp
net/HttpMetaCache.h
net/HttpMetaCache.cpp net/HttpMetaCache.cpp
net/PasteUpload.h net/HttpMetaCache.h
net/MetaCacheSink.cpp
net/MetaCacheSink.h
net/NetAction.h
net/NetJob.cpp
net/NetJob.h
net/PasteUpload.cpp net/PasteUpload.cpp
net/URLConstants.h net/PasteUpload.h
net/Sink.h
net/URLConstants.cpp net/URLConstants.cpp
net/URLConstants.h
net/Validator.h
) )
# Game launch logic # Game launch logic

View File

@ -25,7 +25,9 @@
#include "AssetsUtils.h" #include "AssetsUtils.h"
#include "FileSystem.h" #include "FileSystem.h"
#include "net/MD5EtagDownload.h" #include "net/Download.h"
#include "net/ChecksumValidator.h"
namespace AssetsUtils namespace AssetsUtils
{ {
@ -191,7 +193,12 @@ NetActionPtr AssetObject::getDownloadAction()
QFileInfo objectFile(getLocalPath()); QFileInfo objectFile(getLocalPath());
if ((!objectFile.isFile()) || (objectFile.size() != size)) 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; objectDL->m_total_progress = size;
return objectDL; return objectDL;
} }

View File

@ -1,5 +1,6 @@
#include "Library.h" #include "Library.h"
#include <net/CacheDownload.h> #include <net/Download.h>
#include <net/ChecksumValidator.h>
#include <minecraft/forge/ForgeXzDownload.h> #include <minecraft/forge/ForgeXzDownload.h>
#include <Env.h> #include <Env.h>
#include <FileSystem.h> #include <FileSystem.h>
@ -74,7 +75,7 @@ QList<NetActionPtr> Library::getDownloads(OpSys system, HttpMetaCache * cache, Q
bool isLocal = (hint() == "local"); bool isLocal = (hint() == "local");
bool isForge = (hint() == "forge-pack-xz"); 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); auto entry = cache->resolveEntry("libraries", storage);
if (!entry->isStale()) if (!entry->isStale())
@ -95,7 +96,16 @@ QList<NetActionPtr> Library::getDownloads(OpSys system, HttpMetaCache * cache, Q
} }
else 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; return true;
}; };
@ -105,7 +115,7 @@ QList<NetActionPtr> Library::getDownloads(OpSys system, HttpMetaCache * cache, Q
if(m_mojangDownloads->artifact) if(m_mojangDownloads->artifact)
{ {
auto artifact = 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)) if(m_nativeClassifiers.contains(system))
{ {
@ -118,17 +128,17 @@ QList<NetActionPtr> Library::getDownloads(OpSys system, HttpMetaCache * cache, Q
nat64Classifier.replace("${arch}", "64"); nat64Classifier.replace("${arch}", "64");
auto nat32info = m_mojangDownloads->getDownloadInfo(nat32Classifier); auto nat32info = m_mojangDownloads->getDownloadInfo(nat32Classifier);
if(nat32info) if(nat32info)
add_download(nat32info->path, nat32info->url); add_download(nat32info->path, nat32info->url, nat32info->sha1);
auto nat64info = m_mojangDownloads->getDownloadInfo(nat64Classifier); auto nat64info = m_mojangDownloads->getDownloadInfo(nat64Classifier);
if(nat64info) if(nat64info)
add_download(nat64info->path, nat64info->url); add_download(nat64info->path, nat64info->url, nat64info->sha1);
} }
else else
{ {
auto info = m_mojangDownloads->getDownloadInfo(nativeClassifier); auto info = m_mojangDownloads->getDownloadInfo(nativeClassifier);
if(info) if(info)
{ {
add_download(info->path, info->url); add_download(info->path, info->url, info->sha1);
} }
} }
} }

View File

@ -68,6 +68,7 @@ slots:
protected: protected:
NetJobPtr specificVersionDownloadJob; NetJobPtr specificVersionDownloadJob;
QByteArray versionIndexData;
std::shared_ptr<MinecraftVersion> updatedVersion; std::shared_ptr<MinecraftVersion> updatedVersion;
MinecraftVersionList *m_list; MinecraftVersionList *m_list;
}; };
@ -410,7 +411,7 @@ MCVListVersionUpdateTask::MCVListVersionUpdateTask(MinecraftVersionList *vlist,
void MCVListVersionUpdateTask::executeTask() void MCVListVersionUpdateTask::executeTask()
{ {
auto job = new NetJob("Version index"); 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); specificVersionDownloadJob.reset(job);
connect(specificVersionDownloadJob.get(), SIGNAL(succeeded()), SLOT(json_downloaded())); connect(specificVersionDownloadJob.get(), SIGNAL(succeeded()), SLOT(json_downloaded()));
connect(specificVersionDownloadJob.get(), SIGNAL(failed(QString)), SIGNAL(failed(QString))); connect(specificVersionDownloadJob.get(), SIGNAL(failed(QString)), SIGNAL(failed(QString)));
@ -420,12 +421,11 @@ void MCVListVersionUpdateTask::executeTask()
void MCVListVersionUpdateTask::json_downloaded() void MCVListVersionUpdateTask::json_downloaded()
{ {
NetActionPtr DlJob = specificVersionDownloadJob->first();
auto data = std::dynamic_pointer_cast<ByteArrayDownload>(DlJob)->m_data;
specificVersionDownloadJob.reset(); specificVersionDownloadJob.reset();
QJsonParseError jsonError; QJsonParseError jsonError;
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError); QJsonDocument jsonDoc = QJsonDocument::fromJson(versionIndexData, &jsonError);
versionIndexData.clear();
if (jsonError.error != QJsonParseError::NoError) if (jsonError.error != QJsonParseError::NoError)
{ {

View File

@ -397,7 +397,7 @@ protected:
if (entry->isStale()) if (entry->isStale())
{ {
NetJob *fjob = new NetJob("Forge download"); 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::progress, this, &Task::setProgress);
connect(fjob, &NetJob::status, this, &Task::setStatus); connect(fjob, &NetJob::status, this, &Task::setStatus);
connect(fjob, &NetJob::failed, [this](QString reason) connect(fjob, &NetJob::failed, [this](QString reason)

View File

@ -131,10 +131,8 @@ void ForgeListLoadTask::executeTask()
forgeListEntry->setStale(true); forgeListEntry->setStale(true);
gradleForgeListEntry->setStale(true); gradleForgeListEntry->setStale(true);
job->addNetAction(listDownload = CacheDownload::make(QUrl(URLConstants::FORGE_LEGACY_URL), job->addNetAction(listDownload = Net::Download::makeCached(QUrl(URLConstants::FORGE_LEGACY_URL),forgeListEntry));
forgeListEntry)); job->addNetAction(gradleListDownload = Net::Download::makeCached(QUrl(URLConstants::FORGE_GRADLE_URL), gradleForgeListEntry));
job->addNetAction(gradleListDownload = CacheDownload::make(
QUrl(URLConstants::FORGE_GRADLE_URL), gradleForgeListEntry));
connect(listDownload.get(), SIGNAL(failed(int)), SLOT(listFailed())); connect(listDownload.get(), SIGNAL(failed(int)), SLOT(listFailed()));
connect(gradleListDownload.get(), SIGNAL(failed(int)), SLOT(gradleListFailed())); connect(gradleListDownload.get(), SIGNAL(failed(int)), SLOT(gradleListFailed()));
@ -154,15 +152,14 @@ bool ForgeListLoadTask::parseForgeList(QList<BaseVersionPtr> &out)
{ {
QByteArray data; QByteArray data;
{ {
auto dlJob = listDownload; auto filename = listDownload->getTargetFilepath();
auto filename = std::dynamic_pointer_cast<CacheDownload>(dlJob)->getTargetFilepath();
QFile listFile(filename); QFile listFile(filename);
if (!listFile.open(QIODevice::ReadOnly)) if (!listFile.open(QIODevice::ReadOnly))
{ {
return false; return false;
} }
data = listFile.readAll(); data = listFile.readAll();
dlJob.reset(); listDownload.reset();
} }
QJsonParseError jsonError; QJsonParseError jsonError;
@ -266,15 +263,14 @@ bool ForgeListLoadTask::parseForgeGradleList(QList<BaseVersionPtr> &out)
QMap<int, std::shared_ptr<ForgeVersion>> lookup; QMap<int, std::shared_ptr<ForgeVersion>> lookup;
QByteArray data; QByteArray data;
{ {
auto dlJob = gradleListDownload; auto filename = gradleListDownload->getTargetFilepath();
auto filename = std::dynamic_pointer_cast<CacheDownload>(dlJob)->getTargetFilepath();
QFile listFile(filename); QFile listFile(filename);
if (!listFile.open(QIODevice::ReadOnly)) if (!listFile.open(QIODevice::ReadOnly))
{ {
return false; return false;
} }
data = listFile.readAll(); data = listFile.readAll();
dlJob.reset(); gradleListDownload.reset();
} }
QJsonParseError jsonError; QJsonParseError jsonError;

View File

@ -81,8 +81,8 @@ protected:
NetJobPtr listJob; NetJobPtr listJob;
ForgeVersionList *m_list; ForgeVersionList *m_list;
CacheDownloadPtr listDownload; Net::Download::Ptr listDownload;
CacheDownloadPtr gradleListDownload; Net::Download::Ptr gradleListDownload;
private: private:
bool parseForgeList(QList<BaseVersionPtr> &out); bool parseForgeList(QList<BaseVersionPtr> &out);

View File

@ -114,7 +114,7 @@ void LegacyUpdate::fmllibsStart()
auto entry = metacache->resolveEntry("fmllibs", lib.filename); auto entry = metacache->resolveEntry("fmllibs", lib.filename);
QString urlString = lib.ours ? URLConstants::FMLLIBS_OUR_BASE_URL + lib.filename QString urlString = lib.ours ? URLConstants::FMLLIBS_OUR_BASE_URL + lib.filename
: URLConstants::FMLLIBS_FORGE_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); connect(dljob, &NetJob::succeeded, this, &LegacyUpdate::fmllibsFinished);
@ -372,7 +372,7 @@ void LegacyUpdate::jarStart()
auto metacache = ENV.metacache(); auto metacache = ENV.metacache();
auto entry = metacache->resolveEntry("versions", URLConstants::getJarPath(version_id)); 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(succeeded()), SLOT(jarFinished()));
connect(dljob, SIGNAL(failed(QString)), SLOT(jarFailed(QString))); connect(dljob, SIGNAL(failed(QString)), SLOT(jarFailed(QString)));
connect(dljob, SIGNAL(progress(qint64, qint64)), SIGNAL(progress(qint64, qint64))); connect(dljob, SIGNAL(progress(qint64, qint64)), SIGNAL(progress(qint64, qint64)));

View File

@ -146,8 +146,7 @@ void LLListLoadTask::executeTask()
// verify by poking the server. // verify by poking the server.
liteloaderEntry->setStale(true); liteloaderEntry->setStale(true);
job->addNetAction(listDownload = CacheDownload::make(QUrl(URLConstants::LITELOADER_URL), job->addNetAction(listDownload = Net::Download::makeCached(QUrl(URLConstants::LITELOADER_URL), liteloaderEntry));
liteloaderEntry));
connect(listDownload.get(), SIGNAL(failed(int)), SLOT(listFailed())); connect(listDownload.get(), SIGNAL(failed(int)), SLOT(listFailed()));
@ -167,8 +166,7 @@ void LLListLoadTask::listDownloaded()
{ {
QByteArray data; QByteArray data;
{ {
auto dlJob = listDownload; auto filename = listDownload->getTargetFilepath();
auto filename = std::dynamic_pointer_cast<CacheDownload>(dlJob)->getTargetFilepath();
QFile listFile(filename); QFile listFile(filename);
if (!listFile.open(QIODevice::ReadOnly)) if (!listFile.open(QIODevice::ReadOnly))
{ {
@ -177,7 +175,7 @@ void LLListLoadTask::listDownloaded()
} }
data = listFile.readAll(); data = listFile.readAll();
listFile.close(); listFile.close();
dlJob.reset(); listDownload.reset();
} }
QJsonParseError jsonError; QJsonParseError jsonError;

View File

@ -112,7 +112,7 @@ slots:
protected: protected:
NetJobPtr listJob; NetJobPtr listJob;
CacheDownloadPtr listDownload; Net::Download::Ptr listDownload;
LiteLoaderVersionList *m_list; LiteLoaderVersionList *m_list;
}; };

View File

@ -31,6 +31,7 @@
#include "minecraft/MinecraftProfile.h" #include "minecraft/MinecraftProfile.h"
#include "minecraft/Library.h" #include "minecraft/Library.h"
#include "net/URLConstants.h" #include "net/URLConstants.h"
#include "net/ChecksumValidator.h"
#include "minecraft/AssetsUtils.h" #include "minecraft/AssetsUtils.h"
#include "Exception.h" #include "Exception.h"
#include "MMCZip.h" #include "MMCZip.h"
@ -96,7 +97,13 @@ void OneSixUpdate::assetIndexStart()
auto metacache = ENV.metacache(); auto metacache = ENV.metacache();
auto entry = metacache->resolveEntry("asset_indexes", localPath); auto entry = metacache->resolveEntry("asset_indexes", localPath);
entry->setStale(true); 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); jarlibDownloadJob.reset(job);
connect(jarlibDownloadJob.get(), SIGNAL(succeeded()), SLOT(assetIndexFinished())); connect(jarlibDownloadJob.get(), SIGNAL(succeeded()), SLOT(assetIndexFinished()));
@ -180,7 +187,7 @@ void OneSixUpdate::jarlibStart()
auto metacache = ENV.metacache(); auto metacache = ENV.metacache();
auto entry = metacache->resolveEntry("versions", localPath); auto entry = metacache->resolveEntry("versions", localPath);
job->addNetAction(CacheDownload::make(QUrl(urlstr), entry)); job->addNetAction(Net::Download::makeCached(QUrl(urlstr), entry));
jarlibDownloadJob.reset(job); jarlibDownloadJob.reset(job);
} }
@ -293,7 +300,7 @@ void OneSixUpdate::fmllibsStart()
auto entry = metacache->resolveEntry("fmllibs", lib.filename); auto entry = metacache->resolveEntry("fmllibs", lib.filename);
QString urlString = lib.ours ? URLConstants::FMLLIBS_OUR_BASE_URL + lib.filename QString urlString = lib.ours ? URLConstants::FMLLIBS_OUR_BASE_URL + lib.filename
: URLConstants::FMLLIBS_FORGE_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())); connect(dljob, SIGNAL(succeeded()), SLOT(fmllibsFinished()));

View File

@ -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()
{
// ~_~
}

View File

@ -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();
};

View File

@ -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;
};
}

View File

@ -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;
}

View File

@ -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;
};
}

199
api/logic/net/Download.cpp Normal file
View File

@ -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;
}
}
}

View File

@ -17,47 +17,50 @@
#include "NetAction.h" #include "NetAction.h"
#include "HttpMetaCache.h" #include "HttpMetaCache.h"
#include <QCryptographicHash> #include "Validator.h"
#include <QSaveFile> #include "Sink.h"
#include "multimc_logic_export.h" #include "multimc_logic_export.h"
namespace Net {
typedef std::shared_ptr<class CacheDownload> CacheDownloadPtr; class MULTIMC_LOGIC_EXPORT Download : public NetAction
class MULTIMC_LOGIC_EXPORT CacheDownload : public NetAction
{ {
Q_OBJECT 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 public: /* types */
std::unique_ptr<QSaveFile> m_output_file; typedef std::shared_ptr<class Download> Ptr;
/// the hash-as-you-download
QCryptographicHash md5sum;
bool wroteAnyData = false;
protected: /* con/des */
explicit Download();
public: public:
explicit CacheDownload(QUrl url, MetaEntryPtr entry); virtual ~Download(){};
static CacheDownloadPtr make(QUrl url, MetaEntryPtr entry) static Download::Ptr makeCached(QUrl url, MetaEntryPtr entry);
{ static Download::Ptr makeByteArray(QUrl url, QByteArray *output);
return CacheDownloadPtr(new CacheDownload(url, entry)); static Download::Ptr makeFile(QUrl url, QString path);
}
virtual ~CacheDownload(){}; public: /* methods */
// FIXME: remove this
QString getTargetFilepath() QString getTargetFilepath()
{ {
return m_target_path; return m_target_path;
} }
protected // FIXME: remove this
slots: void addValidator(Validator * v);
private: /* methods */
bool handleRedirect();
protected slots:
virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal); virtual void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
virtual void downloadError(QNetworkReply::NetworkError error); virtual void downloadError(QNetworkReply::NetworkError error);
virtual void downloadFinished(); virtual void downloadFinished();
virtual void downloadReadyRead(); virtual void downloadReadyRead();
public public slots:
slots:
virtual void start(); virtual void start();
private: /* data */
// FIXME: remove this, it has no business being here.
QString m_target_path;
std::unique_ptr<Sink> m_sink;
}; };
}

View File

@ -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;
}
}

27
api/logic/net/FileSink.h Normal file
View File

@ -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;
};
}

View File

@ -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());
}

View File

@ -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();
};

View File

@ -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;
}
}

View File

@ -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;
};
}

View File

@ -59,9 +59,6 @@ public:
/// the network reply /// the network reply
unique_qobject_ptr<QNetworkReply> m_reply; unique_qobject_ptr<QNetworkReply> m_reply;
/// the content of the content-type header
QString m_content_type;
/// source URL /// source URL
QUrl m_url; QUrl m_url;

View File

@ -14,9 +14,7 @@
*/ */
#include "NetJob.h" #include "NetJob.h"
#include "MD5EtagDownload.h" #include "Download.h"
#include "ByteArrayDownload.h"
#include "CacheDownload.h"
#include <QDebug> #include <QDebug>

View File

@ -16,9 +16,7 @@
#pragma once #pragma once
#include <QtNetwork> #include <QtNetwork>
#include "NetAction.h" #include "NetAction.h"
#include "ByteArrayDownload.h" #include "Download.h"
#include "MD5EtagDownload.h"
#include "CacheDownload.h"
#include "HttpMetaCache.h" #include "HttpMetaCache.h"
#include "tasks/Task.h" #include "tasks/Task.h"
#include "QObjectPtr.h" #include "QObjectPtr.h"

70
api/logic/net/Sink.h Normal file
View File

@ -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;
};
}

20
api/logic/net/Validator.h Normal file
View File

@ -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;
};
}

View File

@ -37,7 +37,7 @@ void NewsChecker::reloadNews()
qDebug() << "Reloading news."; qDebug() << "Reloading news.";
NetJob* job = new NetJob("News RSS Feed"); 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::succeeded, this, &NewsChecker::rssDownloadFinished);
QObject::connect(job, &NetJob::failed, this, &NewsChecker::rssDownloadFailed); QObject::connect(job, &NetJob::failed, this, &NewsChecker::rssDownloadFailed);
m_newsNetJob.reset(job); m_newsNetJob.reset(job);
@ -49,13 +49,7 @@ void NewsChecker::rssDownloadFinished()
// Parse the XML file and process the RSS feed entries. // Parse the XML file and process the RSS feed entries.
qDebug() << "Finished loading RSS feed."; qDebug() << "Finished loading RSS feed.";
QByteArray data; m_newsNetJob.reset();
{
ByteArrayDownloadPtr dl = std::dynamic_pointer_cast<ByteArrayDownload>(m_newsNetJob->first());
data = dl->m_data;
m_newsNetJob.reset();
}
QDomDocument doc; QDomDocument doc;
{ {
// Stuff to store error info in. // Stuff to store error info in.
@ -64,12 +58,14 @@ void NewsChecker::rssDownloadFinished()
int errorCol = -1; int errorCol = -1;
// Parse the XML. // 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); QString fullErrorMsg = QString("Error parsing RSS feed XML. %s at %d:%d.").arg(errorMsg, errorLine, errorCol);
fail(fullErrorMsg); fail(fullErrorMsg);
newsData.clear();
return; return;
} }
newsData.clear();
} }
// If the parsing succeeded, read it. // If the parsing succeeded, read it.

View File

@ -74,7 +74,7 @@ protected slots:
void rssDownloadFinished(); void rssDownloadFinished();
void rssDownloadFailed(QString reason); void rssDownloadFailed(QString reason);
protected: protected: /* data */
//! The URL for the RSS feed to fetch. //! The URL for the RSS feed to fetch.
QString m_feedUrl; QString m_feedUrl;
@ -87,21 +87,19 @@ protected:
//! True if news has been loaded. //! True if news has been loaded.
bool m_loadedNews; bool m_loadedNews;
QByteArray newsData;
/*! /*!
* Gets the error message that was given last time the news was loaded. * 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. * If the last news load succeeded, this will be an empty string.
*/ */
QString m_lastLoadError; QString m_lastLoadError;
protected slots:
/// Emits newsLoaded() and sets m_lastLoadError to empty string.
void succeed();
/*! /// Emits newsLoadingFailed() and sets m_lastLoadError to the given message.
* Emits newsLoaded() and sets m_lastLoadError to empty string. void fail(const QString& errorMsg);
*/
void Q_SLOT succeed();
/*!
* Emits newsLoadingFailed() and sets m_lastLoadError to the given message.
*/
void Q_SLOT fail(const QString& errorMsg);
}; };

View File

@ -6,7 +6,7 @@
#include <QDebug> #include <QDebug>
#include "Env.h" #include "Env.h"
#include "net/CacheDownload.h" #include "net/Download.h"
NotificationChecker::NotificationChecker(QObject *parent) NotificationChecker::NotificationChecker(QObject *parent)
@ -55,9 +55,8 @@ void NotificationChecker::checkForNotifications()
m_checkJob.reset(new NetJob("Checking for notifications")); m_checkJob.reset(new NetJob("Checking for notifications"));
auto entry = ENV.metacache()->resolveEntry("root", "notifications.json"); auto entry = ENV.metacache()->resolveEntry("root", "notifications.json");
entry->setStale(true); entry->setStale(true);
m_checkJob->addNetAction(m_download = CacheDownload::make(m_notificationsUrl, entry)); m_checkJob->addNetAction(m_download = Net::Download::makeCached(m_notificationsUrl, entry));
connect(m_download.get(), &CacheDownload::succeeded, this, connect(m_download.get(), &Net::Download::succeeded, this, &NotificationChecker::downloadSucceeded);
&NotificationChecker::downloadSucceeded);
m_checkJob->start(); m_checkJob->start();
} }

View File

@ -3,7 +3,7 @@
#include <QObject> #include <QObject>
#include "net/NetJob.h" #include "net/NetJob.h"
#include "net/CacheDownload.h" #include "net/Download.h"
#include "multimc_logic_export.h" #include "multimc_logic_export.h"
@ -55,7 +55,7 @@ private:
QList<NotificationEntry> m_entries; QList<NotificationEntry> m_entries;
QUrl m_notificationsUrl; QUrl m_notificationsUrl;
NetJobPtr m_checkJob; NetJobPtr m_checkJob;
CacheDownloadPtr m_download; Net::Download::Ptr m_download;
QString m_appVersionChannel; QString m_appVersionChannel;
QString m_appPlatform; QString m_appPlatform;

View File

@ -43,7 +43,7 @@ void StatusChecker::reloadStatus()
// qDebug() << "Reloading status."; // qDebug() << "Reloading status.";
NetJob* job = new NetJob("Status JSON"); 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::succeeded, this, &StatusChecker::statusDownloadFinished);
QObject::connect(job, &NetJob::failed, this, &StatusChecker::statusDownloadFailed); QObject::connect(job, &NetJob::failed, this, &StatusChecker::statusDownloadFailed);
m_statusNetJob.reset(job); m_statusNetJob.reset(job);
@ -55,15 +55,10 @@ void StatusChecker::statusDownloadFinished()
{ {
qDebug() << "Finished loading status JSON."; qDebug() << "Finished loading status JSON.";
m_statusEntries.clear(); m_statusEntries.clear();
QByteArray data; m_statusNetJob.reset();
{
ByteArrayDownloadPtr dl = std::dynamic_pointer_cast<ByteArrayDownload>(m_statusNetJob->first());
data = dl->m_data;
m_statusNetJob.reset();
}
QJsonParseError jsonError; QJsonParseError jsonError;
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError); QJsonDocument jsonDoc = QJsonDocument::fromJson(dataSink, &jsonError);
if (jsonError.error != QJsonParseError::NoError) if (jsonError.error != QJsonParseError::NoError)
{ {

View File

@ -26,35 +26,35 @@
class MULTIMC_LOGIC_EXPORT StatusChecker : public QObject class MULTIMC_LOGIC_EXPORT StatusChecker : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public: /* con/des */
StatusChecker(); StatusChecker();
public: /* methods */
QString getLastLoadErrorMsg() const; QString getLastLoadErrorMsg() const;
bool isLoadingStatus() const; bool isLoadingStatus() const;
QMap<QString, QString> getStatusEntries() const; QMap<QString, QString> getStatusEntries() const;
void Q_SLOT reloadStatus();
protected:
virtual void timerEvent(QTimerEvent *);
signals: signals:
void statusLoading(bool loading); void statusLoading(bool loading);
void statusChanged(QMap<QString, QString> newStatus); void statusChanged(QMap<QString, QString> newStatus);
public slots:
void reloadStatus();
protected: /* methods */
virtual void timerEvent(QTimerEvent *);
protected slots: protected slots:
void statusDownloadFinished(); void statusDownloadFinished();
void statusDownloadFailed(QString reason); void statusDownloadFailed(QString reason);
void succeed();
void fail(const QString& errorMsg);
protected: protected: /* data */
QMap<QString, QString> m_prevEntries; QMap<QString, QString> m_prevEntries;
QMap<QString, QString> m_statusEntries; QMap<QString, QString> m_statusEntries;
NetJobPtr m_statusNetJob; NetJobPtr m_statusNetJob;
QString m_lastLoadError; QString m_lastLoadError;
QByteArray dataSink;
void Q_SLOT succeed();
void Q_SLOT fail(const QString& errorMsg);
}; };

View File

@ -1,6 +1,6 @@
#include "TranslationDownloader.h" #include "TranslationDownloader.h"
#include "net/NetJob.h" #include "net/NetJob.h"
#include "net/CacheDownload.h" #include "net/Download.h"
#include "net/URLConstants.h" #include "net/URLConstants.h"
#include "Env.h" #include "Env.h"
#include <QDebug> #include <QDebug>
@ -12,7 +12,7 @@ void TranslationDownloader::downloadTranslations()
{ {
qDebug() << "Downloading Translations Index..."; qDebug() << "Downloading Translations Index...";
m_index_job.reset(new NetJob("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); m_index_job->addNetAction(m_index_task);
connect(m_index_job.get(), &NetJob::failed, this, &TranslationDownloader::indexFailed); connect(m_index_job.get(), &NetJob::failed, this, &TranslationDownloader::indexFailed);
connect(m_index_job.get(), &NetJob::succeeded, this, &TranslationDownloader::indexRecieved); connect(m_index_job.get(), &NetJob::succeeded, this, &TranslationDownloader::indexRecieved);
@ -22,17 +22,15 @@ void TranslationDownloader::indexRecieved()
{ {
qDebug() << "Got translations index!"; qDebug() << "Got translations index!";
m_dl_job.reset(new NetJob("Translations")); 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) for (const auto line : lines)
{ {
if (!line.isEmpty()) if (!line.isEmpty())
{ {
MetaEntryPtr entry = ENV.metacache()->resolveEntry("translations", "mmc_" + line); MetaEntryPtr entry = ENV.metacache()->resolveEntry("translations", "mmc_" + line);
entry->setStale(true); entry->setStale(true);
CacheDownloadPtr dl = CacheDownload::make( m_dl_job->addNetAction(Net::Download::makeCached(QUrl(URLConstants::TRANSLATIONS_BASE_URL + line), entry));
QUrl(URLConstants::TRANSLATIONS_BASE_URL + line),
entry);
m_dl_job->addNetAction(dl);
} }
} }
connect(m_dl_job.get(), &NetJob::succeeded, this, &TranslationDownloader::dlGood); connect(m_dl_job.get(), &NetJob::succeeded, this, &TranslationDownloader::dlGood);

View File

@ -6,8 +6,9 @@
#include <QObject> #include <QObject>
#include <net/NetJob.h> #include <net/NetJob.h>
#include "multimc_logic_export.h" #include "multimc_logic_export.h"
namespace Net{
class ByteArrayDownload; class Download;
}
class NetJob; class NetJob;
class MULTIMC_LOGIC_EXPORT TranslationDownloader : public QObject class MULTIMC_LOGIC_EXPORT TranslationDownloader : public QObject
@ -26,7 +27,8 @@ private slots:
void dlGood(); void dlGood();
private: private:
std::shared_ptr<ByteArrayDownload> m_index_task; std::shared_ptr<Net::Download> m_index_task;
NetJobPtr m_dl_job; NetJobPtr m_dl_job;
NetJobPtr m_index_job; NetJobPtr m_index_job;
QByteArray m_data;
}; };

View File

@ -43,22 +43,19 @@ void DownloadTask::loadVersionInfo()
{ {
setStatus(tr("Loading version information...")); setStatus(tr("Loading version information..."));
m_currentVersionFileListDownload.reset();
m_newVersionFileListDownload.reset();
NetJob *netJob = new NetJob("Version Info"); NetJob *netJob = new NetJob("Version Info");
// Find the index URL. // Find the index URL.
QUrl newIndexUrl = QUrl(m_status.newRepoUrl).resolved(QString::number(m_status.newVersionId) + ".json"); QUrl newIndexUrl = QUrl(m_status.newRepoUrl).resolved(QString::number(m_status.newVersionId) + ".json");
qDebug() << m_status.newRepoUrl << " turns into " << newIndexUrl; 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 we have a current version URL, get that one too.
if (!m_status.currentRepoUrl.isEmpty()) if (!m_status.currentRepoUrl.isEmpty())
{ {
QUrl cIndexUrl = QUrl(m_status.currentRepoUrl).resolved(QString::number(m_status.currentVersionId) + ".json"); 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, &currentVersionFileListData));
qDebug() << m_status.currentRepoUrl << " turns into " << cIndexUrl; qDebug() << m_status.currentRepoUrl << " turns into " << cIndexUrl;
} }
@ -92,7 +89,7 @@ void DownloadTask::processDownloadedVersionInfo()
setStatus(tr("Reading file list for new version...")); setStatus(tr("Reading file list for new version..."));
qDebug() << "Reading file list for new version..."; qDebug() << "Reading file list for new version...";
QString error; QString error;
if (!parseVersionInfo(m_newVersionFileListDownload->m_data, m_newVersionFileList, error)) if (!parseVersionInfo(newVersionFileListData, m_newVersionFileList, error))
{ {
qCritical() << error; qCritical() << error;
emitFailed(error); emitFailed(error);
@ -106,7 +103,7 @@ void DownloadTask::processDownloadedVersionInfo()
qDebug() << "Reading file list for current version..."; qDebug() << "Reading file list for current version...";
// if this fails, it's not a complete loss. // if this fails, it's not a complete loss.
QString error; 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."; qDebug() << error << "This is not a fatal error.";
} }

View File

@ -64,8 +64,10 @@ protected:
void loadVersionInfo(); void loadVersionInfo();
NetJobPtr m_vinfoNetJob; NetJobPtr m_vinfoNetJob;
ByteArrayDownloadPtr m_currentVersionFileListDownload; QByteArray currentVersionFileListData;
ByteArrayDownloadPtr m_newVersionFileListDownload; QByteArray newVersionFileListData;
Net::Download::Ptr m_currentVersionFileListDownload;
Net::Download::Ptr m_newVersionFileListDownload;
NetJobPtr m_filesNetJob; NetJobPtr m_filesNetJob;

View File

@ -4,6 +4,9 @@
#include <QFile> #include <QFile>
#include <FileSystem.h> #include <FileSystem.h>
#include "net/Download.h"
#include "net/ChecksumValidator.h"
namespace GoUpdate namespace GoUpdate
{ {
@ -189,8 +192,9 @@ bool processFileLists
// We need to download the file to the updatefiles folder and add a task // We need to download the file to the updatefiles folder and add a task
// to copy it to its install path. // to copy it to its install path.
auto download = MD5EtagDownload::make(source.url, dlPath); auto download = Net::Download::makeFile(source.url, dlPath);
download->m_expected_md5 = entry.md5; auto rawMd5 = QByteArray::fromHex(entry.md5.toLatin1());
download->addValidator(new Net::ChecksumValidator(QCryptographicHash::Md5, rawMd5));
job->addNetAction(download); job->addNetAction(download);
ops.append(Operation::CopyOp(dlPath, entry.path, entry.mode)); ops.append(Operation::CopyOp(dlPath, entry.path, entry.mode));
} }

View File

@ -28,11 +28,6 @@ UpdateChecker::UpdateChecker(QString channelListUrl, QString currentChannel, int
m_channelListUrl = channelListUrl; m_channelListUrl = channelListUrl;
m_currentChannel = currentChannel; m_currentChannel = currentChannel;
m_currentBuild = currentBuild; m_currentBuild = currentBuild;
m_updateChecking = false;
m_chanListLoading = false;
m_checkUpdateWaiting = false;
m_chanListLoaded = false;
} }
QList<UpdateChecker::ChannelListEntry> UpdateChecker::getChannelList() const 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")); QUrl indexUrl = QUrl(m_newRepoUrl).resolved(QUrl("index.json"));
auto job = new NetJob("GoUpdate Repository Index"); auto job = new NetJob("GoUpdate Repository Index");
job->addNetAction(ByteArrayDownload::make(indexUrl)); job->addNetAction(Net::Download::makeByteArray(indexUrl, &indexData));
connect(job, &NetJob::succeeded, [this, notifyNoUpdate]() connect(job, &NetJob::succeeded, [this, notifyNoUpdate](){ updateCheckFinished(notifyNoUpdate); });
{ updateCheckFinished(notifyNoUpdate); });
connect(job, &NetJob::failed, this, &UpdateChecker::updateCheckFailed); connect(job, &NetJob::failed, this, &UpdateChecker::updateCheckFailed);
indexJob.reset(job); indexJob.reset(job);
job->start(); job->start();
@ -106,19 +100,15 @@ void UpdateChecker::updateCheckFinished(bool notifyNoUpdate)
qDebug() << "Finished downloading repo index. Checking for new versions."; qDebug() << "Finished downloading repo index. Checking for new versions.";
QJsonParseError jsonError; QJsonParseError jsonError;
QByteArray data; indexJob.reset();
{
ByteArrayDownloadPtr dl =
std::dynamic_pointer_cast<ByteArrayDownload>(indexJob->first());
data = dl->m_data;
indexJob.reset();
}
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError); QJsonDocument jsonDoc = QJsonDocument::fromJson(indexData, &jsonError);
indexData.clear();
if (jsonError.error != QJsonParseError::NoError || !jsonDoc.isObject()) if (jsonError.error != QJsonParseError::NoError || !jsonDoc.isObject())
{ {
qCritical() << "Failed to parse GoUpdate repository index. JSON error" qCritical() << "Failed to parse GoUpdate repository index. JSON error"
<< jsonError.errorString() << "at offset" << jsonError.offset; << jsonError.errorString() << "at offset" << jsonError.offset;
m_updateChecking = false;
return; return;
} }
@ -130,6 +120,7 @@ void UpdateChecker::updateCheckFinished(bool notifyNoUpdate)
{ {
qCritical() << "Failed to check for updates. API version mismatch. We're using" qCritical() << "Failed to check for updates. API version mismatch. We're using"
<< API_VERSION << "server has" << apiVersion; << API_VERSION << "server has" << apiVersion;
m_updateChecking = false;
return; return;
} }
@ -165,7 +156,6 @@ void UpdateChecker::updateCheckFinished(bool notifyNoUpdate)
{ {
emit noUpdateFound(); emit noUpdateFound();
} }
m_updateChecking = false; m_updateChecking = false;
} }
@ -178,6 +168,12 @@ void UpdateChecker::updateChanList(bool notifyNoUpdate)
{ {
qDebug() << "Loading the channel list."; qDebug() << "Loading the channel list.";
if (m_chanListLoading)
{
qDebug() << "Ignoring channel list update request. Already grabbing channel list.";
return;
}
if (m_channelListUrl.isEmpty()) if (m_channelListUrl.isEmpty())
{ {
qCritical() << "Failed to update channel list. No channel list URL set." qCritical() << "Failed to update channel list. No channel list URL set."
@ -188,9 +184,8 @@ void UpdateChecker::updateChanList(bool notifyNoUpdate)
m_chanListLoading = true; m_chanListLoading = true;
NetJob *job = new NetJob("Update System Channel List"); NetJob *job = new NetJob("Update System Channel List");
job->addNetAction(ByteArrayDownload::make(QUrl(m_channelListUrl))); job->addNetAction(Net::Download::makeByteArray(QUrl(m_channelListUrl), &chanlistData));
connect(job, &NetJob::succeeded, [this, notifyNoUpdate]() connect(job, &NetJob::succeeded, [this, notifyNoUpdate]() { chanListDownloadFinished(notifyNoUpdate); });
{ chanListDownloadFinished(notifyNoUpdate); });
QObject::connect(job, &NetJob::failed, this, &UpdateChecker::chanListDownloadFailed); QObject::connect(job, &NetJob::failed, this, &UpdateChecker::chanListDownloadFailed);
chanListJob.reset(job); chanListJob.reset(job);
job->start(); job->start();
@ -198,21 +193,16 @@ void UpdateChecker::updateChanList(bool notifyNoUpdate)
void UpdateChecker::chanListDownloadFinished(bool notifyNoUpdate) void UpdateChecker::chanListDownloadFinished(bool notifyNoUpdate)
{ {
QByteArray data; chanListJob.reset();
{
ByteArrayDownloadPtr dl =
std::dynamic_pointer_cast<ByteArrayDownload>(chanListJob->first());
data = dl->m_data;
chanListJob.reset();
}
QJsonParseError jsonError; QJsonParseError jsonError;
QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError); QJsonDocument jsonDoc = QJsonDocument::fromJson(chanlistData, &jsonError);
chanlistData.clear();
if (jsonError.error != QJsonParseError::NoError) if (jsonError.error != QJsonParseError::NoError)
{ {
// TODO: Report errors to the user. // TODO: Report errors to the user.
qCritical() << "Failed to parse channel list JSON:" << jsonError.errorString() << "at" qCritical() << "Failed to parse channel list JSON:" << jsonError.errorString() << "at" << jsonError.offset;
<< jsonError.offset; m_chanListLoading = false;
return; return;
} }
@ -225,6 +215,7 @@ void UpdateChecker::chanListDownloadFinished(bool notifyNoUpdate)
qCritical() qCritical()
<< "Failed to check for updates. Channel list format version mismatch. We're using" << "Failed to check for updates. Channel list format version mismatch. We're using"
<< CHANLIST_FORMAT << "server has" << formatVersion; << CHANLIST_FORMAT << "server has" << formatVersion;
m_chanListLoading = false;
return; return;
} }

View File

@ -18,8 +18,6 @@
#include "net/NetJob.h" #include "net/NetJob.h"
#include "GoUpdate.h" #include "GoUpdate.h"
#include <QUrl>
#include "multimc_logic_export.h" #include "multimc_logic_export.h"
class MULTIMC_LOGIC_EXPORT UpdateChecker : public QObject class MULTIMC_LOGIC_EXPORT UpdateChecker : public QObject
@ -78,7 +76,9 @@ private:
friend class UpdateCheckerTest; friend class UpdateCheckerTest;
NetJobPtr indexJob; NetJobPtr indexJob;
QByteArray indexData;
NetJobPtr chanListJob; NetJobPtr chanListJob;
QByteArray chanlistData;
QString m_channelListUrl; QString m_channelListUrl;
@ -88,24 +88,24 @@ private:
* True while the system is checking for updates. * True while the system is checking for updates.
* If checkForUpdate is called while this is true, it will be ignored. * 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. * True if the channel list has loaded.
* If this is false, trying to check for updates will call updateChanList first. * 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. * 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. * 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. * 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 * if m_checkUpdateWaiting, this is the last used update channel

View File

@ -15,7 +15,7 @@
#include "BaseWonkoEntityRemoteLoadTask.h" #include "BaseWonkoEntityRemoteLoadTask.h"
#include "net/CacheDownload.h" #include "net/Download.h"
#include "net/HttpMetaCache.h" #include "net/HttpMetaCache.h"
#include "net/NetJob.h" #include "net/NetJob.h"
#include "wonko/format/WonkoFormat.h" #include "wonko/format/WonkoFormat.h"
@ -37,7 +37,7 @@ void BaseWonkoEntityRemoteLoadTask::executeTask()
auto entry = ENV.metacache()->resolveEntry("wonko", url().toString()); auto entry = ENV.metacache()->resolveEntry("wonko", url().toString());
entry->setStale(true); entry->setStale(true);
m_dl = CacheDownload::make(url(), entry); m_dl = Net::Download::makeCached(url(), entry);
job->addNetAction(m_dl); job->addNetAction(m_dl);
connect(job, &NetJob::failed, this, &BaseWonkoEntityRemoteLoadTask::emitFailed); connect(job, &NetJob::failed, this, &BaseWonkoEntityRemoteLoadTask::emitFailed);
connect(job, &NetJob::succeeded, this, &BaseWonkoEntityRemoteLoadTask::networkFinished); connect(job, &NetJob::succeeded, this, &BaseWonkoEntityRemoteLoadTask::networkFinished);

View File

@ -18,6 +18,11 @@
#include "tasks/Task.h" #include "tasks/Task.h"
#include <memory> #include <memory>
namespace Net
{
class Download;
}
class BaseWonkoEntity; class BaseWonkoEntity;
class WonkoIndex; class WonkoIndex;
class WonkoVersionList; class WonkoVersionList;
@ -43,7 +48,7 @@ private:
void executeTask() override; void executeTask() override;
BaseWonkoEntity *m_entity; BaseWonkoEntity *m_entity;
std::shared_ptr<class CacheDownload> m_dl; std::shared_ptr<Net::Download> m_dl;
}; };
class WonkoIndexRemoteLoadTask : public BaseWonkoEntityRemoteLoadTask class WonkoIndexRemoteLoadTask : public BaseWonkoEntityRemoteLoadTask

View File

@ -59,7 +59,7 @@
#include <SkinUtils.h> #include <SkinUtils.h>
#include <net/URLConstants.h> #include <net/URLConstants.h>
#include <net/NetJob.h> #include <net/NetJob.h>
#include <net/CacheDownload.h> #include <net/Download.h>
#include <news/NewsChecker.h> #include <news/NewsChecker.h>
#include <notifications/NotificationChecker.h> #include <notifications/NotificationChecker.h>
#include <tools/BaseProfiler.h> #include <tools/BaseProfiler.h>
@ -519,7 +519,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new MainWindow
auto accounts = MMC->accounts(); auto accounts = MMC->accounts();
QList<CacheDownloadPtr> skin_dls; QList<Net::Download::Ptr> skin_dls;
for (int i = 0; i < accounts->count(); i++) for (int i = 0; i < accounts->count(); i++)
{ {
auto account = accounts->at(i); auto account = accounts->at(i);
@ -531,7 +531,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new MainWindow
for (auto profile : account->profiles()) for (auto profile : account->profiles())
{ {
auto meta = Env::getInstance().metacache()->resolveEntry("skins", profile.id + ".png"); 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); skin_dls.append(action);
meta->setStale(true); meta->setStale(true);
} }
@ -1045,9 +1045,8 @@ InstancePtr MainWindow::instanceFromZipPack(QString instName, QString instGroup,
const QString path = url.host() + '/' + url.path(); const QString path = url.host() + '/' + url.path();
auto entry = ENV.metacache()->resolveEntry("general", path); auto entry = ENV.metacache()->resolveEntry("general", path);
entry->setStale(true); entry->setStale(true);
CacheDownloadPtr dl = CacheDownload::make(url, entry);
NetJob job(tr("Modpack download")); NetJob job(tr("Modpack download"));
job.addNetAction(dl); job.addNetAction(Net::Download::makeCached(url, entry));
// FIXME: possibly causes endless loop problems // FIXME: possibly causes endless loop problems
ProgressDialog dlDialog(this); ProgressDialog dlDialog(this);

View File

@ -109,16 +109,16 @@ AboutDialog::~AboutDialog()
void AboutDialog::loadPatronList() void AboutDialog::loadPatronList()
{ {
NetJob* job = new NetJob("Patreon Patron List"); netJob.reset(new NetJob("Patreon Patron List"));
patronListDownload = ByteArrayDownload::make(QUrl("http://files.multimc.org/patrons.txt")); netJob->addNetAction(Net::Download::makeByteArray(QUrl("http://files.multimc.org/patrons.txt"), &dataSink));
job->addNetAction(patronListDownload); connect(netJob.get(), &NetJob::succeeded, this, &AboutDialog::patronListLoaded);
connect(job, &NetJob::succeeded, this, &AboutDialog::patronListLoaded); netJob->start();
job->start();
} }
void AboutDialog::patronListLoaded() void AboutDialog::patronListLoaded()
{ {
QString patronListStr(patronListDownload->m_data); QString patronListStr(dataSink);
dataSink.clear();
QString html = getCreditsHtml(patronListStr.split("\n", QString::SkipEmptyParts)); QString html = getCreditsHtml(patronListStr.split("\n", QString::SkipEmptyParts));
ui->creditsText->setHtml(html); ui->creditsText->setHtml(html);
} }

View File

@ -16,8 +16,7 @@
#pragma once #pragma once
#include <QDialog> #include <QDialog>
#include <net/NetJob.h>
#include <net/ByteArrayDownload.h>
namespace Ui namespace Ui
{ {
@ -43,5 +42,6 @@ slots:
private: private:
Ui::AboutDialog *ui; Ui::AboutDialog *ui;
ByteArrayDownloadPtr patronListDownload; NetJobPtr netJob;
QByteArray dataSink;
}; };

View File

@ -46,8 +46,7 @@ void UpdateDialog::loadChangelog()
url = QString("https://api.github.com/repos/MultiMC/MultiMC5/compare/%1...%2").arg(BuildConfig.GIT_COMMIT, channel); url = QString("https://api.github.com/repos/MultiMC/MultiMC5/compare/%1...%2").arg(BuildConfig.GIT_COMMIT, channel);
m_changelogType = CHANGELOG_COMMITS; m_changelogType = CHANGELOG_COMMITS;
} }
changelogDownload = ByteArrayDownload::make(QUrl(url)); dljob->addNetAction(Net::Download::makeByteArray(QUrl(url), &changelogData));
dljob->addNetAction(changelogDownload);
connect(dljob.get(), &NetJob::succeeded, this, &UpdateDialog::changelogLoaded); connect(dljob.get(), &NetJob::succeeded, this, &UpdateDialog::changelogLoaded);
connect(dljob.get(), &NetJob::failed, this, &UpdateDialog::changelogFailed); connect(dljob.get(), &NetJob::failed, this, &UpdateDialog::changelogFailed);
dljob->start(); dljob->start();
@ -201,12 +200,13 @@ void UpdateDialog::changelogLoaded()
switch(m_changelogType) switch(m_changelogType)
{ {
case CHANGELOG_COMMITS: case CHANGELOG_COMMITS:
result = reprocessCommits(changelogDownload->m_data); result = reprocessCommits(changelogData);
break; break;
case CHANGELOG_MARKDOWN: case CHANGELOG_MARKDOWN:
result = reprocessMarkdown(changelogDownload->m_data); result = reprocessMarkdown(changelogData);
break; break;
} }
changelogData.clear();
ui->changelogBrowser->setHtml(result); ui->changelogBrowser->setHtml(result);
} }

View File

@ -16,7 +16,6 @@
#pragma once #pragma once
#include <QDialog> #include <QDialog>
#include "net/ByteArrayDownload.h"
#include "net/NetJob.h" #include "net/NetJob.h"
namespace Ui namespace Ui
@ -60,7 +59,7 @@ public slots:
void changelogFailed(QString reason); void changelogFailed(QString reason);
private: private:
ByteArrayDownloadPtr changelogDownload; QByteArray changelogData;
NetJobPtr dljob; NetJobPtr dljob;
ChangelogType m_changelogType = CHANGELOG_MARKDOWN; ChangelogType m_changelogType = CHANGELOG_MARKDOWN;
}; };

View File

@ -131,8 +131,7 @@ void AccountListPage::addAccount(const QString &errMsg)
for (AccountProfile profile : account->profiles()) for (AccountProfile profile : account->profiles())
{ {
auto meta = Env::getInstance().metacache()->resolveEntry("skins", profile.id + ".png"); auto meta = Env::getInstance().metacache()->resolveEntry("skins", profile.id + ".png");
auto action = CacheDownload::make( auto action = Net::Download::makeCached(QUrl("https://" + URLConstants::SKINS_BASE + profile.id + ".png"), meta);
QUrl("https://" + URLConstants::SKINS_BASE + profile.id + ".png"), meta);
job->addNetAction(action); job->addNetAction(action);
meta->setStale(true); meta->setStale(true);
} }