2006-09-30 20:02:39 +04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
2007-07-14 18:31:59 +04:00
|
|
|
* Copyright (C) 2006 Christophe Dumez
|
2006-09-30 20:02:39 +04:00
|
|
|
*
|
2007-07-14 18:31:59 +04:00
|
|
|
* 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.
|
2006-09-30 20:02:39 +04:00
|
|
|
*
|
|
|
|
* 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
|
2007-07-14 18:31:59 +04:00
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* Contact : chris@qbittorrent.org
|
2006-09-30 20:02:39 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DOWNLOADTHREAD_H
|
|
|
|
#define DOWNLOADTHREAD_H
|
|
|
|
|
|
|
|
#include <QThread>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QTemporaryFile>
|
|
|
|
#include <QMutex>
|
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QWaitCondition>
|
|
|
|
#include <curl/curl.h>
|
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
#include "misc.h"
|
|
|
|
|
|
|
|
class downloadThread : public QThread {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private:
|
|
|
|
QStringList url_list;
|
|
|
|
QMutex mutex;
|
|
|
|
QWaitCondition condition;
|
2007-03-09 21:11:43 +03:00
|
|
|
bool abort;
|
2006-09-30 20:02:39 +04:00
|
|
|
|
|
|
|
signals:
|
2007-03-08 01:36:01 +03:00
|
|
|
void downloadFinished(const QString& url, const QString& file_path, int return_code, const QString& errorBuffer);
|
2006-09-30 20:02:39 +04:00
|
|
|
|
|
|
|
public:
|
2007-03-09 21:11:43 +03:00
|
|
|
downloadThread(QObject* parent) : QThread(parent){
|
|
|
|
mutex.lock();
|
|
|
|
abort = false;
|
|
|
|
mutex.unlock();
|
|
|
|
}
|
2006-09-30 20:02:39 +04:00
|
|
|
|
2007-03-09 21:11:43 +03:00
|
|
|
~downloadThread(){
|
2006-09-30 20:02:39 +04:00
|
|
|
mutex.lock();
|
2007-03-09 21:11:43 +03:00
|
|
|
abort = true;
|
|
|
|
condition.wakeOne();
|
|
|
|
mutex.unlock();
|
|
|
|
wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
void downloadUrl(const QString& url){
|
|
|
|
QMutexLocker locker(&mutex);
|
2006-10-05 23:01:41 +04:00
|
|
|
qDebug("In Download thread function, mutex locked");
|
2006-09-30 20:02:39 +04:00
|
|
|
url_list << url;
|
2006-10-05 23:01:41 +04:00
|
|
|
qDebug("In Download thread function, mutex unlocked (url added)");
|
2006-09-30 20:02:39 +04:00
|
|
|
if(!isRunning()){
|
2006-10-05 23:01:41 +04:00
|
|
|
qDebug("In Download thread function, Launching thread (was stopped)");
|
2006-09-30 20:02:39 +04:00
|
|
|
start();
|
2007-03-09 21:11:43 +03:00
|
|
|
}else{
|
|
|
|
condition.wakeOne();
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-09 21:11:43 +03:00
|
|
|
protected:
|
2006-09-30 20:02:39 +04:00
|
|
|
void run(){
|
|
|
|
forever{
|
2007-03-09 21:11:43 +03:00
|
|
|
if(abort)
|
|
|
|
return;
|
2006-09-30 20:02:39 +04:00
|
|
|
mutex.lock();
|
2006-10-05 23:01:41 +04:00
|
|
|
qDebug("In Download thread RUN, mutex locked");
|
2006-09-30 20:02:39 +04:00
|
|
|
if(url_list.size() != 0){
|
|
|
|
QString url = url_list.takeFirst();
|
|
|
|
mutex.unlock();
|
2006-10-05 23:01:41 +04:00
|
|
|
qDebug("In Download thread RUN, mutex unlocked (got url)");
|
2006-09-30 20:02:39 +04:00
|
|
|
CURL *curl;
|
2006-10-26 00:35:41 +04:00
|
|
|
QString filePath;
|
2006-10-05 23:01:41 +04:00
|
|
|
int return_code, response;
|
2006-09-30 20:02:39 +04:00
|
|
|
// XXX: Trick to get a unique filename
|
|
|
|
QTemporaryFile *tmpfile = new QTemporaryFile;
|
|
|
|
if (tmpfile->open()) {
|
2006-10-26 00:35:41 +04:00
|
|
|
filePath = tmpfile->fileName();
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
|
|
|
delete tmpfile;
|
2007-03-09 21:11:43 +03:00
|
|
|
if(abort)
|
|
|
|
return;
|
2006-10-26 00:35:41 +04:00
|
|
|
FILE *file = fopen((const char*)filePath.toUtf8(), "w");
|
2006-09-30 20:02:39 +04:00
|
|
|
if(!file){
|
2006-10-16 13:02:53 +04:00
|
|
|
std::cerr << "Error: could not open temporary file...\n";
|
2006-09-30 20:02:39 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Initilization required by libcurl
|
|
|
|
curl = curl_easy_init();
|
|
|
|
if(!curl){
|
2006-10-16 13:02:53 +04:00
|
|
|
std::cerr << "Error: Failed to init curl...\n";
|
2006-09-30 20:02:39 +04:00
|
|
|
fclose(file);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Set url to download
|
2006-12-05 05:38:02 +03:00
|
|
|
curl_easy_setopt(curl, CURLOPT_URL, url.toLocal8Bit().constData());
|
|
|
|
qDebug("Url: %s", url.toLocal8Bit().constData());
|
2006-09-30 20:02:39 +04:00
|
|
|
// Define our callback to get called when there's data to be written
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, misc::my_fwrite);
|
|
|
|
// Set destination file
|
|
|
|
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
|
|
|
|
// Some SSL mambo jambo
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, FALSE);
|
|
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);
|
2006-10-04 14:56:35 +04:00
|
|
|
// Disable progress meter
|
|
|
|
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1);
|
|
|
|
// Any kind of authentication
|
|
|
|
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
|
2006-12-05 05:38:02 +03:00
|
|
|
//curl_easy_setopt(curl, CURLOPT_VERBOSE, 1);
|
2006-10-04 14:56:35 +04:00
|
|
|
// Auto referrer
|
|
|
|
curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1);
|
|
|
|
// Follow redirections
|
|
|
|
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
|
|
|
|
// Enable cookies
|
|
|
|
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "");
|
2006-09-30 20:02:39 +04:00
|
|
|
// We want error message:
|
|
|
|
char errorBuffer[CURL_ERROR_SIZE];
|
2006-10-05 23:01:41 +04:00
|
|
|
errorBuffer[0]=0; /* prevent junk from being output */
|
2006-09-30 20:02:39 +04:00
|
|
|
return_code = curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errorBuffer);
|
|
|
|
if(return_code){
|
2006-10-16 13:02:53 +04:00
|
|
|
std::cerr << "Error: failed to set error buffer in curl\n";
|
2006-09-30 20:02:39 +04:00
|
|
|
fclose(file);
|
2006-10-26 00:35:41 +04:00
|
|
|
QFile::remove(filePath);
|
2006-09-30 20:02:39 +04:00
|
|
|
return;
|
|
|
|
}
|
2006-10-06 22:07:01 +04:00
|
|
|
unsigned short retries = 0;
|
2006-10-05 23:01:41 +04:00
|
|
|
bool to_many_users = false;
|
|
|
|
do{
|
|
|
|
// Perform Download
|
|
|
|
return_code = curl_easy_perform(curl);
|
|
|
|
// We want HTTP response code
|
|
|
|
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &response);
|
|
|
|
qDebug("HTTP response code: %d", response);
|
|
|
|
if(response/100 == 5){
|
|
|
|
to_many_users = true;
|
|
|
|
++retries;
|
|
|
|
SleeperThread::msleep(1000);
|
|
|
|
}
|
2007-04-05 22:30:20 +04:00
|
|
|
}while(to_many_users && retries < 10 && response!=0);
|
2006-09-30 20:02:39 +04:00
|
|
|
// Cleanup
|
|
|
|
curl_easy_cleanup(curl);
|
|
|
|
// Close tmp file
|
|
|
|
fclose(file);
|
2006-10-26 00:35:41 +04:00
|
|
|
emit downloadFinished(url, filePath, return_code, QString(errorBuffer));
|
2006-10-05 23:01:41 +04:00
|
|
|
qDebug("In Download thread RUN, signal emitted, ErrorBuffer: %s", errorBuffer);
|
2006-09-30 20:02:39 +04:00
|
|
|
}else{
|
2007-03-09 21:11:43 +03:00
|
|
|
qDebug("In Download thread RUN, mutex still locked (no urls) -> sleeping");
|
|
|
|
condition.wait(&mutex);
|
2006-09-30 20:02:39 +04:00
|
|
|
mutex.unlock();
|
2007-03-09 21:11:43 +03:00
|
|
|
qDebug("In Download thread RUN, woke up, mutex unlocked");
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|