Rohit PAI | fdf51f5 | 2025-04-04 11:12:12 +0530 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors |
| 3 | #pragma once |
| 4 | |
| 5 | #include "http_request.hpp" |
rohitpai | 84aad24 | 2025-01-28 09:23:53 +0530 | [diff] [blame^] | 6 | #include "parsing.hpp" |
Rohit PAI | fdf51f5 | 2025-04-04 11:12:12 +0530 | [diff] [blame] | 7 | |
| 8 | #include <boost/beast/http/verb.hpp> |
rohitpai | 84aad24 | 2025-01-28 09:23:53 +0530 | [diff] [blame^] | 9 | #include <nlohmann/json.hpp> |
Rohit PAI | fdf51f5 | 2025-04-04 11:12:12 +0530 | [diff] [blame] | 10 | |
| 11 | #include <string> |
| 12 | #include <string_view> |
| 13 | |
| 14 | namespace redfish |
| 15 | { |
| 16 | |
| 17 | class SubRequest |
| 18 | { |
| 19 | public: |
| 20 | explicit SubRequest(const crow::Request& req) : |
| 21 | url_(req.url().encoded_path()), method_(req.method()) |
rohitpai | 84aad24 | 2025-01-28 09:23:53 +0530 | [diff] [blame^] | 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() && 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 PAI | fdf51f5 | 2025-04-04 11:12:12 +0530 | [diff] [blame] | 45 | |
| 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 | |
rohitpai | 84aad24 | 2025-01-28 09:23:53 +0530 | [diff] [blame^] | 56 | 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 PAI | fdf51f5 | 2025-04-04 11:12:12 +0530 | [diff] [blame] | 78 | private: |
| 79 | std::string url_; |
| 80 | boost::beast::http::verb method_; |
rohitpai | 84aad24 | 2025-01-28 09:23:53 +0530 | [diff] [blame^] | 81 | nlohmann::json::object_t payload_; |
Rohit PAI | fdf51f5 | 2025-04-04 11:12:12 +0530 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | } // namespace redfish |