|
|
@ -41,7 +41,6 @@ |
|
|
|
#include "base/utils/fs.h" |
|
|
|
#include "base/utils/fs.h" |
|
|
|
#include "base/utils/gzip.h" |
|
|
|
#include "base/utils/gzip.h" |
|
|
|
#include "base/utils/misc.h" |
|
|
|
#include "base/utils/misc.h" |
|
|
|
#include "downloadmanager.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace |
|
|
|
namespace |
|
|
|
{ |
|
|
|
{ |
|
|
@ -95,29 +94,30 @@ QString Net::DownloadHandler::url() const |
|
|
|
|
|
|
|
|
|
|
|
void Net::DownloadHandler::processFinishedDownload() |
|
|
|
void Net::DownloadHandler::processFinishedDownload() |
|
|
|
{ |
|
|
|
{ |
|
|
|
QString url = m_reply->url().toString(); |
|
|
|
const QString url = m_reply->url().toString(); |
|
|
|
qDebug("Download finished: %s", qUtf8Printable(url)); |
|
|
|
qDebug("Download finished: %s", qUtf8Printable(url)); |
|
|
|
|
|
|
|
|
|
|
|
// Check if the request was successful
|
|
|
|
// Check if the request was successful
|
|
|
|
if (m_reply->error() != QNetworkReply::NoError) { |
|
|
|
if (m_reply->error() != QNetworkReply::NoError) { |
|
|
|
// Failure
|
|
|
|
// Failure
|
|
|
|
qDebug("Download failure (%s), reason: %s", qUtf8Printable(url), qUtf8Printable(errorCodeToString(m_reply->error()))); |
|
|
|
qDebug("Download failure (%s), reason: %s", qUtf8Printable(url), qUtf8Printable(errorCodeToString(m_reply->error()))); |
|
|
|
emit downloadFailed(m_downloadRequest.url(), errorCodeToString(m_reply->error())); |
|
|
|
emit downloadFailed(m_downloadRequest.url(), errorCodeToString(m_reply->error())); |
|
|
|
this->deleteLater(); |
|
|
|
this->deleteLater(); |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
|
|
|
|
// Check if the server ask us to redirect somewhere else
|
|
|
|
// Check if the server ask us to redirect somewhere else
|
|
|
|
const QVariant redirection = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute); |
|
|
|
const QVariant redirection = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute); |
|
|
|
if (redirection.isValid()) { |
|
|
|
if (redirection.isValid()) { |
|
|
|
// We should redirect
|
|
|
|
// We should redirect
|
|
|
|
handleRedirection(redirection.toUrl()); |
|
|
|
handleRedirection(redirection.toUrl()); |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
|
|
|
|
// Success
|
|
|
|
// Success
|
|
|
|
QByteArray replyData = m_reply->readAll(); |
|
|
|
const QByteArray replyData = (m_reply->rawHeader("Content-Encoding") == "gzip") |
|
|
|
if (m_reply->rawHeader("Content-Encoding") == "gzip") { |
|
|
|
? Utils::Gzip::decompress(m_reply->readAll()) |
|
|
|
// decompress gzip reply
|
|
|
|
: m_reply->readAll(); |
|
|
|
replyData = Utils::Gzip::decompress(replyData); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (m_downloadRequest.saveToFile()) { |
|
|
|
if (m_downloadRequest.saveToFile()) { |
|
|
|
QString filePath; |
|
|
|
QString filePath; |
|
|
@ -132,8 +132,6 @@ void Net::DownloadHandler::processFinishedDownload() |
|
|
|
|
|
|
|
|
|
|
|
this->deleteLater(); |
|
|
|
this->deleteLater(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Net::DownloadHandler::checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal) |
|
|
|
void Net::DownloadHandler::checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -155,13 +153,11 @@ void Net::DownloadHandler::checkDownloadSize(qint64 bytesReceived, qint64 bytesT |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
void Net::DownloadHandler::handleRedirection(QUrl newUrl) |
|
|
|
void Net::DownloadHandler::handleRedirection(const QUrl &newUrl) |
|
|
|
{ |
|
|
|
{ |
|
|
|
// Resolve relative urls
|
|
|
|
// Resolve relative urls
|
|
|
|
if (newUrl.isRelative()) |
|
|
|
const QUrl resolvedUrl = (newUrl.isRelative()) ? m_reply->url().resolved(newUrl) : newUrl; |
|
|
|
newUrl = m_reply->url().resolved(newUrl); |
|
|
|
const QString newUrlString = resolvedUrl.toString(); |
|
|
|
|
|
|
|
|
|
|
|
const QString newUrlString = newUrl.toString(); |
|
|
|
|
|
|
|
qDebug("Redirecting from %s to %s...", qUtf8Printable(m_reply->url().toString()), qUtf8Printable(newUrlString)); |
|
|
|
qDebug("Redirecting from %s to %s...", qUtf8Printable(m_reply->url().toString()), qUtf8Printable(newUrlString)); |
|
|
|
|
|
|
|
|
|
|
|
// Redirect to magnet workaround
|
|
|
|
// Redirect to magnet workaround
|
|
|
@ -174,9 +170,10 @@ void Net::DownloadHandler::handleRedirection(QUrl newUrl) |
|
|
|
emit downloadFailed(m_downloadRequest.url(), tr("Unexpected redirect to magnet URI.")); |
|
|
|
emit downloadFailed(m_downloadRequest.url(), tr("Unexpected redirect to magnet URI.")); |
|
|
|
|
|
|
|
|
|
|
|
this->deleteLater(); |
|
|
|
this->deleteLater(); |
|
|
|
|
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
else { |
|
|
|
|
|
|
|
DownloadHandler *redirected = m_manager->download(DownloadRequest(m_downloadRequest).url(newUrlString)); |
|
|
|
const DownloadHandler *redirected = m_manager->download(DownloadRequest(m_downloadRequest).url(newUrlString)); |
|
|
|
connect(redirected, &DownloadHandler::destroyed, this, &DownloadHandler::deleteLater); |
|
|
|
connect(redirected, &DownloadHandler::destroyed, this, &DownloadHandler::deleteLater); |
|
|
|
connect(redirected, &DownloadHandler::downloadFailed, this, [this](const QString &, const QString &reason) |
|
|
|
connect(redirected, &DownloadHandler::downloadFailed, this, [this](const QString &, const QString &reason) |
|
|
|
{ |
|
|
|
{ |
|
|
@ -197,7 +194,6 @@ void Net::DownloadHandler::handleRedirection(QUrl newUrl) |
|
|
|
emit downloadFinished(url(), data); |
|
|
|
emit downloadFinished(url(), data); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
QString Net::DownloadHandler::errorCodeToString(const QNetworkReply::NetworkError status) |
|
|
|
QString Net::DownloadHandler::errorCodeToString(const QNetworkReply::NetworkError status) |
|
|
|
{ |
|
|
|
{ |
|
|
|