Browse Source

Merge pull request #2153 from pmzqla/webui-versioninfo

WebUI: Allow to retrieve web API version and qBT version
adaptive-webui-19844
sledgehammer999 10 years ago
parent
commit
060d3fc5b9
  1. 146
      src/webui/requesthandler.cpp
  2. 3
      src/webui/requesthandler.h

146
src/webui/requesthandler.cpp

@ -51,6 +51,9 @@
using namespace libtorrent; using namespace libtorrent;
static const int API_VERSION = 2;
static const int API_VERSION_MIN = 2;
const QString WWW_FOLDER = ":/www/public/"; const QString WWW_FOLDER = ":/www/public/";
const QString PRIVATE_FOLDER = ":/www/private/"; const QString PRIVATE_FOLDER = ":/www/private/";
const QString DEFAULT_SCOPE = "public"; const QString DEFAULT_SCOPE = "public";
@ -58,6 +61,7 @@ const QString SCOPE_IMAGES = "images";
const QString SCOPE_THEME = "theme"; const QString SCOPE_THEME = "theme";
const QString DEFAULT_ACTION = "index"; const QString DEFAULT_ACTION = "index";
const QString WEBUI_ACTION = "webui"; const QString WEBUI_ACTION = "webui";
const QString VERSION_INFO = "version";
#define ADD_ACTION(scope, action) actions[#scope][#action] = &RequestHandler::action_##scope##_##action #define ADD_ACTION(scope, action) actions[#scope][#action] = &RequestHandler::action_##scope##_##action
@ -102,6 +106,9 @@ QMap<QString, QMap<QString, RequestHandler::Action> > RequestHandler::initialize
ADD_ACTION(command, topPrio); ADD_ACTION(command, topPrio);
ADD_ACTION(command, bottomPrio); ADD_ACTION(command, bottomPrio);
ADD_ACTION(command, recheck); ADD_ACTION(command, recheck);
ADD_ACTION(version, api);
ADD_ACTION(version, api_min);
ADD_ACTION(version, qbittorrent);
return actions; return actions;
} }
@ -109,8 +116,8 @@ QMap<QString, QMap<QString, RequestHandler::Action> > RequestHandler::initialize
void RequestHandler::action_public_index() void RequestHandler::action_public_index()
{ {
QString path; QString path;
if (!args_.isEmpty())
{ if (!args_.isEmpty()) {
if (args_.back() == "favicon.ico") if (args_.back() == "favicon.ico")
path = ":/Icons/skin/qbittorrent16.png"; path = ":/Icons/skin/qbittorrent16.png";
else else
@ -139,13 +146,11 @@ void RequestHandler::action_public_login()
bool equalUser = misc::slowEquals(request().posts["username"].toUtf8(), pref->getWebUiUsername().toUtf8()); bool equalUser = misc::slowEquals(request().posts["username"].toUtf8(), pref->getWebUiUsername().toUtf8());
bool equalPass = misc::slowEquals(pass.toUtf8(), pref->getWebUiPassword().toUtf8()); bool equalPass = misc::slowEquals(pass.toUtf8(), pref->getWebUiPassword().toUtf8());
if (equalUser && equalPass) if (equalUser && equalPass) {
{
sessionStart(); sessionStart();
print(QByteArray("Ok."), CONTENT_TYPE_TXT); print(QByteArray("Ok."), CONTENT_TYPE_TXT);
} }
else else {
{
QString addr = env().clientAddress.toString(); QString addr = env().clientAddress.toString();
increaseFailedAttempts(); increaseFailedAttempts();
qDebug("client IP: %s (%d failed attempts)", qPrintable(addr), failedAttempts()); qDebug("client IP: %s (%d failed attempts)", qPrintable(addr), failedAttempts());
@ -160,8 +165,7 @@ void RequestHandler::action_public_logout()
void RequestHandler::action_public_theme() void RequestHandler::action_public_theme()
{ {
if (args_.size() != 1) if (args_.size() != 1) {
{
status(404, "Not Found"); status(404, "Not Found");
return; return;
} }
@ -212,6 +216,21 @@ void RequestHandler::action_json_propertiesFiles()
print(btjson::getFilesForTorrent(args_.front()), CONTENT_TYPE_JS); print(btjson::getFilesForTorrent(args_.front()), CONTENT_TYPE_JS);
} }
void RequestHandler::action_version_api()
{
print(QString::number(API_VERSION), CONTENT_TYPE_TXT);
}
void RequestHandler::action_version_api_min()
{
print(QString::number(API_VERSION_MIN), CONTENT_TYPE_TXT);
}
void RequestHandler::action_version_qbittorrent()
{
print(QString(VERSION), CONTENT_TYPE_TXT);
}
void RequestHandler::action_command_shutdown() void RequestHandler::action_command_shutdown()
{ {
qDebug() << "Shutdown request from Web UI"; qDebug() << "Shutdown request from Web UI";
@ -227,22 +246,17 @@ void RequestHandler::action_command_download()
QString urls = request().posts["urls"]; QString urls = request().posts["urls"];
QStringList list = urls.split('\n'); QStringList list = urls.split('\n');
foreach (QString url, list) foreach (QString url, list) {
{
url = url.trimmed(); url = url.trimmed();
if (!url.isEmpty()) if (!url.isEmpty()) {
{ if (url.startsWith("bc://bt/", Qt::CaseInsensitive)) {
if (url.startsWith("bc://bt/", Qt::CaseInsensitive))
{
qDebug("Converting bc link to magnet link"); qDebug("Converting bc link to magnet link");
url = misc::bcLinkToMagnet(url); url = misc::bcLinkToMagnet(url);
} }
else if (url.startsWith("magnet:", Qt::CaseInsensitive)) else if (url.startsWith("magnet:", Qt::CaseInsensitive)) {
{
QBtSession::instance()->addMagnetSkipAddDlg(url); QBtSession::instance()->addMagnetSkipAddDlg(url);
} }
else else {
{
qDebug("Downloading url: %s", qPrintable(url)); qDebug("Downloading url: %s", qPrintable(url));
QBtSession::instance()->downloadUrlAndSkipDialog(url); QBtSession::instance()->downloadUrlAndSkipDialog(url);
} }
@ -254,12 +268,10 @@ void RequestHandler::action_command_upload()
{ {
qDebug() << Q_FUNC_INFO; qDebug() << Q_FUNC_INFO;
foreach(const UploadedFile& torrent, request().files) foreach(const UploadedFile& torrent, request().files) {
{
QString filePath = saveTmpFile(torrent.data); QString filePath = saveTmpFile(torrent.data);
if (!filePath.isEmpty()) if (!filePath.isEmpty()) {
{
QTorrentHandle h = QBtSession::instance()->addTorrent(filePath); QTorrentHandle h = QBtSession::instance()->addTorrent(filePath);
if (!h.is_valid()) { if (!h.is_valid()) {
status(415, "Internal Server Error"); status(415, "Internal Server Error");
@ -268,8 +280,7 @@ void RequestHandler::action_command_upload()
// Clean up // Clean up
fsutils::forceRemove(filePath); fsutils::forceRemove(filePath);
} }
else else {
{
qWarning() << "I/O Error: Could not create temporary file"; qWarning() << "I/O Error: Could not create temporary file";
status(500, "Internal Server Error"); status(500, "Internal Server Error");
print(QObject::tr("I/O Error: Could not create temporary file."), CONTENT_TYPE_TXT); print(QObject::tr("I/O Error: Could not create temporary file."), CONTENT_TYPE_TXT);
@ -281,17 +292,14 @@ void RequestHandler::action_command_addTrackers()
{ {
QString hash = request().posts["hash"]; QString hash = request().posts["hash"];
if (!hash.isEmpty()) if (!hash.isEmpty()) {
{
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (h.is_valid() && h.has_metadata()) if (h.is_valid() && h.has_metadata()) {
{
QString urls = request().posts["urls"]; QString urls = request().posts["urls"];
QStringList list = urls.split('\n'); QStringList list = urls.split('\n');
foreach (const QString& url, list) foreach (const QString& url, list) {
{
announce_entry e(url.toStdString()); announce_entry e(url.toStdString());
h.add_tracker(e); h.add_tracker(e);
} }
@ -332,10 +340,8 @@ void RequestHandler::action_command_setFilePrio()
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (h.is_valid() && h.has_metadata()) if (h.is_valid() && h.has_metadata())
{
h.file_priority(file_id, priority); h.file_priority(file_id, priority);
} }
}
void RequestHandler::action_command_getGlobalUpLimit() void RequestHandler::action_command_getGlobalUpLimit()
{ {
@ -371,10 +377,8 @@ void RequestHandler::action_command_getTorrentUpLimit()
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (h.is_valid()) if (h.is_valid())
{
print(QByteArray::number(h.upload_limit())); print(QByteArray::number(h.upload_limit()));
} }
}
void RequestHandler::action_command_getTorrentDlLimit() void RequestHandler::action_command_getTorrentDlLimit()
{ {
@ -382,10 +386,8 @@ void RequestHandler::action_command_getTorrentDlLimit()
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (h.is_valid()) if (h.is_valid())
{
print(QByteArray::number(h.download_limit())); print(QByteArray::number(h.download_limit()));
} }
}
void RequestHandler::action_command_setTorrentUpLimit() void RequestHandler::action_command_setTorrentUpLimit()
{ {
@ -395,10 +397,8 @@ void RequestHandler::action_command_setTorrentUpLimit()
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (h.is_valid()) if (h.is_valid())
{
h.set_upload_limit(limit); h.set_upload_limit(limit);
} }
}
void RequestHandler::action_command_setTorrentDlLimit() void RequestHandler::action_command_setTorrentDlLimit()
{ {
@ -408,59 +408,46 @@ void RequestHandler::action_command_setTorrentDlLimit()
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (h.is_valid()) if (h.is_valid())
{
h.set_download_limit(limit); h.set_download_limit(limit);
} }
}
void RequestHandler::action_command_delete() void RequestHandler::action_command_delete()
{ {
QStringList hashes = request().posts["hashes"].split("|"); QStringList hashes = request().posts["hashes"].split("|");
foreach (const QString &hash, hashes) foreach (const QString &hash, hashes)
{
QBtSession::instance()->deleteTorrent(hash, false); QBtSession::instance()->deleteTorrent(hash, false);
} }
}
void RequestHandler::action_command_deletePerm() void RequestHandler::action_command_deletePerm()
{ {
QStringList hashes = request().posts["hashes"].split("|"); QStringList hashes = request().posts["hashes"].split("|");
foreach (const QString &hash, hashes) foreach (const QString &hash, hashes)
{
QBtSession::instance()->deleteTorrent(hash, true); QBtSession::instance()->deleteTorrent(hash, true);
} }
}
void RequestHandler::action_command_increasePrio() void RequestHandler::action_command_increasePrio()
{ {
QStringList hashes = request().posts["hashes"].split("|"); QStringList hashes = request().posts["hashes"].split("|");
std::priority_queue<QPair<int, QTorrentHandle>, std::priority_queue<QPair<int, QTorrentHandle>,
std::vector<QPair<int, QTorrentHandle> >, std::vector<QPair<int, QTorrentHandle> >,
std::greater<QPair<int, QTorrentHandle> > > torrent_queue; std::greater<QPair<int, QTorrentHandle> > > torrent_queue;
// Sort torrents by priority // Sort torrents by priority
foreach (const QString &hash, hashes) foreach (const QString &hash, hashes) {
{ try {
try
{
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (!h.is_seed()) if (!h.is_seed())
{
torrent_queue.push(qMakePair(h.queue_position(), h)); torrent_queue.push(qMakePair(h.queue_position(), h));
} }
}
catch(invalid_handle&) {} catch(invalid_handle&) {}
} }
// Increase torrents priority (starting with the ones with highest priority) // Increase torrents priority (starting with the ones with highest priority)
while(!torrent_queue.empty()) while(!torrent_queue.empty()) {
{
QTorrentHandle h = torrent_queue.top().second; QTorrentHandle h = torrent_queue.top().second;
try try {
{
h.queue_position_up(); h.queue_position_up();
} }
catch(invalid_handle&) {} catch(invalid_handle&) {}
@ -472,32 +459,27 @@ void RequestHandler::action_command_increasePrio()
void RequestHandler::action_command_decreasePrio() void RequestHandler::action_command_decreasePrio()
{ {
QStringList hashes = request().posts["hashes"].split("|"); QStringList hashes = request().posts["hashes"].split("|");
std::priority_queue<QPair<int, QTorrentHandle>, std::priority_queue<QPair<int, QTorrentHandle>,
std::vector<QPair<int, QTorrentHandle> >, std::vector<QPair<int, QTorrentHandle> >,
std::less<QPair<int, QTorrentHandle> > > torrent_queue; std::less<QPair<int, QTorrentHandle> > > torrent_queue;
// Sort torrents by priority // Sort torrents by priority
foreach (const QString &hash, hashes) foreach (const QString &hash, hashes) {
{ try {
try
{
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (!h.is_seed()) if (!h.is_seed())
{
torrent_queue.push(qMakePair(h.queue_position(), h)); torrent_queue.push(qMakePair(h.queue_position(), h));
} }
}
catch(invalid_handle&) {} catch(invalid_handle&) {}
} }
// Decrease torrents priority (starting with the ones with lowest priority) // Decrease torrents priority (starting with the ones with lowest priority)
while(!torrent_queue.empty()) while(!torrent_queue.empty()) {
{
QTorrentHandle h = torrent_queue.top().second; QTorrentHandle h = torrent_queue.top().second;
try try {
{
h.queue_position_down(); h.queue_position_down();
} }
catch(invalid_handle&) {} catch(invalid_handle&) {}
@ -508,8 +490,7 @@ void RequestHandler::action_command_decreasePrio()
void RequestHandler::action_command_topPrio() void RequestHandler::action_command_topPrio()
{ {
foreach (const QString &hash, request().posts["hashes"].split("|")) foreach (const QString &hash, request().posts["hashes"].split("|")) {
{
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (h.is_valid()) h.queue_position_top(); if (h.is_valid()) h.queue_position_top();
} }
@ -517,8 +498,7 @@ void RequestHandler::action_command_topPrio()
void RequestHandler::action_command_bottomPrio() void RequestHandler::action_command_bottomPrio()
{ {
foreach (const QString &hash, request().posts["hashes"].split("|")) foreach (const QString &hash, request().posts["hashes"].split("|")) {
{
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (h.is_valid()) h.queue_position_bottom(); if (h.is_valid()) h.queue_position_bottom();
} }
@ -531,30 +511,26 @@ void RequestHandler::action_command_recheck()
bool RequestHandler::isPublicScope() bool RequestHandler::isPublicScope()
{ {
return (scope_ == DEFAULT_SCOPE); return (scope_ == DEFAULT_SCOPE || scope_ == VERSION_INFO);
} }
void RequestHandler::processRequest() void RequestHandler::processRequest()
{ {
if (args_.contains(".") || args_.contains("..")) if (args_.contains(".") || args_.contains("..")) {
{
qDebug() << Q_FUNC_INFO << "Invalid path:" << request().path; qDebug() << Q_FUNC_INFO << "Invalid path:" << request().path;
status(404, "Not Found"); status(404, "Not Found");
return; return;
} }
if (!isPublicScope() && !sessionActive()) if (!isPublicScope() && !sessionActive()) {
{
status(403, "Forbidden"); status(403, "Forbidden");
return; return;
} }
if (actions_.value(scope_).value(action_) != 0) if (actions_.value(scope_).value(action_) != 0) {
{
(this->*(actions_[scope_][action_]))(); (this->*(actions_[scope_][action_]))();
} }
else else {
{
status(404, "Not Found"); status(404, "Not Found");
qDebug() << Q_FUNC_INFO << "Resource not found:" << request().path; qDebug() << Q_FUNC_INFO << "Resource not found:" << request().path;
} }
@ -566,19 +542,15 @@ void RequestHandler::parsePath()
// check action for requested path // check action for requested path
QStringList pathItems = request().path.split('/', QString::SkipEmptyParts); QStringList pathItems = request().path.split('/', QString::SkipEmptyParts);
if (!pathItems.empty()) if (!pathItems.empty()) {
{ if (actions_.contains(pathItems.front())) {
if (actions_.contains(pathItems.front()))
{
scope_ = pathItems.front(); scope_ = pathItems.front();
pathItems.pop_front(); pathItems.pop_front();
} }
} }
if (!pathItems.empty()) if (!pathItems.empty()) {
{ if (actions_[scope_].contains(pathItems.front())) {
if (actions_[scope_].contains(pathItems.front()))
{
action_ = pathItems.front(); action_ = pathItems.front();
pathItems.pop_front(); pathItems.pop_front();
} }

3
src/webui/requesthandler.h

@ -81,6 +81,9 @@ private:
void action_command_topPrio(); void action_command_topPrio();
void action_command_bottomPrio(); void action_command_bottomPrio();
void action_command_recheck(); void action_command_recheck();
void action_version_api();
void action_version_api_min();
void action_version_qbittorrent();
typedef void (RequestHandler::*Action)(); typedef void (RequestHandler::*Action)();

Loading…
Cancel
Save