From 1154ea3873876c91397b957e3f36a86f8c702def Mon Sep 17 00:00:00 2001 From: Gabriele Date: Sat, 22 Nov 2014 19:32:01 +0100 Subject: [PATCH 1/3] Change indentation to 4 spaces. Issue #2192. --- src/webui/requesthandler.cpp | 586 ++++++++++++++++------------------- src/webui/requesthandler.h | 100 +++--- 2 files changed, 318 insertions(+), 368 deletions(-) diff --git a/src/webui/requesthandler.cpp b/src/webui/requesthandler.cpp index da049c625..a13720b69 100644 --- a/src/webui/requesthandler.cpp +++ b/src/webui/requesthandler.cpp @@ -63,534 +63,484 @@ const QString WEBUI_ACTION = "webui"; QMap > RequestHandler::initializeActions() { - QMap > actions; + QMap > actions; - ADD_ACTION(public, webui); - ADD_ACTION(public, index); - ADD_ACTION(public, login); - ADD_ACTION(public, logout); - ADD_ACTION(public, theme); - ADD_ACTION(public, images); - ADD_ACTION(json, torrents); - ADD_ACTION(json, preferences); - ADD_ACTION(json, transferInfo); - ADD_ACTION(json, propertiesGeneral); - ADD_ACTION(json, propertiesTrackers); - ADD_ACTION(json, propertiesFiles); - ADD_ACTION(command, shutdown); - ADD_ACTION(command, download); - ADD_ACTION(command, upload); - ADD_ACTION(command, addTrackers); - ADD_ACTION(command, resumeAll); - ADD_ACTION(command, pauseAll); - ADD_ACTION(command, resume); - ADD_ACTION(command, pause); - ADD_ACTION(command, setPreferences); - ADD_ACTION(command, setFilePrio); - ADD_ACTION(command, getGlobalUpLimit); - ADD_ACTION(command, getGlobalDlLimit); - ADD_ACTION(command, setGlobalUpLimit); - ADD_ACTION(command, setGlobalDlLimit); - ADD_ACTION(command, getTorrentUpLimit); - ADD_ACTION(command, getTorrentDlLimit); - ADD_ACTION(command, setTorrentUpLimit); - ADD_ACTION(command, setTorrentDlLimit); - ADD_ACTION(command, delete); - ADD_ACTION(command, deletePerm); - ADD_ACTION(command, increasePrio); - ADD_ACTION(command, decreasePrio); - ADD_ACTION(command, topPrio); - ADD_ACTION(command, bottomPrio); - ADD_ACTION(command, recheck); + ADD_ACTION(public, webui); + ADD_ACTION(public, index); + ADD_ACTION(public, login); + ADD_ACTION(public, logout); + ADD_ACTION(public, theme); + ADD_ACTION(public, images); + ADD_ACTION(json, torrents); + ADD_ACTION(json, preferences); + ADD_ACTION(json, transferInfo); + ADD_ACTION(json, propertiesGeneral); + ADD_ACTION(json, propertiesTrackers); + ADD_ACTION(json, propertiesFiles); + ADD_ACTION(command, shutdown); + ADD_ACTION(command, download); + ADD_ACTION(command, upload); + ADD_ACTION(command, addTrackers); + ADD_ACTION(command, resumeAll); + ADD_ACTION(command, pauseAll); + ADD_ACTION(command, resume); + ADD_ACTION(command, pause); + ADD_ACTION(command, setPreferences); + ADD_ACTION(command, setFilePrio); + ADD_ACTION(command, getGlobalUpLimit); + ADD_ACTION(command, getGlobalDlLimit); + ADD_ACTION(command, setGlobalUpLimit); + ADD_ACTION(command, setGlobalDlLimit); + ADD_ACTION(command, getTorrentUpLimit); + ADD_ACTION(command, getTorrentDlLimit); + ADD_ACTION(command, setTorrentUpLimit); + ADD_ACTION(command, setTorrentDlLimit); + ADD_ACTION(command, delete); + ADD_ACTION(command, deletePerm); + ADD_ACTION(command, increasePrio); + ADD_ACTION(command, decreasePrio); + ADD_ACTION(command, topPrio); + ADD_ACTION(command, bottomPrio); + ADD_ACTION(command, recheck); - return actions; + return actions; } void RequestHandler::action_public_index() { - QString path; - if (!args_.isEmpty()) - { - if (args_.back() == "favicon.ico") - path = ":/Icons/skin/qbittorrent16.png"; - else - path = WWW_FOLDER + args_.join("/"); - } + QString path; - printFile(path); + if (!args_.isEmpty()) { + if (args_.back() == "favicon.ico") + path = ":/Icons/skin/qbittorrent16.png"; + else + path = WWW_FOLDER + args_.join("/"); + } + + printFile(path); } void RequestHandler::action_public_webui() { - if (!sessionActive()) - printFile(PRIVATE_FOLDER + "login.html"); - else - printFile(PRIVATE_FOLDER + "index.html"); + if (!sessionActive()) + printFile(PRIVATE_FOLDER + "login.html"); + else + printFile(PRIVATE_FOLDER + "index.html"); } void RequestHandler::action_public_login() { - const Preferences* const pref = Preferences::instance(); - QCryptographicHash md5(QCryptographicHash::Md5); + const Preferences* const pref = Preferences::instance(); + QCryptographicHash md5(QCryptographicHash::Md5); - md5.addData(request().posts["password"].toLocal8Bit()); - QString pass = md5.result().toHex(); + md5.addData(request().posts["password"].toLocal8Bit()); + QString pass = md5.result().toHex(); - bool equalUser = misc::slowEquals(request().posts["username"].toUtf8(), pref->getWebUiUsername().toUtf8()); - bool equalPass = misc::slowEquals(pass.toUtf8(), pref->getWebUiPassword().toUtf8()); + bool equalUser = misc::slowEquals(request().posts["username"].toUtf8(), pref->getWebUiUsername().toUtf8()); + bool equalPass = misc::slowEquals(pass.toUtf8(), pref->getWebUiPassword().toUtf8()); - if (equalUser && equalPass) - { - sessionStart(); - print(QByteArray("Ok."), CONTENT_TYPE_TXT); - } - else - { - QString addr = env().clientAddress.toString(); - increaseFailedAttempts(); - qDebug("client IP: %s (%d failed attempts)", qPrintable(addr), failedAttempts()); - print(QByteArray("Fails."), CONTENT_TYPE_TXT); - } + if (equalUser && equalPass) { + sessionStart(); + print(QByteArray("Ok."), CONTENT_TYPE_TXT); + } + else { + QString addr = env().clientAddress.toString(); + increaseFailedAttempts(); + qDebug("client IP: %s (%d failed attempts)", qPrintable(addr), failedAttempts()); + print(QByteArray("Fails."), CONTENT_TYPE_TXT); + } } void RequestHandler::action_public_logout() { - sessionEnd(); + sessionEnd(); } void RequestHandler::action_public_theme() { - if (args_.size() != 1) - { - status(404, "Not Found"); - return; - } + if (args_.size() != 1) { + status(404, "Not Found"); + return; + } #ifdef DISABLE_GUI - QString url = ":/Icons/oxygen/" + args_.front() + ".png"; + QString url = ":/Icons/oxygen/" + args_.front() + ".png"; #else - QString url = IconProvider::instance()->getIconPath(args_.front()); + QString url = IconProvider::instance()->getIconPath(args_.front()); #endif - qDebug() << Q_FUNC_INFO << "There icon:" << url; + qDebug() << Q_FUNC_INFO << "There icon:" << url; - printFile(url); + printFile(url); } void RequestHandler::action_public_images() { - const QString path = ":/Icons/" + args_.join("/"); - printFile(path); + const QString path = ":/Icons/" + args_.join("/"); + printFile(path); } void RequestHandler::action_json_torrents() { - print(btjson::getTorrents(), CONTENT_TYPE_JS); + print(btjson::getTorrents(), CONTENT_TYPE_JS); } void RequestHandler::action_json_preferences() { - print(prefjson::getPreferences(), CONTENT_TYPE_JS); + print(prefjson::getPreferences(), CONTENT_TYPE_JS); } void RequestHandler::action_json_transferInfo() { - print(btjson::getTransferInfo(), CONTENT_TYPE_JS); + print(btjson::getTransferInfo(), CONTENT_TYPE_JS); } 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() { - print(btjson::getTrackersForTorrent(args_.front()), CONTENT_TYPE_JS); + print(btjson::getTrackersForTorrent(args_.front()), CONTENT_TYPE_JS); } void RequestHandler::action_json_propertiesFiles() { - print(btjson::getFilesForTorrent(args_.front()), CONTENT_TYPE_JS); + print(btjson::getFilesForTorrent(args_.front()), CONTENT_TYPE_JS); } void RequestHandler::action_command_shutdown() { - qDebug() << "Shutdown request from Web UI"; - // Special case handling for shutdown, we - // need to reply to the Web UI before - // actually shutting down. + qDebug() << "Shutdown request from Web UI"; + // Special case handling for shutdown, we + // need to reply to the Web UI before + // actually shutting down. - QTimer::singleShot(0, qApp, SLOT(quit())); + QTimer::singleShot(0, qApp, SLOT(quit())); } void RequestHandler::action_command_download() { - QString urls = request().posts["urls"]; - QStringList list = urls.split('\n'); + QString urls = request().posts["urls"]; + QStringList list = urls.split('\n'); - foreach (QString url, list) - { - url = url.trimmed(); - if (!url.isEmpty()) - { - if (url.startsWith("bc://bt/", Qt::CaseInsensitive)) - { - qDebug("Converting bc link to magnet link"); - url = misc::bcLinkToMagnet(url); - } - else if (url.startsWith("magnet:", Qt::CaseInsensitive)) - { - QBtSession::instance()->addMagnetSkipAddDlg(url); - } - else - { - qDebug("Downloading url: %s", qPrintable(url)); - QBtSession::instance()->downloadUrlAndSkipDialog(url); - } + foreach (QString url, list) { + url = url.trimmed(); + if (!url.isEmpty()) { + if (url.startsWith("bc://bt/", Qt::CaseInsensitive)) { + qDebug("Converting bc link to magnet link"); + url = misc::bcLinkToMagnet(url); + } + else if (url.startsWith("magnet:", Qt::CaseInsensitive)) { + QBtSession::instance()->addMagnetSkipAddDlg(url); + } + else { + qDebug("Downloading url: %s", qPrintable(url)); + QBtSession::instance()->downloadUrlAndSkipDialog(url); + } + } } - } } void RequestHandler::action_command_upload() { - qDebug() << Q_FUNC_INFO; + qDebug() << Q_FUNC_INFO; - foreach(const UploadedFile& torrent, request().files) - { - QString filePath = saveTmpFile(torrent.data); + foreach(const UploadedFile& torrent, request().files) { + QString filePath = saveTmpFile(torrent.data); - if (!filePath.isEmpty()) - { - QTorrentHandle h = QBtSession::instance()->addTorrent(filePath); - if (!h.is_valid()) { - 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); + if (!filePath.isEmpty()) { + QTorrentHandle h = QBtSession::instance()->addTorrent(filePath); + if (!h.is_valid()) { + 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); + } + else { + 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); + } } - else - { - 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() { - QString hash = request().posts["hash"]; + QString hash = request().posts["hash"]; - if (!hash.isEmpty()) - { - QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); + if (!hash.isEmpty()) { + QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); - if (h.is_valid() && h.has_metadata()) - { - QString urls = request().posts["urls"]; - QStringList list = urls.split('\n'); + if (h.is_valid() && h.has_metadata()) { + QString urls = request().posts["urls"]; + QStringList list = urls.split('\n'); - foreach (const QString& url, list) - { - announce_entry e(url.toStdString()); - h.add_tracker(e); - } + foreach (const QString& url, list) { + announce_entry e(url.toStdString()); + h.add_tracker(e); + } + } } - } } void RequestHandler::action_command_resumeAll() { - QBtSession::instance()->resumeAllTorrents(); + QBtSession::instance()->resumeAllTorrents(); } void RequestHandler::action_command_pauseAll() { - QBtSession::instance()->pauseAllTorrents(); + QBtSession::instance()->pauseAllTorrents(); } void RequestHandler::action_command_resume() { - QBtSession::instance()->resumeTorrent(request().posts["hash"]); + QBtSession::instance()->resumeTorrent(request().posts["hash"]); } void RequestHandler::action_command_pause() { - QBtSession::instance()->pauseTorrent(request().posts["hash"]); + QBtSession::instance()->pauseTorrent(request().posts["hash"]); } void RequestHandler::action_command_setPreferences() { - prefjson::setPreferences(request().posts["json"]); + prefjson::setPreferences(request().posts["json"]); } void RequestHandler::action_command_setFilePrio() { - QString hash = request().posts["hash"]; - int file_id = request().posts["id"].toInt(); - int priority = request().posts["priority"].toInt(); - QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); + QString hash = request().posts["hash"]; + int file_id = request().posts["id"].toInt(); + int priority = request().posts["priority"].toInt(); + QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); - if (h.is_valid() && h.has_metadata()) - { - h.file_priority(file_id, priority); - } + if (h.is_valid() && h.has_metadata()) + h.file_priority(file_id, priority); } 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() { - 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() { - qlonglong limit = request().posts["limit"].toLongLong(); - if (limit == 0) limit = -1; + qlonglong limit = request().posts["limit"].toLongLong(); + if (limit == 0) limit = -1; - QBtSession::instance()->setUploadRateLimit(limit); - Preferences::instance()->setGlobalUploadLimit(limit/1024.); + QBtSession::instance()->setUploadRateLimit(limit); + Preferences::instance()->setGlobalUploadLimit(limit / 1024.); } void RequestHandler::action_command_setGlobalDlLimit() { - qlonglong limit = request().posts["limit"].toLongLong(); - if (limit == 0) limit = -1; + qlonglong limit = request().posts["limit"].toLongLong(); + if (limit == 0) limit = -1; - QBtSession::instance()->setDownloadRateLimit(limit); - Preferences::instance()->setGlobalDownloadLimit(limit/1024.); + QBtSession::instance()->setDownloadRateLimit(limit); + Preferences::instance()->setGlobalDownloadLimit(limit / 1024.); } void RequestHandler::action_command_getTorrentUpLimit() { - QString hash = request().posts["hash"]; - QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); + QString hash = request().posts["hash"]; + QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); - if (h.is_valid()) - { - print(QByteArray::number(h.upload_limit())); - } + if (h.is_valid()) + print(QByteArray::number(h.upload_limit())); } void RequestHandler::action_command_getTorrentDlLimit() { - QString hash = request().posts["hash"]; - QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); + QString hash = request().posts["hash"]; + QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); - if (h.is_valid()) - { - print(QByteArray::number(h.download_limit())); - } + if (h.is_valid()) + print(QByteArray::number(h.download_limit())); } void RequestHandler::action_command_setTorrentUpLimit() { - QString hash = request().posts["hash"]; - qlonglong limit = request().posts["limit"].toLongLong(); - if (limit == 0) limit = -1; - QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); + QString hash = request().posts["hash"]; + qlonglong limit = request().posts["limit"].toLongLong(); + if (limit == 0) limit = -1; + QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); - if (h.is_valid()) - { - h.set_upload_limit(limit); - } + if (h.is_valid()) + h.set_upload_limit(limit); } void RequestHandler::action_command_setTorrentDlLimit() { - QString hash = request().posts["hash"]; - qlonglong limit = request().posts["limit"].toLongLong(); - if (limit == 0) limit = -1; - QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); + QString hash = request().posts["hash"]; + qlonglong limit = request().posts["limit"].toLongLong(); + if (limit == 0) limit = -1; + QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); - if (h.is_valid()) - { - h.set_download_limit(limit); - } + if (h.is_valid()) + h.set_download_limit(limit); } void RequestHandler::action_command_delete() { - QStringList hashes = request().posts["hashes"].split("|"); - - foreach (const QString &hash, hashes) - { - QBtSession::instance()->deleteTorrent(hash, false); - } + QStringList hashes = request().posts["hashes"].split("|"); + foreach (const QString &hash, hashes) + QBtSession::instance()->deleteTorrent(hash, false); } void RequestHandler::action_command_deletePerm() { - QStringList hashes = request().posts["hashes"].split("|"); - - foreach (const QString &hash, hashes) - { - QBtSession::instance()->deleteTorrent(hash, true); - } + QStringList hashes = request().posts["hashes"].split("|"); + foreach (const QString &hash, hashes) + QBtSession::instance()->deleteTorrent(hash, true); } void RequestHandler::action_command_increasePrio() { - QStringList hashes = request().posts["hashes"].split("|"); - std::priority_queue, - std::vector >, - std::greater > > torrent_queue; + QStringList hashes = request().posts["hashes"].split("|"); - // 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)); - } + std::priority_queue, + std::vector >, + std::greater > > 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&) {} } - catch(invalid_handle&) {} - } - // Increase torrents priority (starting with the ones with highest priority) - while(!torrent_queue.empty()) - { - QTorrentHandle h = torrent_queue.top().second; + // 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(); + try { + h.queue_position_up(); + } + catch(invalid_handle&) {} + + torrent_queue.pop(); } - catch(invalid_handle&) {} - - torrent_queue.pop(); - } } void RequestHandler::action_command_decreasePrio() { - QStringList hashes = request().posts["hashes"].split("|"); - std::priority_queue, - std::vector >, - std::less > > torrent_queue; + QStringList hashes = request().posts["hashes"].split("|"); - // Sort torrents by priority - foreach (const QString &hash, hashes) - { - try - { - QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); + std::priority_queue, + std::vector >, + std::less > > torrent_queue; - if (!h.is_seed()) - { - torrent_queue.push(qMakePair(h.queue_position(), h)); - } + // 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&) {} } - catch(invalid_handle&) {} - } - // Decrease torrents priority (starting with the ones with lowest priority) - while(!torrent_queue.empty()) - { - QTorrentHandle h = torrent_queue.top().second; + // 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(); + try { + h.queue_position_down(); + } + catch(invalid_handle&) {} + + torrent_queue.pop(); } - catch(invalid_handle&) {} - - torrent_queue.pop(); - } } void RequestHandler::action_command_topPrio() { - foreach (const QString &hash, request().posts["hashes"].split("|")) - { - QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); - if (h.is_valid()) h.queue_position_top(); - } + foreach (const QString &hash, request().posts["hashes"].split("|")) { + QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); + if (h.is_valid()) h.queue_position_top(); + } } void RequestHandler::action_command_bottomPrio() { - foreach (const QString &hash, request().posts["hashes"].split("|")) - { - QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); - if (h.is_valid()) h.queue_position_bottom(); - } + foreach (const QString &hash, request().posts["hashes"].split("|")) { + QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash); + if (h.is_valid()) h.queue_position_bottom(); + } } void RequestHandler::action_command_recheck() { - QBtSession::instance()->recheckTorrent(request().posts["hash"]); + QBtSession::instance()->recheckTorrent(request().posts["hash"]); } bool RequestHandler::isPublicScope() { - return (scope_ == DEFAULT_SCOPE); + return (scope_ == DEFAULT_SCOPE); } void RequestHandler::processRequest() { - if (args_.contains(".") || args_.contains("..")) - { - qDebug() << Q_FUNC_INFO << "Invalid path:" << request().path; - status(404, "Not Found"); - return; - } + if (args_.contains(".") || args_.contains("..")) { + qDebug() << Q_FUNC_INFO << "Invalid path:" << request().path; + status(404, "Not Found"); + return; + } - if (!isPublicScope() && !sessionActive()) - { - status(403, "Forbidden"); - return; - } + if (!isPublicScope() && !sessionActive()) { + status(403, "Forbidden"); + return; + } - if (actions_.value(scope_).value(action_) != 0) - { - (this->*(actions_[scope_][action_]))(); - } - else - { - status(404, "Not Found"); - qDebug() << Q_FUNC_INFO << "Resource not found:" << request().path; - } + if (actions_.value(scope_).value(action_) != 0) { + (this->*(actions_[scope_][action_]))(); + } + else { + status(404, "Not Found"); + qDebug() << Q_FUNC_INFO << "Resource not found:" << request().path; + } } void RequestHandler::parsePath() { - if(request().path == "/") action_ = WEBUI_ACTION; + if(request().path == "/") action_ = WEBUI_ACTION; - // check action for requested path - QStringList pathItems = request().path.split('/', QString::SkipEmptyParts); - if (!pathItems.empty()) - { - if (actions_.contains(pathItems.front())) - { - scope_ = pathItems.front(); - pathItems.pop_front(); + // check action for requested path + QStringList pathItems = request().path.split('/', QString::SkipEmptyParts); + if (!pathItems.empty()) { + if (actions_.contains(pathItems.front())) { + scope_ = pathItems.front(); + pathItems.pop_front(); + } } - } - if (!pathItems.empty()) - { - if (actions_[scope_].contains(pathItems.front())) - { - action_ = pathItems.front(); - pathItems.pop_front(); + if (!pathItems.empty()) { + if (actions_[scope_].contains(pathItems.front())) { + action_ = pathItems.front(); + pathItems.pop_front(); + } } - } - args_ = pathItems; + args_ = pathItems; } 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 > RequestHandler::actions_ = RequestHandler::initializeActions(); diff --git a/src/webui/requesthandler.h b/src/webui/requesthandler.h index a92f85269..49d4e24b9 100644 --- a/src/webui/requesthandler.h +++ b/src/webui/requesthandler.h @@ -38,63 +38,63 @@ class WebApplication; class RequestHandler: public AbstractRequestHandler { public: - RequestHandler( - const HttpRequest& request, const HttpEnvironment& env, - WebApplication* app); + RequestHandler( + const HttpRequest& request, const HttpEnvironment& env, + WebApplication* app); private: - // Actions - void action_public_webui(); - void action_public_index(); - void action_public_login(); - void action_public_logout(); - void action_public_theme(); - void action_public_images(); - void action_json_torrents(); - void action_json_preferences(); - void action_json_transferInfo(); - void action_json_propertiesGeneral(); - void action_json_propertiesTrackers(); - void action_json_propertiesFiles(); - void action_command_shutdown(); - void action_command_download(); - void action_command_upload(); - void action_command_addTrackers(); - void action_command_resumeAll(); - void action_command_pauseAll(); - void action_command_resume(); - void action_command_pause(); - void action_command_setPreferences(); - void action_command_setFilePrio(); - void action_command_getGlobalUpLimit(); - void action_command_getGlobalDlLimit(); - void action_command_setGlobalUpLimit(); - void action_command_setGlobalDlLimit(); - void action_command_getTorrentUpLimit(); - void action_command_getTorrentDlLimit(); - void action_command_setTorrentUpLimit(); - void action_command_setTorrentDlLimit(); - void action_command_delete(); - void action_command_deletePerm(); - void action_command_increasePrio(); - void action_command_decreasePrio(); - void action_command_topPrio(); - void action_command_bottomPrio(); - void action_command_recheck(); + // Actions + void action_public_webui(); + void action_public_index(); + void action_public_login(); + void action_public_logout(); + void action_public_theme(); + void action_public_images(); + void action_json_torrents(); + void action_json_preferences(); + void action_json_transferInfo(); + void action_json_propertiesGeneral(); + void action_json_propertiesTrackers(); + void action_json_propertiesFiles(); + void action_command_shutdown(); + void action_command_download(); + void action_command_upload(); + void action_command_addTrackers(); + void action_command_resumeAll(); + void action_command_pauseAll(); + void action_command_resume(); + void action_command_pause(); + void action_command_setPreferences(); + void action_command_setFilePrio(); + void action_command_getGlobalUpLimit(); + void action_command_getGlobalDlLimit(); + void action_command_setGlobalUpLimit(); + void action_command_setGlobalDlLimit(); + void action_command_getTorrentUpLimit(); + void action_command_getTorrentDlLimit(); + void action_command_setTorrentUpLimit(); + void action_command_setTorrentDlLimit(); + void action_command_delete(); + void action_command_deletePerm(); + void action_command_increasePrio(); + void action_command_decreasePrio(); + void action_command_topPrio(); + void action_command_bottomPrio(); + void action_command_recheck(); - typedef void (RequestHandler::*Action)(); + typedef void (RequestHandler::*Action)(); - QString scope_; - QString action_; - QStringList args_; + QString scope_; + QString action_; + QStringList args_; - void processRequest(); + void processRequest(); - bool isPublicScope(); - void parsePath(); + bool isPublicScope(); + void parsePath(); - static QMap > initializeActions(); - static QMap > actions_; + static QMap > initializeActions(); + static QMap > actions_; }; #endif // REQUESTHANDLER_H From 8b5512c2448e15b79f7be9c77751bc1fff8501fa Mon Sep 17 00:00:00 2001 From: Gabriele Date: Sat, 22 Nov 2014 20:32:04 +0100 Subject: [PATCH 2/3] WebUI: Assign a version to the Web API and allow to get it New GET methods: * version/api: current API version * version/api_min: min API version supported The API version is an integer and it's returned as plain text. Whenever the API is changed, the api version is increased. Whenever the API is changed in a way that is not backward compatible, api_min is increased. --- src/webui/requesthandler.cpp | 18 +++++++++++++++++- src/webui/requesthandler.h | 2 ++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/webui/requesthandler.cpp b/src/webui/requesthandler.cpp index a13720b69..02689faaa 100644 --- a/src/webui/requesthandler.cpp +++ b/src/webui/requesthandler.cpp @@ -51,6 +51,9 @@ using namespace libtorrent; +static const int API_VERSION = 2; +static const int API_VERSION_MIN = 2; + const QString WWW_FOLDER = ":/www/public/"; const QString PRIVATE_FOLDER = ":/www/private/"; const QString DEFAULT_SCOPE = "public"; @@ -58,6 +61,7 @@ const QString SCOPE_IMAGES = "images"; const QString SCOPE_THEME = "theme"; const QString DEFAULT_ACTION = "index"; const QString WEBUI_ACTION = "webui"; +const QString VERSION_INFO = "version"; #define ADD_ACTION(scope, action) actions[#scope][#action] = &RequestHandler::action_##scope##_##action @@ -102,6 +106,8 @@ QMap > RequestHandler::initialize ADD_ACTION(command, topPrio); ADD_ACTION(command, bottomPrio); ADD_ACTION(command, recheck); + ADD_ACTION(version, api); + ADD_ACTION(version, api_min); return actions; } @@ -209,6 +215,16 @@ void RequestHandler::action_json_propertiesFiles() 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_command_shutdown() { qDebug() << "Shutdown request from Web UI"; @@ -489,7 +505,7 @@ void RequestHandler::action_command_recheck() bool RequestHandler::isPublicScope() { - return (scope_ == DEFAULT_SCOPE); + return (scope_ == DEFAULT_SCOPE || scope_ == VERSION_INFO); } void RequestHandler::processRequest() diff --git a/src/webui/requesthandler.h b/src/webui/requesthandler.h index 49d4e24b9..6e83b7a1a 100644 --- a/src/webui/requesthandler.h +++ b/src/webui/requesthandler.h @@ -81,6 +81,8 @@ private: void action_command_topPrio(); void action_command_bottomPrio(); void action_command_recheck(); + void action_version_api(); + void action_version_api_min(); typedef void (RequestHandler::*Action)(); From d6169b14dd0191c0aa4f860673a97847fbbcf2e6 Mon Sep 17 00:00:00 2001 From: Gabriele Date: Sat, 22 Nov 2014 20:32:33 +0100 Subject: [PATCH 3/3] WebUI: Allow to retrieve qBittorrent version New GET method: * version/qbittorrent: qBittorrent version Closes #1872. --- src/webui/requesthandler.cpp | 6 ++++++ src/webui/requesthandler.h | 1 + 2 files changed, 7 insertions(+) diff --git a/src/webui/requesthandler.cpp b/src/webui/requesthandler.cpp index 02689faaa..0bcded468 100644 --- a/src/webui/requesthandler.cpp +++ b/src/webui/requesthandler.cpp @@ -108,6 +108,7 @@ QMap > RequestHandler::initialize ADD_ACTION(command, recheck); ADD_ACTION(version, api); ADD_ACTION(version, api_min); + ADD_ACTION(version, qbittorrent); return actions; } @@ -225,6 +226,11 @@ 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() { qDebug() << "Shutdown request from Web UI"; diff --git a/src/webui/requesthandler.h b/src/webui/requesthandler.h index 6e83b7a1a..f1d019999 100644 --- a/src/webui/requesthandler.h +++ b/src/webui/requesthandler.h @@ -83,6 +83,7 @@ private: void action_command_recheck(); void action_version_api(); void action_version_api_min(); + void action_version_qbittorrent(); typedef void (RequestHandler::*Action)();