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. 632
      src/webui/requesthandler.cpp
  2. 103
      src/webui/requesthandler.h

632
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,539 +61,508 @@ 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
QMap<QString, QMap<QString, RequestHandler::Action> > RequestHandler::initializeActions() QMap<QString, QMap<QString, RequestHandler::Action> > RequestHandler::initializeActions()
{ {
QMap<QString,QMap<QString, RequestHandler::Action> > actions; QMap<QString,QMap<QString, RequestHandler::Action> > actions;
ADD_ACTION(public, webui); ADD_ACTION(public, webui);
ADD_ACTION(public, index); ADD_ACTION(public, index);
ADD_ACTION(public, login); ADD_ACTION(public, login);
ADD_ACTION(public, logout); ADD_ACTION(public, logout);
ADD_ACTION(public, theme); ADD_ACTION(public, theme);
ADD_ACTION(public, images); ADD_ACTION(public, images);
ADD_ACTION(json, torrents); ADD_ACTION(json, torrents);
ADD_ACTION(json, preferences); ADD_ACTION(json, preferences);
ADD_ACTION(json, transferInfo); ADD_ACTION(json, transferInfo);
ADD_ACTION(json, propertiesGeneral); ADD_ACTION(json, propertiesGeneral);
ADD_ACTION(json, propertiesTrackers); ADD_ACTION(json, propertiesTrackers);
ADD_ACTION(json, propertiesFiles); ADD_ACTION(json, propertiesFiles);
ADD_ACTION(command, shutdown); ADD_ACTION(command, shutdown);
ADD_ACTION(command, download); ADD_ACTION(command, download);
ADD_ACTION(command, upload); ADD_ACTION(command, upload);
ADD_ACTION(command, addTrackers); ADD_ACTION(command, addTrackers);
ADD_ACTION(command, resumeAll); ADD_ACTION(command, resumeAll);
ADD_ACTION(command, pauseAll); ADD_ACTION(command, pauseAll);
ADD_ACTION(command, resume); ADD_ACTION(command, resume);
ADD_ACTION(command, pause); ADD_ACTION(command, pause);
ADD_ACTION(command, setPreferences); ADD_ACTION(command, setPreferences);
ADD_ACTION(command, setFilePrio); ADD_ACTION(command, setFilePrio);
ADD_ACTION(command, getGlobalUpLimit); ADD_ACTION(command, getGlobalUpLimit);
ADD_ACTION(command, getGlobalDlLimit); ADD_ACTION(command, getGlobalDlLimit);
ADD_ACTION(command, setGlobalUpLimit); ADD_ACTION(command, setGlobalUpLimit);
ADD_ACTION(command, setGlobalDlLimit); ADD_ACTION(command, setGlobalDlLimit);
ADD_ACTION(command, getTorrentUpLimit); ADD_ACTION(command, getTorrentUpLimit);
ADD_ACTION(command, getTorrentDlLimit); ADD_ACTION(command, getTorrentDlLimit);
ADD_ACTION(command, setTorrentUpLimit); ADD_ACTION(command, setTorrentUpLimit);
ADD_ACTION(command, setTorrentDlLimit); ADD_ACTION(command, setTorrentDlLimit);
ADD_ACTION(command, delete); ADD_ACTION(command, delete);
ADD_ACTION(command, deletePerm); ADD_ACTION(command, deletePerm);
ADD_ACTION(command, increasePrio); ADD_ACTION(command, increasePrio);
ADD_ACTION(command, decreasePrio); ADD_ACTION(command, decreasePrio);
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);
return actions; ADD_ACTION(version, api_min);
ADD_ACTION(version, qbittorrent);
return actions;
} }
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
path = WWW_FOLDER + args_.join("/"); path = WWW_FOLDER + args_.join("/");
} }
printFile(path); printFile(path);
} }
void RequestHandler::action_public_webui() void RequestHandler::action_public_webui()
{ {
if (!sessionActive()) if (!sessionActive())
printFile(PRIVATE_FOLDER + "login.html"); printFile(PRIVATE_FOLDER + "login.html");
else else
printFile(PRIVATE_FOLDER + "index.html"); printFile(PRIVATE_FOLDER + "index.html");
} }
void RequestHandler::action_public_login() void RequestHandler::action_public_login()
{ {
const Preferences* const pref = Preferences::instance(); const Preferences* const pref = Preferences::instance();
QCryptographicHash md5(QCryptographicHash::Md5); QCryptographicHash md5(QCryptographicHash::Md5);
md5.addData(request().posts["password"].toLocal8Bit()); md5.addData(request().posts["password"].toLocal8Bit());
QString pass = md5.result().toHex(); QString pass = md5.result().toHex();
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();
{ increaseFailedAttempts();
QString addr = env().clientAddress.toString(); qDebug("client IP: %s (%d failed attempts)", qPrintable(addr), failedAttempts());
increaseFailedAttempts(); print(QByteArray("Fails."), CONTENT_TYPE_TXT);
qDebug("client IP: %s (%d failed attempts)", qPrintable(addr), failedAttempts()); }
print(QByteArray("Fails."), CONTENT_TYPE_TXT);
}
} }
void RequestHandler::action_public_logout() void RequestHandler::action_public_logout()
{ {
sessionEnd(); sessionEnd();
} }
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; }
}
#ifdef DISABLE_GUI #ifdef DISABLE_GUI
QString url = ":/Icons/oxygen/" + args_.front() + ".png"; QString url = ":/Icons/oxygen/" + args_.front() + ".png";
#else #else
QString url = IconProvider::instance()->getIconPath(args_.front()); QString url = IconProvider::instance()->getIconPath(args_.front());
#endif #endif
qDebug() << Q_FUNC_INFO << "There icon:" << url; qDebug() << Q_FUNC_INFO << "There icon:" << url;
printFile(url); printFile(url);
} }
void RequestHandler::action_public_images() void RequestHandler::action_public_images()
{ {
const QString path = ":/Icons/" + args_.join("/"); const QString path = ":/Icons/" + args_.join("/");
printFile(path); printFile(path);
} }
void RequestHandler::action_json_torrents() void RequestHandler::action_json_torrents()
{ {
print(btjson::getTorrents(), CONTENT_TYPE_JS); print(btjson::getTorrents(), CONTENT_TYPE_JS);
} }
void RequestHandler::action_json_preferences() void RequestHandler::action_json_preferences()
{ {
print(prefjson::getPreferences(), CONTENT_TYPE_JS); print(prefjson::getPreferences(), CONTENT_TYPE_JS);
} }
void RequestHandler::action_json_transferInfo() void RequestHandler::action_json_transferInfo()
{ {
print(btjson::getTransferInfo(), CONTENT_TYPE_JS); print(btjson::getTransferInfo(), CONTENT_TYPE_JS);
} }
void RequestHandler::action_json_propertiesGeneral() void RequestHandler::action_json_propertiesGeneral()
{ {
print(btjson::getPropertiesForTorrent(args_.front()), CONTENT_TYPE_JS); print(btjson::getPropertiesForTorrent(args_.front()), CONTENT_TYPE_JS);
} }
void RequestHandler::action_json_propertiesTrackers() void RequestHandler::action_json_propertiesTrackers()
{ {
print(btjson::getTrackersForTorrent(args_.front()), CONTENT_TYPE_JS); print(btjson::getTrackersForTorrent(args_.front()), CONTENT_TYPE_JS);
} }
void RequestHandler::action_json_propertiesFiles() 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";
// Special case handling for shutdown, we // Special case handling for shutdown, we
// need to reply to the Web UI before // need to reply to the Web UI before
// actually shutting down. // actually shutting down.
QTimer::singleShot(0, qApp, SLOT(quit())); QTimer::singleShot(0, qApp, SLOT(quit()));
} }
void RequestHandler::action_command_download() 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)) {
{ qDebug("Converting bc link to magnet link");
if (url.startsWith("bc://bt/", Qt::CaseInsensitive)) url = misc::bcLinkToMagnet(url);
{ }
qDebug("Converting bc link to magnet link"); else if (url.startsWith("magnet:", Qt::CaseInsensitive)) {
url = misc::bcLinkToMagnet(url); QBtSession::instance()->addMagnetSkipAddDlg(url);
} }
else if (url.startsWith("magnet:", Qt::CaseInsensitive)) else {
{ qDebug("Downloading url: %s", qPrintable(url));
QBtSession::instance()->addMagnetSkipAddDlg(url); QBtSession::instance()->downloadUrlAndSkipDialog(url);
} }
else }
{
qDebug("Downloading url: %s", qPrintable(url));
QBtSession::instance()->downloadUrlAndSkipDialog(url);
}
} }
}
} }
void RequestHandler::action_command_upload() 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);
{ if (!h.is_valid()) {
QTorrentHandle h = QBtSession::instance()->addTorrent(filePath); status(415, "Internal Server Error");
if (!h.is_valid()) { print(QObject::tr("Error: '%1' is not a valid torrent file.\n").arg(torrent.filename), CONTENT_TYPE_TXT);
status(415, "Internal Server Error"); }
print(QObject::tr("Error: '%1' is not a valid torrent file.\n").arg(torrent.filename), CONTENT_TYPE_TXT); // Clean up
} fsutils::forceRemove(filePath);
// Clean up }
fsutils::forceRemove(filePath); else {
} qWarning() << "I/O Error: Could not create temporary file";
else status(500, "Internal Server Error");
{ print(QObject::tr("I/O Error: Could not create temporary file."), CONTENT_TYPE_TXT);
qWarning() << "I/O Error: Could not create temporary file"; }
status(500, "Internal Server Error");
print(QObject::tr("I/O Error: Could not create temporary file."), CONTENT_TYPE_TXT);
} }
}
} }
void RequestHandler::action_command_addTrackers() 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());
{ h.add_tracker(e);
announce_entry e(url.toStdString()); }
h.add_tracker(e); }
}
} }
}
} }
void RequestHandler::action_command_resumeAll() void RequestHandler::action_command_resumeAll()
{ {
QBtSession::instance()->resumeAllTorrents(); QBtSession::instance()->resumeAllTorrents();
} }
void RequestHandler::action_command_pauseAll() void RequestHandler::action_command_pauseAll()
{ {
QBtSession::instance()->pauseAllTorrents(); QBtSession::instance()->pauseAllTorrents();
} }
void RequestHandler::action_command_resume() void RequestHandler::action_command_resume()
{ {
QBtSession::instance()->resumeTorrent(request().posts["hash"]); QBtSession::instance()->resumeTorrent(request().posts["hash"]);
} }
void RequestHandler::action_command_pause() void RequestHandler::action_command_pause()
{ {
QBtSession::instance()->pauseTorrent(request().posts["hash"]); QBtSession::instance()->pauseTorrent(request().posts["hash"]);
} }
void RequestHandler::action_command_setPreferences() void RequestHandler::action_command_setPreferences()
{ {
prefjson::setPreferences(request().posts["json"]); prefjson::setPreferences(request().posts["json"]);
} }
void RequestHandler::action_command_setFilePrio() void RequestHandler::action_command_setFilePrio()
{ {
QString hash = request().posts["hash"]; QString hash = request().posts["hash"];
int file_id = request().posts["id"].toInt(); int file_id = request().posts["id"].toInt();
int priority = request().posts["priority"].toInt(); int priority = request().posts["priority"].toInt();
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()
{ {
print(QByteArray::number(QBtSession::instance()->getSession()->settings().upload_rate_limit)); print(QByteArray::number(QBtSession::instance()->getSession()->settings().upload_rate_limit));
} }
void RequestHandler::action_command_getGlobalDlLimit() void RequestHandler::action_command_getGlobalDlLimit()
{ {
print(QByteArray::number(QBtSession::instance()->getSession()->settings().download_rate_limit)); print(QByteArray::number(QBtSession::instance()->getSession()->settings().download_rate_limit));
} }
void RequestHandler::action_command_setGlobalUpLimit() void RequestHandler::action_command_setGlobalUpLimit()
{ {
qlonglong limit = request().posts["limit"].toLongLong(); qlonglong limit = request().posts["limit"].toLongLong();
if (limit == 0) limit = -1; if (limit == 0) limit = -1;
QBtSession::instance()->setUploadRateLimit(limit); QBtSession::instance()->setUploadRateLimit(limit);
Preferences::instance()->setGlobalUploadLimit(limit/1024.); Preferences::instance()->setGlobalUploadLimit(limit / 1024.);
} }
void RequestHandler::action_command_setGlobalDlLimit() void RequestHandler::action_command_setGlobalDlLimit()
{ {
qlonglong limit = request().posts["limit"].toLongLong(); qlonglong limit = request().posts["limit"].toLongLong();
if (limit == 0) limit = -1; if (limit == 0) limit = -1;
QBtSession::instance()->setDownloadRateLimit(limit); QBtSession::instance()->setDownloadRateLimit(limit);
Preferences::instance()->setGlobalDownloadLimit(limit/1024.); Preferences::instance()->setGlobalDownloadLimit(limit / 1024.);
} }
void RequestHandler::action_command_getTorrentUpLimit() void RequestHandler::action_command_getTorrentUpLimit()
{ {
QString hash = request().posts["hash"]; QString hash = request().posts["hash"];
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()
{ {
QString hash = request().posts["hash"]; QString hash = request().posts["hash"];
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()
{ {
QString hash = request().posts["hash"]; QString hash = request().posts["hash"];
qlonglong limit = request().posts["limit"].toLongLong(); qlonglong limit = request().posts["limit"].toLongLong();
if (limit == 0) limit = -1; if (limit == 0) limit = -1;
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()
{ {
QString hash = request().posts["hash"]; QString hash = request().posts["hash"];
qlonglong limit = request().posts["limit"].toLongLong(); qlonglong limit = request().posts["limit"].toLongLong();
if (limit == 0) limit = -1; if (limit == 0) limit = -1;
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::vector<QPair<int, QTorrentHandle> >,
std::greater<QPair<int, QTorrentHandle> > > torrent_queue;
// Sort torrents by priority
foreach (const QString &hash, hashes)
{
try
{
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (!h.is_seed())
{
torrent_queue.push(qMakePair(h.queue_position(), h));
}
}
catch(invalid_handle&) {}
}
// Increase torrents priority (starting with the ones with highest priority) std::priority_queue<QPair<int, QTorrentHandle>,
while(!torrent_queue.empty()) std::vector<QPair<int, QTorrentHandle> >,
{ std::greater<QPair<int, QTorrentHandle> > > torrent_queue;
QTorrentHandle h = torrent_queue.top().second;
try // Sort torrents by priority
{ foreach (const QString &hash, hashes) {
h.queue_position_up(); try {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (!h.is_seed())
torrent_queue.push(qMakePair(h.queue_position(), h));
}
catch(invalid_handle&) {}
} }
catch(invalid_handle&) {}
torrent_queue.pop(); // Increase torrents priority (starting with the ones with highest priority)
} while(!torrent_queue.empty()) {
QTorrentHandle h = torrent_queue.top().second;
try {
h.queue_position_up();
}
catch(invalid_handle&) {}
torrent_queue.pop();
}
} }
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::vector<QPair<int, QTorrentHandle> >,
std::less<QPair<int, QTorrentHandle> > > torrent_queue;
// Sort torrents by priority
foreach (const QString &hash, hashes)
{
try
{
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (!h.is_seed())
{
torrent_queue.push(qMakePair(h.queue_position(), h));
}
}
catch(invalid_handle&) {}
}
// Decrease torrents priority (starting with the ones with lowest priority) std::priority_queue<QPair<int, QTorrentHandle>,
while(!torrent_queue.empty()) std::vector<QPair<int, QTorrentHandle> >,
{ std::less<QPair<int, QTorrentHandle> > > torrent_queue;
QTorrentHandle h = torrent_queue.top().second;
try // Sort torrents by priority
{ foreach (const QString &hash, hashes) {
h.queue_position_down(); try {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if (!h.is_seed())
torrent_queue.push(qMakePair(h.queue_position(), h));
}
catch(invalid_handle&) {}
} }
catch(invalid_handle&) {}
torrent_queue.pop(); // Decrease torrents priority (starting with the ones with lowest priority)
} while(!torrent_queue.empty()) {
QTorrentHandle h = torrent_queue.top().second;
try {
h.queue_position_down();
}
catch(invalid_handle&) {}
torrent_queue.pop();
}
} }
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(); }
}
} }
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(); }
}
} }
void RequestHandler::action_command_recheck() void RequestHandler::action_command_recheck()
{ {
QBtSession::instance()->recheckTorrent(request().posts["hash"]); QBtSession::instance()->recheckTorrent(request().posts["hash"]);
} }
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");
{ return;
status(403, "Forbidden"); }
return;
} if (actions_.value(scope_).value(action_) != 0) {
(this->*(actions_[scope_][action_]))();
if (actions_.value(scope_).value(action_) != 0) }
{ else {
(this->*(actions_[scope_][action_]))(); status(404, "Not Found");
} qDebug() << Q_FUNC_INFO << "Resource not found:" << request().path;
else }
{
status(404, "Not Found");
qDebug() << Q_FUNC_INFO << "Resource not found:" << request().path;
}
} }
void RequestHandler::parsePath() void RequestHandler::parsePath()
{ {
if(request().path == "/") action_ = WEBUI_ACTION; if(request().path == "/") action_ = WEBUI_ACTION;
// 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();
{ pathItems.pop_front();
scope_ = pathItems.front(); }
pathItems.pop_front();
} }
}
if (!pathItems.empty()) {
if (!pathItems.empty()) if (actions_[scope_].contains(pathItems.front())) {
{ action_ = pathItems.front();
if (actions_[scope_].contains(pathItems.front())) pathItems.pop_front();
{ }
action_ = pathItems.front();
pathItems.pop_front();
} }
}
args_ = pathItems; args_ = pathItems;
} }
RequestHandler::RequestHandler(const HttpRequest &request, const HttpEnvironment &env, WebApplication *app) RequestHandler::RequestHandler(const HttpRequest &request, const HttpEnvironment &env, WebApplication *app)
: AbstractRequestHandler(request, env, app), scope_(DEFAULT_SCOPE), action_(DEFAULT_ACTION) : AbstractRequestHandler(request, env, app), scope_(DEFAULT_SCOPE), action_(DEFAULT_ACTION)
{ {
parsePath(); parsePath();
} }
QMap<QString, QMap<QString, RequestHandler::Action> > RequestHandler::actions_ = RequestHandler::initializeActions(); QMap<QString, QMap<QString, RequestHandler::Action> > RequestHandler::actions_ = RequestHandler::initializeActions();

