blob: df61aefc98f7930b6e6365a2aacecce4c04c3f80 [file] [log] [blame]
Ed Tanousf4c99e72021-10-04 17:02:43 -07001#pragma once
2
3#include "utils/query_param.hpp"
4
5#include <bmcweb_config.h>
6
7namespace redfish
8{
9
Nan Zhoua6b91252022-04-04 13:10:40 -070010// Sets up the Redfish Route and delegates some of the query parameter
11// processing. |queryCapabilities| stores which query parameters will be
12// handled by redfish-core/lib codes, then default query parameter handler won't
13// process these parameters.
14[[nodiscard]] inline bool setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +000015 crow::App& app, const crow::Request& req,
16 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Nan Zhoua6b91252022-04-04 13:10:40 -070017 query_param::Query& delegated,
18 const query_param::QueryCapabilities& queryCapabilities)
Ed Tanousf4c99e72021-10-04 17:02:43 -070019{
Ed Tanous142ec9a2022-03-24 18:20:45 -070020 BMCWEB_LOG_DEBUG << "setup redfish route";
21
22 // Section 7.4 of the redfish spec "Redfish Services shall process the
23 // [OData-Version header] in the following table as defined by the HTTP 1.1
24 // specification..."
25 // Required to pass redfish-protocol-validator REQ_HEADERS_ODATA_VERSION
26 std::string_view odataHeader = req.getHeaderValue("OData-Version");
27 if (!odataHeader.empty() && odataHeader != "4.0")
28 {
Carson Labrado3ba00072022-06-06 19:40:56 +000029 messages::preconditionFailed(asyncResp->res);
Ed Tanous142ec9a2022-03-24 18:20:45 -070030 return false;
31 }
32
Carson Labrado3ba00072022-06-06 19:40:56 +000033 asyncResp->res.addHeader("OData-Version", "4.0");
Ed Tanousc02a74f2022-05-11 14:46:44 -070034
Ed Tanousf4c99e72021-10-04 17:02:43 -070035 std::optional<query_param::Query> queryOpt =
Carson Labrado3ba00072022-06-06 19:40:56 +000036 query_param::parseParameters(req.urlView.params(), asyncResp->res);
Ed Tanousf4c99e72021-10-04 17:02:43 -070037 if (queryOpt == std::nullopt)
38 {
39 return false;
40 }
41
Ed Tanous7cf436c2022-03-22 23:53:51 -070042 // If this isn't a get, no need to do anything with parameters
43 if (req.method() != boost::beast::http::verb::get)
44 {
45 return true;
46 }
47
Nan Zhoua6b91252022-04-04 13:10:40 -070048 delegated = query_param::delegate(queryCapabilities, *queryOpt);
Ed Tanousf4c99e72021-10-04 17:02:43 -070049 std::function<void(crow::Response&)> handler =
Carson Labrado3ba00072022-06-06 19:40:56 +000050 asyncResp->res.releaseCompleteRequestHandler();
51 asyncResp->res.setCompleteRequestHandler(
Ed Tanousf4c99e72021-10-04 17:02:43 -070052 [&app, handler(std::move(handler)),
Ed Tanous8a592812022-06-04 09:06:59 -070053 query{*queryOpt}](crow::Response& resIn) mutable {
54 processAllParams(app, query, handler, resIn);
Ed Tanous002d39b2022-05-31 08:59:27 -070055 });
Ed Tanousf4c99e72021-10-04 17:02:43 -070056 return true;
57}
Nan Zhoua6b91252022-04-04 13:10:40 -070058
59// Sets up the Redfish Route. All parameters are handled by the default handler.
Carson Labrado3ba00072022-06-06 19:40:56 +000060[[nodiscard]] inline bool
61 setUpRedfishRoute(crow::App& app, const crow::Request& req,
62 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Nan Zhoua6b91252022-04-04 13:10:40 -070063{
64 // This route |delegated| is never used
65 query_param::Query delegated;
Carson Labrado3ba00072022-06-06 19:40:56 +000066 return setUpRedfishRouteWithDelegation(app, req, asyncResp, delegated,
Nan Zhoua6b91252022-04-04 13:10:40 -070067 query_param::QueryCapabilities{});
68}
Ed Tanousf4c99e72021-10-04 17:02:43 -070069} // namespace redfish