Browse Source

Allow to specify proxy option per request

adaptive-webui-19844
Vladimir Golovnev (Glassez) 2 years ago
parent
commit
4745a40f0b
No known key found for this signature in database
GPG Key ID: 52A2C7DEE2DFA6F7
  1. 2
      src/base/bittorrent/sessionimpl.cpp
  2. 8
      src/base/net/dnsupdater.cpp
  3. 37
      src/base/net/downloadhandlerimpl.cpp
  4. 49
      src/base/net/downloadhandlerimpl.h
  5. 205
      src/base/net/downloadmanager.cpp
  6. 24
      src/base/net/downloadmanager.h
  7. 2
      src/base/net/geoipmanager.cpp
  8. 4
      src/base/rss/rss_feed.cpp
  9. 5
      src/base/search/searchpluginmanager.cpp
  10. 5
      src/gui/addnewtorrentdialog.cpp
  11. 4
      src/gui/mainwindow.cpp
  12. 4
      src/gui/programupdater.cpp
  13. 2
      src/gui/properties/trackersadditiondialog.cpp
  14. 4
      src/gui/search/pluginselectdialog.cpp
  15. 4
      src/gui/transferlistfilterswidget.cpp

2
src/base/bittorrent/sessionimpl.cpp

@ -2493,7 +2493,7 @@ bool SessionImpl::addTorrent(const QString &source, const AddTorrentParams &para @@ -2493,7 +2493,7 @@ bool SessionImpl::addTorrent(const QString &source, const AddTorrentParams &para
LogMsg(tr("Downloading torrent, please wait... Source: \"%1\"").arg(source));
// Launch downloader
Net::DownloadManager::instance()->download(Net::DownloadRequest(source).limit(MAX_TORRENT_SIZE)
, this, &SessionImpl::handleDownloadFinished);
, true, this, &SessionImpl::handleDownloadFinished);
m_downloadedTorrents[source] = params;
return true;
}

8
src/base/net/dnsupdater.cpp

@ -80,8 +80,8 @@ void DNSUpdater::checkPublicIP() @@ -80,8 +80,8 @@ void DNSUpdater::checkPublicIP()
Q_ASSERT(m_state == OK);
DownloadManager::instance()->download(
DownloadRequest(u"http://checkip.dyndns.org"_qs).userAgent(QStringLiteral("qBittorrent/" QBT_VERSION_2))
, this, &DNSUpdater::ipRequestFinished);
DownloadRequest(u"http://checkip.dyndns.org"_qs).userAgent(QStringLiteral("qBittorrent/" QBT_VERSION_2))
, true, this, &DNSUpdater::ipRequestFinished);
m_lastIPCheckTime = QDateTime::currentDateTime();
}
@ -128,8 +128,8 @@ void DNSUpdater::updateDNSService() @@ -128,8 +128,8 @@ void DNSUpdater::updateDNSService()
m_lastIPCheckTime = QDateTime::currentDateTime();
DownloadManager::instance()->download(
DownloadRequest(getUpdateUrl()).userAgent(QStringLiteral("qBittorrent/" QBT_VERSION_2))
, this, &DNSUpdater::ipUpdateFinished);
DownloadRequest(getUpdateUrl()).userAgent(QStringLiteral("qBittorrent/" QBT_VERSION_2))
, true, this, &DNSUpdater::ipUpdateFinished);
}
QString DNSUpdater::getUpdateUrl() const

37
src/base/net/downloadhandlerimpl.cpp

