diff options
author | Ed Tanous <ed.tanous@intel.com> | 2018-09-05 18:30:59 +0300 |
---|---|---|
committer | Ed Tanous <ed.tanous@intel.com> | 2018-09-05 18:44:12 +0300 |
commit | 1abe55ef9844afcddcab9d862ae06118f3a2390c (patch) | |
tree | d0b5fcfd0b1cd679a8995af3eb0b6d31b368380e /include/persistent_data_middleware.hpp | |
parent | a38b0b206300c792979b900f506b85e535f5708a (diff) | |
download | bmcweb-1abe55ef9844afcddcab9d862ae06118f3a2390c.tar.xz |
Move to clang-format-6.0
This commit moves the codebase to the lastest clang-format file from
upstream, as well as clang-format-6.0.
Change-Id: Ice8313468097c0c42317fbb9e10ddf036e8cff4c
Signed-off-by: Ed Tanous <ed.tanous@intel.com>
Diffstat (limited to 'include/persistent_data_middleware.hpp')
-rw-r--r-- | include/persistent_data_middleware.hpp | 234 |
1 files changed, 139 insertions, 95 deletions
diff --git a/include/persistent_data_middleware.hpp b/include/persistent_data_middleware.hpp index 706f6f423a..b384f02304 100644 --- a/include/persistent_data_middleware.hpp +++ b/include/persistent_data_middleware.hpp @@ -1,121 +1,165 @@ #pragma once -#include <nlohmann/json.hpp> -#include <pam_authenticate.hpp> -#include <sessions.hpp> -#include <webassets.hpp> -#include <random> #include <crow/app.h> #include <crow/http_request.h> #include <crow/http_response.h> + #include <boost/container/flat_map.hpp> #include <boost/uuid/uuid.hpp> #include <boost/uuid/uuid_generators.hpp> #include <boost/uuid/uuid_io.hpp> +#include <nlohmann/json.hpp> +#include <pam_authenticate.hpp> +#include <random> +#include <sessions.hpp> +#include <webassets.hpp> -namespace crow { - -namespace persistent_data { +namespace crow +{ -class Middleware { - // todo(ed) should read this from a fixed location somewhere, not CWD - static constexpr const char* filename = "bmcweb_persistent_data.json"; - int jsonRevision = 1; +namespace persistent_data +{ - public: - struct Context {}; +class Middleware +{ + // todo(ed) should read this from a fixed location somewhere, not CWD + static constexpr const char* filename = "bmcweb_persistent_data.json"; + int jsonRevision = 1; - Middleware() { readData(); } + public: + struct Context + { + }; - ~Middleware() { - if (persistent_data::SessionStore::getInstance().needsWrite()) { - writeData(); + Middleware() + { + readData(); } - } - - void beforeHandle(crow::Request& req, Response& res, Context& ctx) {} - - void afterHandle(Request& req, Response& res, Context& ctx) {} - - // TODO(ed) this should really use protobuf, or some other serialization - // library, but adding another dependency is somewhat outside the scope of - // this application for the moment - void readData() { - std::ifstream persistentFile(filename); - int fileRevision = 0; - if (persistentFile.is_open()) { - // call with exceptions disabled - auto data = nlohmann::json::parse(persistentFile, nullptr, false); - if (data.is_discarded()) { - BMCWEB_LOG_ERROR << "Error parsing persistent data in json file."; - } else { - for (const auto& item : data.items()) { - if (item.key() == "revision") { - fileRevision = 0; - - const uint64_t* uintPtr = item.value().get_ptr<const uint64_t*>(); - if (uintPtr == nullptr) { - BMCWEB_LOG_ERROR << "Failed to read revision flag"; - } else { - fileRevision = *uintPtr; - } - } else if (item.key() == "system_uuid") { - const std::string* jSystemUuid = - item.value().get_ptr<const std::string*>(); - if (jSystemUuid != nullptr) { - systemUuid = *jSystemUuid; - } - } else if (item.key() == "sessions") { - for (const auto& elem : item.value()) { - std::shared_ptr<UserSession> newSession = - UserSession::fromJson(elem); - if (newSession == nullptr) { - BMCWEB_LOG_ERROR - << "Problem reading session from persistent store"; - continue; - } - - BMCWEB_LOG_DEBUG << "Restored session: " << newSession->csrfToken - << " " << newSession->uniqueId << " " - << newSession->sessionToken; - SessionStore::getInstance().authTokens.emplace( - newSession->sessionToken, newSession); - } - } else { - // Do nothing in the case of extra fields. We may have cases where - // fields are added in the future, and we want to at least attempt - // to gracefully support downgrades in that case, even if we don't - // officially support it - } + ~Middleware() + { + if (persistent_data::SessionStore::getInstance().needsWrite()) + { + writeData(); } - } } - bool needWrite = false; - if (systemUuid.empty()) { - systemUuid = boost::uuids::to_string(boost::uuids::random_generator()()); - needWrite = true; + void beforeHandle(crow::Request& req, Response& res, Context& ctx) + { } - if (fileRevision < jsonRevision) { - needWrite = true; + + void afterHandle(Request& req, Response& res, Context& ctx) + { } - // write revision changes or system uuid changes immediately - if (needWrite) { - writeData(); + + // TODO(ed) this should really use protobuf, or some other serialization + // library, but adding another dependency is somewhat outside the scope of + // this application for the moment + void readData() + { + std::ifstream persistentFile(filename); + int fileRevision = 0; + if (persistentFile.is_open()) + { + // call with exceptions disabled + auto data = nlohmann::json::parse(persistentFile, nullptr, false); + if (data.is_discarded()) + { + BMCWEB_LOG_ERROR + << "Error parsing persistent data in json file."; + } + else + { + for (const auto& item : data.items()) + { + if (item.key() == "revision") + { + fileRevision = 0; + + const uint64_t* uintPtr = + item.value().get_ptr<const uint64_t*>(); + if (uintPtr == nullptr) + { + BMCWEB_LOG_ERROR << "Failed to read revision flag"; + } + else + { + fileRevision = *uintPtr; + } + } + else if (item.key() == "system_uuid") + { + const std::string* jSystemUuid = + item.value().get_ptr<const std::string*>(); + if (jSystemUuid != nullptr) + { + systemUuid = *jSystemUuid; + } + } + else if (item.key() == "sessions") + { + for (const auto& elem : item.value()) + { + std::shared_ptr<UserSession> newSession = + UserSession::fromJson(elem); + + if (newSession == nullptr) + { + BMCWEB_LOG_ERROR << "Problem reading session " + "from persistent store"; + continue; + } + + BMCWEB_LOG_DEBUG + << "Restored session: " << newSession->csrfToken + << " " << newSession->uniqueId << " " + << newSession->sessionToken; + SessionStore::getInstance().authTokens.emplace( + newSession->sessionToken, newSession); + } + } + else + { + // Do nothing in the case of extra fields. We may have + // cases where fields are added in the future, and we + // want to at least attempt to gracefully support + // downgrades in that case, even if we don't officially + // support it + } + } + } + } + bool needWrite = false; + + if (systemUuid.empty()) + { + systemUuid = + boost::uuids::to_string(boost::uuids::random_generator()()); + needWrite = true; + } + if (fileRevision < jsonRevision) + { + needWrite = true; + } + // write revision changes or system uuid changes immediately + if (needWrite) + { + writeData(); + } } - } - void writeData() { - std::ofstream persistentFile(filename); - nlohmann::json data{{"sessions", SessionStore::getInstance().authTokens}, - {"system_uuid", systemUuid}, - {"revision", jsonRevision}}; - persistentFile << data; - } + void writeData() + { + std::ofstream persistentFile(filename); + nlohmann::json data{ + {"sessions", SessionStore::getInstance().authTokens}, + {"system_uuid", systemUuid}, + {"revision", jsonRevision}}; + persistentFile << data; + } - std::string systemUuid{""}; + std::string systemUuid{""}; }; -} // namespace persistent_data -} // namespace crow +} // namespace persistent_data +} // namespace crow |