Browse Source

Fixed memory leak in torrent downloader

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

45
src/downloadthread.cpp

@ -54,49 +54,47 @@ 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;
}
// Check if the server ask us to redirect somewhere lese
const QVariant redirection = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
if(redirection.isValid()) { if(redirection.isValid()) {
// We should redirect // We should redirect
qDebug("Redirecting from %s to %s", qPrintable(url), qPrintable(redirection.toUrl().toString())); qDebug("Redirecting from %s to %s", qPrintable(url), qPrintable(redirection.toUrl().toString()));
m_redirectMapping.insert(redirection.toUrl().toString(), url); m_redirectMapping.insert(redirection.toUrl().toString(), url);
downloadUrl(redirection.toUrl().toString()); downloadUrl(redirection.toUrl().toString());
reply->deleteLater();
return; return;
} }
// Checking if it was redirecting, restoring initial URL // Checking if it was redirected, restoring initial URL
if(m_redirectMapping.contains(url)) { if(m_redirectMapping.contains(url)) {
url = m_redirectMapping.take(url); url = m_redirectMapping.take(url);
} }
// Success // Success
QString filePath; QTemporaryFile tmpfile;
QTemporaryFile *tmpfile = new QTemporaryFile; tmpfile.setAutoRemove(false);
tmpfile->setAutoRemove(false); if (tmpfile.open()) {
if (tmpfile->open()) { QString filePath = tmpfile.fileName();
filePath = tmpfile->fileName();
qDebug("Temporary filename is: %s", qPrintable(filePath)); qDebug("Temporary filename is: %s", qPrintable(filePath));
if(reply->open(QIODevice::ReadOnly)) { if(reply->isOpen() || reply->open(QIODevice::ReadOnly)) {
// TODO: Support GZIP compression // TODO: Support GZIP compression
tmpfile->write(reply->readAll()); tmpfile.write(reply->readAll());
reply->close(); tmpfile.close();
tmpfile->close();
delete tmpfile;
// Send finished signal // Send finished signal
emit downloadFinished(url, filePath); emit downloadFinished(url, filePath);
} else { } else {
// Error when reading the request // Error when reading the request
tmpfile->close();
delete tmpfile;
emit downloadFailure(url, tr("I/O Error")); emit downloadFailure(url, tr("I/O Error"));
} }
} else { } else {
delete tmpfile;
emit downloadFailure(url, tr("I/O Error")); 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) {
if(bytesTotal > 0) {
QNetworkReply *reply = static_cast<QNetworkReply*>(sender()); QNetworkReply *reply = static_cast<QNetworkReply*>(sender());
if(bytesTotal > 0) {
// 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