2007-09-29 12:00:14 +04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
|
|
|
* 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.
|
|
|
|
*
|
2009-04-05 21:00:55 +04:00
|
|
|
* 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.
|
|
|
|
*
|
2007-09-29 12:00:14 +04:00
|
|
|
* Contact : chris@qbittorrent.org
|
|
|
|
*/
|
|
|
|
|
2009-11-04 18:04:11 +03:00
|
|
|
#include <QTemporaryFile>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkProxy>
|
2010-05-17 18:57:45 +04:00
|
|
|
#include <QNetworkCookie>
|
|
|
|
#include <QNetworkCookieJar>
|
|
|
|
|
|
|
|
#include "downloadthread.h"
|
|
|
|
#include "preferences.h"
|
2010-11-16 23:34:31 +03:00
|
|
|
#include "rsssettings.h"
|
2010-07-16 19:03:18 +04:00
|
|
|
#include "qinisettings.h"
|
2007-09-29 12:00:14 +04:00
|
|
|
|
2009-11-04 18:04:11 +03:00
|
|
|
/** Download Thread **/
|
2008-07-27 20:13:57 +04:00
|
|
|
|
2009-11-04 18:04:11 +03:00
|
|
|
downloadThread::downloadThread(QObject* parent) : QObject(parent) {
|
2010-05-12 23:41:12 +04:00
|
|
|
connect(&networkManager, SIGNAL(finished (QNetworkReply*)), this, SLOT(processDlFinished(QNetworkReply*)));
|
2010-04-08 20:33:47 +04:00
|
|
|
#ifndef QT_NO_OPENSSL
|
2010-05-12 23:41:12 +04:00
|
|
|
connect(&networkManager, SIGNAL(sslErrors(QNetworkReply*,QList<QSslError>)), this, SLOT(ignoreSslErrors(QNetworkReply*,QList<QSslError>)));
|
2010-04-08 20:28:47 +04:00
|
|
|
#endif
|
2007-09-29 12:00:14 +04:00
|
|
|
}
|
|
|
|
|
2009-11-04 18:04:11 +03:00
|
|
|
void downloadThread::processDlFinished(QNetworkReply* reply) {
|
2010-05-12 23:41:12 +04:00
|
|
|
QString url = reply->url().toEncoded().data();
|
|
|
|
qDebug("Download finished: %s", qPrintable(url));
|
2009-11-04 18:04:11 +03:00
|
|
|
if(reply->error() != QNetworkReply::NoError) {
|
|
|
|
// Failure
|
2010-05-12 22:34:16 +04:00
|
|
|
qDebug("Download failure (%s), reason: %s", qPrintable(url), qPrintable(errorCodeToString(reply->error())));
|
2009-11-04 18:04:11 +03:00
|
|
|
emit downloadFailure(url, errorCodeToString(reply->error()));
|
2009-08-24 07:38:31 +04:00
|
|
|
} else {
|
2010-01-29 21:37:47 +03:00
|
|
|
QVariant redirection = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
|
|
|
|
if(redirection.isValid()) {
|
|
|
|
// We should redirect
|
2010-03-04 23:19:25 +03:00
|
|
|
qDebug("Redirecting from %s to %s", qPrintable(url), qPrintable(redirection.toUrl().toString()));
|
2010-01-29 21:37:47 +03:00
|
|
|
redirect_mapping.insert(redirection.toUrl().toString(), url);
|
|
|
|
downloadUrl(redirection.toUrl().toString());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Checking if it was redirecting, restoring initial URL
|
|
|
|
if(redirect_mapping.contains(url)) {
|
|
|
|
url = redirect_mapping.take(url);
|
|
|
|
}
|
2009-11-04 18:04:11 +03:00
|
|
|
// Success
|
|
|
|
QString filePath;
|
2010-05-30 21:51:40 +04:00
|
|
|
QTemporaryFile *tmpfile = new QTemporaryFile;
|
|
|
|
tmpfile->setAutoRemove(false);
|
|
|
|
if (tmpfile->open()) {
|
|
|
|
filePath = tmpfile->fileName();
|
2010-03-04 23:19:25 +03:00
|
|
|
qDebug("Temporary filename is: %s", qPrintable(filePath));
|
2009-11-04 18:04:11 +03:00
|
|
|
if(reply->open(QIODevice::ReadOnly)) {
|
2010-02-11 00:14:44 +03:00
|
|
|
// TODO: Support GZIP compression
|
2010-05-30 21:51:40 +04:00
|
|
|
tmpfile->write(reply->readAll());
|
2009-11-04 18:04:11 +03:00
|
|
|
reply->close();
|
2010-05-30 21:51:40 +04:00
|
|
|
tmpfile->close();
|
|
|
|
delete tmpfile;
|
2009-11-04 18:04:11 +03:00
|
|
|
// Send finished signal
|
|
|
|
emit downloadFinished(url, filePath);
|
|
|
|
} else {
|
|
|
|
// Error when reading the request
|
2010-05-30 21:51:40 +04:00
|
|
|
tmpfile->close();
|
|
|
|
delete tmpfile;
|
2009-11-04 18:04:11 +03:00
|
|
|
emit downloadFailure(url, tr("I/O Error"));
|
2007-11-24 01:57:49 +03:00
|
|
|
}
|
|
|
|
} else {
|
2010-05-30 21:51:40 +04:00
|
|
|
delete tmpfile;
|
2009-11-04 18:04:11 +03:00
|
|
|
emit downloadFailure(url, tr("I/O Error"));
|
2007-09-29 12:00:14 +04:00
|
|
|
}
|
|
|
|
}
|
2009-11-04 18:04:11 +03:00
|
|
|
// Clean up
|
|
|
|
reply->deleteLater();
|
2007-09-29 12:00:14 +04:00
|
|
|
}
|
|
|
|
|
2010-05-17 23:23:16 +04:00
|
|
|
void downloadThread::loadCookies(const QString &host_name, QString url) {
|
2010-11-18 20:02:29 +03:00
|
|
|
const QList<QByteArray> raw_cookies = RssSettings().getHostNameCookies(host_name);
|
2010-05-17 18:57:45 +04:00
|
|
|
QNetworkCookieJar *cookie_jar = networkManager.cookieJar();
|
|
|
|
QList<QNetworkCookie> cookies;
|
2010-05-17 23:23:16 +04:00
|
|
|
qDebug("Loading cookies for host name: %s", qPrintable(host_name));
|
2010-05-17 18:57:45 +04:00
|
|
|
foreach(const QByteArray& raw_cookie, raw_cookies) {
|
|
|
|
QList<QByteArray> cookie_parts = raw_cookie.split('=');
|
|
|
|
if(cookie_parts.size() == 2) {
|
|
|
|
qDebug("Loading cookie: %s", raw_cookie.constData());
|
|
|
|
cookies << QNetworkCookie(cookie_parts.first(), cookie_parts.last());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cookie_jar->setCookiesFromUrl(cookies, url);
|
|
|
|
networkManager.setCookieJar(cookie_jar);
|
|
|
|
}
|
|
|
|
|
2010-03-12 23:00:51 +03:00
|
|
|
void downloadThread::downloadTorrentUrl(QString url){
|
2010-05-17 18:57:45 +04:00
|
|
|
// Load cookies
|
|
|
|
QString host_name = QUrl::fromEncoded(url.toLocal8Bit()).host();
|
|
|
|
if(!host_name.isEmpty())
|
|
|
|
loadCookies(host_name, url);
|
|
|
|
// Process request
|
2010-03-12 23:00:51 +03:00
|
|
|
QNetworkReply *reply = downloadUrl(url);
|
|
|
|
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(checkDownloadSize(qint64,qint64)));
|
|
|
|
}
|
|
|
|
|
|
|
|
QNetworkReply* downloadThread::downloadUrl(QString url){
|
2009-11-04 18:04:11 +03:00
|
|
|
// Update proxy settings
|
|
|
|
applyProxySettings();
|
2010-05-17 18:57:45 +04:00
|
|
|
// Load cookies
|
|
|
|
QString host_name = QUrl::fromEncoded(url.toLocal8Bit()).host();
|
|
|
|
if(!host_name.isEmpty())
|
|
|
|
loadCookies(host_name, url);
|
2009-11-04 18:04:11 +03:00
|
|
|
// Process download request
|
2010-05-12 23:41:12 +04:00
|
|
|
qDebug("url is %s", qPrintable(url));
|
2010-07-23 18:05:53 +04:00
|
|
|
const QUrl qurl = QUrl::fromEncoded(url.toLocal8Bit());
|
2010-05-12 23:41:12 +04:00
|
|
|
QNetworkRequest request(qurl);
|
2009-12-01 20:36:11 +03:00
|
|
|
// 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");
|
2010-05-12 22:34:16 +04:00
|
|
|
qDebug("Downloading %s...", request.url().toEncoded().data());
|
2010-05-17 23:23:16 +04:00
|
|
|
qDebug("%d cookies for this URL", networkManager.cookieJar()->cookiesForUrl(url).size());
|
|
|
|
for(int i=0; i<networkManager.cookieJar()->cookiesForUrl(url).size(); ++i) {
|
|
|
|
qDebug("%s=%s", networkManager.cookieJar()->cookiesForUrl(url).at(i).name().data(), networkManager.cookieJar()->cookiesForUrl(url).at(i).value().data());
|
|
|
|
qDebug("Domain: %s, Path: %s", qPrintable(networkManager.cookieJar()->cookiesForUrl(url).at(i).domain()), qPrintable(networkManager.cookieJar()->cookiesForUrl(url).at(i).path()));
|
|
|
|
}
|
2010-05-12 23:41:12 +04:00
|
|
|
return networkManager.get(request);
|
2010-03-12 23:00:51 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void downloadThread::checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal) {
|
|
|
|
if(bytesTotal > 0) {
|
|
|
|
QNetworkReply *reply = static_cast<QNetworkReply*>(sender());
|
|
|
|
// Total number of bytes is available
|
|
|
|
if(bytesTotal > 1048576) {
|
|
|
|
// More than 1MB, this is probably not a torrent file, aborting...
|
|
|
|
reply->abort();
|
|
|
|
} else {
|
|
|
|
disconnect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(checkDownloadSize(qint64,qint64)));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(bytesReceived > 1048576) {
|
|
|
|
// More than 1MB, this is probably not a torrent file, aborting...
|
|
|
|
QNetworkReply *reply = static_cast<QNetworkReply*>(sender());
|
|
|
|
reply->abort();
|
|
|
|
}
|
|
|
|
}
|
2007-09-29 12:00:14 +04:00
|
|
|
}
|
|
|
|
|
2009-11-04 18:04:11 +03:00
|
|
|
void downloadThread::applyProxySettings() {
|
|
|
|
QNetworkProxy proxy;
|
2010-07-16 19:03:18 +04:00
|
|
|
QIniSettings settings("qBittorrent", "qBittorrent");
|
2009-11-04 18:04:11 +03:00
|
|
|
int intValue = settings.value(QString::fromUtf8("Preferences/Connection/HTTPProxyType"), 0).toInt();
|
|
|
|
if(intValue > 0) {
|
|
|
|
// Proxy enabled
|
|
|
|
QString IP = settings.value(QString::fromUtf8("Preferences/Connection/HTTPProxy/IP"), "0.0.0.0").toString();
|
|
|
|
proxy.setHostName(IP);
|
|
|
|
QString port = settings.value(QString::fromUtf8("Preferences/Connection/HTTPProxy/Port"), 8080).toString();
|
2010-03-07 00:11:47 +03:00
|
|
|
qDebug("Using proxy: %s", qPrintable(IP));
|
2009-11-04 18:04:11 +03:00
|
|
|
proxy.setPort(port.toUShort());
|
|
|
|
// Default proxy type is HTTP, we must change if it is SOCKS5
|
2010-10-24 01:46:05 +04:00
|
|
|
if(intValue == Proxy::SOCKS5 || intValue == Proxy::SOCKS5_PW) {
|
2009-11-04 18:04:11 +03:00
|
|
|
qDebug("Proxy is SOCKS5, not HTTP");
|
|
|
|
proxy.setType(QNetworkProxy::Socks5Proxy);
|
|
|
|
} else {
|
|
|
|
proxy.setType(QNetworkProxy::HttpProxy);
|
2009-08-23 12:10:56 +04:00
|
|
|
}
|
2009-11-04 18:04:11 +03:00
|
|
|
// Authentication?
|
|
|
|
if(intValue > 2) {
|
|
|
|
qDebug("Proxy requires authentication, authenticating");
|
|
|
|
QString username = settings.value(QString::fromUtf8("Preferences/Connection/HTTPProxy/Username"), QString()).toString();
|
|
|
|
proxy.setUser(username);
|
|
|
|
QString password = settings.value(QString::fromUtf8("Preferences/Connection/HTTPProxy/Password"), QString()).toString();
|
|
|
|
proxy.setPassword(password);
|
2007-09-29 12:00:14 +04:00
|
|
|
}
|
|
|
|
|
2009-11-04 18:04:11 +03:00
|
|
|
} else {
|
|
|
|
proxy.setType(QNetworkProxy::NoProxy);
|
2008-07-27 20:13:57 +04:00
|
|
|
}
|
2010-05-12 23:41:12 +04:00
|
|
|
networkManager.setProxy(proxy);
|
2007-09-29 12:00:14 +04:00
|
|
|
}
|
|
|
|
|
2009-11-04 18:04:11 +03:00
|
|
|
QString downloadThread::errorCodeToString(QNetworkReply::NetworkError status) {
|
|
|
|
switch(status){
|
|
|
|
case QNetworkReply::HostNotFoundError:
|
|
|
|
return tr("The remote host name was not found (invalid hostname)");
|
|
|
|
case QNetworkReply::OperationCanceledError:
|
|
|
|
return tr("The operation was canceled");
|
|
|
|
case QNetworkReply::RemoteHostClosedError:
|
|
|
|
return tr("The remote server closed the connection prematurely, before the entire reply was received and processed");
|
|
|
|
case QNetworkReply::TimeoutError:
|
|
|
|
return tr("The connection to the remote server timed out");
|
|
|
|
case QNetworkReply::SslHandshakeFailedError:
|
|
|
|
return tr("SSL/TLS handshake failed");
|
|
|
|
case QNetworkReply::ConnectionRefusedError:
|
|
|
|
return tr("The remote server refused the connection");
|
|
|
|
case QNetworkReply::ProxyConnectionRefusedError:
|
|
|
|
return tr("The connection to the proxy server was refused");
|
|
|
|
case QNetworkReply::ProxyConnectionClosedError:
|
|
|
|
return tr("The proxy server closed the connection prematurely");
|
|
|
|
case QNetworkReply::ProxyNotFoundError:
|
|
|
|
return tr("The proxy host name was not found");
|
|
|
|
case QNetworkReply::ProxyTimeoutError:
|
|
|
|
return tr("The connection to the proxy timed out or the proxy did not reply in time to the request sent");
|
|
|
|
case QNetworkReply::ProxyAuthenticationRequiredError:
|
|
|
|
return tr("The proxy requires authentication in order to honour the request but did not accept any credentials offered");
|
|
|
|
case QNetworkReply::ContentAccessDenied:
|
|
|
|
return tr("The access to the remote content was denied (401)");
|
|
|
|
case QNetworkReply::ContentOperationNotPermittedError:
|
|
|
|
return tr("The operation requested on the remote content is not permitted");
|
|
|
|
case QNetworkReply::ContentNotFoundError:
|
|
|
|
return tr("The remote content was not found at the server (404)");
|
|
|
|
case QNetworkReply::AuthenticationRequiredError:
|
|
|
|
return tr("The remote server requires authentication to serve the content but the credentials provided were not accepted");
|
|
|
|
case QNetworkReply::ProtocolUnknownError:
|
|
|
|
return tr("The Network Access API cannot honor the request because the protocol is not known");
|
|
|
|
case QNetworkReply::ProtocolInvalidOperationError:
|
|
|
|
return tr("The requested operation is invalid for this protocol");
|
|
|
|
case QNetworkReply::UnknownNetworkError:
|
|
|
|
return tr("An unknown network-related error was detected");
|
|
|
|
case QNetworkReply::UnknownProxyError:
|
|
|
|
return tr("An unknown proxy-related error was detected");
|
|
|
|
case QNetworkReply::UnknownContentError:
|
|
|
|
return tr("An unknown error related to the remote content was detected");
|
|
|
|
case QNetworkReply::ProtocolFailure:
|
|
|
|
return tr("A breakdown in protocol was detected");
|
|
|
|
default:
|
|
|
|
return tr("Unknown error");
|
2008-07-27 20:13:57 +04:00
|
|
|
}
|
2007-09-29 12:00:14 +04:00
|
|
|
}
|
2010-04-06 20:25:24 +04:00
|
|
|
|
2010-04-08 20:33:47 +04:00
|
|
|
#ifndef QT_NO_OPENSSL
|
2010-04-06 20:25:24 +04:00
|
|
|
void downloadThread::ignoreSslErrors(QNetworkReply* reply,QList<QSslError> errors) {
|
2010-04-07 14:00:32 +04:00
|
|
|
Q_UNUSED(errors)
|
2010-04-06 20:25:24 +04:00
|
|
|
// Ignore all SSL errors
|
2010-04-07 14:00:32 +04:00
|
|
|
reply->ignoreSslErrors();
|
2010-04-06 20:25:24 +04:00
|
|
|
}
|
2010-04-08 20:28:47 +04:00
|
|
|
#endif
|