xref: /openbmc/bmcweb/redfish-core/include/sub_request.hpp (revision d0a341b96370e44a815de13205d19552fb3b1650)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #pragma once
4 
5 #include "http_request.hpp"
6 #include "parsing.hpp"
7 
8 #include <boost/beast/http/verb.hpp>
9 #include <nlohmann/json.hpp>
10 
11 #include <string>
12 #include <string_view>
13 
14 namespace redfish
15 {
16 
17 class SubRequest
18 {
19   public:
SubRequest(const crow::Request & req)20     explicit SubRequest(const crow::Request& req) :
21         url_(req.url().encoded_path()), method_(req.method())
22     {
23         // Extract OEM payload if present
24         if (req.method() == boost::beast::http::verb::patch ||
25             req.method() == boost::beast::http::verb::post)
26         {
27             nlohmann::json reqJson;
28             if (parseRequestAsJson(req, reqJson) != JsonParseResult::Success)
29             {
30                 return;
31             }
32 
33             auto oemIt = reqJson.find("Oem");
34             if (oemIt != reqJson.end())
35             {
36                 const nlohmann::json::object_t* oemObj =
37                     oemIt->get_ptr<const nlohmann::json::object_t*>();
38                 if (oemObj != nullptr && !oemObj->empty())
39                 {
40                     payload_ = *oemObj;
41                 }
42             }
43         }
44     }
45 
url() const46     std::string_view url() const
47     {
48         return url_;
49     }
50 
method() const51     boost::beast::http::verb method() const
52     {
53         return method_;
54     }
55 
payload() const56     const nlohmann::json::object_t& payload() const
57     {
58         return payload_;
59     }
60 
needHandling() const61     bool needHandling() const
62     {
63         if (method_ == boost::beast::http::verb::get)
64         {
65             return true;
66         }
67 
68         if ((method_ == boost::beast::http::verb::patch ||
69              method_ == boost::beast::http::verb::post) &&
70             !payload_.empty())
71         {
72             return true;
73         }
74 
75         return false;
76     }
77 
78   private:
79     std::string url_;
80     boost::beast::http::verb method_;
81     nlohmann::json::object_t payload_;
82 };
83 
84 } // namespace redfish
85