103
src/webui/requesthandler.h

@ -38,63 +38,66 @@ class WebApplication;
class RequestHandler: public AbstractRequestHandler class RequestHandler: public AbstractRequestHandler
{ {
public: public:
RequestHandler( RequestHandler(
const HttpRequest& request, const HttpEnvironment& env, const HttpRequest& request, const HttpEnvironment& env,
WebApplication* app); WebApplication* app);
private: private:
// Actions // Actions
void action_public_webui(); void action_public_webui();
void action_public_index(); void action_public_index();
void action_public_login(); void action_public_login();
void action_public_logout(); void action_public_logout();
void action_public_theme(); void action_public_theme();
void action_public_images(); void action_public_images();
void action_json_torrents(); void action_json_torrents();
void action_json_preferences(); void action_json_preferences();
void action_json_transferInfo(); void action_json_transferInfo();
void action_json_propertiesGeneral(); void action_json_propertiesGeneral();
void action_json_propertiesTrackers(); void action_json_propertiesTrackers();
void action_json_propertiesFiles(); void action_json_propertiesFiles();
void action_command_shutdown(); void action_command_shutdown();
void action_command_download(); void action_command_download();
void action_command_upload(); void action_command_upload();
void action_command_addTrackers(); void action_command_addTrackers();
void action_command_resumeAll(); void action_command_resumeAll();
void action_command_pauseAll(); void action_command_pauseAll();
void action_command_resume(); void action_command_resume();
void action_command_pause(); void action_command_pause();
void action_command_setPreferences(); void action_command_setPreferences();
void action_command_setFilePrio(); void action_command_setFilePrio();
void action_command_getGlobalUpLimit(); void action_command_getGlobalUpLimit();
void action_command_getGlobalDlLimit(); void action_command_getGlobalDlLimit();
void action_command_setGlobalUpLimit(); void action_command_setGlobalUpLimit();
void action_command_setGlobalDlLimit(); void action_command_setGlobalDlLimit();
void action_command_getTorrentUpLimit(); void action_command_getTorrentUpLimit();
void action_command_getTorrentDlLimit(); void action_command_getTorrentDlLimit();
void action_command_setTorrentUpLimit(); void action_command_setTorrentUpLimit();
void action_command_setTorrentDlLimit(); void action_command_setTorrentDlLimit();
void action_command_delete(); void action_command_delete();
void action_command_deletePerm(); void action_command_deletePerm();
void action_command_increasePrio(); void action_command_increasePrio();
void action_command_decreasePrio(); void action_command_decreasePrio();
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)();
QString scope_; QString scope_;
QString action_; QString action_;
QStringList args_; QStringList args_;
void processRequest(); void processRequest();
bool isPublicScope(); bool isPublicScope();
void parsePath(); void parsePath();
static QMap<QString, QMap<QString, Action> > initializeActions(); static QMap<QString, QMap<QString, Action> > initializeActions();
static QMap<QString, QMap<QString, Action> > actions_; static QMap<QString, QMap<QString, Action> > actions_;
}; };
#endif // REQUESTHANDLER_H #endif // REQUESTHANDLER_H

Loading…
Cancel
Save