Browse Source

Implement DownloadRequest helper

adaptive-webui-19844
Vladimir Golovnev (Glassez) 6 years ago
parent
commit
a9a6b74416
No known key found for this signature in database
GPG Key ID: 52A2C7DEE2DFA6F7
  1. 39
      src/base/net/downloadhandler.cpp
  2. 8
      src/base/net/downloadhandler.h
  3. 76
      src/base/net/downloadmanager.cpp
  4. 31
      src/base/net/downloadmanager.h

39
src/base/net/downloadhandler.cpp

@ -47,14 +47,11 @@ static QString errorCodeToString(QNetworkReply::NetworkError status); @@ -47,14 +47,11 @@ static QString errorCodeToString(QNetworkReply::NetworkError status);
using namespace Net;
DownloadHandler::DownloadHandler(QNetworkReply *reply, DownloadManager *manager, bool saveToFile, qint64 limit, bool handleRedirectToMagnet)
DownloadHandler::DownloadHandler(QNetworkReply *reply, DownloadManager *manager, const DownloadRequest &downloadRequest)
: QObject(manager)
, m_reply(reply)
, m_manager(manager)
, m_saveToFile(saveToFile)
, m_sizeLimit(limit)
, m_handleRedirectToMagnet(handleRedirectToMagnet)
, m_url(reply->url().toString())
, m_downloadRequest(downloadRequest)
{
init();
}
@ -68,7 +65,7 @@ DownloadHandler::~DownloadHandler() @@ -68,7 +65,7 @@ DownloadHandler::~DownloadHandler()
// Returns original url
QString DownloadHandler::url() const
{
return m_url;
return m_downloadRequest.url();
}
void DownloadHandler::processFinishedDownload()
@ -79,7 +76,7 @@ void DownloadHandler::processFinishedDownload() @@ -79,7 +76,7 @@ void DownloadHandler::processFinishedDownload()
if (m_reply->error() != QNetworkReply::NoError) {
// Failure
qDebug("Download failure (%s), reason: %s", qUtf8Printable(url), qUtf8Printable(errorCodeToString(m_reply->error())));
emit downloadFailed(m_url, errorCodeToString(m_reply->error()));
emit downloadFailed(m_downloadRequest.url(), errorCodeToString(m_reply->error()));
this->deleteLater();
}
else {
@ -97,15 +94,15 @@ void DownloadHandler::processFinishedDownload() @@ -97,15 +94,15 @@ void DownloadHandler::processFinishedDownload()
replyData = Utils::Gzip::decompress(replyData);
}
if (m_saveToFile) {
if (m_downloadRequest.saveToFile()) {
QString filePath;
if (saveToFile(replyData, filePath))
emit downloadFinished(m_url, filePath);
emit downloadFinished(m_downloadRequest.url(), filePath);
else
emit downloadFailed(m_url, tr("I/O Error"));
}
emit downloadFailed(m_downloadRequest.url(), tr("I/O Error"));
}
else {
emit downloadFinished(m_url, replyData);
emit downloadFinished(m_downloadRequest.url(), replyData);
}
this->deleteLater();
@ -119,24 +116,24 @@ void DownloadHandler::checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal) @@ -119,24 +116,24 @@ void DownloadHandler::checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal)
if (bytesTotal > 0) {
// Total number of bytes is available
if (bytesTotal > m_sizeLimit) {
if (bytesTotal > m_downloadRequest.limit()) {
m_reply->abort();
emit downloadFailed(m_url, msg.arg(Utils::Misc::friendlyUnit(bytesTotal), Utils::Misc::friendlyUnit(m_sizeLimit)));
emit downloadFailed(m_downloadRequest.url(), msg.arg(Utils::Misc::friendlyUnit(bytesTotal), Utils::Misc::friendlyUnit(m_downloadRequest.limit())));
}
else {
disconnect(m_reply, &QNetworkReply::downloadProgress, this, &Net::DownloadHandler::checkDownloadSize);
}
}
else if (bytesReceived > m_sizeLimit) {
else if (bytesReceived > m_downloadRequest.limit()) {
m_reply->abort();
emit downloadFailed(m_url, msg.arg(Utils::Misc::friendlyUnit(bytesReceived), Utils::Misc::friendlyUnit(m_sizeLimit)));
emit downloadFailed(m_downloadRequest.url(), msg.arg(Utils::Misc::friendlyUnit(bytesReceived), Utils::Misc::friendlyUnit(m_downloadRequest.limit())));
}
}
void DownloadHandler::init()
{
m_reply->setParent(this);
if (m_sizeLimit > 0)
if (m_downloadRequest.limit() > 0)
connect(m_reply, &QNetworkReply::downloadProgress, this, &Net::DownloadHandler::checkDownloadSize);
connect(m_reply, &QNetworkReply::finished, this, &Net::DownloadHandler::processFinishedDownload);
}
@ -181,15 +178,15 @@ void DownloadHandler::handleRedirection(QUrl newUrl) @@ -181,15 +178,15 @@ void DownloadHandler::handleRedirection(QUrl newUrl)
if (newUrlString.startsWith("magnet:", Qt::CaseInsensitive)) {
qDebug("Magnet redirect detected.");
m_reply->abort();
if (m_handleRedirectToMagnet)
emit redirectedToMagnet(m_url, newUrlString);
if (m_downloadRequest.handleRedirectToMagnet())
emit redirectedToMagnet(m_downloadRequest.url(), newUrlString);
else
emit downloadFailed(m_url, tr("Unexpected redirect to magnet URI."));
emit downloadFailed(m_downloadRequest.url(), tr("Unexpected redirect to magnet URI."));
this->deleteLater();
}
else {
DownloadHandler *tmp = m_manager->downloadUrl(newUrlString, m_saveToFile, m_sizeLimit, m_handleRedirectToMagnet);
DownloadHandler *tmp = m_manager->download(DownloadRequest(m_downloadRequest).url(newUrlString));
m_reply->deleteLater();
m_reply = tmp->m_reply;
init();

8
src/base/net/downloadhandler.h

@ -31,6 +31,7 @@ @@ -31,6 +31,7 @@
#define NET_DOWNLOADHANDLER_H
#include <QObject>
#include "downloadmanager.h"
class QNetworkAccessManager;
class QNetworkReply;
@ -45,7 +46,7 @@ namespace Net @@ -45,7 +46,7 @@ namespace Net
Q_OBJECT
public:
DownloadHandler(QNetworkReply *reply, DownloadManager *manager, bool saveToFile = false, qint64 limit = 0, bool handleRedirectToMagnet = false);
DownloadHandler(QNetworkReply *reply, DownloadManager *manager, const DownloadRequest &downloadRequest);
~DownloadHandler();
QString url() const;
@ -67,10 +68,7 @@ namespace Net @@ -67,10 +68,7 @@ namespace Net
QNetworkReply *m_reply;
DownloadManager *m_manager;
bool m_saveToFile;
qint64 m_sizeLimit;
bool m_handleRedirectToMagnet;
QString m_url;
const DownloadRequest m_downloadRequest;
};
}

