Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Send 204 when WebAPI response contains no data #21349

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/webui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ add_library(qbt_webui STATIC
# headers
api/apicontroller.h
api/apierror.h
api/apistatus.h
api/appcontroller.h
api/authcontroller.h
api/isessionmanager.h
Expand Down
6 changes: 6 additions & 0 deletions src/webui/api/apicontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ void APIResult::clear()
data.clear();
mimeType.clear();
filename.clear();
status = APIStatus::Ok;
}

APIController::APIController(IApplication *app, QObject *parent)
Expand Down Expand Up @@ -105,3 +106,8 @@ void APIController::setResult(const QByteArray &result, const QString &mimeType,
m_result.mimeType = mimeType;
m_result.filename = filename;
}

void APIController::setStatus(const APIStatus status)
{
m_result.status = status;
}
4 changes: 4 additions & 0 deletions src/webui/api/apicontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include <QVariant>

#include "base/applicationcomponent.h"
#include "apistatus.h"

using DataMap = QHash<QString, QByteArray>;
using StringMap = QHash<QString, QString>;
Expand All @@ -43,6 +44,7 @@ struct APIResult
QVariant data;
QString mimeType;
QString filename;
APIStatus status = APIStatus::Ok;

void clear();
};
Expand All @@ -67,6 +69,8 @@ class APIController : public ApplicationComponent<QObject>
void setResult(const QJsonObject &result);
void setResult(const QByteArray &result, const QString &mimeType = {}, const QString &filename = {});

void setStatus(APIStatus status);

private:
StringMap m_params;
DataMap m_data;
Expand Down
35 changes: 35 additions & 0 deletions src/webui/api/apistatus.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2024 Thomas Piccirello <thomas@piccirello.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/

#pragma once

enum class APIStatus
{
Ok,
Async
};
7 changes: 6 additions & 1 deletion src/webui/api/appcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ void AppController::shutdownAction()
{
QCoreApplication::exit();
});
setResult(QString());
}

void AppController::preferencesAction()
Expand Down Expand Up @@ -1157,6 +1158,8 @@ void AppController::setPreferencesAction()

// Save preferences
pref->apply();

setResult(QString());
}

void AppController::defaultSavePathAction()
Expand All @@ -1167,9 +1170,9 @@ void AppController::defaultSavePathAction()
void AppController::sendTestEmailAction()
{
app()->sendTestEmail();
setResult(QString());
}


void AppController::getDirectoryContentAction()
{
requireParams({u"dirPath"_s});
Expand Down Expand Up @@ -1259,6 +1262,8 @@ void AppController::setCookiesAction()
}

Net::DownloadManager::instance()->setAllCookies(cookies);

setResult(QString());
}

void AppController::networkInterfaceListAction()
Expand Down
3 changes: 3 additions & 0 deletions src/webui/api/authcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ AuthController::AuthController(ISessionManager *sessionManager, IApplication *ap
void AuthController::setUsername(const QString &username)
{
m_username = username;
setResult(QString());
}

void AuthController::setPasswordHash(const QByteArray &passwordHash)
{
m_passwordHash = passwordHash;
setResult(QString());
}

void AuthController::loginAction()
Expand Down Expand Up @@ -99,6 +101,7 @@ void AuthController::loginAction()
void AuthController::logoutAction() const
{
m_sessionManager->sessionEnd();
setResult(QString());
}

bool AuthController::isBanned() const
Expand Down
20 changes: 20 additions & 0 deletions src/webui/api/rsscontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ void RSSController::addFolderAction()
const nonstd::expected<void, QString> result = RSS::Session::instance()->addFolder(path);
if (!result)
throw APIError(APIErrorType::Conflict, result.error());

setResult(QString());
}

void RSSController::addFeedAction()
Expand All @@ -64,6 +66,8 @@ void RSSController::addFeedAction()
const nonstd::expected<void, QString> result = RSS::Session::instance()->addFeed(url, (path.isEmpty() ? url : path));
if (!result)
throw APIError(APIErrorType::Conflict, result.error());

setResult(QString());
}

void RSSController::setFeedURLAction()
Expand All @@ -75,6 +79,8 @@ void RSSController::setFeedURLAction()
const nonstd::expected<void, QString> result = RSS::Session::instance()->setFeedURL(path, url);
if (!result)
throw APIError(APIErrorType::Conflict, result.error());

setResult(QString());
}

void RSSController::removeItemAction()
Expand All @@ -85,6 +91,8 @@ void RSSController::removeItemAction()
const nonstd::expected<void, QString> result = RSS::Session::instance()->removeItem(path);
if (!result)
throw APIError(APIErrorType::Conflict, result.error());

