14c25d66eSEd Tanous #pragma once 24c25d66eSEd Tanous 33ccb3adbSEd Tanous #include "app.hpp" 481d523a7SEd Tanous #include "error_messages.hpp" 53ccb3adbSEd Tanous #include "http_request.hpp" 63ccb3adbSEd Tanous #include "http_response.hpp" 73ccb3adbSEd Tanous #include "query.hpp" 83ccb3adbSEd Tanous #include "registries/privilege_registry.hpp" 981d523a7SEd Tanous #include "utility.hpp" 1081d523a7SEd Tanous 11ef4c65b7SEd Tanous #include <boost/url/format.hpp> 12ef4c65b7SEd Tanous 133544d2a7SEd Tanous #include <ranges> 144c25d66eSEd Tanous #include <string> 154c25d66eSEd Tanous 164c25d66eSEd Tanous namespace redfish 174c25d66eSEd Tanous { 184c25d66eSEd Tanous 19d3355c5cSEd Tanous inline void redfishGet(App& app, const crow::Request& req, 20d3355c5cSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 214c25d66eSEd Tanous { 223ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 231e925c84SEd Tanous { 241e925c84SEd Tanous return; 251e925c84SEd Tanous } 261476687dSEd Tanous asyncResp->res.jsonValue["v1"] = "/redfish/v1/"; 27d3355c5cSEd Tanous } 28d3355c5cSEd Tanous 298c623a96SEd Tanous inline void redfish404(App& app, const crow::Request& req, 308c623a96SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 318c623a96SEd Tanous const std::string& path) 328c623a96SEd Tanous { 338c623a96SEd Tanous asyncResp->res.addHeader(boost::beast::http::field::allow, ""); 348c623a96SEd Tanous 358c623a96SEd Tanous // If we fall to this route, we didn't have a more specific route, so return 368c623a96SEd Tanous // 404 37686b7093SNan Zhou if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 388c623a96SEd Tanous { 398c623a96SEd Tanous return; 408c623a96SEd Tanous } 418c623a96SEd Tanous 4262598e31SEd Tanous BMCWEB_LOG_WARNING("404 on path {}", path); 438c623a96SEd Tanous 4439662a3bSEd Tanous std::string name = req.url().segments().back(); 458c623a96SEd Tanous // Note, if we hit the wildcard route, we don't know the "type" the user was 468c623a96SEd Tanous // actually requesting, but giving them a return with an empty string is 478c623a96SEd Tanous // still better than nothing. 48079360aeSEd Tanous messages::resourceNotFound(asyncResp->res, "", name); 498c623a96SEd Tanous } 508c623a96SEd Tanous 5144c70412SEd Tanous inline void redfish405(App& app, const crow::Request& req, 5244c70412SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 5344c70412SEd Tanous const std::string& path) 5444c70412SEd Tanous { 5544c70412SEd Tanous // If we fall to this route, we didn't have a more specific route, so return 5644c70412SEd Tanous // 405 5744c70412SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 5844c70412SEd Tanous { 5944c70412SEd Tanous return; 6044c70412SEd Tanous } 6144c70412SEd Tanous 6262598e31SEd Tanous BMCWEB_LOG_WARNING("405 on path {}", path); 6344c70412SEd Tanous asyncResp->res.result(boost::beast::http::status::method_not_allowed); 6444c70412SEd Tanous if (req.method() == boost::beast::http::verb::delete_) 6544c70412SEd Tanous { 6644c70412SEd Tanous messages::resourceCannotBeDeleted(asyncResp->res); 6744c70412SEd Tanous } 6844c70412SEd Tanous else 6944c70412SEd Tanous { 7044c70412SEd Tanous messages::operationNotAllowed(asyncResp->res); 7144c70412SEd Tanous } 7244c70412SEd Tanous } 7344c70412SEd Tanous 7481d523a7SEd Tanous inline void 7581d523a7SEd Tanous jsonSchemaIndexGet(App& app, const crow::Request& req, 7681d523a7SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 7781d523a7SEd Tanous { 7881d523a7SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 7981d523a7SEd Tanous { 8081d523a7SEd Tanous return; 8181d523a7SEd Tanous } 8281d523a7SEd Tanous nlohmann::json& json = asyncResp->res.jsonValue; 8381d523a7SEd Tanous json["@odata.id"] = "/redfish/v1/JsonSchemas"; 8481d523a7SEd Tanous json["@odata.type"] = "#JsonSchemaFileCollection.JsonSchemaFileCollection"; 8581d523a7SEd Tanous json["Name"] = "JsonSchemaFile Collection"; 8681d523a7SEd Tanous json["Description"] = "Collection of JsonSchemaFiles"; 8781d523a7SEd Tanous nlohmann::json::array_t members; 88a529a6aaSEd Tanous 89a529a6aaSEd Tanous std::error_code ec; 90a529a6aaSEd Tanous std::filesystem::directory_iterator dirList( 91a529a6aaSEd Tanous "/usr/share/www/redfish/v1/JsonSchemas", ec); 92a529a6aaSEd Tanous if (ec) 9381d523a7SEd Tanous { 94a529a6aaSEd Tanous messages::internalError(asyncResp->res); 95a529a6aaSEd Tanous return; 96a529a6aaSEd Tanous } 97a529a6aaSEd Tanous for (const std::filesystem::path& file : dirList) 98a529a6aaSEd Tanous { 99a529a6aaSEd Tanous std::string filename = file.filename(); 100a529a6aaSEd Tanous std::vector<std::string> split; 101a529a6aaSEd Tanous bmcweb::split(split, filename, '.'); 102a529a6aaSEd Tanous if (split.empty()) 103a529a6aaSEd Tanous { 104a529a6aaSEd Tanous continue; 105a529a6aaSEd Tanous } 10681d523a7SEd Tanous nlohmann::json::object_t member; 107bd79bce8SPatrick Williams member["@odata.id"] = 108bd79bce8SPatrick Williams boost::urls::format("/redfish/v1/JsonSchemas/{}", split[0]); 109ad539545SPatrick Williams members.emplace_back(std::move(member)); 11081d523a7SEd Tanous } 111a529a6aaSEd Tanous 112a529a6aaSEd Tanous json["Members@odata.count"] = members.size(); 11381d523a7SEd Tanous json["Members"] = std::move(members); 11481d523a7SEd Tanous } 11581d523a7SEd Tanous 11681d523a7SEd Tanous inline void jsonSchemaGet(App& app, const crow::Request& req, 11781d523a7SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 11881d523a7SEd Tanous const std::string& schema) 11981d523a7SEd Tanous { 12081d523a7SEd Tanous if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 12181d523a7SEd Tanous { 12281d523a7SEd Tanous return; 12381d523a7SEd Tanous } 12481d523a7SEd Tanous 125a529a6aaSEd Tanous std::error_code ec; 126a529a6aaSEd Tanous std::filesystem::directory_iterator dirList( 127a529a6aaSEd Tanous "/usr/share/www/redfish/v1/JsonSchemas", ec); 128a529a6aaSEd Tanous if (ec) 12981d523a7SEd Tanous { 130d8a5d5d8SJiaqing Zhao messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema); 13181d523a7SEd Tanous return; 13281d523a7SEd Tanous } 133a529a6aaSEd Tanous for (const std::filesystem::path& file : dirList) 134a529a6aaSEd Tanous { 135a529a6aaSEd Tanous std::string filename = file.filename(); 136a529a6aaSEd Tanous std::vector<std::string> split; 137a529a6aaSEd Tanous bmcweb::split(split, filename, '.'); 138a529a6aaSEd Tanous if (split.empty()) 139a529a6aaSEd Tanous { 140a529a6aaSEd Tanous continue; 141a529a6aaSEd Tanous } 142a529a6aaSEd Tanous BMCWEB_LOG_DEBUG("Checking {}", split[0]); 143a529a6aaSEd Tanous if (split[0] != schema) 144a529a6aaSEd Tanous { 145a529a6aaSEd Tanous continue; 146a529a6aaSEd Tanous } 14781d523a7SEd Tanous 14881d523a7SEd Tanous nlohmann::json& json = asyncResp->res.jsonValue; 149bd79bce8SPatrick Williams json["@odata.id"] = 150bd79bce8SPatrick Williams boost::urls::format("/redfish/v1/JsonSchemas/{}", schema); 15181d523a7SEd Tanous json["@odata.type"] = "#JsonSchemaFile.v1_0_2.JsonSchemaFile"; 15281d523a7SEd Tanous json["Name"] = schema + " Schema File"; 15381d523a7SEd Tanous json["Description"] = schema + " Schema File Location"; 15481d523a7SEd Tanous json["Id"] = schema; 155a529a6aaSEd Tanous std::string schemaName = std::format("#{}.{}", schema, schema); 15681d523a7SEd Tanous json["Schema"] = std::move(schemaName); 15781d523a7SEd Tanous constexpr std::array<std::string_view, 1> languages{"en"}; 15881d523a7SEd Tanous json["Languages"] = languages; 15981d523a7SEd Tanous json["Languages@odata.count"] = languages.size(); 16081d523a7SEd Tanous 16181d523a7SEd Tanous nlohmann::json::array_t locationArray; 16281d523a7SEd Tanous nlohmann::json::object_t locationEntry; 16381d523a7SEd Tanous locationEntry["Language"] = "en"; 164a529a6aaSEd Tanous 165a529a6aaSEd Tanous locationEntry["PublicationUri"] = boost::urls::format( 166a529a6aaSEd Tanous "http://redfish.dmtf.org/schemas/v1/{}", filename); 167ef4c65b7SEd Tanous locationEntry["Uri"] = boost::urls::format( 168a529a6aaSEd Tanous "/redfish/v1/JsonSchemas/{}/{}", schema, filename); 16981d523a7SEd Tanous 17081d523a7SEd Tanous locationArray.emplace_back(locationEntry); 17181d523a7SEd Tanous 17281d523a7SEd Tanous json["Location"] = std::move(locationArray); 17381d523a7SEd Tanous json["Location@odata.count"] = 1; 174a529a6aaSEd Tanous return; 175a529a6aaSEd Tanous } 176a529a6aaSEd Tanous messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema); 177a529a6aaSEd Tanous } 178a529a6aaSEd Tanous 179a529a6aaSEd Tanous inline void 180a529a6aaSEd Tanous jsonSchemaGetFile(const crow::Request& /*req*/, 181a529a6aaSEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 182a529a6aaSEd Tanous const std::string& schema, const std::string& schemaFile) 183a529a6aaSEd Tanous { 184a529a6aaSEd Tanous // Sanity check the filename 185a529a6aaSEd Tanous if (schemaFile.find_first_not_of( 186a529a6aaSEd Tanous "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.") != 187a529a6aaSEd Tanous std::string::npos) 188a529a6aaSEd Tanous { 189a529a6aaSEd Tanous messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema); 190a529a6aaSEd Tanous return; 191a529a6aaSEd Tanous } 192a529a6aaSEd Tanous // Schema path should look like /redfish/v1/JsonSchemas/Foo/Foo.x.json 193a529a6aaSEd Tanous // Make sure the two paths match. 194a529a6aaSEd Tanous if (!schemaFile.starts_with(schema)) 195a529a6aaSEd Tanous { 196a529a6aaSEd Tanous messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema); 197a529a6aaSEd Tanous return; 198a529a6aaSEd Tanous } 199a529a6aaSEd Tanous std::filesystem::path filepath("/usr/share/www/redfish/v1/JsonSchemas"); 200a529a6aaSEd Tanous filepath /= schemaFile; 201a529a6aaSEd Tanous if (filepath.is_relative()) 202a529a6aaSEd Tanous { 203a529a6aaSEd Tanous messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema); 204a529a6aaSEd Tanous return; 205a529a6aaSEd Tanous } 206a529a6aaSEd Tanous 207*d51c61b4SMyung Bae crow::OpenCode ec = asyncResp->res.openFile(filepath); 208*d51c61b4SMyung Bae if (ec == crow::OpenCode::FileDoesNotExist) 209*d51c61b4SMyung Bae { 210*d51c61b4SMyung Bae messages::resourceNotFound(asyncResp->res, "JsonSchemaFile", schema); 211*d51c61b4SMyung Bae return; 212*d51c61b4SMyung Bae } 213*d51c61b4SMyung Bae if (ec == crow::OpenCode::InternalError) 214a529a6aaSEd Tanous { 215a529a6aaSEd Tanous BMCWEB_LOG_DEBUG("failed to read file"); 216*d51c61b4SMyung Bae messages::internalError(asyncResp->res); 217a529a6aaSEd Tanous return; 218a529a6aaSEd Tanous } 21981d523a7SEd Tanous } 22081d523a7SEd Tanous 221f65fca6aSEd Tanous inline void requestRoutesRedfish(App& app) 222d3355c5cSEd Tanous { 223d3355c5cSEd Tanous BMCWEB_ROUTE(app, "/redfish/") 224d3355c5cSEd Tanous .methods(boost::beast::http::verb::get)( 225d3355c5cSEd Tanous std::bind_front(redfishGet, std::ref(app))); 2268c623a96SEd Tanous 227a529a6aaSEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/<str>") 228a529a6aaSEd Tanous .privileges(redfish::privileges::getJsonSchemaFile) 229a529a6aaSEd Tanous .methods(boost::beast::http::verb::get)(jsonSchemaGetFile); 230a529a6aaSEd Tanous 23181d523a7SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/<str>/") 2320ea4b4e2SEd Tanous .privileges(redfish::privileges::getJsonSchemaFileCollection) 23381d523a7SEd Tanous .methods(boost::beast::http::verb::get)( 23481d523a7SEd Tanous std::bind_front(jsonSchemaGet, std::ref(app))); 23581d523a7SEd Tanous 23681d523a7SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/JsonSchemas/") 2370ea4b4e2SEd Tanous .privileges(redfish::privileges::getJsonSchemaFile) 23881d523a7SEd Tanous .methods(boost::beast::http::verb::get)( 23981d523a7SEd Tanous std::bind_front(jsonSchemaIndexGet, std::ref(app))); 240e9dd1d31SEd Tanous 241e9dd1d31SEd Tanous // Note, this route must always be registered last 242e9dd1d31SEd Tanous BMCWEB_ROUTE(app, "/redfish/<path>") 2430ea4b4e2SEd Tanous .notFound() 2440ea4b4e2SEd Tanous .privileges(redfish::privileges::privilegeSetLogin)( 2450ea4b4e2SEd Tanous std::bind_front(redfish404, std::ref(app))); 24644c70412SEd Tanous 24744c70412SEd Tanous BMCWEB_ROUTE(app, "/redfish/<path>") 2480ea4b4e2SEd Tanous .methodNotAllowed() 2490ea4b4e2SEd Tanous .privileges(redfish::privileges::privilegeSetLogin)( 2500ea4b4e2SEd Tanous std::bind_front(redfish405, std::ref(app))); 2514c25d66eSEd Tanous } 252f65fca6aSEd Tanous 2534c25d66eSEd Tanous } // namespace redfish 254