Changed coding style

This commit is contained in:
emeric
2023-10-02 20:32:28 +02:00
parent 4fbacd691d
commit 7c7fbba319
18 changed files with 1265 additions and 1311 deletions
+1 -2
View File
@@ -22,8 +22,7 @@
namespace StringUtils namespace StringUtils
{ {
template<> template<>
std::optional<API::Subsonic::ProtocolVersion> std::optional<API::Subsonic::ProtocolVersion> readAs(std::string_view str)
readAs(std::string_view str)
{ {
// Expects "X.Y.Z" // Expects "X.Y.Z"
const auto numbers{ StringUtils::splitString(str, ".") }; const auto numbers{ StringUtils::splitString(str, ".") };
+2 -1
View File
@@ -35,6 +35,7 @@ namespace API::Subsonic
namespace StringUtils namespace StringUtils
{ {
template<> std::optional<API::Subsonic::ProtocolVersion> readAs(std::string_view str); template<>
std::optional<API::Subsonic::ProtocolVersion> readAs(std::string_view str);
} }
+10 -20
View File
@@ -26,32 +26,27 @@
namespace API::Subsonic namespace API::Subsonic
{ {
std::string std::string idToString(Database::ArtistId id)
idToString(Database::ArtistId id)
{ {
return "ar-" + id.toString(); return "ar-" + id.toString();
} }
std::string std::string idToString(Database::ReleaseId id)
idToString(Database::ReleaseId id)
{ {
return "al-" + id.toString(); return "al-" + id.toString();
} }
std::string std::string idToString(RootId)
idToString(RootId)
{ {
return "root"; return "root";
} }
std::string std::string idToString(Database::TrackId id)
idToString(Database::TrackId id)
{ {
return "tr-" + id.toString(); return "tr-" + id.toString();
} }
std::string std::string idToString(Database::TrackListId id)
idToString(Database::TrackListId id)
{ {
return "pl-" + id.toString(); return "pl-" + id.toString();
} }
@@ -60,8 +55,7 @@ namespace API::Subsonic
namespace StringUtils namespace StringUtils
{ {
template<> template<>
std::optional<Database::ArtistId> std::optional<Database::ArtistId> readAs(std::string_view str)
readAs(std::string_view str)
{ {
std::vector<std::string_view> values{ StringUtils::splitString(str, "-") }; std::vector<std::string_view> values{ StringUtils::splitString(str, "-") };
if (values.size() != 2) if (values.size() != 2)
@@ -77,8 +71,7 @@ namespace StringUtils
} }
template<> template<>
std::optional<Database::ReleaseId> std::optional<Database::ReleaseId> readAs(std::string_view str)
readAs(std::string_view str)
{ {
std::vector<std::string_view> values{ StringUtils::splitString(str, "-") }; std::vector<std::string_view> values{ StringUtils::splitString(str, "-") };
if (values.size() != 2) if (values.size() != 2)
@@ -94,8 +87,7 @@ namespace StringUtils
} }
template<> template<>
std::optional<API::Subsonic::RootId> std::optional<API::Subsonic::RootId> readAs(std::string_view str)
readAs(std::string_view str)
{ {
if (str == "root") if (str == "root")
return API::Subsonic::RootId{}; return API::Subsonic::RootId{};
@@ -104,8 +96,7 @@ namespace StringUtils
} }
template<> template<>
std::optional<Database::TrackId> std::optional<Database::TrackId> readAs(std::string_view str)
readAs(std::string_view str)
{ {
std::vector<std::string_view> values{ StringUtils::splitString(str, "-") }; std::vector<std::string_view> values{ StringUtils::splitString(str, "-") };
if (values.size() != 2) if (values.size() != 2)
@@ -121,8 +112,7 @@ namespace StringUtils
} }
template<> template<>
std::optional<Database::TrackListId> std::optional<Database::TrackListId> readAs(std::string_view str)
readAs(std::string_view str)
{ {
std::vector<std::string_view> values{ StringUtils::splitString(str, "-") }; std::vector<std::string_view> values{ StringUtils::splitString(str, "-") };
if (values.size() != 2) if (values.size() != 2)
+5 -10
View File
@@ -40,23 +40,18 @@ namespace API::Subsonic
namespace StringUtils namespace StringUtils
{ {
template<> template<>
std::optional<API::Subsonic::RootId> std::optional<API::Subsonic::RootId> readAs(std::string_view str);
readAs(std::string_view str);
template<> template<>
std::optional<Database::ArtistId> std::optional<Database::ArtistId> readAs(std::string_view str);
readAs(std::string_view str);
template<> template<>
std::optional<Database::ReleaseId> std::optional<Database::ReleaseId> readAs(std::string_view str);
readAs(std::string_view str);
template<> template<>
std::optional<Database::TrackId> std::optional<Database::TrackId> readAs(std::string_view str);
readAs(std::string_view str);
template<> template<>
std::optional<Database::TrackListId> std::optional<Database::TrackListId> readAs(std::string_view str);
readAs(std::string_view str);
} }
+24 -38
View File
@@ -54,16 +54,14 @@ using namespace Database;
namespace API::Subsonic namespace API::Subsonic
{ {
std::unique_ptr<Wt::WResource> createSubsonicResource(Database::Db& db)
std::unique_ptr<Wt::WResource>
createSubsonicResource(Database::Db& db)
{ {
return std::make_unique<SubsonicResource>(db); return std::make_unique<SubsonicResource>(db);
} }
static namespace
std::unordered_map<std::string, ProtocolVersion> {
readConfigProtocolVersions() std::unordered_map<std::string, ProtocolVersion> readConfigProtocolVersions()
{ {
std::unordered_map<std::string, ProtocolVersion> res; std::unordered_map<std::string, ProtocolVersion> res;
@@ -76,13 +74,7 @@ readConfigProtocolVersions()
return res; return res;
} }
SubsonicResource::SubsonicResource(Db& db)
: _serverProtocolVersionsByClient {readConfigProtocolVersions()}
, _db {db}
{
}
static
std::string parameterMapToDebugString(const Wt::Http::ParameterMap& parameterMap) std::string parameterMapToDebugString(const Wt::Http::ParameterMap& parameterMap)
{ {
auto censorValue = [](const std::string& type, const std::string& value) -> std::string auto censorValue = [](const std::string& type, const std::string& value) -> std::string
@@ -115,9 +107,7 @@ std::string parameterMapToDebugString(const Wt::Http::ParameterMap& parameterMap
return res; return res;
} }
static void checkUserTypeIsAllowed(RequestContext& context, EnumSet<Database::UserType> allowedUserTypes)
void
checkUserTypeIsAllowed(RequestContext& context, EnumSet<Database::UserType> allowedUserTypes)
{ {
auto transaction{ context.dbSession.createSharedTransaction() }; auto transaction{ context.dbSession.createSharedTransaction() };
@@ -129,16 +119,12 @@ checkUserTypeIsAllowed(RequestContext& context, EnumSet<Database::UserType> allo
throw UserNotAuthorizedError{}; throw UserNotAuthorizedError{};
} }
static Response handlePingRequest(RequestContext& context)
Response
handlePingRequest(RequestContext& context)
{ {
return Response::createOkResponse(context.serverProtocolVersion); return Response::createOkResponse(context.serverProtocolVersion);
} }
static Response handleGetLicenseRequest(RequestContext& context)
Response
handleGetLicenseRequest(RequestContext& context)
{ {
Response response{ Response::createOkResponse(context.serverProtocolVersion) }; Response response{ Response::createOkResponse(context.serverProtocolVersion) };
@@ -150,13 +136,12 @@ handleGetLicenseRequest(RequestContext& context)
return response; return response;
} }
static Response handleNotImplemented(RequestContext&)
Response
handleNotImplemented(RequestContext&)
{ {
throw NotImplementedGenericError{}; throw NotImplementedGenericError{};
} }
using RequestHandlerFunc = std::function<Response(RequestContext& context)>; using RequestHandlerFunc = std::function<Response(RequestContext& context)>;
using CheckImplementedFunc = std::function<void()>; using CheckImplementedFunc = std::function<void()>;
struct RequestEntryPointInfo struct RequestEntryPointInfo
@@ -279,9 +264,16 @@ static std::unordered_map<std::string, MediaRetrievalHandlerFunc> mediaRetrieval
{"/stream", handleStream}, {"/stream", handleStream},
{"/getCoverArt", handleGetCoverArt}, {"/getCoverArt", handleGetCoverArt},
}; };
}
void
SubsonicResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Response &response) SubsonicResource::SubsonicResource(Db& db)
: _serverProtocolVersionsByClient{ readConfigProtocolVersions() }
, _db{ db }
{
}
void SubsonicResource::handleRequest(const Wt::Http::Request& request, Wt::Http::Response& response)
{ {
static std::atomic<std::size_t> curRequestId{}; static std::atomic<std::size_t> curRequestId{};
@@ -343,8 +335,7 @@ SubsonicResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Resp
} }
} }
ProtocolVersion ProtocolVersion SubsonicResource::getServerProtocolVersion(const std::string& clientName) const
SubsonicResource::getServerProtocolVersion(const std::string& clientName) const
{ {
auto it{ _serverProtocolVersionsByClient.find(clientName) }; auto it{ _serverProtocolVersionsByClient.find(clientName) };
if (it == std::cend(_serverProtocolVersionsByClient)) if (it == std::cend(_serverProtocolVersionsByClient))
@@ -353,8 +344,7 @@ SubsonicResource::getServerProtocolVersion(const std::string& clientName) const
return it->second; return it->second;
} }
void void SubsonicResource::checkProtocolVersion(ProtocolVersion client, ProtocolVersion server)
SubsonicResource::checkProtocolVersion(ProtocolVersion client, ProtocolVersion server)
{ {
if (client.major > server.major) if (client.major > server.major)
throw ServerMustUpgradeError{}; throw ServerMustUpgradeError{};
@@ -369,8 +359,7 @@ SubsonicResource::checkProtocolVersion(ProtocolVersion client, ProtocolVersion s
} }
} }
ClientInfo ClientInfo SubsonicResource::getClientInfo(const Wt::Http::ParameterMap& parameters)
SubsonicResource::getClientInfo(const Wt::Http::ParameterMap& parameters)
{ {
ClientInfo res; ClientInfo res;
@@ -386,8 +375,7 @@ SubsonicResource::getClientInfo(const Wt::Http::ParameterMap& parameters)
return res; return res;
} }
RequestContext RequestContext SubsonicResource::buildRequestContext(const Wt::Http::Request& request)
SubsonicResource::buildRequestContext(const Wt::Http::Request& request)
{ {
const Wt::Http::ParameterMap& parameters{ request.getParameterMap() }; const Wt::Http::ParameterMap& parameters{ request.getParameterMap() };
const ClientInfo clientInfo{ getClientInfo(parameters) }; const ClientInfo clientInfo{ getClientInfo(parameters) };
@@ -396,8 +384,7 @@ SubsonicResource::buildRequestContext(const Wt::Http::Request& request)
return { parameters, _db.getTLSSession(), userId, clientInfo, getServerProtocolVersion(clientInfo.name) }; return { parameters, _db.getTLSSession(), userId, clientInfo, getServerProtocolVersion(clientInfo.name) };
} }
Database::UserId Database::UserId SubsonicResource::authenticateUser(const Wt::Http::Request& request, const ClientInfo& clientInfo)
SubsonicResource::authenticateUser(const Wt::Http::Request& request, const ClientInfo& clientInfo)
{ {
if (auto * authEnvService{ Service<::Auth::IEnvService>::get() }) if (auto * authEnvService{ Service<::Auth::IEnvService>::get() })
{ {
@@ -409,8 +396,7 @@ SubsonicResource::authenticateUser(const Wt::Http::Request& request, const Clien
} }
else if (auto * authPasswordService{ Service<::Auth::IPasswordService>::get() }) else if (auto * authPasswordService{ Service<::Auth::IPasswordService>::get() })
{ {
const auto checkResult {authPasswordService->checkUserPassword(boost::asio::ip::address::from_string(request.clientAddress()), const auto checkResult{ authPasswordService->checkUserPassword(boost::asio::ip::address::from_string(request.clientAddress()), clientInfo.user, clientInfo.password) };
clientInfo.user, clientInfo.password)};
switch (checkResult.state) switch (checkResult.state)
{ {
+17 -34
View File
@@ -34,8 +34,7 @@
namespace API::Subsonic namespace API::Subsonic
{ {
std::string std::string ResponseFormatToMimeType(ResponseFormat format)
ResponseFormatToMimeType(ResponseFormat format)
{ {
switch (format) switch (format)
{ {
@@ -46,8 +45,7 @@ ResponseFormatToMimeType(ResponseFormat format)
return ""; return "";
} }
void void Response::Node::setValue(std::string_view value)
Response::Node::setValue(std::string_view value)
{ {
if (!_children.empty() || !_childrenArrays.empty()) if (!_children.empty() || !_childrenArrays.empty())
throw LmsException{ "Node already has children" }; throw LmsException{ "Node already has children" };
@@ -55,8 +53,7 @@ Response::Node::setValue(std::string_view value)
_value = std::string{ value }; _value = std::string{ value };
} }
void void Response::Node::setValue(long long value)
Response::Node::setValue(long long value)
{ {
if (!_children.empty() || !_childrenArrays.empty()) if (!_children.empty() || !_childrenArrays.empty())
throw LmsException{ "Node already has children" }; throw LmsException{ "Node already has children" };
@@ -64,14 +61,12 @@ Response::Node::setValue(long long value)
_value = value; _value = value;
} }
void void Response::Node::setAttribute(std::string_view key, std::string_view value)
Response::Node::setAttribute(std::string_view key, std::string_view value)
{ {
_attributes[std::string{ key }] = std::string{ value }; _attributes[std::string{ key }] = std::string{ value };
} }
void void Response::Node::addChild(const std::string& key, Node node)
Response::Node::addChild(const std::string& key, Node node)
{ {
if (_value) if (_value)
throw LmsException{ "Node already has a value" }; throw LmsException{ "Node already has a value" };
@@ -79,8 +74,7 @@ Response::Node::addChild(const std::string& key, Node node)
_children[key].emplace_back(std::move(node)); _children[key].emplace_back(std::move(node));
} }
void void Response::Node::addArrayChild(const std::string& key, Node node)
Response::Node::addArrayChild(const std::string& key, Node node)
{ {
if (_value) if (_value)
throw LmsException{ "Node already has a value" }; throw LmsException{ "Node already has a value" };
@@ -89,28 +83,24 @@ Response::Node::addArrayChild(const std::string& key, Node node)
} }
Response::Node& Response::Node& Response::Node::createChild(const std::string& key)
Response::Node::createChild(const std::string& key)
{ {
_children[key].emplace_back(); _children[key].emplace_back();
return _children[key].back(); return _children[key].back();
} }
Response::Node& Response::Node& Response::Node::createArrayChild(const std::string& key)
Response::Node::createArrayChild(const std::string& key)
{ {
_childrenArrays[key].emplace_back(); _childrenArrays[key].emplace_back();
return _childrenArrays[key].back(); return _childrenArrays[key].back();
} }
void void Response::Node::setVersionAttribute(ProtocolVersion protocolVersion)
Response::Node::setVersionAttribute(ProtocolVersion protocolVersion)
{ {
setAttribute("version", std::to_string(protocolVersion.major) + "." + std::to_string(protocolVersion.minor) + "." + std::to_string(protocolVersion.patch)); setAttribute("version", std::to_string(protocolVersion.major) + "." + std::to_string(protocolVersion.minor) + "." + std::to_string(protocolVersion.patch));
} }
Response Response Response::createOkResponse(ProtocolVersion protocolVersion)
Response::createOkResponse(ProtocolVersion protocolVersion)
{ {
Response response; Response response;
Node& responseNode{ response._root.createChild("subsonic-response") }; Node& responseNode{ response._root.createChild("subsonic-response") };
@@ -122,8 +112,7 @@ Response::createOkResponse(ProtocolVersion protocolVersion)
return response; return response;
} }
Response Response Response::createFailedResponse(ProtocolVersion protocolVersion, const Error& error)
Response::createFailedResponse(ProtocolVersion protocolVersion, const Error& error)
{ {
Response response; Response response;
Node& responseNode{ response._root.createChild("subsonic-response") }; Node& responseNode{ response._root.createChild("subsonic-response") };
@@ -139,26 +128,22 @@ Response::createFailedResponse(ProtocolVersion protocolVersion, const Error& err
return response; return response;
} }
void void Response::addNode(const std::string& key, Node node)
Response::addNode(const std::string& key, Node node)
{ {
return _root._children["subsonic-response"].front().addChild(key, std::move(node)); return _root._children["subsonic-response"].front().addChild(key, std::move(node));
} }
Response::Node& Response::Node& Response::createNode(const std::string& key)
Response::createNode(const std::string& key)
{ {
return _root._children["subsonic-response"].front().createChild(key); return _root._children["subsonic-response"].front().createChild(key);
} }
Response::Node& Response::Node& Response::createArrayNode(const std::string& key)
Response::createArrayNode(const std::string& key)
{ {
return _root._children["subsonic-response"].front().createArrayChild(key); return _root._children["subsonic-response"].front().createArrayChild(key);
} }
void void Response::write(std::ostream& os, ResponseFormat format)
Response::write(std::ostream& os, ResponseFormat format)
{ {
switch (format) switch (format)
{ {
@@ -171,8 +156,7 @@ Response::write(std::ostream& os, ResponseFormat format)
} }
} }
void void Response::writeXML(std::ostream& os)
Response::writeXML(std::ostream& os)
{ {
std::function<boost::property_tree::ptree(const Response::Node&)> nodeToPropertyTree = [&](const Response::Node& node) std::function<boost::property_tree::ptree(const Response::Node&)> nodeToPropertyTree = [&](const Response::Node& node)
{ {
@@ -223,8 +207,7 @@ Response::writeXML(std::ostream& os)
boost::property_tree::write_xml(os, root); boost::property_tree::write_xml(os, root);
} }
void void Response::writeJSON(std::ostream& os)
Response::writeJSON(std::ostream& os)
{ {
namespace Json = Wt::Json; namespace Json = Wt::Json;