Changed coding style
This commit is contained in:
@@ -22,8 +22,7 @@
|
||||
namespace StringUtils
|
||||
{
|
||||
template<>
|
||||
std::optional<API::Subsonic::ProtocolVersion>
|
||||
readAs(std::string_view str)
|
||||
std::optional<API::Subsonic::ProtocolVersion> readAs(std::string_view str)
|
||||
{
|
||||
// Expects "X.Y.Z"
|
||||
const auto numbers{ StringUtils::splitString(str, ".") };
|
||||
|
||||
@@ -35,6 +35,7 @@ namespace API::Subsonic
|
||||
|
||||
namespace StringUtils
|
||||
{
|
||||
template<> std::optional<API::Subsonic::ProtocolVersion> readAs(std::string_view str);
|
||||
template<>
|
||||
std::optional<API::Subsonic::ProtocolVersion> readAs(std::string_view str);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,32 +26,27 @@
|
||||
|
||||
namespace API::Subsonic
|
||||
{
|
||||
std::string
|
||||
idToString(Database::ArtistId id)
|
||||
std::string idToString(Database::ArtistId id)
|
||||
{
|
||||
return "ar-" + id.toString();
|
||||
}
|
||||
|
||||
std::string
|
||||
idToString(Database::ReleaseId id)
|
||||
std::string idToString(Database::ReleaseId id)
|
||||
{
|
||||
return "al-" + id.toString();
|
||||
}
|
||||
|
||||
std::string
|
||||
idToString(RootId)
|
||||
std::string idToString(RootId)
|
||||
{
|
||||
return "root";
|
||||
}
|
||||
|
||||
std::string
|
||||
idToString(Database::TrackId id)
|
||||
std::string idToString(Database::TrackId id)
|
||||
{
|
||||
return "tr-" + id.toString();
|
||||
}
|
||||
|
||||
std::string
|
||||
idToString(Database::TrackListId id)
|
||||
std::string idToString(Database::TrackListId id)
|
||||
{
|
||||
return "pl-" + id.toString();
|
||||
}
|
||||
@@ -60,8 +55,7 @@ namespace API::Subsonic
|
||||
namespace StringUtils
|
||||
{
|
||||
template<>
|
||||
std::optional<Database::ArtistId>
|
||||
readAs(std::string_view str)
|
||||
std::optional<Database::ArtistId> readAs(std::string_view str)
|
||||
{
|
||||
std::vector<std::string_view> values{ StringUtils::splitString(str, "-") };
|
||||
if (values.size() != 2)
|
||||
@@ -77,8 +71,7 @@ namespace StringUtils
|
||||
}
|
||||
|
||||
template<>
|
||||
std::optional<Database::ReleaseId>
|
||||
readAs(std::string_view str)
|
||||
std::optional<Database::ReleaseId> readAs(std::string_view str)
|
||||
{
|
||||
std::vector<std::string_view> values{ StringUtils::splitString(str, "-") };
|
||||
if (values.size() != 2)
|
||||
@@ -94,8 +87,7 @@ namespace StringUtils
|
||||
}
|
||||
|
||||
template<>
|
||||
std::optional<API::Subsonic::RootId>
|
||||
readAs(std::string_view str)
|
||||
std::optional<API::Subsonic::RootId> readAs(std::string_view str)
|
||||
{
|
||||
if (str == "root")
|
||||
return API::Subsonic::RootId{};
|
||||
@@ -104,8 +96,7 @@ namespace StringUtils
|
||||
}
|
||||
|
||||
template<>
|
||||
std::optional<Database::TrackId>
|
||||
readAs(std::string_view str)
|
||||
std::optional<Database::TrackId> readAs(std::string_view str)
|
||||
{
|
||||
std::vector<std::string_view> values{ StringUtils::splitString(str, "-") };
|
||||
if (values.size() != 2)
|
||||
@@ -121,8 +112,7 @@ namespace StringUtils
|
||||
}
|
||||
|
||||
template<>
|
||||
std::optional<Database::TrackListId>
|
||||
readAs(std::string_view str)
|
||||
std::optional<Database::TrackListId> readAs(std::string_view str)
|
||||
{
|
||||
std::vector<std::string_view> values{ StringUtils::splitString(str, "-") };
|
||||
if (values.size() != 2)
|
||||
|
||||
@@ -40,23 +40,18 @@ namespace API::Subsonic
|
||||
namespace StringUtils
|
||||
{
|
||||
template<>
|
||||
std::optional<API::Subsonic::RootId>
|
||||
readAs(std::string_view str);
|
||||
std::optional<API::Subsonic::RootId> readAs(std::string_view str);
|
||||
|
||||
template<>
|
||||
std::optional<Database::ArtistId>
|
||||
readAs(std::string_view str);
|
||||
std::optional<Database::ArtistId> readAs(std::string_view str);
|
||||
|
||||
template<>
|
||||
std::optional<Database::ReleaseId>
|
||||
readAs(std::string_view str);
|
||||
std::optional<Database::ReleaseId> readAs(std::string_view str);
|
||||
|
||||
template<>
|
||||
std::optional<Database::TrackId>
|
||||
readAs(std::string_view str);
|
||||
std::optional<Database::TrackId> readAs(std::string_view str);
|
||||
|
||||
template<>
|
||||
std::optional<Database::TrackListId>
|
||||
readAs(std::string_view str);
|
||||
std::optional<Database::TrackListId> readAs(std::string_view str);
|
||||
}
|
||||
|
||||
|
||||
@@ -54,16 +54,14 @@ using namespace Database;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
static
|
||||
std::unordered_map<std::string, ProtocolVersion>
|
||||
readConfigProtocolVersions()
|
||||
namespace
|
||||
{
|
||||
std::unordered_map<std::string, ProtocolVersion> readConfigProtocolVersions()
|
||||
{
|
||||
std::unordered_map<std::string, ProtocolVersion> res;
|
||||
|
||||
@@ -76,13 +74,7 @@ readConfigProtocolVersions()
|
||||
return res;
|
||||
}
|
||||
|
||||
SubsonicResource::SubsonicResource(Db& db)
|
||||
: _serverProtocolVersionsByClient {readConfigProtocolVersions()}
|
||||
, _db {db}
|
||||
{
|
||||
}
|
||||
|
||||
static
|
||||
std::string parameterMapToDebugString(const Wt::Http::ParameterMap& parameterMap)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
checkUserTypeIsAllowed(RequestContext& context, EnumSet<Database::UserType> allowedUserTypes)
|
||||
void checkUserTypeIsAllowed(RequestContext& context, EnumSet<Database::UserType> allowedUserTypes)
|
||||
{
|
||||
auto transaction{ context.dbSession.createSharedTransaction() };
|
||||
|
||||
@@ -129,16 +119,12 @@ checkUserTypeIsAllowed(RequestContext& context, EnumSet<Database::UserType> allo
|
||||
throw UserNotAuthorizedError{};
|
||||
}
|
||||
|
||||
static
|
||||
Response
|
||||
handlePingRequest(RequestContext& context)
|
||||
Response handlePingRequest(RequestContext& context)
|
||||
{
|
||||
return Response::createOkResponse(context.serverProtocolVersion);
|
||||
}
|
||||
|
||||
static
|
||||
Response
|
||||
handleGetLicenseRequest(RequestContext& context)
|
||||
Response handleGetLicenseRequest(RequestContext& context)
|
||||
{
|
||||
Response response{ Response::createOkResponse(context.serverProtocolVersion) };
|
||||
|
||||
@@ -150,13 +136,12 @@ handleGetLicenseRequest(RequestContext& context)
|
||||
return response;
|
||||
}
|
||||
|
||||
static
|
||||
Response
|
||||
handleNotImplemented(RequestContext&)
|
||||
Response handleNotImplemented(RequestContext&)
|
||||
{
|
||||
throw NotImplementedGenericError{};
|
||||
}
|
||||
|
||||
|
||||
using RequestHandlerFunc = std::function<Response(RequestContext& context)>;
|
||||
using CheckImplementedFunc = std::function<void()>;
|
||||
struct RequestEntryPointInfo
|
||||
@@ -279,9 +264,16 @@ static std::unordered_map<std::string, MediaRetrievalHandlerFunc> mediaRetrieval
|
||||
{"/stream", handleStream},
|
||||
{"/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{};
|
||||
|
||||
@@ -343,8 +335,7 @@ SubsonicResource::handleRequest(const Wt::Http::Request &request, Wt::Http::Resp
|
||||
}
|
||||
}
|
||||
|
||||
ProtocolVersion
|
||||
SubsonicResource::getServerProtocolVersion(const std::string& clientName) const
|
||||
ProtocolVersion SubsonicResource::getServerProtocolVersion(const std::string& clientName) const
|
||||
{
|
||||
auto it{ _serverProtocolVersionsByClient.find(clientName) };
|
||||
if (it == std::cend(_serverProtocolVersionsByClient))
|
||||
@@ -353,8 +344,7 @@ SubsonicResource::getServerProtocolVersion(const std::string& clientName) const
|
||||
return it->second;
|
||||
}
|
||||
|
||||
void
|
||||
SubsonicResource::checkProtocolVersion(ProtocolVersion client, ProtocolVersion server)
|
||||
void SubsonicResource::checkProtocolVersion(ProtocolVersion client, ProtocolVersion server)
|
||||
{
|
||||
if (client.major > server.major)
|
||||
throw ServerMustUpgradeError{};
|
||||
@@ -369,8 +359,7 @@ SubsonicResource::checkProtocolVersion(ProtocolVersion client, ProtocolVersion s
|
||||
}
|
||||
}
|
||||
|
||||
ClientInfo
|
||||
SubsonicResource::getClientInfo(const Wt::Http::ParameterMap& parameters)
|
||||
ClientInfo SubsonicResource::getClientInfo(const Wt::Http::ParameterMap& parameters)
|
||||
{
|
||||
ClientInfo res;
|
||||
|
||||
@@ -386,8 +375,7 @@ SubsonicResource::getClientInfo(const Wt::Http::ParameterMap& parameters)
|
||||
return res;
|
||||
}
|
||||
|
||||
RequestContext
|
||||
SubsonicResource::buildRequestContext(const Wt::Http::Request& request)
|
||||
RequestContext SubsonicResource::buildRequestContext(const Wt::Http::Request& request)
|
||||
{
|
||||
const Wt::Http::ParameterMap& parameters{ request.getParameterMap() };
|
||||
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) };
|
||||
}
|
||||
|
||||
Database::UserId
|
||||
SubsonicResource::authenticateUser(const Wt::Http::Request& request, const ClientInfo& clientInfo)
|
||||
Database::UserId SubsonicResource::authenticateUser(const Wt::Http::Request& request, const ClientInfo& clientInfo)
|
||||
{
|
||||
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() })
|
||||
{
|
||||
const auto checkResult {authPasswordService->checkUserPassword(boost::asio::ip::address::from_string(request.clientAddress()),
|
||||
clientInfo.user, clientInfo.password)};
|
||||
const auto checkResult{ authPasswordService->checkUserPassword(boost::asio::ip::address::from_string(request.clientAddress()), clientInfo.user, clientInfo.password) };
|
||||
|
||||
switch (checkResult.state)
|
||||
{
|
||||
|
||||
@@ -34,8 +34,7 @@
|
||||
namespace API::Subsonic
|
||||
{
|
||||
|
||||
std::string
|
||||
ResponseFormatToMimeType(ResponseFormat format)
|
||||
std::string ResponseFormatToMimeType(ResponseFormat format)
|
||||
{
|
||||
switch (format)
|
||||
{
|
||||
@@ -46,8 +45,7 @@ ResponseFormatToMimeType(ResponseFormat format)
|
||||
return "";
|
||||
}
|
||||
|
||||
void
|
||||
Response::Node::setValue(std::string_view value)
|
||||
void Response::Node::setValue(std::string_view value)
|
||||
{
|
||||
if (!_children.empty() || !_childrenArrays.empty())
|
||||
throw LmsException{ "Node already has children" };
|
||||
@@ -55,8 +53,7 @@ Response::Node::setValue(std::string_view value)
|
||||
_value = std::string{ value };
|
||||
}
|
||||
|
||||
void
|
||||
Response::Node::setValue(long long value)
|
||||
void Response::Node::setValue(long long value)
|
||||
{
|
||||
if (!_children.empty() || !_childrenArrays.empty())
|
||||
throw LmsException{ "Node already has children" };
|
||||
@@ -64,14 +61,12 @@ Response::Node::setValue(long long value)
|
||||
_value = value;
|
||||
}
|
||||
|
||||
void
|
||||
Response::Node::setAttribute(std::string_view key, std::string_view value)
|
||||
void Response::Node::setAttribute(std::string_view key, std::string_view value)
|
||||
{
|
||||
_attributes[std::string{ key }] = std::string{ value };
|
||||
}
|
||||
|
||||
void
|
||||
Response::Node::addChild(const std::string& key, Node node)
|
||||
void Response::Node::addChild(const std::string& key, Node node)
|
||||
{
|
||||
if (_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));
|
||||
}
|
||||
|
||||
void
|
||||
Response::Node::addArrayChild(const std::string& key, Node node)
|
||||
void Response::Node::addArrayChild(const std::string& key, Node node)
|
||||
{
|
||||
if (_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::createChild(const std::string& key)
|
||||
Response::Node& Response::Node::createChild(const std::string& key)
|
||||
{
|
||||
_children[key].emplace_back();
|
||||
return _children[key].back();
|
||||
}
|
||||
|
||||
Response::Node&
|
||||
Response::Node::createArrayChild(const std::string& key)
|
||||
Response::Node& Response::Node::createArrayChild(const std::string& key)
|
||||
{
|
||||
_childrenArrays[key].emplace_back();
|
||||
return _childrenArrays[key].back();
|
||||
}
|
||||
|
||||
void
|
||||
Response::Node::setVersionAttribute(ProtocolVersion protocolVersion)
|
||||
void Response::Node::setVersionAttribute(ProtocolVersion protocolVersion)
|
||||
{
|
||||
setAttribute("version", std::to_string(protocolVersion.major) + "." + std::to_string(protocolVersion.minor) + "." + std::to_string(protocolVersion.patch));
|
||||
}
|
||||
|
||||
Response
|
||||
Response::createOkResponse(ProtocolVersion protocolVersion)
|
||||
Response Response::createOkResponse(ProtocolVersion protocolVersion)
|
||||
{
|
||||
Response response;
|
||||
Node& responseNode{ response._root.createChild("subsonic-response") };
|
||||
@@ -122,8 +112,7 @@ Response::createOkResponse(ProtocolVersion protocolVersion)
|
||||
return response;
|
||||
}
|
||||
|
||||
Response
|
||||
Response::createFailedResponse(ProtocolVersion protocolVersion, const Error& error)
|
||||
Response Response::createFailedResponse(ProtocolVersion protocolVersion, const Error& error)
|
||||
{
|
||||
Response response;
|
||||
Node& responseNode{ response._root.createChild("subsonic-response") };
|
||||
@@ -139,26 +128,22 @@ Response::createFailedResponse(ProtocolVersion protocolVersion, const Error& err
|
||||
return response;
|
||||
}
|
||||
|
||||
void
|
||||
Response::addNode(const std::string& key, Node node)
|
||||
void Response::addNode(const std::string& key, Node node)
|
||||
{
|
||||
return _root._children["subsonic-response"].front().addChild(key, std::move(node));
|
||||
}
|
||||
|
||||
Response::Node&
|
||||
Response::createNode(const std::string& key)
|
||||
Response::Node& Response::createNode(const std::string& key)
|
||||
{
|
||||
return _root._children["subsonic-response"].front().createChild(key);
|
||||
}
|
||||
|
||||
Response::Node&
|
||||
Response::createArrayNode(const std::string& key)
|
||||
Response::Node& Response::createArrayNode(const std::string& key)
|
||||
{
|
||||
return _root._children["subsonic-response"].front().createArrayChild(key);
|
||||
}
|
||||
|
||||
void
|
||||
Response::write(std::ostream& os, ResponseFormat format)
|
||||
void Response::write(std::ostream& os, ResponseFormat format)
|
||||
{
|
||||
switch (format)
|
||||
{
|
||||
@@ -171,8 +156,7 @@ Response::write(std::ostream& os, ResponseFormat format)
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
Response::writeXML(std::ostream& os)
|
||||
void Response::writeXML(std::ostream& os)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
void
|
||||
Response::writeJSON(std::ostream& os)
|
||||
void Response::writeJSON(std::ostream& os)
|
||||
{
|
||||
namespace Json = Wt::Json;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user