setResult(QString());
}

void RSSController::moveItemAction()
Expand All @@ -96,6 +104,8 @@ void RSSController::moveItemAction()
const nonstd::expected<void, QString> result = RSS::Session::instance()->moveItem(itemPath, destPath);
if (!result)
throw APIError(APIErrorType::Conflict, result.error());

setResult(QString());
}

void RSSController::itemsAction()
Expand Down Expand Up @@ -130,6 +140,8 @@ void RSSController::markAsReadAction()
{
item->markAsRead();
}

setResult(QString());
}

void RSSController::refreshItemAction()
Expand All @@ -140,6 +152,8 @@ void RSSController::refreshItemAction()
RSS::Item *item = RSS::Session::instance()->itemByPath(itemPath);
if (item)
item->refresh();

setResult(QString());
}

void RSSController::setRuleAction()
Expand All @@ -151,6 +165,8 @@ void RSSController::setRuleAction()

const auto jsonObj = QJsonDocument::fromJson(ruleDef).object();
RSS::AutoDownloader::instance()->setRule(RSS::AutoDownloadRule::fromJsonObject(jsonObj, ruleName));

setResult(QString());
}

void RSSController::renameRuleAction()
Expand All @@ -161,6 +177,8 @@ void RSSController::renameRuleAction()
const QString newRuleName {params()[u"newRuleName"_s].trimmed()};

RSS::AutoDownloader::instance()->renameRule(ruleName, newRuleName);

setResult(QString());
}

void RSSController::removeRuleAction()
Expand All @@ -169,6 +187,8 @@ void RSSController::removeRuleAction()

const QString ruleName {params()[u"ruleName"_s].trimmed()};
RSS::AutoDownloader::instance()->removeRule(ruleName);

setResult(QString());
}

void RSSController::rulesAction()
Expand Down
16 changes: 16 additions & 0 deletions src/webui/api/searchcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ void SearchController::stopAction()
searchHandler->cancelSearch();
m_activeSearches.remove(id);
}

setResult(QString());
}

void SearchController::statusAction()
Expand Down Expand Up @@ -215,6 +217,8 @@ void SearchController::deleteAction()
searchHandler->cancelSearch();
m_activeSearches.remove(id);
m_searchHandlers.erase(iter);

setResult(QString());
}

void SearchController::downloadTorrentAction()
Expand All @@ -238,6 +242,8 @@ void SearchController::downloadTorrentAction()
downloadHandler->deleteLater();
});
}

setResult(QString());
}

void SearchController::pluginsAction()
Expand All @@ -253,6 +259,8 @@ void SearchController::installPluginAction()
const QStringList sources = params()[u"sources"_s].split(u'|');
for (const QString &source : sources)
SearchPluginManager::instance()->installPlugin(source);

setResult(QString());
}

void SearchController::uninstallPluginAction()
Expand All @@ -262,6 +270,8 @@ void SearchController::uninstallPluginAction()
const QStringList names = params()[u"names"_s].split(u'|');
for (const QString &name : names)
SearchPluginManager::instance()->uninstallPlugin(name.trimmed());

setResult(QString());
}

void SearchController::enablePluginAction()
Expand All @@ -273,6 +283,8 @@ void SearchController::enablePluginAction()

for (const QString &name : names)
SearchPluginManager::instance()->enablePlugin(name.trimmed(), enable);

setResult(QString());
}

void SearchController::updatePluginsAction()
Expand All @@ -282,6 +294,8 @@ void SearchController::updatePluginsAction()
connect(pluginManager, &SearchPluginManager::checkForUpdatesFinished, this, &SearchController::checkForUpdatesFinished);
connect(pluginManager, &SearchPluginManager::checkForUpdatesFailed, this, &SearchController::checkForUpdatesFailed);
pluginManager->checkForUpdates();

setResult(QString());
}

void SearchController::checkForUpdatesFinished(const QHash<QString, PluginVersion> &updateInfo)
Expand All @@ -300,6 +314,8 @@ void SearchController::checkForUpdatesFinished(const QHash<QString, PluginVersio
LogMsg(tr("Updating plugin %1").arg(pluginName), Log::INFO);
pluginManager->updatePlugin(pluginName);
}

setResult(QString());
}

void SearchController::checkForUpdatesFailed(const QString &reason)
Expand Down
2 changes: 2 additions & 0 deletions src/webui/api/torrentcreatorcontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,6 @@ void TorrentCreatorController::deleteTaskAction()

if (!m_torrentCreationManager->deleteTask(id))
throw APIError(APIErrorType::NotFound);

setResult(QString());
}
Loading
Loading