2006-09-30 20:02:39 +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.
|
|
|
|
*
|
|
|
|
* Contact : chris@qbittorrent.org
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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;
|
|
|
|
|
|
|
|
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:
|
|
|
|
downloadThread(QObject* parent) : QThread(parent){}
|
|
|
|
|
|
|
|
void downloadUrl(const QString& url){
|
|
|
|
mutex.lock();
|
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;
|
|
|
|
mutex.unlock();
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void run(){
|
|
|
|
forever{
|
|
|
|
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;
|
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);
|
|
|
|
}
|
|
|
|
}while(to_many_users && retries < 10);
|
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{
|
|
|
|
mutex.unlock();
|
2006-10-05 23:01:41 +04:00
|
|
|
qDebug("In Download thread RUN, mutex unlocked (no urls) -> stopping");
|
2006-09-30 20:02:39 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|