@ -56,16 +56,18 @@ namespace @@ -56,16 +56,18 @@ namespace
}
}
DownloadHandlerImpl::DownloadHandlerImpl(Net::DownloadManager *manager, const Net::DownloadRequest &downloadRequest)
Net::DownloadHandlerImpl::DownloadHandlerImpl(DownloadManager *manager
, const DownloadRequest &downloadRequest, const bool useProxy)
: DownloadHandler {manager}
, m_manager {manager}
, m_downloadRequest {downloadRequest}
, m_useProxy {useProxy}
{
m_result.url = url();
m_result.status = Net::DownloadStatus::Success;
m_result.status = DownloadStatus::Success;
}
void DownloadHandlerImpl::cancel()
void Net::DownloadHandlerImpl::cancel()
{
if (m_reply)
{
@ -78,7 +80,7 @@ void DownloadHandlerImpl::cancel() @@ -78,7 +80,7 @@ void DownloadHandlerImpl::cancel()
}
}
void DownloadHandlerImpl::assignNetworkReply(QNetworkReply *reply)
void Net::DownloadHandlerImpl::assignNetworkReply(QNetworkReply *reply)
{
Q_ASSERT(reply);
Q_ASSERT(!m_reply);
@ -91,17 +93,22 @@ void DownloadHandlerImpl::assignNetworkReply(QNetworkReply *reply) @@ -91,17 +93,22 @@ void DownloadHandlerImpl::assignNetworkReply(QNetworkReply *reply)
}
// Returns original url
QString DownloadHandlerImpl::url() const
QString Net::DownloadHandlerImpl::url() const
{
return m_downloadRequest.url();
}
const Net::DownloadRequest DownloadHandlerImpl::downloadRequest() const
Net::DownloadRequest Net::DownloadHandlerImpl::downloadRequest() const
{
return m_downloadRequest;
}
void DownloadHandlerImpl::processFinishedDownload()
bool Net::DownloadHandlerImpl::useProxy() const
{
return m_useProxy;
}
void Net::DownloadHandlerImpl::processFinishedDownload()
{
qDebug("Download finished: %s", qUtf8Printable(url()));
@ -156,7 +163,7 @@ void DownloadHandlerImpl::processFinishedDownload() @@ -156,7 +163,7 @@ void DownloadHandlerImpl::processFinishedDownload()
finish();
}
void DownloadHandlerImpl::checkDownloadSize(const qint64 bytesReceived, const qint64 bytesTotal)
void Net::DownloadHandlerImpl::checkDownloadSize(const qint64 bytesReceived, const qint64 bytesTotal)
{
if ((bytesTotal > 0) && (bytesTotal <= m_downloadRequest.limit()))
{
@ -175,7 +182,7 @@ void DownloadHandlerImpl::checkDownloadSize(const qint64 bytesReceived, const qi @@ -175,7 +182,7 @@ void DownloadHandlerImpl::checkDownloadSize(const qint64 bytesReceived, const qi
}
}
void DownloadHandlerImpl::handleRedirection(const QUrl &newUrl)
void Net::DownloadHandlerImpl::handleRedirection(const QUrl &newUrl)
{
if (m_redirectionCount >= MAX_REDIRECTIONS)
{
@ -202,9 +209,9 @@ void DownloadHandlerImpl::handleRedirection(const QUrl &newUrl) @@ -202,9 +209,9 @@ void DownloadHandlerImpl::handleRedirection(const QUrl &newUrl)
}
auto redirected = static_cast<DownloadHandlerImpl *>(
m_manager->download(Net::DownloadRequest(m_downloadRequest).url(newUrlString)));
m_manager->download(DownloadRequest(m_downloadRequest).url(newUrlString), useProxy()));
redirected->m_redirectionCount = m_redirectionCount + 1;
connect(redirected, &DownloadHandlerImpl::finished, this, [this](const Net::DownloadResult &result)
connect(redirected, &DownloadHandlerImpl::finished, this, [this](const DownloadResult &result)
{
m_result = result;
m_result.url = url();
@ -212,18 +219,18 @@ void DownloadHandlerImpl::handleRedirection(const QUrl &newUrl) @@ -212,18 +219,18 @@ void DownloadHandlerImpl::handleRedirection(const QUrl &newUrl)
});
}
void DownloadHandlerImpl::setError(const QString &error)
void Net::DownloadHandlerImpl::setError(const QString &error)
{
m_result.errorString = error;
m_result.status = Net::DownloadStatus::Failed;
m_result.status = DownloadStatus::Failed;
}
void DownloadHandlerImpl::finish()
void Net::DownloadHandlerImpl::finish()
{
emit finished(m_result);
}
QString DownloadHandlerImpl::errorCodeToString(const QNetworkReply::NetworkError status)
QString Net::DownloadHandlerImpl::errorCodeToString(const QNetworkReply::NetworkError status)
{
switch (status)
{

49
src/base/net/downloadhandlerimpl.h

@ -36,33 +36,38 @@ @@ -36,33 +36,38 @@
class QObject;
class QUrl;
class DownloadHandlerImpl final : public Net::DownloadHandler
namespace Net
{
Q_OBJECT
Q_DISABLE_COPY_MOVE(DownloadHandlerImpl)
class DownloadHandlerImpl final : public DownloadHandler
{
Q_OBJECT
Q_DISABLE_COPY_MOVE(DownloadHandlerImpl)
public:
DownloadHandlerImpl(Net::DownloadManager *manager, const Net::DownloadRequest &downloadRequest);
public:
DownloadHandlerImpl(DownloadManager *manager, const DownloadRequest &downloadRequest, bool useProxy);
void cancel() override;
void cancel() override;
QString url() const;
const Net::DownloadRequest downloadRequest() const;
QString url() const;
DownloadRequest downloadRequest() const;
bool useProxy() const;
void assignNetworkReply(QNetworkReply *reply);
void assignNetworkReply(QNetworkReply *reply);
private:
void processFinishedDownload();
void checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal);
void handleRedirection(const QUrl &newUrl);
void setError(const QString &error);
void finish();
private:
void processFinishedDownload();
void checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal);
void handleRedirection(const QUrl &newUrl);
void setError(const QString &error);
void finish();
static QString errorCodeToString(QNetworkReply::NetworkError status);
static QString errorCodeToString(QNetworkReply::NetworkError status);
Net::DownloadManager *m_manager = nullptr;
QNetworkReply *m_reply = nullptr;
const Net::DownloadRequest m_downloadRequest;
short m_redirectionCount = 0;
Net::DownloadResult m_result;
};
DownloadManager *m_manager = nullptr;
QNetworkReply *m_reply = nullptr;
const DownloadRequest m_downloadRequest;
const bool m_useProxy = false;
short m_redirectionCount = 0;
DownloadResult m_result;
};
}

205
src/base/net/downloadmanager.cpp

@ -33,6 +33,7 @@ @@ -33,6 +33,7 @@
#include <QDateTime>
#include <QDebug>
#include <QNetworkAccessManager>
#include <QNetworkCookie>
#include <QNetworkCookieJar>
#include <QNetworkProxy>
@ -51,101 +52,79 @@ namespace @@ -51,101 +52,79 @@ namespace
{
// Disguise as Firefox to avoid web server banning
const char DEFAULT_USER_AGENT[] = "Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Firefox/68.0";
}
class NetworkCookieJar final : public QNetworkCookieJar
class Net::DownloadManager::NetworkCookieJar final : public QNetworkCookieJar
{
public:
explicit NetworkCookieJar(QObject *parent = nullptr)
: QNetworkCookieJar(parent)
{
public:
explicit NetworkCookieJar(QObject *parent = nullptr)
: QNetworkCookieJar(parent)
const QDateTime now = QDateTime::currentDateTime();
QList<QNetworkCookie> cookies = Preferences::instance()->getNetworkCookies();
for (const QNetworkCookie &cookie : asConst(Preferences::instance()->getNetworkCookies()))
{
const QDateTime now = QDateTime::currentDateTime();
QList<QNetworkCookie> cookies = Preferences::instance()->getNetworkCookies();
for (const QNetworkCookie &cookie : asConst(Preferences::instance()->getNetworkCookies()))
{
if (cookie.isSessionCookie() || (cookie.expirationDate() <= now))
cookies.removeAll(cookie);
}
setAllCookies(cookies);
if (cookie.isSessionCookie() || (cookie.expirationDate() <= now))
cookies.removeAll(cookie);
}
~NetworkCookieJar() override
setAllCookies(cookies);
}
~NetworkCookieJar() override
{
const QDateTime now = QDateTime::currentDateTime();
QList<QNetworkCookie> cookies = allCookies();
for (const QNetworkCookie &cookie : asConst(allCookies()))
{
const QDateTime now = QDateTime::currentDateTime();
QList<QNetworkCookie> cookies = allCookies();
for (const QNetworkCookie &cookie : asConst(allCookies()))
{
if (cookie.isSessionCookie() || (cookie.expirationDate() <= now))
cookies.removeAll(cookie);
}
Preferences::instance()->setNetworkCookies(cookies);
if (cookie.isSessionCookie() || (cookie.expirationDate() <= now))
cookies.removeAll(cookie);
}
using QNetworkCookieJar::allCookies;
using QNetworkCookieJar::setAllCookies;
Preferences::instance()->setNetworkCookies(cookies);
}
QList<QNetworkCookie> cookiesForUrl(const QUrl &url) const override
{
const QDateTime now = QDateTime::currentDateTime();
QList<QNetworkCookie> cookies = QNetworkCookieJar::cookiesForUrl(url);
for (const QNetworkCookie &cookie : asConst(QNetworkCookieJar::cookiesForUrl(url)))
{
if (!cookie.isSessionCookie() && (cookie.expirationDate() <= now))
cookies.removeAll(cookie);
}
return cookies;
}
using QNetworkCookieJar::allCookies;
using QNetworkCookieJar::setAllCookies;
bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url) override
QList<QNetworkCookie> cookiesForUrl(const QUrl &url) const override
{
const QDateTime now = QDateTime::currentDateTime();
QList<QNetworkCookie> cookies = QNetworkCookieJar::cookiesForUrl(url);
for (const QNetworkCookie &cookie : asConst(QNetworkCookieJar::cookiesForUrl(url)))
{
const QDateTime now = QDateTime::currentDateTime();
QList<QNetworkCookie> cookies = cookieList;
for (const QNetworkCookie &cookie : cookieList)
{
if (!cookie.isSessionCookie() && (cookie.expirationDate() <= now))
cookies.removeAll(cookie);
}
return QNetworkCookieJar::setCookiesFromUrl(cookies, url);
if (!cookie.isSessionCookie() && (cookie.expirationDate() <= now))
cookies.removeAll(cookie);
}
};
QNetworkRequest createNetworkRequest(const Net::DownloadRequest &downloadRequest)
{
QNetworkRequest request {downloadRequest.url()};
if (downloadRequest.userAgent().isEmpty())
request.setRawHeader("User-Agent", DEFAULT_USER_AGENT);
else
request.setRawHeader("User-Agent", downloadRequest.userAgent().toUtf8());
return cookies;
}
// Spoof HTTP Referer to allow adding torrent link from Torcache/KickAssTorrents
request.setRawHeader("Referer", request.url().toEncoded().data());
#ifdef QT_NO_COMPRESS
// The macro "QT_NO_COMPRESS" defined in QT will disable the zlib related features
// and reply data auto-decompression in QT will also be disabled. But we can support
// gzip encoding and manually decompress the reply data.
request.setRawHeader("Accept-Encoding", "gzip");
#endif
// Qt doesn't support Magnet protocol so we need to handle redirections manually
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::ManualRedirectPolicy);
bool setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url) override
{
const QDateTime now = QDateTime::currentDateTime();
QList<QNetworkCookie> cookies = cookieList;
for (const QNetworkCookie &cookie : cookieList)
{
if (!cookie.isSessionCookie() && (cookie.expirationDate() <= now))
cookies.removeAll(cookie);
}
return request;
return QNetworkCookieJar::setCookiesFromUrl(cookies, url);
}
}
};
Net::DownloadManager *Net::DownloadManager::m_instance = nullptr;
Net::DownloadManager::DownloadManager(QObject *parent)
: QObject(parent)
, m_networkCookieJar {new NetworkCookieJar(this)}
{
connect(&m_networkManager, &QNetworkAccessManager::sslErrors, this, &Net::DownloadManager::ignoreSslErrors);
connect(&m_networkManager, &QNetworkAccessManager::finished, this, &DownloadManager::handleReplyFinished);
m_networkManager->setCookieJar(m_networkCookieJar);
connect(m_networkManager, &QNetworkAccessManager::sslErrors, this, &Net::DownloadManager::ignoreSslErrors);
connect(ProxyConfigurationManager::instance(), &ProxyConfigurationManager::proxyConfigurationChanged
, this, &DownloadManager::applyProxySettings);
m_networkManager.setCookieJar(new NetworkCookieJar(this));
applyProxySettings();
}
@ -166,14 +145,18 @@ Net::DownloadManager *Net::DownloadManager::instance() @@ -166,14 +145,18 @@ Net::DownloadManager *Net::DownloadManager::instance()
return m_instance;
}
Net::DownloadHandler *Net::DownloadManager::download(const DownloadRequest &downloadRequest)
Net::DownloadHandler *Net::DownloadManager::download(const DownloadRequest &downloadRequest, const bool useProxy)
{
// Process download request
const QNetworkRequest request = createNetworkRequest(downloadRequest);
const ServiceID id = ServiceID::fromURL(request.url());
const ServiceID id = ServiceID::fromURL(downloadRequest.url());
const bool isSequentialService = m_sequentialServices.contains(id);
auto downloadHandler = new DownloadHandlerImpl {this, downloadRequest};
auto downloadHandler = new DownloadHandlerImpl(this, downloadRequest, useProxy);
connect(downloadHandler, &DownloadHandler::finished, this
, [this, downloadHandler]
{
handleDownloadFinished(downloadHandler);
});
connect(downloadHandler, &DownloadHandler::finished, downloadHandler, &QObject::deleteLater);
connect(downloadHandler, &QObject::destroyed, this, [this, id, downloadHandler]()
{
@ -189,7 +172,7 @@ Net::DownloadHandler *Net::DownloadManager::download(const DownloadRequest &down @@ -189,7 +172,7 @@ Net::DownloadHandler *Net::DownloadManager::download(const DownloadRequest &down
qDebug("Downloading %s...", qUtf8Printable(downloadRequest.url()));
if (isSequentialService)
m_busyServices.insert(id);
downloadHandler->assignNetworkReply(m_networkManager.get(request));
processRequest(downloadHandler);
}
return downloadHandler;
@ -202,32 +185,32 @@ void Net::DownloadManager::registerSequentialService(const Net::ServiceID &servi @@ -202,32 +185,32 @@ void Net::DownloadManager::registerSequentialService(const Net::ServiceID &servi
QList<QNetworkCookie> Net::DownloadManager::cookiesForUrl(const QUrl &url) const
{
return m_networkManager.cookieJar()->cookiesForUrl(url);
return m_networkCookieJar->cookiesForUrl(url);
}
bool Net::DownloadManager::setCookiesFromUrl(const QList<QNetworkCookie> &cookieList, const QUrl &url)
{
return m_networkManager.cookieJar()->setCookiesFromUrl(cookieList, url);
return m_networkCookieJar->setCookiesFromUrl(cookieList, url);
}
QList<QNetworkCookie> Net::DownloadManager::allCookies() const
{
return static_cast<NetworkCookieJar *>(m_networkManager.cookieJar())->allCookies();
return m_networkCookieJar->allCookies();
}
void Net::DownloadManager::setAllCookies(const QList<QNetworkCookie> &cookieList)
{
static_cast<NetworkCookieJar *>(m_networkManager.cookieJar())->setAllCookies(cookieList);
m_networkCookieJar->setAllCookies(cookieList);
}
bool Net::DownloadManager::deleteCookie(const QNetworkCookie &cookie)
{
return static_cast<NetworkCookieJar *>(m_networkManager.cookieJar())->deleteCookie(cookie);
return m_networkCookieJar->deleteCookie(cookie);
}
bool Net::DownloadManager::hasSupportedScheme(const QString &url)
{
const QStringList schemes = instance()->m_networkManager.supportedSchemes();
const QStringList schemes = QNetworkAccessManager().supportedSchemes();
return std::any_of(schemes.cbegin(), schemes.cend(), [&url](const QString &scheme)
{
return url.startsWith((scheme + u':'), Qt::CaseInsensitive);
@ -238,46 +221,42 @@ void Net::DownloadManager::applyProxySettings() @@ -238,46 +221,42 @@ void Net::DownloadManager::applyProxySettings()
{
const auto *proxyManager = ProxyConfigurationManager::instance();
const ProxyConfiguration proxyConfig = proxyManager->proxyConfiguration();
QNetworkProxy proxy;
m_proxy = QNetworkProxy(QNetworkProxy::NoProxy);
if (!proxyManager->isProxyOnlyForTorrents() && (proxyConfig.type != ProxyType::None))
{
// Proxy enabled
proxy.setHostName(proxyConfig.ip);
proxy.setPort(proxyConfig.port);
// Default proxy type is HTTP, we must change if it is SOCKS5
if ((proxyConfig.type == ProxyType::SOCKS5) || (proxyConfig.type == ProxyType::SOCKS5_PW))
{
qDebug() << Q_FUNC_INFO << "using SOCKS proxy";
proxy.setType(QNetworkProxy::Socks5Proxy);
m_proxy.setType(QNetworkProxy::Socks5Proxy);
}
else
{
qDebug() << Q_FUNC_INFO << "using HTTP proxy";
proxy.setType(QNetworkProxy::HttpProxy);
m_proxy.setType(QNetworkProxy::HttpProxy);
}
m_proxy.setHostName(proxyConfig.ip);
m_proxy.setPort(proxyConfig.port);
// Authentication?
if (proxyManager->isAuthenticationRequired())
{
qDebug("Proxy requires authentication, authenticating...");
proxy.setUser(proxyConfig.username);
proxy.setPassword(proxyConfig.password);
m_proxy.setUser(proxyConfig.username);
m_proxy.setPassword(proxyConfig.password);
}
}
else
{
proxy.setType(QNetworkProxy::NoProxy);
}
m_networkManager.setProxy(proxy);
}
void Net::DownloadManager::handleReplyFinished(const QNetworkReply *reply)
void Net::DownloadManager::handleDownloadFinished(DownloadHandlerImpl *finishedHandler)
{
// QNetworkReply::url() may be different from that of the original request
// so we need QNetworkRequest::url() to properly process Sequential Services
// in the case when the redirection occurred.
const ServiceID id = ServiceID::fromURL(reply->request().url());
const ServiceID id = ServiceID::fromURL(finishedHandler->url());
const auto waitingJobsIter = m_waitingJobs.find(id);
if ((waitingJobsIter == m_waitingJobs.end()) || waitingJobsIter.value().isEmpty())
{
@ -286,12 +265,38 @@ void Net::DownloadManager::handleReplyFinished(const QNetworkReply *reply) @@ -286,12 +265,38 @@ void Net::DownloadManager::handleReplyFinished(const QNetworkReply *reply)
return;
}
auto handler = static_cast<DownloadHandlerImpl *>(waitingJobsIter.value().dequeue());
auto handler = waitingJobsIter.value().dequeue();
qDebug("Downloading %s...", qUtf8Printable(handler->url()));
handler->assignNetworkReply(m_networkManager.get(createNetworkRequest(handler->downloadRequest())));
processRequest(handler);
handler->disconnect(this);
}
void Net::DownloadManager::processRequest(DownloadHandlerImpl *downloadHandler)
{
m_networkManager->setProxy((downloadHandler->useProxy() == true) ? m_proxy : QNetworkProxy(QNetworkProxy::NoProxy));
const DownloadRequest downloadRequest = downloadHandler->downloadRequest();
QNetworkRequest request {downloadRequest.url()};
if (downloadRequest.userAgent().isEmpty())
request.setRawHeader("User-Agent", DEFAULT_USER_AGENT);
else
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());
#ifdef QT_NO_COMPRESS
// The macro "QT_NO_COMPRESS" defined in QT will disable the zlib related features
// and reply data auto-decompression in QT will also be disabled. But we can support
// gzip encoding and manually decompress the reply data.
request.setRawHeader("Accept-Encoding", "gzip");
#endif
// Qt doesn't support Magnet protocol so we need to handle redirections manually
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::ManualRedirectPolicy);
downloadHandler->assignNetworkReply(m_networkManager->get(request));
}
void Net::DownloadManager::ignoreSslErrors(QNetworkReply *reply, const QList<QSslError> &errors)
{
QStringList errorList;

24
src/base/net/downloadmanager.h

@ -31,13 +31,14 @@ @@ -31,13 +31,14 @@
#include <QtGlobal>
#include <QHash>
#include <QNetworkAccessManager>
#include <QNetworkProxy>
#include <QObject>
#include <QQueue>
#include <QSet>
#include "base/path.h"
class QNetworkAccessManager;
class QNetworkCookie;
class QNetworkReply;
class QSslError;
@ -123,6 +124,8 @@ namespace Net @@ -123,6 +124,8 @@ namespace Net
void finished(const DownloadResult &result);
};
class DownloadHandlerImpl;
class DownloadManager : public QObject
{
Q_OBJECT
@ -133,10 +136,10 @@ namespace Net @@ -133,10 +136,10 @@ namespace Net
static void freeInstance();
static DownloadManager *instance();
DownloadHandler *download(const DownloadRequest &downloadRequest);
DownloadHandler *download(const DownloadRequest &downloadRequest, bool useProxy);
template <typename Context, typename Func>
void download(const DownloadRequest &downloadRequest, Context context, Func &&slot);
void download(const DownloadRequest &downloadRequest, bool useProxy, Context context, Func &&slot);
void registerSequentialService(const ServiceID &serviceID);
@ -152,23 +155,28 @@ namespace Net @@ -152,23 +155,28 @@ namespace Net
void ignoreSslErrors(QNetworkReply *, const QList<QSslError> &);
private:
class NetworkCookieJar;
explicit DownloadManager(QObject *parent = nullptr);
void applyProxySettings();
void handleReplyFinished(const QNetworkReply *reply);
void handleDownloadFinished(DownloadHandlerImpl *finishedHandler);
void processRequest(DownloadHandlerImpl *downloadHandler);
static DownloadManager *m_instance;
QNetworkAccessManager m_networkManager;
NetworkCookieJar *m_networkCookieJar = nullptr;
QNetworkAccessManager *m_networkManager = nullptr;
QNetworkProxy m_proxy;
QSet<ServiceID> m_sequentialServices;
QSet<ServiceID> m_busyServices;
QHash<ServiceID, QQueue<DownloadHandler *>> m_waitingJobs;
QHash<ServiceID, QQueue<DownloadHandlerImpl *>> m_waitingJobs;
};
template <typename Context, typename Func>
void DownloadManager::download(const DownloadRequest &downloadRequest, Context context, Func &&slot)
void DownloadManager::download(const DownloadRequest &downloadRequest, bool useProxy, Context context, Func &&slot)
{
const DownloadHandler *handler = download(downloadRequest);
const DownloadHandler *handler = download(downloadRequest, useProxy);
connect(handler, &DownloadHandler::finished, context, slot);
}
}

2
src/base/net/geoipmanager.cpp

@ -129,7 +129,7 @@ void GeoIPManager::downloadDatabaseFile() @@ -129,7 +129,7 @@ void GeoIPManager::downloadDatabaseFile()
{
const QDateTime curDatetime = QDateTime::currentDateTimeUtc();
const QString curUrl = DATABASE_URL.arg(QLocale::c().toString(curDatetime, u"yyyy-MM"));
DownloadManager::instance()->download({curUrl}, this, &GeoIPManager::downloadFinished);
DownloadManager::instance()->download({curUrl}, true, this, &GeoIPManager::downloadFinished);
}
QString GeoIPManager::lookup(const QHostAddress &hostAddr) const

4
src/base/rss/rss_feed.cpp

@ -148,7 +148,7 @@ void Feed::refresh() @@ -148,7 +148,7 @@ void Feed::refresh()
// NOTE: Should we allow manually refreshing for disabled session?
m_downloadHandler = Net::DownloadManager::instance()->download(m_url);
m_downloadHandler = Net::DownloadManager::instance()->download(m_url, true);
connect(m_downloadHandler, &Net::DownloadHandler::finished, this, &Feed::handleDownloadFinished);
if (!m_iconPath.exists())
@ -378,7 +378,7 @@ void Feed::downloadIcon() @@ -378,7 +378,7 @@ void Feed::downloadIcon()
const auto iconUrl = u"%1://%2/favicon.ico"_qs.arg(url.scheme(), url.host());
Net::DownloadManager::instance()->download(
Net::DownloadRequest(iconUrl).saveToFile(true).destFileName(m_iconPath)
, this, &Feed::handleIconDownloadFinished);
, true, this, &Feed::handleIconDownloadFinished);
}
int Feed::updateArticles(const QList<QVariantHash> &loadedArticles)

5
src/base/search/searchpluginmanager.cpp

@ -38,6 +38,7 @@ @@ -38,6 +38,7 @@
#include <QDomNode>
#include <QPointer>
#include <QProcess>
#include <QUrl>
#include "base/global.h"
#include "base/logger.h"
@ -212,7 +213,7 @@ void SearchPluginManager::installPlugin(const QString &source) @@ -212,7 +213,7 @@ void SearchPluginManager::installPlugin(const QString &source)
{
using namespace Net;
DownloadManager::instance()->download(DownloadRequest(source).saveToFile(true)
, this, &SearchPluginManager::pluginDownloadFinished);
, true, this, &SearchPluginManager::pluginDownloadFinished);
}
else
{
@ -328,7 +329,7 @@ void SearchPluginManager::checkForUpdates() @@ -328,7 +329,7 @@ void SearchPluginManager::checkForUpdates()
// Download version file from update server
using namespace Net;
DownloadManager::instance()->download({m_updateUrl + u"versions.txt"}
, this, &SearchPluginManager::versionInfoDownloadFinished);
, true, this, &SearchPluginManager::versionInfoDownloadFinished);
}
SearchDownloadHandler *SearchPluginManager::downloadTorrent(const QString &siteUrl, const QString &url)

5
src/gui/addnewtorrentdialog.cpp

@ -32,6 +32,7 @@ @@ -32,6 +32,7 @@
#include <algorithm>
#include <QAction>
#include <QDateTime>
#include <QDebug>
#include <QDir>
#include <QFileDialog>
@ -479,8 +480,8 @@ void AddNewTorrentDialog::show(const QString &source, const BitTorrent::AddTorre @@ -479,8 +480,8 @@ void AddNewTorrentDialog::show(const QString &source, const BitTorrent::AddTorre
{
// Launch downloader
Net::DownloadManager::instance()->download(
Net::DownloadRequest(source).limit(MAX_TORRENT_SIZE)
, dlg, &AddNewTorrentDialog::handleDownloadFinished);
Net::DownloadRequest(source).limit(MAX_TORRENT_SIZE)
, true, dlg, &AddNewTorrentDialog::handleDownloadFinished);
return;
}

4
src/gui/mainwindow.cpp

@ -1948,8 +1948,8 @@ void MainWindow::installPython() @@ -1948,8 +1948,8 @@ void MainWindow::installPython()
const auto installerURL = u"https://www.python.org/ftp/python/3.8.10/python-3.8.10.exe"_qs;
#endif
Net::DownloadManager::instance()->download(
Net::DownloadRequest(installerURL).saveToFile(true)
, this, &MainWindow::pythonDownloadFinished);
Net::DownloadRequest(installerURL).saveToFile(true)
, true, this, &MainWindow::pythonDownloadFinished);
}
void MainWindow::pythonDownloadFinished(const Net::DownloadResult &result)

4
src/gui/programupdater.cpp

@ -76,8 +76,8 @@ void ProgramUpdater::checkForUpdates() const @@ -76,8 +76,8 @@ void ProgramUpdater::checkForUpdates() const
// Don't change this User-Agent. In case our updater goes haywire,
// the filehost can identify it and contact us.
Net::DownloadManager::instance()->download(
Net::DownloadRequest(RSS_URL).userAgent(QStringLiteral("qBittorrent/" QBT_VERSION_2 " ProgramUpdater (www.qbittorrent.org)"))
, this, &ProgramUpdater::rssDownloadFinished);
Net::DownloadRequest(RSS_URL).userAgent(QStringLiteral("qBittorrent/" QBT_VERSION_2 " ProgramUpdater (www.qbittorrent.org)"))
, true, this, &ProgramUpdater::rssDownloadFinished);
}
QString ProgramUpdater::getNewVersion() const

