summaryrefslogtreecommitdiff
path: root/redfish-core/include/sub_request.hpp
blob: c44644a8944a1ed37e924acc520d83ee3e44d1af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
#pragma once

#include "http_request.hpp"
#include "parsing.hpp"

#include <boost/beast/http/verb.hpp>
#include <nlohmann/json.hpp>

#include <string>
#include <string_view>

namespace redfish
{

class SubRequest
{
  public:
    explicit SubRequest(const crow::Request& req) :
        url_(req.url().encoded_path()), method_(req.method())
    {
        // Extract OEM payload if present
        if (req.method() == boost::beast::http::verb::patch ||
            req.method() == boost::beast::http::verb::post)
        {
            nlohmann::json reqJson;
            if (parseRequestAsJson(req, reqJson) != JsonParseResult::Success)
            {
                return;
            }

            auto oemIt = reqJson.find("Oem");
            if (oemIt != reqJson.end())
            {
                const nlohmann::json::object_t* oemObj =
                    oemIt->get_ptr<const nlohmann::json::object_t*>();
                if (oemObj != nullptr && !oemObj->empty())
                {
                    payload_ = *oemObj;
                }
            }
        }
    }

    std::string_view url() const
    {
        return url_;
    }

    boost::beast::http::verb method() const
    {
        return method_;
    }

    const nlohmann::json::object_t& payload() const
    {
        return payload_;
    }

    bool needHandling() const
    {
        if (method_ == boost::beast::http::verb::get)
        {
            return true;
        }

        if ((method_ == boost::beast::http::verb::patch ||
             method_ == boost::beast::http::verb::post) &&
            !payload_.empty())
        {
            return true;
        }

        return false;
    }

  private:
    std::string url_;
    boost::beast::http::verb method_;
    nlohmann::json::object_t payload_;
};

} // namespace redfish