2017-01-08 09:28:05 +05:30
|
|
|
/* Copyright 2013-2017 MultiMC Contributors
|
2016-05-28 23:24:17 +05:30
|
|
|
*
|
|
|
|
* 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()
|
|
|
|
{
|
2017-05-04 02:41:52 +05:30
|
|
|
m_status = Job_NotStarted;
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
2016-10-28 05:49:19 +05:30
|
|
|
Download::Ptr Download::makeCached(QUrl url, MetaEntryPtr entry, Options options)
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
|
|
|
Download * dl = new Download();
|
|
|
|
dl->m_url = url;
|
2016-10-28 05:49:19 +05:30
|
|
|
dl->m_options = options;
|
2016-05-28 23:24:17 +05:30
|
|
|
auto md5Node = new ChecksumValidator(QCryptographicHash::Md5);
|
|
|
|
auto cachedNode = new MetaCacheSink(entry, md5Node);
|
|
|
|
dl->m_sink.reset(cachedNode);
|
|
|
|
dl->m_target_path = entry->getFullPath();
|
2017-05-04 02:41:52 +05:30
|
|
|
return std::shared_ptr<Download>(dl);
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
2016-10-28 05:49:19 +05:30
|
|
|
Download::Ptr Download::makeByteArray(QUrl url, QByteArray *output, Options options)
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
|
|
|
Download * dl = new Download();
|
|
|
|
dl->m_url = url;
|
2016-10-28 05:49:19 +05:30
|
|
|
dl->m_options = options;
|
2016-05-28 23:24:17 +05:30
|
|
|
dl->m_sink.reset(new ByteArraySink(output));
|
2017-05-04 02:41:52 +05:30
|
|
|
return std::shared_ptr<Download>(dl);
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
2016-10-28 05:49:19 +05:30
|
|
|
Download::Ptr Download::makeFile(QUrl url, QString path, Options options)
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
|
|
|
Download * dl = new Download();
|
|
|
|
dl->m_url = url;
|
2016-10-28 05:49:19 +05:30
|
|
|
dl->m_options = options;
|
2016-05-28 23:24:17 +05:30
|
|
|
dl->m_sink.reset(new FileSink(path));
|
2017-05-04 02:41:52 +05:30
|
|
|
return std::shared_ptr<Download>(dl);
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void Download::addValidator(Validator * v)
|
|
|
|
{
|
|
|
|
m_sink->addValidator(v);
|
|
|
|
}
|
|
|
|
|
2017-05-04 02:41:52 +05:30
|
|
|
void Download::start()
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
2017-05-04 02:41:52 +05:30
|
|
|
if(m_status == Job_Aborted)
|
2016-08-14 06:03:31 +05:30
|
|
|
{
|
|
|
|
qWarning() << "Attempt to start an aborted Download:" << m_url.toString();
|
2017-05-04 02:41:52 +05:30
|
|
|
emit aborted(m_index_within_job);
|
2016-08-14 06:03:31 +05:30
|
|
|
return;
|
|
|
|
}
|
2016-05-28 23:24:17 +05:30
|
|
|
QNetworkRequest request(m_url);
|
|
|
|
m_status = m_sink->init(request);
|
|
|
|
switch(m_status)
|
|
|
|
{
|
2017-05-04 02:41:52 +05:30
|
|
|
case Job_Finished:
|
|
|
|
emit succeeded(m_index_within_job);
|
2016-05-28 23:24:17 +05:30
|
|
|
qDebug() << "Download cache hit " << m_url.toString();
|
|
|
|
return;
|
2017-05-04 02:41:52 +05:30
|
|
|
case Job_InProgress:
|
2016-05-28 23:24:17 +05:30
|
|
|
qDebug() << "Downloading " << m_url.toString();
|
|
|
|
break;
|
2017-05-21 23:50:37 +05:30
|
|
|
case Job_Failed_Proceed: // this is meaningless in this context. We do need a sink.
|
2017-05-04 02:41:52 +05:30
|
|
|
case Job_NotStarted:
|
|
|
|
case Job_Failed:
|
|
|
|
emit failed(m_index_within_job);
|
2016-05-28 23:24:17 +05:30
|
|
|
return;
|
2017-05-04 02:41:52 +05:30
|
|
|
case Job_Aborted:
|
2016-08-14 06:03:31 +05:30
|
|
|
return;
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
request.setHeader(QNetworkRequest::UserAgentHeader, "MultiMC/5.0");
|
|
|
|
|
2016-11-07 02:28:54 +05:30
|
|
|
QNetworkReply *rep = ENV.qnam().get(request);
|
2016-05-28 23:24:17 +05:30
|
|
|
|
|
|
|
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)));
|
2017-11-02 03:51:00 +05:30
|
|
|
connect(rep, &QNetworkReply::sslErrors, this, &Download::sslErrors);
|
|
|
|
connect(rep, &QNetworkReply::readyRead, this, &Download::downloadReadyRead);
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void Download::downloadProgress(qint64 bytesReceived, qint64 bytesTotal)
|
|
|
|
{
|
2017-05-04 02:41:52 +05:30
|
|
|
m_total_progress = bytesTotal;
|
2016-05-28 23:24:17 +05:30
|
|
|
m_progress = bytesReceived;
|
2017-05-04 02:41:52 +05:30
|
|
|
emit netActionProgress(m_index_within_job, bytesReceived, bytesTotal);
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void Download::downloadError(QNetworkReply::NetworkError error)
|
|
|
|
{
|
2016-08-14 06:03:31 +05:30
|
|
|
if(error == QNetworkReply::OperationCanceledError)
|
|
|
|
{
|
|
|
|
qCritical() << "Aborted " << m_url.toString();
|
2017-05-04 02:41:52 +05:30
|
|
|
m_status = Job_Aborted;
|
2016-08-14 06:03:31 +05:30
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-10-28 05:49:19 +05:30
|
|
|
if(m_options & Option::AcceptLocalFiles)
|
|
|
|
{
|
|
|
|
if(m_sink->hasLocalData())
|
|
|
|
{
|
2017-05-04 02:41:52 +05:30
|
|
|
m_status = Job_Failed_Proceed;
|
2016-10-28 05:49:19 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2016-08-14 06:03:31 +05:30
|
|
|
// error happened during download.
|
|
|
|
qCritical() << "Failed " << m_url.toString() << " with reason " << error;
|
2017-05-04 02:41:52 +05:30
|
|
|
m_status = Job_Failed;
|
2016-08-14 06:03:31 +05:30
|
|
|
}
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
2017-11-02 03:22:11 +05:30
|
|
|
void Download::sslErrors(const QList<QSslError> & errors)
|
|
|
|
{
|
|
|
|
int i = 1;
|
|
|
|
for (auto error : errors)
|
|
|
|
{
|
|
|
|
qCritical() << "Download" << m_url.toString() << "SSL Error #" << i << " : " << error.errorString();
|
|
|
|
auto cert = error.certificate();
|
|
|
|
qCritical() << "Certificate in question:\n" << cert.toText();
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-04 02:41:52 +05:30
|
|
|
bool Download::handleRedirect()
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
2018-01-21 08:19:54 +05:30
|
|
|
QUrl redirect = m_reply->header(QNetworkRequest::LocationHeader).toUrl();
|
|
|
|
if(!redirect.isValid())
|
|
|
|
{
|
|
|
|
if(!m_reply->hasRawHeader("Location"))
|
|
|
|
{
|
|
|
|
// no redirect -> it's fine to continue
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// there is a Location header, but it's not correct. we need to apply some workarounds...
|
|
|
|
QByteArray redirectBA = m_reply->rawHeader("Location");
|
|
|
|
if(redirectBA.size() == 0)
|
|
|
|
{
|
|
|
|
// empty, yet present redirect header? WTF?
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
QString redirectStr = QString::fromUtf8(redirectBA);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* IF the URL begins with //, we need to insert the URL scheme.
|
|
|
|
* See: https://bugreports.qt-project.org/browse/QTBUG-41061
|
|
|
|
*/
|
|
|
|
if(redirectStr.startsWith("//"))
|
|
|
|
{
|
|
|
|
redirectStr = m_reply->url().scheme() + ":" + redirectStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Next, make sure the URL is parsed in tolerant mode. Qt doesn't parse the location header in tolerant mode, which causes issues.
|
|
|
|
* FIXME: report Qt bug for this
|
|
|
|
*/
|
|
|
|
redirect = QUrl(redirectStr, QUrl::TolerantMode);
|
2018-01-22 07:39:00 +05:30
|
|
|
if(!redirect.isValid())
|
|
|
|
{
|
|
|
|
qWarning() << "Failed to parse redirect URL:" << redirectStr;
|
|
|
|
downloadError(QNetworkReply::ProtocolFailure);
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-21 08:19:54 +05:30
|
|
|
qDebug() << "Fixed location header:" << redirect;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
qDebug() << "Location header:" << redirect;
|
|
|
|
}
|
|
|
|
|
2018-01-22 07:39:00 +05:30
|
|
|
m_url = QUrl(redirect.toString());
|
|
|
|
qDebug() << "Following redirect to " << m_url.toString();
|
|
|
|
start();
|
|
|
|
return true;
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Download::downloadFinished()
|
|
|
|
{
|
|
|
|
// handle HTTP redirection first
|
|
|
|
if(handleRedirect())
|
|
|
|
{
|
|
|
|
qDebug() << "Download redirected:" << m_url.toString();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the download failed before this point ...
|
2017-05-04 02:41:52 +05:30
|
|
|
if (m_status == Job_Failed_Proceed)
|
2016-10-28 05:49:19 +05:30
|
|
|
{
|
|
|
|
qDebug() << "Download failed but we are allowed to proceed:" << m_url.toString();
|
|
|
|
m_sink->abort();
|
|
|
|
m_reply.reset();
|
2017-05-04 02:41:52 +05:30
|
|
|
emit succeeded(m_index_within_job);
|
2016-10-28 05:49:19 +05:30
|
|
|
return;
|
|
|
|
}
|
2017-05-04 02:41:52 +05:30
|
|
|
else if (m_status == Job_Failed)
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
|
|
|
qDebug() << "Download failed in previous step:" << m_url.toString();
|
|
|
|
m_sink->abort();
|
|
|
|
m_reply.reset();
|
2017-05-04 02:41:52 +05:30
|
|
|
emit failed(m_index_within_job);
|
2016-05-28 23:24:17 +05:30
|
|
|
return;
|
|
|
|
}
|
2017-05-04 02:41:52 +05:30
|
|
|
else if(m_status == Job_Aborted)
|
2016-08-14 06:03:31 +05:30
|
|
|
{
|
|
|
|
qDebug() << "Download aborted in previous step:" << m_url.toString();
|
|
|
|
m_sink->abort();
|
|
|
|
m_reply.reset();
|
2017-05-04 02:41:52 +05:30
|
|
|
emit aborted(m_index_within_job);
|
2016-08-14 06:03:31 +05:30
|
|
|
return;
|
|
|
|
}
|
2016-05-28 23:24:17 +05:30
|
|
|
|
|
|
|
// 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());
|
2017-05-04 02:41:52 +05:30
|
|
|
if (m_status != Job_Finished)
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
|
|
|
qDebug() << "Download failed to finalize:" << m_url.toString();
|
|
|
|
m_sink->abort();
|
|
|
|
m_reply.reset();
|
2017-05-04 02:41:52 +05:30
|
|
|
emit failed(m_index_within_job);
|
2016-05-28 23:24:17 +05:30
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_reply.reset();
|
|
|
|
qDebug() << "Download succeeded:" << m_url.toString();
|
2017-05-04 02:41:52 +05:30
|
|
|
emit succeeded(m_index_within_job);
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
void Download::downloadReadyRead()
|
|
|
|
{
|
2017-05-04 02:41:52 +05:30
|
|
|
if(m_status == Job_InProgress)
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
|
|
|
auto data = m_reply->readAll();
|
|
|
|
m_status = m_sink->write(data);
|
2017-05-04 02:41:52 +05:30
|
|
|
if(m_status == Job_Failed)
|
2016-05-28 23:24:17 +05:30
|
|
|
{
|
|
|
|
qCritical() << "Failed to process response chunk for " << m_target_path;
|
|
|
|
}
|
|
|
|
// qDebug() << "Download" << m_url.toString() << "gained" << data.size() << "bytes";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-04 02:41:52 +05:30
|
|
|
qCritical() << "Cannot write to " << m_target_path << ", illegal status" << m_status;
|
2016-05-28 23:24:17 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-08-14 06:03:31 +05:30
|
|
|
|
|
|
|
bool Net::Download::abort()
|
|
|
|
{
|
|
|
|
if(m_reply)
|
|
|
|
{
|
|
|
|
m_reply->abort();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-04 02:41:52 +05:30
|
|
|
m_status = Job_Aborted;
|
2016-08-14 06:03:31 +05:30
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-05-04 02:41:52 +05:30
|
|
|
bool Net::Download::canAbort()
|
2016-08-14 06:03:31 +05:30
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|