xref: /openbmc/bmcweb/features/redfish/include/utils/redfish_aggregator_utils.hpp (revision 456512bc3fa1cd6318da38c7f1d162c8c4db9336)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright OpenBMC Authors
3 #pragma once
4 #include "http_request.hpp"
5 
6 #include <boost/beast/http/field.hpp>
7 #include <boost/beast/http/fields.hpp>
8 #include <boost/url/url.hpp>
9 
10 #include <memory>
11 namespace redfish
12 {
13 // Drop any incoming x-auth-token headers and keep Host and Content-Type. Set
14 // Accept.
15 inline crow::Request createNewRequest(const crow::Request& localReq)
16 {
17     boost::system::error_code ec;
18     // Note, this is an expensive copy.  It ideally shouldn't be done, but no
19     // option at this point.
20     crow::Request req(localReq.body(), ec);
21     if (ec)
22     {
23         BMCWEB_LOG_ERROR("Failed to set body.  Continuing");
24     }
25 
26     // Preserve method and target (URI) from the original request
27     req.method(localReq.method());
28     if (!req.target(localReq.target()))
29     {
30         BMCWEB_LOG_ERROR("Failed to set target on aggregated request");
31     }
32 
33     for (const auto& field : localReq.fields())
34     {
35         // Drop any incoming x-auth-token headers and keep Host and
36         // Content-Type. Set Accept.
37         auto headerName = field.name();
38         if (headerName == boost::beast::http::field::content_type ||
39             headerName == boost::beast::http::field::host)
40         {
41             req.addHeader(headerName, field.value());
42         }
43     }
44     // Set Accept header to application/json, application/octet-stream
45     req.addHeader(boost::beast::http::field::accept,
46                   "application/json, application/octet-stream");
47     return req;
48 }
49 } // namespace redfish
50