Browse Source

Fixed memory leak in torrent downloader

Code clean up
adaptive-webui-19844
Christophe Dumez 14 years ago
parent
commit
6ff7c6ed06
  1. 83
      src/downloadthread.cpp
  2. 10
      src/downloadthread.h

83
src/downloadthread.cpp

@ -38,7 +38,7 @@
#include "downloadthread.h" #include "downloadthread.h"
#include "preferences.h" #include "preferences.h"
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
#include "rsssettings.h" #include "rsssettings.h"
#endif #endif
#include "qinisettings.h" #include "qinisettings.h"
@ -54,48 +54,46 @@ downloadThread::downloadThread(QObject* parent) : QObject(parent) {
void downloadThread::processDlFinished(QNetworkReply* reply) { void downloadThread::processDlFinished(QNetworkReply* reply) {
QString url = reply->url().toString(); QString url = reply->url().toString();
qDebug("Download finished: %s", qPrintable(url)); qDebug("Download finished: %s", qPrintable(url));
// Check if the request was successful
if(reply->error() != QNetworkReply::NoError) { if(reply->error() != QNetworkReply::NoError) {
// Failure // Failure
qDebug("Download failure (%s), reason: %s", qPrintable(url), qPrintable(errorCodeToString(reply->error()))); qDebug("Download failure (%s), reason: %s", qPrintable(url), qPrintable(errorCodeToString(reply->error())));
emit downloadFailure(url, errorCodeToString(reply->error())); emit downloadFailure(url, errorCodeToString(reply->error()));
} else { reply->deleteLater();
QVariant redirection = reply->attribute(QNetworkRequest::RedirectionTargetAttribute); return;
if(redirection.isValid()) { }
// We should redirect // Check if the server ask us to redirect somewhere lese
qDebug("Redirecting from %s to %s", qPrintable(url), qPrintable(redirection.toUrl().toString())); const QVariant redirection = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
m_redirectMapping.insert(redirection.toUrl().toString(), url); if(redirection.isValid()) {
downloadUrl(redirection.toUrl().toString()); // We should redirect
return; qDebug("Redirecting from %s to %s", qPrintable(url), qPrintable(redirection.toUrl().toString()));
} m_redirectMapping.insert(redirection.toUrl().toString(), url);
// Checking if it was redirecting, restoring initial URL downloadUrl(redirection.toUrl().toString());
if(m_redirectMapping.contains(url)) { reply->deleteLater();
url = m_redirectMapping.take(url); return;
} }
// Success // Checking if it was redirected, restoring initial URL
QString filePath; if(m_redirectMapping.contains(url)) {
QTemporaryFile *tmpfile = new QTemporaryFile; url = m_redirectMapping.take(url);
tmpfile->setAutoRemove(false); }
if (tmpfile->open()) { // Success
filePath = tmpfile->fileName(); QTemporaryFile tmpfile;
qDebug("Temporary filename is: %s", qPrintable(filePath)); tmpfile.setAutoRemove(false);
if(reply->open(QIODevice::ReadOnly)) { if (tmpfile.open()) {
// TODO: Support GZIP compression QString filePath = tmpfile.fileName();
tmpfile->write(reply->readAll()); qDebug("Temporary filename is: %s", qPrintable(filePath));
reply->close(); if(reply->isOpen() || reply->open(QIODevice::ReadOnly)) {
tmpfile->close(); // TODO: Support GZIP compression
delete tmpfile; tmpfile.write(reply->readAll());
// Send finished signal tmpfile.close();
emit downloadFinished(url, filePath); // Send finished signal
} else { emit downloadFinished(url, filePath);
// Error when reading the request
tmpfile->close();
delete tmpfile;
emit downloadFailure(url, tr("I/O Error"));
}
} else { } else {
delete tmpfile; // Error when reading the request
emit downloadFailure(url, tr("I/O Error")); emit downloadFailure(url, tr("I/O Error"));
} }
} else {
emit downloadFailure(url, tr("I/O Error"));
} }
// Clean up // Clean up
reply->deleteLater(); reply->deleteLater();
@ -119,7 +117,7 @@ void downloadThread::loadCookies(const QString &host_name, QString url) {
} }
#endif #endif
void downloadThread::downloadTorrentUrl(QString url) { void downloadThread::downloadTorrentUrl(const QString &url) {
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
// Load cookies // Load cookies
QString host_name = QUrl::fromEncoded(url.toLocal8Bit()).host(); QString host_name = QUrl::fromEncoded(url.toLocal8Bit()).host();
@ -131,12 +129,12 @@ void downloadThread::downloadTorrentUrl(QString url) {
connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(checkDownloadSize(qint64,qint64))); connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(checkDownloadSize(qint64,qint64)));
} }
QNetworkReply* downloadThread::downloadUrl(QString url){ QNetworkReply* downloadThread::downloadUrl(const QString &url){
// Update proxy settings // Update proxy settings
applyProxySettings(); applyProxySettings();
#ifndef DISABLE_GUI #ifndef DISABLE_GUI
// Load cookies // Load cookies
QString host_name = QUrl::fromEncoded(url.toLocal8Bit()).host(); QString host_name = QUrl::fromEncoded(url.toUtf8()).host();
if(!host_name.isEmpty()) if(!host_name.isEmpty())
loadCookies(host_name, url); loadCookies(host_name, url);
#endif #endif
@ -157,20 +155,21 @@ QNetworkReply* downloadThread::downloadUrl(QString url){
} }
void downloadThread::checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal) { void downloadThread::checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal) {
QNetworkReply *reply = static_cast<QNetworkReply*>(sender());
if(bytesTotal > 0) { if(bytesTotal > 0) {
QNetworkReply *reply = static_cast<QNetworkReply*>(sender());
// Total number of bytes is available // Total number of bytes is available
if(bytesTotal > 1048576) { if(bytesTotal > 1048576) {
// More than 1MB, this is probably not a torrent file, aborting... // More than 1MB, this is probably not a torrent file, aborting...
reply->abort(); reply->abort();
reply->deleteLater();
} else { } else {
disconnect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(checkDownloadSize(qint64,qint64))); disconnect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(checkDownloadSize(qint64,qint64)));
} }
} else { } else {
if(bytesReceived > 1048576) { if(bytesReceived > 1048576) {
// More than 1MB, this is probably not a torrent file, aborting... // More than 1MB, this is probably not a torrent file, aborting...
QNetworkReply *reply = static_cast<QNetworkReply*>(sender());
reply->abort(); reply->abort();
reply->deleteLater();
} }
} }
} }
@ -253,7 +252,7 @@ QString downloadThread::errorCodeToString(QNetworkReply::NetworkError status) {
} }
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
void downloadThread::ignoreSslErrors(QNetworkReply* reply,QList<QSslError> errors) { void downloadThread::ignoreSslErrors(QNetworkReply* reply, const QList<QSslError> &errors) {
Q_UNUSED(errors) Q_UNUSED(errors)
// Ignore all SSL errors // Ignore all SSL errors
reply->ignoreSslErrors(); reply->ignoreSslErrors();

10
src/downloadthread.h

@ -43,19 +43,19 @@ class downloadThread : public QObject {
public: public:
downloadThread(QObject* parent = 0); downloadThread(QObject* parent = 0);
QNetworkReply* downloadUrl(QString url); QNetworkReply* downloadUrl(const QString &url);
void downloadTorrentUrl(QString url); void downloadTorrentUrl(const QString &url);
//void setProxy(QString IP, int port, QString username, QString password); //void setProxy(QString IP, int port, QString username, QString password);
signals: signals:
void downloadFinished(QString url, QString file_path); void downloadFinished(const QString &url, const QString &file_path);
void downloadFailure(QString url, QString reason); void downloadFailure(const QString &url, const QString &reason);
private slots: private slots:
void processDlFinished(QNetworkReply* reply); void processDlFinished(QNetworkReply* reply);
void checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal); void checkDownloadSize(qint64 bytesReceived, qint64 bytesTotal);
#ifndef QT_NO_OPENSSL #ifndef QT_NO_OPENSSL
void ignoreSslErrors(QNetworkReply*,QList<QSslError>); void ignoreSslErrors(QNetworkReply*,const QList<QSslError>&);
#endif #endif
private: private:

Loading…
Cancel
Save