xref: /openbmc/bmcweb/redfish-core/include/utils/etag_utils.hpp (revision 0ddb8edfb150ddf93b069e655ce3624d1e2fec54)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #pragma once
4 
5 #include "async_resp.hpp"
6 #include "http_response.hpp"
7 #include "json_utils.hpp"
8 
9 #include <nlohmann/json.hpp>
10 
11 #include <functional>
12 #include <memory>
13 #include <utility>
14 
15 namespace redfish
16 {
17 
18 namespace etag_utils
19 {
20 
21 namespace details
22 {
23 
etagOmitDateTimeHandler(const std::function<void (crow::Response &)> & oldCompleteRequestHandler,crow::Response & res)24 inline void etagOmitDateTimeHandler(
25     const std::function<void(crow::Response&)>& oldCompleteRequestHandler,
26     crow::Response& res)
27 {
28     size_t hash = json_util::hashJsonWithoutKey(res.jsonValue, "DateTime");
29     std::string etag = std::format("\"{:08X}\"", hash);
30     res.setCurrentOverrideEtag(etag);
31     oldCompleteRequestHandler(res);
32 }
33 
34 } // namespace details
35 
setEtagOmitDateTimeHandler(const std::shared_ptr<bmcweb::AsyncResp> & asyncResp)36 inline void setEtagOmitDateTimeHandler(
37     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
38 {
39     std::function<void(crow::Response&)> oldCompleteRequestHandler =
40         asyncResp->res.releaseCompleteRequestHandler();
41     asyncResp->res.setCompleteRequestHandler(
42         std::bind_front(details::etagOmitDateTimeHandler,
43                         std::move(oldCompleteRequestHandler)));
44 }
45 
46 } // namespace etag_utils
47 
48 } // namespace redfish
49