2
src/gui/properties/trackersadditiondialog.cpp

@ -87,7 +87,7 @@ void TrackersAdditionDialog::onDownloadButtonClicked() @@ -87,7 +87,7 @@ void TrackersAdditionDialog::onDownloadButtonClicked()
m_ui->downloadButton->setEnabled(false);
setCursor(Qt::WaitCursor);
Net::DownloadManager::instance()->download(url, this, &TrackersAdditionDialog::onTorrentListDownloadFinished);
Net::DownloadManager::instance()->download(url, true, this, &TrackersAdditionDialog::onTorrentListDownloadFinished);
}
void TrackersAdditionDialog::onTorrentListDownloadFinished(const Net::DownloadResult &result)

4
src/gui/search/pluginselectdialog.cpp

@ -311,8 +311,8 @@ void PluginSelectDialog::addNewPlugin(const QString &pluginName) @@ -311,8 +311,8 @@ void PluginSelectDialog::addNewPlugin(const QString &pluginName)
// Icon is missing, we must download it
using namespace Net;
DownloadManager::instance()->download(
DownloadRequest(plugin->url + u"/favicon.ico").saveToFile(true)
, this, &PluginSelectDialog::iconDownloadFinished);
DownloadRequest(plugin->url + u"/favicon.ico").saveToFile(true)
, true, this, &PluginSelectDialog::iconDownloadFinished);
}
item->setText(PLUGIN_VERSION, plugin->version.toString());
}

4
src/gui/transferlistfilterswidget.cpp

@ -658,8 +658,8 @@ void TrackerFiltersList::downloadFavicon(const QString &url) @@ -658,8 +658,8 @@ void TrackerFiltersList::downloadFavicon(const QString &url)
{
if (!m_downloadTrackerFavicon) return;
Net::DownloadManager::instance()->download(
Net::DownloadRequest(url).saveToFile(true)
, this, &TrackerFiltersList::handleFavicoDownloadFinished);
Net::DownloadRequest(url).saveToFile(true), true
, this, &TrackerFiltersList::handleFavicoDownloadFinished);
}
void TrackerFiltersList::handleFavicoDownloadFinished(const Net::DownloadResult &result)

Loading…
Cancel
Save