blob: df8cf70b73c416d9b2975572ec64825a65991c1d [file] [log] [blame]
Rohit PAIfdf51f52025-04-04 11:12:12 +05301// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright OpenBMC Authors
3#pragma once
4
5#include "http_request.hpp"
rohitpai84aad242025-01-28 09:23:53 +05306#include "parsing.hpp"
Rohit PAIfdf51f52025-04-04 11:12:12 +05307
8#include <boost/beast/http/verb.hpp>
rohitpai84aad242025-01-28 09:23:53 +05309#include <nlohmann/json.hpp>
Rohit PAIfdf51f52025-04-04 11:12:12 +053010
11#include <string>
12#include <string_view>
13
14namespace redfish
15{
16
17class SubRequest
18{
19 public:
20 explicit SubRequest(const crow::Request& req) :
21 url_(req.url().encoded_path()), method_(req.method())
rohitpai84aad242025-01-28 09:23:53 +053022 {
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() && oemIt->is_object())
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 }
Rohit PAIfdf51f52025-04-04 11:12:12 +053045
46 std::string_view url() const
47 {
48 return url_;
49 }
50
51 boost::beast::http::verb method() const
52 {
53 return method_;
54 }
55
rohitpai84aad242025-01-28 09:23:53 +053056 const nlohmann::json::object_t& payload() const
57 {
58 return payload_;
59 }
60
61 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
Rohit PAIfdf51f52025-04-04 11:12:12 +053078 private:
79 std::string url_;
80 boost::beast::http::verb method_;
rohitpai84aad242025-01-28 09:23:53 +053081 nlohmann::json::object_t payload_;
Rohit PAIfdf51f52025-04-04 11:12:12 +053082};
83
84} // namespace redfish