mirror of
https://github.com/d47081/qBittorrent.git
synced 2025-01-11 07:18:08 +00:00
Allow to specify proxy option per request
This commit is contained in:
parent
8993d87b32
commit
4745a40f0b
@ -2493,7 +2493,7 @@ bool SessionImpl::addTorrent(const QString &source, const AddTorrentParams ¶
|
||||
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;
|
||||
}
|
||||
|
@ -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()
|
||||
|
||||
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
|
||||
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
// 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()
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
});
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
|
@ -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;
|
||||
};
|
||||
}
|
||||
|
@ -33,6 +33,7 @@
|
||||
|
||||
#include <QDateTime>
|
||||
#include <QDebug>
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkCookie>
|
||||
#include <QNetworkCookieJar>
|
||||
#include <QNetworkProxy>
|
||||
@ -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
|
||||
{
|
||||
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()))
|
||||
{
|
||||
if (cookie.isSessionCookie() || (cookie.expirationDate() <= now))
|
||||
cookies.removeAll(cookie);
|
||||
}
|
||||
|
||||
setAllCookies(cookies);
|
||||
}
|
||||
|
||||
~NetworkCookieJar() override
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
using QNetworkCookieJar::allCookies;
|
||||
using QNetworkCookieJar::setAllCookies;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 QNetworkCookieJar::setCookiesFromUrl(cookies, url);
|
||||
}
|
||||
};
|
||||
|
||||
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());
|
||||
|
||||
// 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);
|
||||
|
||||
return request;
|
||||
}
|
||||
}
|
||||
|
||||
class Net::DownloadManager::NetworkCookieJar final : public QNetworkCookieJar
|
||||
{
|
||||
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()))
|
||||
{
|
||||
if (cookie.isSessionCookie() || (cookie.expirationDate() <= now))
|
||||
cookies.removeAll(cookie);
|
||||
}
|
||||
|
||||
setAllCookies(cookies);
|
||||
}
|
||||
|
||||
~NetworkCookieJar() override
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
using QNetworkCookieJar::allCookies;
|
||||
using QNetworkCookieJar::setAllCookies;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 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()
|
||||
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
|
||||
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
|
||||
|
||||
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()
|
||||
{
|
||||
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)
|
||||
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;
|
||||
|
@ -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
|
||||
void finished(const DownloadResult &result);
|
||||
};
|
||||
|
||||
class DownloadHandlerImpl;
|
||||
|
||||
class DownloadManager : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
@ -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
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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()
|
||||
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)
|
||||
|
@ -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)
|
||||
{
|
||||
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()
|
||||
// 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)
|
||||
|
@ -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
|
||||
{
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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());
|
||||
}
|
||||
|
@ -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…
Reference in New Issue
Block a user