Browse Source

Implement DownloadRequest helper

adaptive-webui-19844
Vladimir Golovnev (Glassez) 7 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);
using namespace Net; 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) : QObject(manager)
, m_reply(reply) , m_reply(reply)
, m_manager(manager) , m_manager(manager)
, m_saveToFile(saveToFile) , m_downloadRequest(downloadRequest)
, m_sizeLimit(limit)
, m_handleRedirectToMagnet(handleRedirectToMagnet)
, m_url(reply->url().toString())
{ {
init(); init();
} }
@ -68,7 +65,7 @@ DownloadHandler::~DownloadHandler()
// Returns original url // Returns original url
QString DownloadHandler::url() const QString DownloadHandler::url() const
{ {
return m_url; return m_downloadRequest.url();
} }
void DownloadHandler::processFinishedDownload() void DownloadHandler::processFinishedDownload()
@ -79,7 +76,7 @@ void DownloadHandler::processFinishedDownload()
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_url, errorCodeToString(m_reply->error())); emit downloadFailed(m_downloadRequest.url(), errorCodeToString(m_reply->error()));
this->deleteLater(); this->deleteLater();
} }
else { else {
@ -97,15 +94,15 @@ void DownloadHandler::processFinishedDownload()
replyData = Utils::Gzip::decompress(replyData); replyData = Utils::Gzip::decompress(replyData);
} }
if (m_saveToFile) { if (m_downloadRequest.saveToFile()) {
QString filePath; QString filePath;
if (saveToFile(replyData, filePath)) if (saveToFile(replyData, filePath))
emit downloadFinished(m_url, filePath); emit downloadFinished(m_downloadRequest.url(), filePath);
else else
emit downloadFailed(m_url, tr("I/O Error")); emit downloadFailed(m_downloadRequest.url(), tr("I/O Error"));
} }
else { else {
emit downloadFinished(m_url, replyData); emit downloadFinished(m_downloadRequest.url(), replyData);
} }
this->deleteLater(); this->deleteLater();
@ -119,24 +116,24 @@ void DownloadHandler::checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal)
if (bytesTotal > 0) { if (bytesTotal > 0) {
// Total number of bytes is available // Total number of bytes is available
if (bytesTotal > m_sizeLimit) { if (bytesTotal > m_downloadRequest.limit()) {
m_reply->abort(); 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 { else {
disconnect(m_reply, &QNetworkReply::downloadProgress, this, &Net::DownloadHandler::checkDownloadSize); disconnect(m_reply, &QNetworkReply::downloadProgress, this, &Net::DownloadHandler::checkDownloadSize);
} }
} }
else if (bytesReceived > m_sizeLimit) { else if (bytesReceived > m_downloadRequest.limit()) {
m_reply->abort(); 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() void DownloadHandler::init()
{ {
m_reply->setParent(this); 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::downloadProgress, this, &Net::DownloadHandler::checkDownloadSize);
connect(m_reply, &QNetworkReply::finished, this, &Net::DownloadHandler::processFinishedDownload); connect(m_reply, &QNetworkReply::finished, this, &Net::DownloadHandler::processFinishedDownload);
} }
@ -181,15 +178,15 @@ void DownloadHandler::handleRedirection(QUrl newUrl)
if (newUrlString.startsWith("magnet:", Qt::CaseInsensitive)) { if (newUrlString.startsWith("magnet:", Qt::CaseInsensitive)) {
qDebug("Magnet redirect detected."); qDebug("Magnet redirect detected.");
m_reply->abort(); m_reply->abort();
if (m_handleRedirectToMagnet) if (m_downloadRequest.handleRedirectToMagnet())
emit redirectedToMagnet(m_url, newUrlString); emit redirectedToMagnet(m_downloadRequest.url(), newUrlString);
else else
emit downloadFailed(m_url, tr("Unexpected redirect to magnet URI.")); emit downloadFailed(m_downloadRequest.url(), tr("Unexpected redirect to magnet URI."));
this->deleteLater(); this->deleteLater();
} }
else { 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->deleteLater();
m_reply = tmp->m_reply; m_reply = tmp->m_reply;
init(); init();

8
src/base/net/downloadhandler.h

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

76
src/base/net/downloadmanager.cpp

@ -138,28 +138,30 @@ DownloadManager *DownloadManager::instance()
} }
DownloadHandler *DownloadManager::downloadUrl(const QString &url, bool saveToFile, qint64 limit, bool handleRedirectToMagnet, const QString &userAgent) 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 // Update proxy settings
applyProxySettings(); applyProxySettings();
// Process download request // Process download request
qDebug("url is %s", qUtf8Printable(url)); QNetworkRequest request(downloadRequest.url());
const QUrl qurl = QUrl(url);
QNetworkRequest request(qurl);
if (userAgent.isEmpty()) if (downloadRequest.userAgent().isEmpty())
request.setRawHeader("User-Agent", DEFAULT_USER_AGENT); request.setRawHeader("User-Agent", DEFAULT_USER_AGENT);
else 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 // Spoof HTTP Referer to allow adding torrent link from Torcache/KickAssTorrents
request.setRawHeader("Referer", request.url().toEncoded().data()); request.setRawHeader("Referer", request.url().toEncoded().data());
qDebug("Downloading %s...", request.url().toEncoded().data()); qDebug("Downloading %s...", request.url().toEncoded().data());
qDebug() << "Cookies:" << m_networkManager.cookieJar()->cookiesForUrl(request.url());
// accept gzip // accept gzip
request.setRawHeader("Accept-Encoding", "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 QList<QNetworkCookie> DownloadManager::cookiesForUrl(const QUrl &url) const
@ -228,3 +230,63 @@ void DownloadManager::ignoreSslErrors(QNetworkReply *reply, const QList<QSslErro
reply->ignoreSslErrors(); reply->ignoreSslErrors();
} }
#endif #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
{ {
class DownloadHandler; 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 class DownloadManager : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY(DownloadManager)
public: public:
static void initInstance(); static void initInstance();
@ -52,6 +82,7 @@ namespace Net
static DownloadManager *instance(); static DownloadManager *instance();
DownloadHandler *downloadUrl(const QString &url, bool saveToFile = false, qint64 limit = 0, bool handleRedirectToMagnet = false, const QString &userAgent = ""); 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; QList<QNetworkCookie> cookiesForUrl(const QUrl &url) const;
bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url); bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url);
QList<QNetworkCookie> allCookies() const; QList<QNetworkCookie> allCookies() const;

Loading…
Cancel
Save