| 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 |  | 
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 30 | #include "redfish_aggregator.hpp" | 
| Carson Labrado | 05916ce | 2022-08-01 19:38:18 +0000 | [diff] [blame] | 31 |  | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 32 | namespace redfish | 
|  | 33 | { | 
| Ed Tanous | 2d6cb56 | 2022-07-07 20:44:54 -0700 | [diff] [blame] | 34 | inline void | 
|  | 35 | afterIfMatchRequest(crow::App& app, | 
|  | 36 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, | 
|  | 37 | crow::Request& req, const std::string& ifMatchHeader, | 
|  | 38 | const crow::Response& resIn) | 
|  | 39 | { | 
|  | 40 | std::string computedEtag = resIn.computeEtag(); | 
|  | 41 | BMCWEB_LOG_DEBUG << "User provided if-match etag " << ifMatchHeader | 
|  | 42 | << " computed etag " << computedEtag; | 
|  | 43 | if (computedEtag != ifMatchHeader) | 
|  | 44 | { | 
|  | 45 | messages::preconditionFailed(asyncResp->res); | 
|  | 46 | return; | 
|  | 47 | } | 
|  | 48 | // Restart the request without if-match | 
|  | 49 | req.req.erase(boost::beast::http::field::if_match); | 
|  | 50 | BMCWEB_LOG_DEBUG << "Restarting request"; | 
|  | 51 | app.handle(req, asyncResp); | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | inline bool handleIfMatch(crow::App& app, const crow::Request& req, | 
|  | 55 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) | 
|  | 56 | { | 
|  | 57 | if (req.session == nullptr) | 
|  | 58 | { | 
|  | 59 | // If the user isn't authenticated, don't even attempt to parse match | 
|  | 60 | // parameters | 
|  | 61 | return true; | 
|  | 62 | } | 
|  | 63 |  | 
|  | 64 | std::string ifMatch{ | 
|  | 65 | req.getHeaderValue(boost::beast::http::field::if_match)}; | 
|  | 66 | if (ifMatch.empty()) | 
|  | 67 | { | 
|  | 68 | // No If-Match header.  Nothing to do | 
|  | 69 | return true; | 
|  | 70 | } | 
|  | 71 | if (req.req.method() != boost::beast::http::verb::patch && | 
|  | 72 | req.req.method() != boost::beast::http::verb::post && | 
|  | 73 | req.req.method() != boost::beast::http::verb::delete_) | 
|  | 74 | { | 
|  | 75 | messages::preconditionFailed(asyncResp->res); | 
|  | 76 | return false; | 
|  | 77 | } | 
|  | 78 | boost::system::error_code ec; | 
|  | 79 |  | 
|  | 80 | // Try to GET the same resource | 
|  | 81 | crow::Request newReq({boost::beast::http::verb::get, req.url, 11}, ec); | 
|  | 82 |  | 
|  | 83 | if (ec) | 
|  | 84 | { | 
|  | 85 | messages::internalError(asyncResp->res); | 
|  | 86 | return false; | 
|  | 87 | } | 
|  | 88 |  | 
|  | 89 | // New request has the same credentials as the old request | 
|  | 90 | newReq.session = req.session; | 
|  | 91 |  | 
|  | 92 | // Construct a new response object to fill in, and check the hash of before | 
|  | 93 | // we modify the Resource. | 
|  | 94 | std::shared_ptr<bmcweb::AsyncResp> getReqAsyncResp = | 
|  | 95 | std::make_shared<bmcweb::AsyncResp>(); | 
|  | 96 |  | 
|  | 97 | getReqAsyncResp->res.setCompleteRequestHandler(std::bind_front( | 
|  | 98 | afterIfMatchRequest, std::ref(app), asyncResp, req, ifMatch)); | 
|  | 99 |  | 
|  | 100 | app.handle(newReq, getReqAsyncResp); | 
|  | 101 | return false; | 
|  | 102 | } | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 103 |  | 
| Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 104 | // Sets up the Redfish Route and delegates some of the query parameter | 
|  | 105 | // processing. |queryCapabilities| stores which query parameters will be | 
|  | 106 | // handled by redfish-core/lib codes, then default query parameter handler won't | 
|  | 107 | // process these parameters. | 
|  | 108 | [[nodiscard]] inline bool setUpRedfishRouteWithDelegation( | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 109 | crow::App& app, const crow::Request& req, | 
|  | 110 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, | 
| Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 111 | query_param::Query& delegated, | 
|  | 112 | const query_param::QueryCapabilities& queryCapabilities) | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 113 | { | 
| Ed Tanous | 142ec9a | 2022-03-24 18:20:45 -0700 | [diff] [blame] | 114 | BMCWEB_LOG_DEBUG << "setup redfish route"; | 
|  | 115 |  | 
|  | 116 | // Section 7.4 of the redfish spec "Redfish Services shall process the | 
|  | 117 | // [OData-Version header] in the following table as defined by the HTTP 1.1 | 
|  | 118 | // specification..." | 
|  | 119 | // Required to pass redfish-protocol-validator REQ_HEADERS_ODATA_VERSION | 
|  | 120 | std::string_view odataHeader = req.getHeaderValue("OData-Version"); | 
|  | 121 | if (!odataHeader.empty() && odataHeader != "4.0") | 
|  | 122 | { | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 123 | messages::preconditionFailed(asyncResp->res); | 
| Ed Tanous | 142ec9a | 2022-03-24 18:20:45 -0700 | [diff] [blame] | 124 | return false; | 
|  | 125 | } | 
|  | 126 |  | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 127 | asyncResp->res.addHeader("OData-Version", "4.0"); | 
| Ed Tanous | c02a74f | 2022-05-11 14:46:44 -0700 | [diff] [blame] | 128 |  | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 129 | std::optional<query_param::Query> queryOpt = | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 130 | query_param::parseParameters(req.urlView.params(), asyncResp->res); | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 131 | if (queryOpt == std::nullopt) | 
|  | 132 | { | 
|  | 133 | return false; | 
|  | 134 | } | 
|  | 135 |  | 
| Ed Tanous | 2d6cb56 | 2022-07-07 20:44:54 -0700 | [diff] [blame] | 136 | if (!handleIfMatch(app, req, asyncResp)) | 
|  | 137 | { | 
|  | 138 | return false; | 
|  | 139 | } | 
|  | 140 |  | 
| Carson Labrado | 05916ce | 2022-08-01 19:38:18 +0000 | [diff] [blame] | 141 | bool needToCallHandlers = true; | 
|  | 142 |  | 
|  | 143 | #ifdef BMCWEB_ENABLE_REDFISH_AGGREGATION | 
|  | 144 | needToCallHandlers = RedfishAggregator::getInstance().beginAggregation( | 
|  | 145 | req, asyncResp) == Result::LocalHandle; | 
|  | 146 |  | 
|  | 147 | // If the request should be forwarded to a satellite BMC then we don't want | 
|  | 148 | // to write anything to the asyncResp since it will get overwritten later. | 
|  | 149 | #endif | 
|  | 150 |  | 
| Ed Tanous | 7cf436c | 2022-03-22 23:53:51 -0700 | [diff] [blame] | 151 | // If this isn't a get, no need to do anything with parameters | 
|  | 152 | if (req.method() != boost::beast::http::verb::get) | 
|  | 153 | { | 
| Carson Labrado | 05916ce | 2022-08-01 19:38:18 +0000 | [diff] [blame] | 154 | return needToCallHandlers; | 
| Ed Tanous | 7cf436c | 2022-03-22 23:53:51 -0700 | [diff] [blame] | 155 | } | 
|  | 156 |  | 
| Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 157 | delegated = query_param::delegate(queryCapabilities, *queryOpt); | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 158 | std::function<void(crow::Response&)> handler = | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 159 | asyncResp->res.releaseCompleteRequestHandler(); | 
| Nan Zhou | 827c490 | 2022-08-03 04:57:55 +0000 | [diff] [blame] | 160 |  | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 161 | asyncResp->res.setCompleteRequestHandler( | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 162 | [&app, handler(std::move(handler)), | 
| Nan Zhou | 827c490 | 2022-08-03 04:57:55 +0000 | [diff] [blame] | 163 | query{std::move(*queryOpt)}](crow::Response& resIn) mutable { | 
| Ed Tanous | 8a59281 | 2022-06-04 09:06:59 -0700 | [diff] [blame] | 164 | processAllParams(app, query, handler, resIn); | 
| Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 165 | }); | 
| Nan Zhou | 827c490 | 2022-08-03 04:57:55 +0000 | [diff] [blame] | 166 |  | 
| Carson Labrado | 05916ce | 2022-08-01 19:38:18 +0000 | [diff] [blame] | 167 | return needToCallHandlers; | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 168 | } | 
| Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 169 |  | 
|  | 170 | // 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] | 171 | [[nodiscard]] inline bool | 
|  | 172 | setUpRedfishRoute(crow::App& app, const crow::Request& req, | 
|  | 173 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) | 
| Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 174 | { | 
|  | 175 | // This route |delegated| is never used | 
|  | 176 | query_param::Query delegated; | 
| Carson Labrado | 3ba0007 | 2022-06-06 19:40:56 +0000 | [diff] [blame] | 177 | return setUpRedfishRouteWithDelegation(app, req, asyncResp, delegated, | 
| Nan Zhou | a6b9125 | 2022-04-04 13:10:40 -0700 | [diff] [blame] | 178 | query_param::QueryCapabilities{}); | 
|  | 179 | } | 
| Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 180 | } // namespace redfish |