| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 1 | #pragma once | 
 | 2 |  | 
| Nan Zhou | e796c26 | 2022-08-02 19:56:29 +0000 | [diff] [blame] | 3 | #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 Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 11 | #include "utils/query_param.hpp" | 
 | 12 |  | 
| Nan Zhou | e796c26 | 2022-08-02 19:56:29 +0000 | [diff] [blame] | 13 | #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> | 
 | 19 | #include <new> | 
 | 20 | #include <optional> | 
 | 21 | #include <string> | 
 | 22 | #include <string_view> | 
 | 23 | #include <type_traits> | 
 | 24 | #include <utility> | 
 | 25 |  | 
 | 26 | // IWYU pragma: no_forward_declare crow::App | 
 | 27 | // IWYU pragma: no_include <boost/url/impl/params_view.hpp> | 
 | 28 | // IWYU pragma: no_include <boost/url/impl/url_view.hpp> | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 29 |  | 
| Carson Labrado | 05916ce | 2022-08-01 19:38:18 +0000 | [diff] [blame] | 30 | #include <redfish_aggregator.hpp> | 
 | 31 |  | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 32 | namespace redfish | 
 | 33 | { | 
 | 34 |  | 
| Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 35 | // Sets up the Redfish Route and delegates some of the query parameter | 
 | 36 | // processing. |queryCapabilities| stores which query parameters will be | 
 | 37 | // handled by redfish-core/lib codes, then default query parameter handler won't | 
 | 38 | // process these parameters. | 
 | 39 | [[nodiscard]] inline bool setUpRedfishRouteWithDelegation( | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 40 |     crow::App& app, const crow::Request& req, | 
 | 41 |     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, | 
| Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 42 |     query_param::Query& delegated, | 
 | 43 |     const query_param::QueryCapabilities& queryCapabilities) | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 44 | { | 
| Ed Tanous | 142ec9a | 2022-03-24 18:20:45 -0700 | [diff] [blame] | 45 |     BMCWEB_LOG_DEBUG << "setup redfish route"; | 
 | 46 |  | 
 | 47 |     // Section 7.4 of the redfish spec "Redfish Services shall process the | 
 | 48 |     // [OData-Version header] in the following table as defined by the HTTP 1.1 | 
 | 49 |     // specification..." | 
 | 50 |     // Required to pass redfish-protocol-validator REQ_HEADERS_ODATA_VERSION | 
 | 51 |     std::string_view odataHeader = req.getHeaderValue("OData-Version"); | 
 | 52 |     if (!odataHeader.empty() && odataHeader != "4.0") | 
 | 53 |     { | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 54 |         messages::preconditionFailed(asyncResp->res); | 
| Ed Tanous | 142ec9a | 2022-03-24 18:20:45 -0700 | [diff] [blame] | 55 |         return false; | 
 | 56 |     } | 
 | 57 |  | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 58 |     asyncResp->res.addHeader("OData-Version", "4.0"); | 
| Ed Tanous | c02a74f | 2022-05-11 14:46:44 -0700 | [diff] [blame] | 59 |  | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 60 |     std::optional<query_param::Query> queryOpt = | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 61 |         query_param::parseParameters(req.urlView.params(), asyncResp->res); | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 62 |     if (queryOpt == std::nullopt) | 
 | 63 |     { | 
 | 64 |         return false; | 
 | 65 |     } | 
 | 66 |  | 
| Carson Labrado | 05916ce | 2022-08-01 19:38:18 +0000 | [diff] [blame] | 67 |     bool needToCallHandlers = true; | 
 | 68 |  | 
 | 69 | #ifdef BMCWEB_ENABLE_REDFISH_AGGREGATION | 
 | 70 |     needToCallHandlers = RedfishAggregator::getInstance().beginAggregation( | 
 | 71 |                              req, asyncResp) == Result::LocalHandle; | 
 | 72 |  | 
 | 73 |     // If the request should be forwarded to a satellite BMC then we don't want | 
 | 74 |     // to write anything to the asyncResp since it will get overwritten later. | 
 | 75 | #endif | 
 | 76 |  | 
| Ed Tanous | 7cf436c | 2022-03-22 23:53:51 -0700 | [diff] [blame] | 77 |     // If this isn't a get, no need to do anything with parameters | 
 | 78 |     if (req.method() != boost::beast::http::verb::get) | 
 | 79 |     { | 
| Carson Labrado | 05916ce | 2022-08-01 19:38:18 +0000 | [diff] [blame] | 80 |         return needToCallHandlers; | 
| Ed Tanous | 7cf436c | 2022-03-22 23:53:51 -0700 | [diff] [blame] | 81 |     } | 
 | 82 |  | 
| Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 83 |     delegated = query_param::delegate(queryCapabilities, *queryOpt); | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 84 |     std::function<void(crow::Response&)> handler = | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 85 |         asyncResp->res.releaseCompleteRequestHandler(); | 
| Nan Zhou | 827c490 | 2022-08-03 04:57:55 +0000 | [diff] [blame] | 86 |  | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 87 |     asyncResp->res.setCompleteRequestHandler( | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 88 |         [&app, handler(std::move(handler)), | 
| Nan Zhou | 827c490 | 2022-08-03 04:57:55 +0000 | [diff] [blame] | 89 |          query{std::move(*queryOpt)}](crow::Response& resIn) mutable { | 
| Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 90 |         processAllParams(app, query, handler, resIn); | 
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 91 |     }); | 
| Nan Zhou | 827c490 | 2022-08-03 04:57:55 +0000 | [diff] [blame] | 92 |  | 
| Carson Labrado | 05916ce | 2022-08-01 19:38:18 +0000 | [diff] [blame] | 93 |     return needToCallHandlers; | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 94 | } | 
| Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 95 |  | 
 | 96 | // 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] | 97 | [[nodiscard]] inline bool | 
 | 98 |     setUpRedfishRoute(crow::App& app, const crow::Request& req, | 
 | 99 |                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) | 
| Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 100 | { | 
 | 101 |     // This route |delegated| is never used | 
 | 102 |     query_param::Query delegated; | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 103 |     return setUpRedfishRouteWithDelegation(app, req, asyncResp, delegated, | 
| Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 104 |                                            query_param::QueryCapabilities{}); | 
 | 105 | } | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 106 | } // namespace redfish |