Have separate API controller instances per session

PR #16848.
This commit is contained in:
Vladimir Golovnev 2022-04-12 13:39:35 +03:00 committed by GitHub
parent 189514c6de
commit 4471a6377e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 103 additions and 159 deletions

View file

@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2014 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2014, 2022 Vladimir Golovnev <glassez@yandex.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -119,16 +119,8 @@ namespace
WebApplication::WebApplication(QObject *parent)
: QObject(parent)
, m_cacheID {QString::number(Utils::Random::rand(), 36)}
, m_authController {new AuthController(this, this)}
{
registerAPIController(u"app"_qs, new AppController(this, this));
registerAPIController(u"auth"_qs, new AuthController(this, this));
registerAPIController(u"log"_qs, new LogController(this, this));
registerAPIController(u"rss"_qs, new RSSController(this, this));
registerAPIController(u"search"_qs, new SearchController(this, this));
registerAPIController(u"sync"_qs, new SyncController(this, this));
registerAPIController(u"torrents"_qs, new TorrentsController(this, this));
registerAPIController(u"transfer"_qs, new TransferController(this, this));
declarePublicAPI(u"auth/login"_qs);
configure();
@ -260,9 +252,16 @@ void WebApplication::doProcessRequest()
const QString action = match.captured(u"action"_qs);
const QString scope = match.captured(u"scope"_qs);
APIController *controller = m_apiControllers.value(scope);
APIController *controller = nullptr;
if (session())
controller = session()->getAPIController(scope);
if (!controller)
throw NotFoundHTTPError();
{
if (scope == u"auth")
controller = m_authController;
else
throw NotFoundHTTPError();
}
if (!session() && !isPublicAPI(scope, action))
throw ForbiddenHTTPError();
@ -414,14 +413,6 @@ void WebApplication::configure()
}
}
void WebApplication::registerAPIController(const QString &scope, APIController *controller)
{
Q_ASSERT(controller);
Q_ASSERT(!m_apiControllers.value(scope));
m_apiControllers[scope] = controller;
}
void WebApplication::declarePublicAPI(const QString &apiPath)
{
m_publicAPIs << apiPath;
@ -607,6 +598,13 @@ void WebApplication::sessionStart()
});
m_currentSession = new WebSession(generateSid());
m_currentSession->registerAPIController<AppController>(u"app"_qs);
m_currentSession->registerAPIController<LogController>(u"log"_qs);
m_currentSession->registerAPIController<RSSController>(u"rss"_qs);
m_currentSession->registerAPIController<SearchController>(u"search"_qs);
m_currentSession->registerAPIController<SyncController>(u"sync"_qs);
m_currentSession->registerAPIController<TorrentsController>(u"torrents"_qs);
m_currentSession->registerAPIController<TransferController>(u"transfer"_qs);
m_sessions[m_currentSession->id()] = m_currentSession;
QNetworkCookie cookie(C_SID, m_currentSession->id().toUtf8());
@ -775,12 +773,7 @@ void WebSession::updateTimestamp()
m_timer.start();
}
QVariant WebSession::getData(const QString &id) const
APIController *WebSession::getAPIController(const QString &scope) const
{
return m_data.value(id);
}
void WebSession::setData(const QString &id, const QVariant &data)
{
m_data[id] = data;
return m_apiControllers.value(scope);
}