76
src/base/net/downloadmanager.cpp

@ -138,28 +138,30 @@ DownloadManager *DownloadManager::instance() @@ -138,28 +138,30 @@ DownloadManager *DownloadManager::instance()
}
DownloadHandler *DownloadManager::downloadUrl(const QString &url, bool saveToFile, qint64 limit, bool handleRedirectToMagnet, const QString &userAgent)
{
return download(DownloadRequest(url).saveToFile(saveToFile).limit(limit).handleRedirectToMagnet(handleRedirectToMagnet).userAgent(userAgent));
}
DownloadHandler *DownloadManager::download(const DownloadRequest &downloadRequest)
{
// Update proxy settings
applyProxySettings();
// Process download request
qDebug("url is %s", qUtf8Printable(url));
const QUrl qurl = QUrl(url);
QNetworkRequest request(qurl);
QNetworkRequest request(downloadRequest.url());
if (userAgent.isEmpty())
if (downloadRequest.userAgent().isEmpty())
request.setRawHeader("User-Agent", DEFAULT_USER_AGENT);
else
request.setRawHeader("User-Agent", userAgent.toUtf8());
request.setRawHeader("User-Agent", downloadRequest.userAgent().toUtf8());
// Spoof HTTP Referer to allow adding torrent link from Torcache/KickAssTorrents
request.setRawHeader("Referer", request.url().toEncoded().data());
qDebug("Downloading %s...", request.url().toEncoded().data());
qDebug() << "Cookies:" << m_networkManager.cookieJar()->cookiesForUrl(request.url());
// accept gzip
request.setRawHeader("Accept-Encoding", "gzip");
return new DownloadHandler(m_networkManager.get(request), this, saveToFile, limit, handleRedirectToMagnet);
return new DownloadHandler(m_networkManager.get(request), this, downloadRequest);
}
QList<QNetworkCookie> DownloadManager::cookiesForUrl(const QUrl &url) const
@ -228,3 +230,63 @@ void DownloadManager::ignoreSslErrors(QNetworkReply *reply, const QList<QSslErro @@ -228,3 +230,63 @@ void DownloadManager::ignoreSslErrors(QNetworkReply *reply, const QList<QSslErro
reply->ignoreSslErrors();
}
#endif
Net::DownloadRequest::DownloadRequest(const QString &url)
: m_url {url}
{
}
QString Net::DownloadRequest::url() const
{
return m_url;
}
Net::DownloadRequest &Net::DownloadRequest::url(const QString &value)
{
m_url = value;
return *this;
}
QString Net::DownloadRequest::userAgent() const
{
return m_userAgent;
}
Net::DownloadRequest &Net::DownloadRequest::userAgent(const QString &value)
{
m_userAgent = value;
return *this;
}
qint64 Net::DownloadRequest::limit() const
{
return m_limit;
}
Net::DownloadRequest &Net::DownloadRequest::limit(qint64 value)
{
m_limit = value;
return *this;
}
bool Net::DownloadRequest::saveToFile() const
{
return m_saveToFile;
}
Net::DownloadRequest &Net::DownloadRequest::saveToFile(bool value)
{
m_saveToFile = value;
return *this;
}
bool Net::DownloadRequest::handleRedirectToMagnet() const
{
return m_handleRedirectToMagnet;
}
Net::DownloadRequest &Net::DownloadRequest::handleRedirectToMagnet(bool value)
{
m_handleRedirectToMagnet = value;
return *this;
}

31
src/base/net/downloadmanager.h

@ -42,9 +42,39 @@ namespace Net @@ -42,9 +42,39 @@ namespace Net
{
class DownloadHandler;
class DownloadRequest
{
public:
DownloadRequest(const QString &url);
DownloadRequest(const DownloadRequest &other) = default;
QString url() const;
DownloadRequest &url(const QString &value);
QString userAgent() const;
DownloadRequest &userAgent(const QString &value);
qint64 limit() const;
DownloadRequest &limit(qint64 value);
bool saveToFile() const;
DownloadRequest &saveToFile(bool value);
bool handleRedirectToMagnet() const;
DownloadRequest &handleRedirectToMagnet(bool value);
private:
QString m_url;
QString m_userAgent;
qint64 m_limit = 0;
bool m_saveToFile = false;
bool m_handleRedirectToMagnet = false;
};
class DownloadManager : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(DownloadManager)
public:
static void initInstance();
@ -52,6 +82,7 @@ namespace Net @@ -52,6 +82,7 @@ namespace Net
static DownloadManager *instance();
DownloadHandler *downloadUrl(const QString &url, bool saveToFile = false, qint64 limit = 0, bool handleRedirectToMagnet = false, const QString &userAgent = "");
DownloadHandler *download(const DownloadRequest &downloadRequest);
QList<QNetworkCookie> cookiesForUrl(const QUrl &url) const;
bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url);
QList<QNetworkCookie> allCookies() const;

Loading…
Cancel
Save