2008-05-17 16:44:42 +04:00
|
|
|
/*
|
2009-04-05 22:48:45 +04:00
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
|
|
|
* Copyright (C) 2006 Ishan Arora and Christophe Dumez
|
2008-05-17 16:44:42 +04:00
|
|
|
*
|
2009-04-05 22:48:45 +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.
|
2008-05-17 16:44:42 +04:00
|
|
|
*
|
2009-04-05 22:48:45 +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.
|
2008-05-17 16:44:42 +04:00
|
|
|
*
|
2009-04-05 22:48:45 +04:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* Contact : chris@qbittorrent.org
|
2008-05-17 16:44:42 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "httpconnection.h"
|
|
|
|
#include "httpserver.h"
|
|
|
|
#include "eventmanager.h"
|
|
|
|
#include "json.h"
|
2009-11-17 11:15:26 +03:00
|
|
|
#include "bittorrent.h"
|
2008-05-17 16:44:42 +04:00
|
|
|
#include <QTcpSocket>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QStringList>
|
|
|
|
#include <QHttpRequestHeader>
|
|
|
|
#include <QHttpResponseHeader>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QDebug>
|
2008-07-31 16:03:28 +04:00
|
|
|
#include <QTemporaryFile>
|
2008-05-17 16:44:42 +04:00
|
|
|
|
2009-11-20 11:01:59 +03:00
|
|
|
HttpConnection::HttpConnection(QTcpSocket *socket, Bittorrent *BTSession, HttpServer *parent)
|
2009-11-17 11:15:26 +03:00
|
|
|
: QObject(parent), socket(socket), parent(parent), BTSession(BTSession)
|
2008-05-17 16:44:42 +04:00
|
|
|
{
|
2009-08-21 18:05:03 +04:00
|
|
|
socket->setParent(this);
|
|
|
|
connect(socket, SIGNAL(readyRead()), this, SLOT(read()));
|
|
|
|
connect(socket, SIGNAL(disconnected()), this, SLOT(deleteLater()));
|
2008-05-17 16:44:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
HttpConnection::~HttpConnection()
|
|
|
|
{
|
2008-09-14 00:33:07 +04:00
|
|
|
delete socket;
|
2008-05-17 16:44:42 +04:00
|
|
|
}
|
|
|
|
|
2008-09-14 14:14:54 +04:00
|
|
|
void HttpConnection::processDownloadedFile(QString url, QString file_path) {
|
2009-07-12 12:13:00 +04:00
|
|
|
qDebug("URL %s successfully downloaded !", (const char*)url.toLocal8Bit());
|
2009-08-21 18:05:03 +04:00
|
|
|
emit torrentReadyToBeDownloaded(file_path, false, url, false);
|
2008-09-14 14:14:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HttpConnection::handleDownloadFailure(QString url, QString reason) {
|
2009-07-12 12:13:00 +04:00
|
|
|
std::cerr << "Could not download " << (const char*)url.toLocal8Bit() << ", reason: " << (const char*)reason.toLocal8Bit() << "\n";
|
2008-09-14 14:14:54 +04:00
|
|
|
}
|
|
|
|
|
2008-05-17 16:44:42 +04:00
|
|
|
void HttpConnection::read()
|
|
|
|
{
|
2009-08-21 18:05:03 +04:00
|
|
|
QByteArray input = socket->readAll();
|
|
|
|
/*qDebug(" -------");
|
|
|
|
qDebug("|REQUEST|");
|
|
|
|
qDebug(" -------"); */
|
|
|
|
//qDebug("%s", input.toAscii().constData());
|
|
|
|
if(input.size() > 100000) {
|
|
|
|
qDebug("Request too big");
|
|
|
|
generator.setStatusLine(400, "Bad Request");
|
|
|
|
write();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
parser.write(input);
|
|
|
|
if(parser.isError())
|
|
|
|
{
|
|
|
|
generator.setStatusLine(400, "Bad Request");
|
|
|
|
write();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
if (parser.isParsable())
|
|
|
|
respond();
|
2008-05-17 16:44:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HttpConnection::write()
|
|
|
|
{
|
2009-08-21 18:05:03 +04:00
|
|
|
QByteArray output = generator.toByteArray();
|
|
|
|
/*qDebug(" --------");
|
|
|
|
qDebug("|RESPONSE|");
|
|
|
|
qDebug(" --------");
|
|
|
|
qDebug()<<output;*/
|
|
|
|
socket->write(output);
|
|
|
|
socket->disconnectFromHost();
|
2008-05-17 16:44:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HttpConnection::respond()
|
|
|
|
{
|
2009-08-21 18:05:03 +04:00
|
|
|
//qDebug("Respond called");
|
|
|
|
QStringList auth = parser.value("Authorization").split(" ", QString::SkipEmptyParts);
|
2009-07-12 12:13:00 +04:00
|
|
|
if (auth.size() != 2 || QString::compare(auth[0], "Basic", Qt::CaseInsensitive) != 0 || !parent->isAuthorized(auth[1].toLocal8Bit()))
|
2009-08-21 18:05:03 +04:00
|
|
|
{
|
|
|
|
generator.setStatusLine(401, "Unauthorized");
|
|
|
|
generator.setValue("WWW-Authenticate", "Basic realm=\"you know what\"");
|
|
|
|
write();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QString url = parser.url();
|
|
|
|
QStringList list = url.split('/', QString::SkipEmptyParts);
|
|
|
|
if (list.contains(".") || list.contains(".."))
|
|
|
|
{
|
|
|
|
respondNotFound();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (list.size() == 0)
|
|
|
|
list.append("index.html");
|
|
|
|
if (list.size() == 2)
|
|
|
|
{
|
|
|
|
if (list[0] == "json")
|
|
|
|
{
|
|
|
|
if (list[1] == "events")
|
|
|
|
{
|
|
|
|
respondJson();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (list[0] == "command")
|
|
|
|
{
|
|
|
|
QString command = list[1];
|
|
|
|
respondCommand(command);
|
|
|
|
generator.setStatusLine(200, "OK");
|
|
|
|
write();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (list[0] == "images")
|
|
|
|
list[0] = "Icons";
|
|
|
|
else
|
|
|
|
list.prepend("webui");
|
|
|
|
url = ":/" + list.join("/");
|
|
|
|
QFile file(url);
|
|
|
|
if(!file.open(QIODevice::ReadOnly))
|
|
|
|
{
|
|
|
|
respondNotFound();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QString ext = list.last();
|
|
|
|
int index = ext.lastIndexOf('.') + 1;
|
|
|
|
if (index > 0)
|
|
|
|
ext.remove(0, index);
|
|
|
|
else
|
|
|
|
ext.clear();
|
|
|
|
QByteArray data = file.readAll();
|
|
|
|
generator.setStatusLine(200, "OK");
|
|
|
|
generator.setContentTypeByExt(ext);
|
|
|
|
generator.setMessage(data);
|
|
|
|
write();
|
2008-05-17 16:44:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HttpConnection::respondNotFound()
|
|
|
|
{
|
2009-08-21 18:05:03 +04:00
|
|
|
generator.setStatusLine(404, "File not found");
|
|
|
|
write();
|
2008-05-17 16:44:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HttpConnection::respondJson()
|
|
|
|
{
|
2009-08-21 18:05:03 +04:00
|
|
|
EventManager* manager = parent->eventManager();
|
|
|
|
QString string = json::toJson(manager->getEventList());
|
|
|
|
generator.setStatusLine(200, "OK");
|
|
|
|
generator.setContentTypeByExt("js");
|
|
|
|
generator.setMessage(string);
|
|
|
|
write();
|
2008-05-17 16:44:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void HttpConnection::respondCommand(QString command)
|
|
|
|
{
|
2009-08-21 18:05:03 +04:00
|
|
|
if(command == "download")
|
|
|
|
{
|
|
|
|
QString urls = parser.post("urls");
|
|
|
|
QStringList list = urls.split('\n');
|
|
|
|
foreach(QString url, list){
|
|
|
|
url = url.trimmed();
|
|
|
|
if(!url.isEmpty()){
|
|
|
|
if(url.startsWith("magnet:", Qt::CaseInsensitive)) {
|
|
|
|
emit MagnetReadyToBeDownloaded(url);
|
|
|
|
} else {
|
|
|
|
qDebug("Downloading url: %s", (const char*)url.toLocal8Bit());
|
|
|
|
emit UrlReadyToBeDownloaded(url);
|
2008-12-30 01:46:18 +03:00
|
|
|
}
|
2009-08-21 18:05:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(command == "upload")
|
|
|
|
{
|
|
|
|
QByteArray torrentfile = parser.torrent();
|
2009-08-24 07:38:31 +04:00
|
|
|
// Get a unique filename
|
2009-08-21 18:05:03 +04:00
|
|
|
QString filePath;
|
2009-08-24 07:38:31 +04:00
|
|
|
QTemporaryFile tmpfile;
|
|
|
|
tmpfile.setAutoRemove(false);
|
|
|
|
if (tmpfile.open()) {
|
|
|
|
filePath = tmpfile.fileName();
|
|
|
|
} else {
|
|
|
|
std::cerr << "I/O Error: Could not create temporary file" << std::endl;
|
|
|
|
return;
|
2009-08-21 18:05:03 +04:00
|
|
|
}
|
2009-08-24 07:38:31 +04:00
|
|
|
tmpfile.close();
|
|
|
|
// Now temporary file is created but closed so that it can be used.
|
|
|
|
// write torrent to temporary file
|
2009-08-21 18:05:03 +04:00
|
|
|
QFile torrent(filePath);
|
|
|
|
if(torrent.open(QIODevice::WriteOnly)) {
|
|
|
|
torrent.write(torrentfile);
|
|
|
|
torrent.close();
|
|
|
|
}
|
|
|
|
emit torrentReadyToBeDownloaded(filePath, false, QString(), false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(command == "resumeall") {
|
|
|
|
emit resumeAllTorrents();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(command == "pauseall") {
|
|
|
|
emit pauseAllTorrents();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(command == "resume") {
|
|
|
|
emit resumeTorrent(parser.post("hash"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(command == "pause") {
|
|
|
|
emit pauseTorrent(parser.post("hash"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(command == "delete") {
|
|
|
|
emit deleteTorrent(parser.post("hash"), false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(command == "deletePerm") {
|
|
|
|
emit deleteTorrent(parser.post("hash"), true);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(command == "increasePrio") {
|
2009-11-17 11:15:26 +03:00
|
|
|
QTorrentHandle h = BTSession->getTorrentHandle(parser.post("hash"));
|
|
|
|
if(h.is_valid()) h.queue_position_up();
|
2009-08-21 18:05:03 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(command == "decreasePrio") {
|
2009-11-17 11:15:26 +03:00
|
|
|
QTorrentHandle h = BTSession->getTorrentHandle(parser.post("hash"));
|
|
|
|
if(h.is_valid()) h.queue_position_down();
|
2009-08-21 18:05:03 +04:00
|
|
|
return;
|
|
|
|
}
|
2009-11-17 11:15:26 +03:00
|
|
|
if(command == "recheck"){
|
|
|
|
recheckTorrent(parser.post("hash"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(command == "recheckall"){
|
|
|
|
recheckAllTorrents();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpConnection::recheckTorrent(QString hash) {
|
|
|
|
QTorrentHandle h = BTSession->getTorrentHandle(hash);
|
|
|
|
if(h.is_valid() && !h.is_paused()){
|
|
|
|
h.force_recheck();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HttpConnection::recheckAllTorrents() {
|
|
|
|
std::vector<torrent_handle> torrents = BTSession->getTorrents();
|
|
|
|
std::vector<torrent_handle>::iterator torrentIT;
|
|
|
|
for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) {
|
|
|
|
QTorrentHandle h = QTorrentHandle(*torrentIT);
|
|
|
|
if(h.is_valid() && !h.is_paused())
|
|
|
|
h.force_recheck();
|
|
|
|
}
|
2008-05-17 16:44:42 +04:00
|
|
|
}
|