From 3eeed813d64081e333ee941f329fae9ca8d379a6 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Sat, 28 Mar 2015 20:22:22 +0300 Subject: [PATCH] Implement new download manager. --- src/core/net/downloadhandler.cpp | 289 +++++++++++++++++++++++++++++++ src/core/net/downloadhandler.h | 75 ++++++++ src/core/net/downloadmanager.cpp | 149 ++++++++++++++++ src/core/net/downloadmanager.h | 76 ++++++++ 4 files changed, 589 insertions(+) create mode 100644 src/core/net/downloadhandler.cpp create mode 100644 src/core/net/downloadhandler.h create mode 100644 src/core/net/downloadmanager.cpp create mode 100644 src/core/net/downloadmanager.h diff --git a/src/core/net/downloadhandler.cpp b/src/core/net/downloadhandler.cpp new file mode 100644 index 000000000..fe86a9de1 --- /dev/null +++ b/src/core/net/downloadhandler.cpp @@ -0,0 +1,289 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2015 Vladimir Golovnev + * Copyright (C) 2006 Christophe Dumez + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give permission to + * link this program with the OpenSSL project's "OpenSSL" library (or with + * modified versions of it that use the same license as the "OpenSSL" library), + * and distribute the linked executables. You must obey the GNU General Public + * License in all respects for all of the code used other than "OpenSSL". If you + * modify file(s), you may extend this exception to your version of the file(s), + * but you are not obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + */ + +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "fs_utils.h" +#include "misc.h" +#include "downloadmanager.h" +#include "downloadhandler.h" + +static QString errorCodeToString(QNetworkReply::NetworkError status); +static QByteArray gUncompress(Bytef *inData, uInt len); + +using namespace Net; + +DownloadHandler::DownloadHandler(QNetworkReply *reply, DownloadManager *manager, qint64 limit) + : QObject(manager) + , m_reply(reply) + , m_manager(manager) + , m_sizeLimit(limit) + , m_url(reply->url().toString()) +{ + init(); +} + +DownloadHandler::~DownloadHandler() +{ + if (m_reply) + delete m_reply; +} + +// Returns original url +QString DownloadHandler::url() const +{ + return m_url; +} + +void DownloadHandler::processFinishedDownload() +{ + QString url = m_reply->url().toString(); + qDebug("Download finished: %s", qPrintable(url)); + // Check if the request was successful + if (m_reply->error() != QNetworkReply::NoError) { + // Failure + qDebug("Download failure (%s), reason: %s", qPrintable(url), qPrintable(errorCodeToString(m_reply->error()))); + emit downloadFailed(m_url, errorCodeToString(m_reply->error())); + this->deleteLater(); + } + else { + // Check if the server ask us to redirect somewhere else + const QVariant redirection = m_reply->attribute(QNetworkRequest::RedirectionTargetAttribute); + if (redirection.isValid()) { + // We should redirect + handleRedirection(redirection.toUrl()); + } + else { + // Success + QString filePath; + if (saveToFile(filePath)) + emit downloadFinished(m_url, filePath); + else + emit downloadFailed(m_url, tr("I/O Error")); + this->deleteLater(); + } + } +} + +void DownloadHandler::checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal) +{ + QString msg = tr("The file size is %1. It exceeds the download limit of %2."); + + if (bytesTotal > 0) { + // Total number of bytes is available + if (bytesTotal > m_sizeLimit) { + m_reply->abort(); + emit downloadFailed(m_url, msg.arg(misc::friendlyUnit(bytesTotal)).arg(misc::friendlyUnit(m_sizeLimit))); + } + else { + disconnect(m_reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(checkDownloadSize(qint64, qint64))); + } + } + else if (bytesReceived > m_sizeLimit) { + m_reply->abort(); + emit downloadFailed(m_url, msg.arg(misc::friendlyUnit(bytesReceived)).arg(misc::friendlyUnit(m_sizeLimit))); + } +} + +void DownloadHandler::init() +{ + m_reply->setParent(this); + if (m_sizeLimit > 0) + connect(m_reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(checkDownloadSize(qint64, qint64))); + connect(m_reply, SIGNAL(finished()), this, SLOT(processFinishedDownload())); +} + +bool DownloadHandler::saveToFile(QString &filePath) +{ + QTemporaryFile *tmpfile = new QTemporaryFile; + if (!tmpfile->open()) { + delete tmpfile; + return false; + } + + tmpfile->setAutoRemove(false); + filePath = tmpfile->fileName(); + qDebug("Temporary filename is: %s", qPrintable(filePath)); + if (m_reply->isOpen() || m_reply->open(QIODevice::ReadOnly)) { + QByteArray replyData = m_reply->readAll(); + if (m_reply->rawHeader("Content-Encoding") == "gzip") { + // uncompress gzip reply + replyData = gUncompress(reinterpret_cast(replyData.data()), static_cast(replyData.length())); + } + tmpfile->write(replyData); + tmpfile->close(); + // XXX: tmpfile needs to be deleted on Windows before using the file + // or it will complain that the file is used by another process. + delete tmpfile; + return true; + } + else { + delete tmpfile; + fsutils::forceRemove(filePath); + } + + return false; +} + +void DownloadHandler::handleRedirection(QUrl newUrl) +{ + // Resolve relative urls + if (newUrl.isRelative()) + newUrl = m_reply->url().resolved(newUrl); + + const QString newUrlString = newUrl.toString(); + qDebug("Redirecting from %s to %s", qPrintable(m_reply->url().toString()), qPrintable(newUrlString)); + + // Redirect to magnet workaround + if (newUrlString.startsWith("magnet:", Qt::CaseInsensitive)) { + qDebug("Magnet redirect detected."); + m_reply->abort(); + emit redirectedToMagnet(m_url, newUrlString); + this->deleteLater(); + } + else { + DownloadHandler *tmp = m_manager->downloadUrl(newUrlString, m_sizeLimit); + m_reply->deleteLater(); + m_reply = tmp->m_reply; + m_sizeLimit = tmp->m_sizeLimit; + init(); + tmp->m_reply = 0; + delete tmp; + } +} + +QString errorCodeToString(QNetworkReply::NetworkError status) +{ + switch(status) { + case QNetworkReply::HostNotFoundError: + return QObject::tr("The remote host name was not found (invalid hostname)"); + case QNetworkReply::OperationCanceledError: + return QObject::tr("The operation was canceled"); + case QNetworkReply::RemoteHostClosedError: + return QObject::tr("The remote server closed the connection prematurely, before the entire reply was received and processed"); + case QNetworkReply::TimeoutError: + return QObject::tr("The connection to the remote server timed out"); + case QNetworkReply::SslHandshakeFailedError: + return QObject::tr("SSL/TLS handshake failed"); + case QNetworkReply::ConnectionRefusedError: + return QObject::tr("The remote server refused the connection"); + case QNetworkReply::ProxyConnectionRefusedError: + return QObject::tr("The connection to the proxy server was refused"); + case QNetworkReply::ProxyConnectionClosedError: + return QObject::tr("The proxy server closed the connection prematurely"); + case QNetworkReply::ProxyNotFoundError: + return QObject::tr("The proxy host name was not found"); + case QNetworkReply::ProxyTimeoutError: + return QObject::tr("The connection to the proxy timed out or the proxy did not reply in time to the request sent"); + case QNetworkReply::ProxyAuthenticationRequiredError: + return QObject::tr("The proxy requires authentication in order to honour the request but did not accept any credentials offered"); + case QNetworkReply::ContentAccessDenied: + return QObject::tr("The access to the remote content was denied (401)"); + case QNetworkReply::ContentOperationNotPermittedError: + return QObject::tr("The operation requested on the remote content is not permitted"); + case QNetworkReply::ContentNotFoundError: + return QObject::tr("The remote content was not found at the server (404)"); + case QNetworkReply::AuthenticationRequiredError: + return QObject::tr("The remote server requires authentication to serve the content but the credentials provided were not accepted"); + case QNetworkReply::ProtocolUnknownError: + return QObject::tr("The Network Access API cannot honor the request because the protocol is not known"); + case QNetworkReply::ProtocolInvalidOperationError: + return QObject::tr("The requested operation is invalid for this protocol"); + case QNetworkReply::UnknownNetworkError: + return QObject::tr("An unknown network-related error was detected"); + case QNetworkReply::UnknownProxyError: + return QObject::tr("An unknown proxy-related error was detected"); + case QNetworkReply::UnknownContentError: + return QObject::tr("An unknown error related to the remote content was detected"); + case QNetworkReply::ProtocolFailure: + return QObject::tr("A breakdown in protocol was detected"); + default: + return QObject::tr("Unknown error"); + } +} + +QByteArray gUncompress(Bytef *inData, uInt len) +{ + if (len <= 4) { + qWarning("gUncompress: Input data is truncated"); + return QByteArray(); + } + + QByteArray result; + + z_stream strm; + static const int CHUNK_SIZE = 1024; + char out[CHUNK_SIZE]; + + /* allocate inflate state */ + strm.zalloc = Z_NULL; + strm.zfree = Z_NULL; + strm.opaque = Z_NULL; + strm.avail_in = len; + strm.next_in = inData; + + const int windowBits = 15; + const int ENABLE_ZLIB_GZIP = 32; + + int ret = inflateInit2(&strm, windowBits | ENABLE_ZLIB_GZIP); // gzip decoding + if (ret != Z_OK) + return QByteArray(); + + // run inflate() + do { + strm.avail_out = CHUNK_SIZE; + strm.next_out = reinterpret_cast(out); + + ret = inflate(&strm, Z_NO_FLUSH); + Q_ASSERT(ret != Z_STREAM_ERROR); // state not clobbered + + switch (ret) { + case Z_NEED_DICT: + case Z_DATA_ERROR: + case Z_MEM_ERROR: + (void) inflateEnd(&strm); + return QByteArray(); + } + + result.append(out, CHUNK_SIZE - strm.avail_out); + } + while (!strm.avail_out); + + // clean up and return + inflateEnd(&strm); + return result; +} diff --git a/src/core/net/downloadhandler.h b/src/core/net/downloadhandler.h new file mode 100644 index 000000000..d427eabad --- /dev/null +++ b/src/core/net/downloadhandler.h @@ -0,0 +1,75 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2015 Vladimir Golovnev + * Copyright (C) 2006 Christophe Dumez + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give permission to + * link this program with the OpenSSL project's "OpenSSL" library (or with + * modified versions of it that use the same license as the "OpenSSL" library), + * and distribute the linked executables. You must obey the GNU General Public + * License in all respects for all of the code used other than "OpenSSL". If you + * modify file(s), you may extend this exception to your version of the file(s), + * but you are not obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + */ + +#ifndef NET_DOWNLOADHANDLER_H +#define NET_DOWNLOADHANDLER_H + +#include + +QT_BEGIN_NAMESPACE +class QNetworkAccessManager; +class QNetworkReply; +QT_END_NAMESPACE + +namespace Net +{ + class DownloadManager; + + class DownloadHandler : public QObject + { + Q_OBJECT + + public: + DownloadHandler(QNetworkReply *reply, DownloadManager *manager, qint64 limit = 0); + ~DownloadHandler(); + + QString url() const; + + signals: + void downloadFinished(const QString &url, const QString &filePath); + void downloadFailed(const QString &url, const QString &reason); + void redirectedToMagnet(const QString &url, const QString &magnetUri); + + private slots: + void processFinishedDownload(); + void checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal); + + private: + void init(); + bool saveToFile(QString &filePath); + void handleRedirection(QUrl newUrl); + + QNetworkReply *m_reply; + DownloadManager *m_manager; + qint64 m_sizeLimit; + QString m_url; + }; +} + +#endif // NET_DOWNLOADHANDLER_H diff --git a/src/core/net/downloadmanager.cpp b/src/core/net/downloadmanager.cpp new file mode 100644 index 000000000..1d04d56ee --- /dev/null +++ b/src/core/net/downloadmanager.cpp @@ -0,0 +1,149 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2015 Vladimir Golovnev + * Copyright (C) 2006 Christophe Dumez + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give permission to + * link this program with the OpenSSL project's "OpenSSL" library (or with + * modified versions of it that use the same license as the "OpenSSL" library), + * and distribute the linked executables. You must obey the GNU General Public + * License in all respects for all of the code used other than "OpenSSL". If you + * modify file(s), you may extend this exception to your version of the file(s), + * but you are not obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "core/preferences.h" +#include "downloadhandler.h" +#include "downloadmanager.h" + +using namespace Net; + +DownloadManager *DownloadManager::m_instance = 0; + +DownloadManager::DownloadManager(QObject *parent) + : QObject(parent) +{ +#ifndef QT_NO_OPENSSL + connect(&m_networkManager, SIGNAL(sslErrors(QNetworkReply *, QList)), this, SLOT(ignoreSslErrors(QNetworkReply *, QList))); +#endif +} + +DownloadManager::~DownloadManager() +{ +} + +void DownloadManager::initInstance() +{ + if (!m_instance) + m_instance = new DownloadManager; +} + +void DownloadManager::freeInstance() +{ + if (m_instance) { + delete m_instance; + m_instance = 0; + } +} + +DownloadManager *DownloadManager::instance() +{ + return m_instance; +} + +DownloadHandler *DownloadManager::downloadUrl(const QString &url, qint64 limit) +{ + // Update proxy settings + applyProxySettings(); + + // Process download request + qDebug("url is %s", qPrintable(url)); + const QUrl qurl = QUrl::fromEncoded(url.toUtf8()); + QNetworkRequest request(qurl); + + // Spoof Firefox 3.5 user agent to avoid + // Web server banning + request.setRawHeader("User-Agent", "Mozilla/5.0 (X11; U; Linux i686 (x86_64); en-US; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5"); + + qDebug("Downloading %s...", request.url().toEncoded().data()); + // accept gzip + request.setRawHeader("Accept-Encoding", "gzip"); + return new DownloadHandler(m_networkManager.get(request), this, limit); +} + +QList DownloadManager::cookiesForUrl(const QString &url) const +{ + return m_networkManager.cookieJar()->cookiesForUrl(url); +} + +bool DownloadManager::setCookiesFromUrl(const QList &cookieList, const QUrl &url) +{ + qDebug("Setting %d cookies for url: %s", cookieList.size(), qPrintable(url.toString())); + return m_networkManager.cookieJar()->setCookiesFromUrl(cookieList, url); +} + +void DownloadManager::applyProxySettings() +{ + QNetworkProxy proxy; + const Preferences* const pref = Preferences::instance(); + + if (pref->isProxyEnabled()) { + // Proxy enabled + proxy.setHostName(pref->getProxyIp()); + proxy.setPort(pref->getProxyPort()); + // Default proxy type is HTTP, we must change if it is SOCKS5 + const int proxyType = pref->getProxyType(); + if ((proxyType == Proxy::SOCKS5) || (proxyType == Proxy::SOCKS5_PW)) { + qDebug() << Q_FUNC_INFO << "using SOCKS proxy"; + proxy.setType(QNetworkProxy::Socks5Proxy); + } + else { + qDebug() << Q_FUNC_INFO << "using HTTP proxy"; + proxy.setType(QNetworkProxy::HttpProxy); + } + // Authentication? + if (pref->isProxyAuthEnabled()) { + qDebug("Proxy requires authentication, authenticating"); + proxy.setUser(pref->getProxyUsername()); + proxy.setPassword(pref->getProxyPassword()); + } + } + else { + proxy.setType(QNetworkProxy::NoProxy); + } + + m_networkManager.setProxy(proxy); +} + +#ifndef QT_NO_OPENSSL +void DownloadManager::ignoreSslErrors(QNetworkReply *reply, const QList &errors) +{ + Q_UNUSED(errors) + // Ignore all SSL errors + reply->ignoreSslErrors(); +} +#endif diff --git a/src/core/net/downloadmanager.h b/src/core/net/downloadmanager.h new file mode 100644 index 000000000..ab3556acc --- /dev/null +++ b/src/core/net/downloadmanager.h @@ -0,0 +1,76 @@ +/* + * Bittorrent Client using Qt and libtorrent. + * Copyright (C) 2015 Vladimir Golovnev + * Copyright (C) 2006 Christophe Dumez + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * In addition, as a special exception, the copyright holders give permission to + * link this program with the OpenSSL project's "OpenSSL" library (or with + * modified versions of it that use the same license as the "OpenSSL" library), + * and distribute the linked executables. You must obey the GNU General Public + * License in all respects for all of the code used other than "OpenSSL". If you + * modify file(s), you may extend this exception to your version of the file(s), + * but you are not obligated to do so. If you do not wish to do so, delete this + * exception statement from your version. + */ + +#ifndef NET_DOWNLOADMANAGER_H +#define NET_DOWNLOADMANAGER_H + +#include +#include + +QT_BEGIN_NAMESPACE +class QNetworkReply; +class QNetworkCookie; +class QSslError; +class QUrl; +QT_END_NAMESPACE + +namespace Net +{ + class DownloadHandler; + + class DownloadManager : public QObject + { + Q_OBJECT + + public: + static void initInstance(); + static void freeInstance(); + static DownloadManager *instance(); + + DownloadHandler *downloadUrl(const QString &url, qint64 limit = 0); + QList cookiesForUrl(const QString &url) const; + bool setCookiesFromUrl(const QList &cookieList, const QUrl &url); + + private slots: + #ifndef QT_NO_OPENSSL + void ignoreSslErrors(QNetworkReply *,const QList &); + #endif + + private: + DownloadManager(QObject *parent = 0); + ~DownloadManager(); + + void applyProxySettings(); + + static DownloadManager *m_instance; + QNetworkAccessManager m_networkManager; + }; +} + +#endif // NET_DOWNLOADMANAGER_H