diff --git a/src/httpconnection.cpp b/src/httpconnection.cpp index 4f9537a6f..06b11b194 100644 --- a/src/httpconnection.cpp +++ b/src/httpconnection.cpp @@ -30,6 +30,7 @@ #include #include #include +#include HttpConnection::HttpConnection(QTcpSocket *socket, HttpServer *parent) : QObject(parent), socket(socket), parent(parent) @@ -45,11 +46,17 @@ HttpConnection::~HttpConnection() void HttpConnection::read() { - QString input = socket->readAll(); + QByteArray input = socket->readAll(); qDebug(" -------"); qDebug("|REQUEST|"); qDebug(" -------"); - qDebug("%s", input.toAscii().constData()); + //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()) { @@ -74,6 +81,7 @@ void HttpConnection::write() void HttpConnection::respond() { + qDebug("Respond called"); QStringList auth = parser.value("Authorization").split(" ", QString::SkipEmptyParts); if (auth.size() != 2 || QString::compare(auth[0], "Basic", Qt::CaseInsensitive) != 0 || !parent->isAuthorized(auth[1].toUtf8())) { @@ -176,6 +184,25 @@ void HttpConnection::respondCommand(QString command) emit urlsReadyToBeDownloaded(url_list_cleaned); return; } + if(command == "upload") + { + QByteArray torrentfile = parser.torrent(); + // XXX: Trick to get a unique filename + QString filePath; + QTemporaryFile *tmpfile = new QTemporaryFile(); + if (tmpfile->open()) { + filePath = tmpfile->fileName(); + } + delete tmpfile; + // write it to HD + 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(); diff --git a/src/httpconnection.h b/src/httpconnection.h index 9ca4b65f7..a6ef630df 100644 --- a/src/httpconnection.h +++ b/src/httpconnection.h @@ -57,6 +57,7 @@ class HttpConnection : public QObject signals: void urlsReadyToBeDownloaded(const QStringList&); + void torrentReadyToBeDownloaded(QString, bool, QString, bool); void deleteTorrent(QString hash); void resumeTorrent(QString hash); void pauseTorrent(QString hash); diff --git a/src/httprequestparser.cpp b/src/httprequestparser.cpp index 069836f63..d2c04f591 100644 --- a/src/httprequestparser.cpp +++ b/src/httprequestparser.cpp @@ -49,11 +49,11 @@ QString HttpRequestParser::url() const return path; } -QString HttpRequestParser::message() const +QByteArray HttpRequestParser::message() const { if(isParsable()) return data; - return QString(); + return QByteArray(); } QString HttpRequestParser::get(const QString key) const @@ -66,7 +66,12 @@ QString HttpRequestParser::post(const QString key) const return postMap[key]; } -void HttpRequestParser::write(QString str) +QByteArray HttpRequestParser::torrent() const +{ + return torrent_content; +} + +void HttpRequestParser::write(QByteArray str) { while (!headerDone && str.size()>0) { @@ -111,7 +116,7 @@ void HttpRequestParser::write(QString str) if(contentType() == "application/x-www-form-urlencoded") { QUrl url; - url.setEncodedQuery(data.toAscii()); + url.setEncodedQuery(data); QListIterator > i(url.queryItems()); while (i.hasNext()) { @@ -120,9 +125,15 @@ void HttpRequestParser::write(QString str) qDebug() << pair.first << "=" << post(pair.first); } } + if(contentType() == "multipart/form-data") + { + //qDebug() << data.right(data.size()-data.indexOf("\r\n\r\n")-QByteArray("\r\n\r\n").size()); + torrent_content = data.right(data.size()-data.indexOf("\r\n\r\n")-QByteArray("\r\n\r\n").size()); + } } } else error = true; } + qDebug() << "isError: " << isError(); } diff --git a/src/httprequestparser.h b/src/httprequestparser.h index b0648b2be..1abb781e2 100644 --- a/src/httprequestparser.h +++ b/src/httprequestparser.h @@ -30,10 +30,11 @@ class HttpRequestParser : public QHttpRequestHeader bool headerDone; bool messageDone; bool error; - QString data; + QByteArray data; QString path; QMap postMap; QMap getMap; + QByteArray torrent_content; public: HttpRequestParser(); @@ -41,10 +42,11 @@ class HttpRequestParser : public QHttpRequestHeader bool isParsable() const; bool isError() const; QString url() const; - QString message() const; + QByteArray message() const; QString get(const QString key) const; QString post(const QString key) const; - void write(QString str); + QByteArray torrent() const; + void write(QByteArray str); }; #endif diff --git a/src/httpserver.cpp b/src/httpserver.cpp index 2478bff82..38d7e650e 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -62,6 +62,7 @@ void HttpServer::newHttpConnection() HttpConnection *connection = new HttpConnection(socket, this); //connect connection to BTSession connect(connection, SIGNAL(urlsReadyToBeDownloaded(const QStringList&)), BTSession, SLOT(downloadFromURLList(const QStringList&))); + connect(connection, SIGNAL(torrentReadyToBeDownloaded(QString, bool, QString, bool)), BTSession, SLOT(addTorrent(QString, bool, QString, bool))); connect(connection, SIGNAL(deleteTorrent(QString)), BTSession, SLOT(deleteTorrent(QString))); connect(connection, SIGNAL(pauseTorrent(QString)), BTSession, SLOT(pauseTorrent(QString))); connect(connection, SIGNAL(resumeTorrent(QString)), BTSession, SLOT(resumeTorrent(QString))); diff --git a/src/webui.qrc b/src/webui.qrc index cab1dbf5b..9927a7359 100644 --- a/src/webui.qrc +++ b/src/webui.qrc @@ -2,6 +2,7 @@ webui/index.html webui/download.html + webui/upload.html webui/about.html webui/css/mocha.css webui/css/dynamicTable.css diff --git a/src/webui/index.html b/src/webui/index.html index 13ec0a952..0e9901a3c 100644 --- a/src/webui/index.html +++ b/src/webui/index.html @@ -27,6 +27,7 @@ File
  • @@ -52,6 +53,7 @@
    + diff --git a/src/webui/scripts/mocha-events.js b/src/webui/scripts/mocha-events.js index 42d4a65c9..8ea7004d6 100644 --- a/src/webui/scripts/mocha-events.js +++ b/src/webui/scripts/mocha-events.js @@ -35,6 +35,23 @@ function attachMochaLinkEvents(){ height: 270 }); }); + + addClickEvent('upload', function(e){ + new Event(e).stop(); + document.mochaUI.newWindow({ + id: 'uploadPage', + title: 'Upload torrent file', + loadMethod: 'iframe', + contentURL:'upload.html', + scrollbars: false, + resizable: false, + maximizable: false, + paddingVertical: 0, + paddingHorizontal: 0, + width: 500, + height: 120 + }); + }); addClickEvent('delete', function(e){ new Event(e).stop(); diff --git a/src/webui/upload.html b/src/webui/upload.html new file mode 100644 index 000000000..81e54b7d1 --- /dev/null +++ b/src/webui/upload.html @@ -0,0 +1,21 @@ + + + + + Upload local torrent file + + + + + + +
    +

    Download local torrent

    +
    + +

    Point to torrent file

    + Download +
    +
    + +