blob: 89f862d8b8d5d01a248637e9b30ac3869410d2a1 [file] [log] [blame]
Ed Tanousf4c99e72021-10-04 17:02:43 -07001#pragma once
2
Nan Zhoue796c262022-08-02 19:56:29 +00003#include "bmcweb_config.h"
4
5#include "app.hpp"
6#include "async_resp.hpp"
7#include "error_messages.hpp"
8#include "http_request.hpp"
9#include "http_response.hpp"
10#include "logging.hpp"
Ed Tanousf4c99e72021-10-04 17:02:43 -070011#include "utils/query_param.hpp"
12
Nan Zhoue796c262022-08-02 19:56:29 +000013#include <boost/beast/http/verb.hpp>
14#include <boost/url/params_view.hpp>
15#include <boost/url/url_view.hpp>
16
17#include <functional>
18#include <memory>
Nan Zhoue796c262022-08-02 19:56:29 +000019#include <optional>
20#include <string>
21#include <string_view>
22#include <type_traits>
23#include <utility>
24
25// IWYU pragma: no_forward_declare crow::App
Ed Tanousf4c99e72021-10-04 17:02:43 -070026
Ed Tanous3ccb3ad2023-01-13 17:40:03 -080027#include "redfish_aggregator.hpp"
Carson Labrado05916ce2022-08-01 19:38:18 +000028
Ed Tanousf4c99e72021-10-04 17:02:43 -070029namespace redfish
30{
Jonathan Doman102a4cd2024-04-15 16:56:23 -070031inline void afterIfMatchRequest(
32 crow::App& app, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
33 const std::shared_ptr<crow::Request>& req, const std::string& ifMatchHeader,
34 const crow::Response& resIn)
Ed Tanous2d6cb562022-07-07 20:44:54 -070035{
36 std::string computedEtag = resIn.computeEtag();
Ed Tanous62598e32023-07-17 17:06:25 -070037 BMCWEB_LOG_DEBUG("User provided if-match etag {} computed etag {}",
38 ifMatchHeader, computedEtag);
Ed Tanous2d6cb562022-07-07 20:44:54 -070039 if (computedEtag != ifMatchHeader)
40 {
41 messages::preconditionFailed(asyncResp->res);
42 return;
43 }
44 // Restart the request without if-match
Jonathan Doman102a4cd2024-04-15 16:56:23 -070045 req->clearHeader(boost::beast::http::field::if_match);
Ed Tanous62598e32023-07-17 17:06:25 -070046 BMCWEB_LOG_DEBUG("Restarting request");
Ed Tanous2d6cb562022-07-07 20:44:54 -070047 app.handle(req, asyncResp);
48}
49
50inline bool handleIfMatch(crow::App& app, const crow::Request& req,
51 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
52{
53 if (req.session == nullptr)
54 {
55 // If the user isn't authenticated, don't even attempt to parse match
56 // parameters
57 return true;
58 }
59
60 std::string ifMatch{
61 req.getHeaderValue(boost::beast::http::field::if_match)};
62 if (ifMatch.empty())
63 {
64 // No If-Match header. Nothing to do
65 return true;
66 }
Hieu Huynha1cbc192023-03-29 07:24:42 +000067 if (ifMatch == "*")
68 {
69 // Representing any resource
70 // https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-Match
71 return true;
72 }
Myung Bae1873a042024-04-01 09:27:39 -050073 if (req.method() != boost::beast::http::verb::patch &&
74 req.method() != boost::beast::http::verb::post &&
75 req.method() != boost::beast::http::verb::delete_)
Ed Tanous2d6cb562022-07-07 20:44:54 -070076 {
77 messages::preconditionFailed(asyncResp->res);
78 return false;
79 }
80 boost::system::error_code ec;
81
82 // Try to GET the same resource
Jonathan Doman102a4cd2024-04-15 16:56:23 -070083 auto getReq = std::make_shared<crow::Request>(
84 crow::Request::Body{boost::beast::http::verb::get,
85 req.url().encoded_path(), 11},
86 ec);
Ed Tanous2d6cb562022-07-07 20:44:54 -070087
88 if (ec)
89 {
90 messages::internalError(asyncResp->res);
91 return false;
92 }
93
94 // New request has the same credentials as the old request
Jonathan Doman102a4cd2024-04-15 16:56:23 -070095 getReq->session = req.session;
Ed Tanous2d6cb562022-07-07 20:44:54 -070096
97 // Construct a new response object to fill in, and check the hash of before
98 // we modify the Resource.
99 std::shared_ptr<bmcweb::AsyncResp> getReqAsyncResp =
100 std::make_shared<bmcweb::AsyncResp>();
101
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700102 // Ideally we would have a shared_ptr to the original Request which we could
103 // modify to remove the If-Match and restart it. But instead we have to make
104 // a full copy to restart it.
Ed Tanous2d6cb562022-07-07 20:44:54 -0700105 getReqAsyncResp->res.setCompleteRequestHandler(std::bind_front(
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700106 afterIfMatchRequest, std::ref(app), asyncResp,
107 std::make_shared<crow::Request>(req), std::move(ifMatch)));
Ed Tanous2d6cb562022-07-07 20:44:54 -0700108
Jonathan Doman102a4cd2024-04-15 16:56:23 -0700109 app.handle(getReq, getReqAsyncResp);
Ed Tanous2d6cb562022-07-07 20:44:54 -0700110 return false;
111}
Ed Tanousf4c99e72021-10-04 17:02:43 -0700112
Nan Zhoua6b91252022-04-04 13:10:40 -0700113// Sets up the Redfish Route and delegates some of the query parameter
114// processing. |queryCapabilities| stores which query parameters will be
115// handled by redfish-core/lib codes, then default query parameter handler won't
116// process these parameters.
117[[nodiscard]] inline bool setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +0000118 crow::App& app, const crow::Request& req,
119 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Nan Zhoua6b91252022-04-04 13:10:40 -0700120 query_param::Query& delegated,
121 const query_param::QueryCapabilities& queryCapabilities)
Ed Tanousf4c99e72021-10-04 17:02:43 -0700122{
Ed Tanous62598e32023-07-17 17:06:25 -0700123 BMCWEB_LOG_DEBUG("setup redfish route");
Ed Tanous142ec9a2022-03-24 18:20:45 -0700124
125 // Section 7.4 of the redfish spec "Redfish Services shall process the
126 // [OData-Version header] in the following table as defined by the HTTP 1.1
127 // specification..."
128 // Required to pass redfish-protocol-validator REQ_HEADERS_ODATA_VERSION
129 std::string_view odataHeader = req.getHeaderValue("OData-Version");
130 if (!odataHeader.empty() && odataHeader != "4.0")
131 {
Carson Labrado3ba00072022-06-06 19:40:56 +0000132 messages::preconditionFailed(asyncResp->res);
Ed Tanous142ec9a2022-03-24 18:20:45 -0700133 return false;
134 }
135
Carson Labrado3ba00072022-06-06 19:40:56 +0000136 asyncResp->res.addHeader("OData-Version", "4.0");
Ed Tanousc02a74f2022-05-11 14:46:44 -0700137
Ed Tanousf4c99e72021-10-04 17:02:43 -0700138 std::optional<query_param::Query> queryOpt =
Ed Tanous39662a32023-02-06 15:09:46 -0800139 query_param::parseParameters(req.url().params(), asyncResp->res);
Ed Tanouse01d0c32023-06-30 13:21:32 -0700140 if (!queryOpt)
Ed Tanousf4c99e72021-10-04 17:02:43 -0700141 {
142 return false;
143 }
144
Ed Tanous2d6cb562022-07-07 20:44:54 -0700145 if (!handleIfMatch(app, req, asyncResp))
146 {
147 return false;
148 }
149
Carson Labrado05916ce2022-08-01 19:38:18 +0000150 bool needToCallHandlers = true;
151
Ed Tanous25b54db2024-04-17 15:40:31 -0700152 if constexpr (BMCWEB_REDFISH_AGGREGATION)
153 {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400154 needToCallHandlers =
155 RedfishAggregator::beginAggregation(req, asyncResp) ==
156 Result::LocalHandle;
Carson Labrado05916ce2022-08-01 19:38:18 +0000157
Ed Tanous25b54db2024-04-17 15:40:31 -0700158 // If the request should be forwarded to a satellite BMC then we don't
159 // want to write anything to the asyncResp since it will get overwritten
160 // later.
161 }
Carson Labrado05916ce2022-08-01 19:38:18 +0000162
Ed Tanous7cf436c2022-03-22 23:53:51 -0700163 // If this isn't a get, no need to do anything with parameters
164 if (req.method() != boost::beast::http::verb::get)
165 {
Carson Labrado05916ce2022-08-01 19:38:18 +0000166 return needToCallHandlers;
Ed Tanous7cf436c2022-03-22 23:53:51 -0700167 }
168
Nan Zhoua6b91252022-04-04 13:10:40 -0700169 delegated = query_param::delegate(queryCapabilities, *queryOpt);
Ed Tanousf4c99e72021-10-04 17:02:43 -0700170 std::function<void(crow::Response&)> handler =
Carson Labrado3ba00072022-06-06 19:40:56 +0000171 asyncResp->res.releaseCompleteRequestHandler();
Nan Zhou827c4902022-08-03 04:57:55 +0000172
Carson Labrado3ba00072022-06-06 19:40:56 +0000173 asyncResp->res.setCompleteRequestHandler(
Willy Tu32cdb4a2023-05-23 10:58:39 -0700174 [&app, handler(std::move(handler)), query{std::move(*queryOpt)},
175 delegated{delegated}](crow::Response& resIn) mutable {
Patrick Williamsbd79bce2024-08-16 15:22:20 -0400176 processAllParams(app, query, delegated, handler, resIn);
177 });
Nan Zhou827c4902022-08-03 04:57:55 +0000178
Carson Labrado05916ce2022-08-01 19:38:18 +0000179 return needToCallHandlers;
Ed Tanousf4c99e72021-10-04 17:02:43 -0700180}
Nan Zhoua6b91252022-04-04 13:10:40 -0700181
182// Sets up the Redfish Route. All parameters are handled by the default handler.
Carson Labrado3ba00072022-06-06 19:40:56 +0000183[[nodiscard]] inline bool
184 setUpRedfishRoute(crow::App& app, const crow::Request& req,
185 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Nan Zhoua6b91252022-04-04 13:10:40 -0700186{
187 // This route |delegated| is never used
188 query_param::Query delegated;
Carson Labrado3ba00072022-06-06 19:40:56 +0000189 return setUpRedfishRouteWithDelegation(app, req, asyncResp, delegated,
Nan Zhoua6b91252022-04-04 13:10:40 -0700190 query_param::QueryCapabilities{});
191}
Ed Tanousf4c99e72021-10-04 17:02:43 -0700192} // namespace redfish