summaryrefslogtreecommitdiff
path: root/redfish-core/include/utils/redfish_aggregator_utils.hpp
blob: 34148a5ad3e6e4926f071a3e0eea943327529f8e (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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
#pragma once
#include "http_request.hpp"

#include <boost/beast/http/field.hpp>
#include <boost/beast/http/fields.hpp>
#include <boost/url/url.hpp>

#include <memory>
namespace redfish
{
// Drop any incoming x-auth-token headers and keep Host and Content-Type. Set
// Accept.
inline crow::Request createNewRequest(const crow::Request& localReq)
{
    boost::system::error_code ec;
    // Note, this is an expensive copy.  It ideally shouldn't be done, but no
    // option at this point.
    crow::Request req(localReq.body(), ec);
    if (ec)
    {
        BMCWEB_LOG_ERROR("Failed to set body.  Continuing");
    }

    // Preserve method and target (URI) from the original request
    req.method(localReq.method());
    if (!req.target(localReq.target()))
    {
        BMCWEB_LOG_ERROR("Failed to set target on aggregated request");
    }

    for (const auto& field : localReq.fields())
    {
        // Drop any incoming x-auth-token headers and keep Host and
        // Content-Type. Set Accept.
        auto headerName = field.name();
        if (headerName == boost::beast::http::field::content_type ||
            headerName == boost::beast::http::field::host)
        {
            req.addHeader(headerName, field.value());
        }
    }
    // Set Accept header to application/json, application/octet-stream
    req.addHeader(boost::beast::http::field::accept,
                  "application/json, application/octet-stream");
    return req;
}
} // namespace redfish