Browse Source

Change indentation to 4 spaces. Issue #2192.

adaptive-webui-19844
Gabriele 10 years ago
parent
commit
1154ea3873
  1. 126
      src/webui/requesthandler.cpp

126
src/webui/requesthandler.cpp

@ -109,8 +109,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 +139,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 +158,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;
} }
@ -227,22 +224,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 +246,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 +258,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 +270,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,9 +318,7 @@ 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()
@ -353,7 +337,7 @@ void RequestHandler::action_command_setGlobalUpLimit()
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()
@ -362,7 +346,7 @@ void RequestHandler::action_command_setGlobalDlLimit()
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()
@ -371,9 +355,7 @@ 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,9 +364,7 @@ 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,9 +375,7 @@ 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 +386,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 +437,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 +468,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 +476,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();
} }
@ -536,25 +494,21 @@ bool RequestHandler::isPublicScope()
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 +520,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();
} }

Loading…
Cancel
Save