| 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( | 
|  | 15 | crow::App& app, const crow::Request& req, crow::Response& res, | 
|  | 16 | query_param::Query& delegated, | 
|  | 17 | const query_param::QueryCapabilities& queryCapabilities) | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 18 | { | 
| Ed Tanous | 142ec9a | 2022-03-24 18:20:45 -0700 | [diff] [blame] | 19 | BMCWEB_LOG_DEBUG << "setup redfish route"; | 
|  | 20 |  | 
|  | 21 | // Section 7.4 of the redfish spec "Redfish Services shall process the | 
|  | 22 | // [OData-Version header] in the following table as defined by the HTTP 1.1 | 
|  | 23 | // specification..." | 
|  | 24 | // Required to pass redfish-protocol-validator REQ_HEADERS_ODATA_VERSION | 
|  | 25 | std::string_view odataHeader = req.getHeaderValue("OData-Version"); | 
|  | 26 | if (!odataHeader.empty() && odataHeader != "4.0") | 
|  | 27 | { | 
|  | 28 | messages::preconditionFailed(res); | 
|  | 29 | return false; | 
|  | 30 | } | 
|  | 31 |  | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 32 | std::optional<query_param::Query> queryOpt = | 
|  | 33 | query_param::parseParameters(req.urlView.params(), res); | 
|  | 34 | if (queryOpt == std::nullopt) | 
|  | 35 | { | 
|  | 36 | return false; | 
|  | 37 | } | 
|  | 38 |  | 
| Ed Tanous | 7cf436c | 2022-03-22 23:53:51 -0700 | [diff] [blame] | 39 | // If this isn't a get, no need to do anything with parameters | 
|  | 40 | if (req.method() != boost::beast::http::verb::get) | 
|  | 41 | { | 
|  | 42 | return true; | 
|  | 43 | } | 
|  | 44 |  | 
| Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 45 | delegated = query_param::delegate(queryCapabilities, *queryOpt); | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 46 | std::function<void(crow::Response&)> handler = | 
|  | 47 | res.releaseCompleteRequestHandler(); | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 48 | res.setCompleteRequestHandler( | 
|  | 49 | [&app, handler(std::move(handler)), | 
|  | 50 | query{*queryOpt}](crow::Response& res) mutable { | 
| Ed Tanous | 7cf436c | 2022-03-22 23:53:51 -0700 | [diff] [blame] | 51 | processAllParams(app, query, handler, res); | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 52 | }); | 
|  | 53 | return true; | 
|  | 54 | } | 
| Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 55 |  | 
|  | 56 | // Sets up the Redfish Route. All parameters are handled by the default handler. | 
|  | 57 | [[nodiscard]] inline bool setUpRedfishRoute(crow::App& app, | 
|  | 58 | const crow::Request& req, | 
|  | 59 | crow::Response& res) | 
|  | 60 | { | 
|  | 61 | // This route |delegated| is never used | 
|  | 62 | query_param::Query delegated; | 
|  | 63 | return setUpRedfishRouteWithDelegation(app, req, res, delegated, | 
|  | 64 | query_param::QueryCapabilities{}); | 
|  | 65 | } | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 66 | } // namespace redfish |