Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "utils/query_param.hpp" |
| 4 | |
| 5 | #include <bmcweb_config.h> |
| 6 | |
| 7 | namespace redfish |
| 8 | { |
| 9 | |
Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 10 | // 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 Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 15 | crow::App& app, const crow::Request& req, |
| 16 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 17 | query_param::Query& delegated, |
| 18 | const query_param::QueryCapabilities& queryCapabilities) |
Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 19 | { |
Ed Tanous | 142ec9a | 2022-03-24 18:20:45 -0700 | [diff] [blame] | 20 | 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 Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 29 | messages::preconditionFailed(asyncResp->res); |
Ed Tanous | 142ec9a | 2022-03-24 18:20:45 -0700 | [diff] [blame] | 30 | return false; |
| 31 | } |
| 32 | |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 33 | asyncResp->res.addHeader("OData-Version", "4.0"); |
Ed Tanous | c02a74f | 2022-05-11 14:46:44 -0700 | [diff] [blame] | 34 | |
Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 35 | std::optional<query_param::Query> queryOpt = |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 36 | query_param::parseParameters(req.urlView.params(), asyncResp->res); |
Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 37 | if (queryOpt == std::nullopt) |
| 38 | { |
| 39 | return false; |
| 40 | } |
| 41 | |
Ed Tanous | 7cf436c | 2022-03-22 23:53:51 -0700 | [diff] [blame] | 42 | // 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 Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 48 | delegated = query_param::delegate(queryCapabilities, *queryOpt); |
Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 49 | std::function<void(crow::Response&)> handler = |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 50 | asyncResp->res.releaseCompleteRequestHandler(); |
| 51 | asyncResp->res.setCompleteRequestHandler( |
Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 52 | [&app, handler(std::move(handler)), |
Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 53 | query{*queryOpt}](crow::Response& resIn) mutable { |
| 54 | processAllParams(app, query, handler, resIn); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 55 | }); |
Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 56 | return true; |
| 57 | } |
Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 58 | |
| 59 | // Sets up the Redfish Route. All parameters are handled by the default handler. |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 60 | [[nodiscard]] inline bool |
| 61 | setUpRedfishRoute(crow::App& app, const crow::Request& req, |
| 62 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 63 | { |
| 64 | // This route |delegated| is never used |
| 65 | query_param::Query delegated; |
Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 66 | return setUpRedfishRouteWithDelegation(app, req, asyncResp, delegated, |
Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 67 | query_param::QueryCapabilities{}); |
| 68 | } |
Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 69 | } // namespace redfish |