xref: /openbmc/bmcweb/redfish-core/include/query.hpp (revision 39662a3be1877afd893b70d78a449f91ba36c260)
1f4c99e70SEd Tanous #pragma once
2f4c99e70SEd Tanous 
3e796c262SNan Zhou #include "bmcweb_config.h"
4e796c262SNan Zhou 
5e796c262SNan Zhou #include "app.hpp"
6e796c262SNan Zhou #include "async_resp.hpp"
7e796c262SNan Zhou #include "error_messages.hpp"
8e796c262SNan Zhou #include "http_request.hpp"
9e796c262SNan Zhou #include "http_response.hpp"
10e796c262SNan Zhou #include "logging.hpp"
11f4c99e70SEd Tanous #include "utils/query_param.hpp"
12f4c99e70SEd Tanous 
13e796c262SNan Zhou #include <boost/beast/http/verb.hpp>
14e796c262SNan Zhou #include <boost/url/params_view.hpp>
15e796c262SNan Zhou #include <boost/url/url_view.hpp>
16e796c262SNan Zhou 
17e796c262SNan Zhou #include <functional>
18e796c262SNan Zhou #include <memory>
19e796c262SNan Zhou #include <new>
20e796c262SNan Zhou #include <optional>
21e796c262SNan Zhou #include <string>
22e796c262SNan Zhou #include <string_view>
23e796c262SNan Zhou #include <type_traits>
24e796c262SNan Zhou #include <utility>
25e796c262SNan Zhou 
26e796c262SNan Zhou // IWYU pragma: no_forward_declare crow::App
27e796c262SNan Zhou // IWYU pragma: no_include <boost/url/impl/params_view.hpp>
28e796c262SNan Zhou // IWYU pragma: no_include <boost/url/impl/url_view.hpp>
29f4c99e70SEd Tanous 
303ccb3adbSEd Tanous #include "redfish_aggregator.hpp"
3105916cefSCarson Labrado 
32f4c99e70SEd Tanous namespace redfish
33f4c99e70SEd Tanous {
342d6cb56bSEd Tanous inline void
352d6cb56bSEd Tanous     afterIfMatchRequest(crow::App& app,
362d6cb56bSEd Tanous                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
372d6cb56bSEd Tanous                         crow::Request& req, const std::string& ifMatchHeader,
382d6cb56bSEd Tanous                         const crow::Response& resIn)
392d6cb56bSEd Tanous {
402d6cb56bSEd Tanous     std::string computedEtag = resIn.computeEtag();
412d6cb56bSEd Tanous     BMCWEB_LOG_DEBUG << "User provided if-match etag " << ifMatchHeader
422d6cb56bSEd Tanous                      << " computed etag " << computedEtag;
432d6cb56bSEd Tanous     if (computedEtag != ifMatchHeader)
442d6cb56bSEd Tanous     {
452d6cb56bSEd Tanous         messages::preconditionFailed(asyncResp->res);
462d6cb56bSEd Tanous         return;
472d6cb56bSEd Tanous     }
482d6cb56bSEd Tanous     // Restart the request without if-match
492d6cb56bSEd Tanous     req.req.erase(boost::beast::http::field::if_match);
502d6cb56bSEd Tanous     BMCWEB_LOG_DEBUG << "Restarting request";
512d6cb56bSEd Tanous     app.handle(req, asyncResp);
522d6cb56bSEd Tanous }
532d6cb56bSEd Tanous 
542d6cb56bSEd Tanous inline bool handleIfMatch(crow::App& app, const crow::Request& req,
552d6cb56bSEd Tanous                           const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
562d6cb56bSEd Tanous {
572d6cb56bSEd Tanous     if (req.session == nullptr)
582d6cb56bSEd Tanous     {
592d6cb56bSEd Tanous         // If the user isn't authenticated, don't even attempt to parse match
602d6cb56bSEd Tanous         // parameters
612d6cb56bSEd Tanous         return true;
622d6cb56bSEd Tanous     }
632d6cb56bSEd Tanous 
642d6cb56bSEd Tanous     std::string ifMatch{
652d6cb56bSEd Tanous         req.getHeaderValue(boost::beast::http::field::if_match)};
662d6cb56bSEd Tanous     if (ifMatch.empty())
672d6cb56bSEd Tanous     {
682d6cb56bSEd Tanous         // No If-Match header.  Nothing to do
692d6cb56bSEd Tanous         return true;
702d6cb56bSEd Tanous     }
712d6cb56bSEd Tanous     if (req.req.method() != boost::beast::http::verb::patch &&
722d6cb56bSEd Tanous         req.req.method() != boost::beast::http::verb::post &&
732d6cb56bSEd Tanous         req.req.method() != boost::beast::http::verb::delete_)
742d6cb56bSEd Tanous     {
752d6cb56bSEd Tanous         messages::preconditionFailed(asyncResp->res);
762d6cb56bSEd Tanous         return false;
772d6cb56bSEd Tanous     }
782d6cb56bSEd Tanous     boost::system::error_code ec;
792d6cb56bSEd Tanous 
802d6cb56bSEd Tanous     // Try to GET the same resource
81*39662a3bSEd Tanous     crow::Request newReq(
82*39662a3bSEd Tanous         {boost::beast::http::verb::get, req.url().encoded_path(), 11}, ec);
832d6cb56bSEd Tanous 
842d6cb56bSEd Tanous     if (ec)
852d6cb56bSEd Tanous     {
862d6cb56bSEd Tanous         messages::internalError(asyncResp->res);
872d6cb56bSEd Tanous         return false;
882d6cb56bSEd Tanous     }
892d6cb56bSEd Tanous 
902d6cb56bSEd Tanous     // New request has the same credentials as the old request
912d6cb56bSEd Tanous     newReq.session = req.session;
922d6cb56bSEd Tanous 
932d6cb56bSEd Tanous     // Construct a new response object to fill in, and check the hash of before
942d6cb56bSEd Tanous     // we modify the Resource.
952d6cb56bSEd Tanous     std::shared_ptr<bmcweb::AsyncResp> getReqAsyncResp =
962d6cb56bSEd Tanous         std::make_shared<bmcweb::AsyncResp>();
972d6cb56bSEd Tanous 
982d6cb56bSEd Tanous     getReqAsyncResp->res.setCompleteRequestHandler(std::bind_front(
992d6cb56bSEd Tanous         afterIfMatchRequest, std::ref(app), asyncResp, req, ifMatch));
1002d6cb56bSEd Tanous 
1012d6cb56bSEd Tanous     app.handle(newReq, getReqAsyncResp);
1022d6cb56bSEd Tanous     return false;
1032d6cb56bSEd Tanous }
104f4c99e70SEd Tanous 
105a6b9125fSNan Zhou // Sets up the Redfish Route and delegates some of the query parameter
106a6b9125fSNan Zhou // processing. |queryCapabilities| stores which query parameters will be
107a6b9125fSNan Zhou // handled by redfish-core/lib codes, then default query parameter handler won't
108a6b9125fSNan Zhou // process these parameters.
109a6b9125fSNan Zhou [[nodiscard]] inline bool setUpRedfishRouteWithDelegation(
1103ba00073SCarson Labrado     crow::App& app, const crow::Request& req,
1113ba00073SCarson Labrado     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
112a6b9125fSNan Zhou     query_param::Query& delegated,
113a6b9125fSNan Zhou     const query_param::QueryCapabilities& queryCapabilities)
114f4c99e70SEd Tanous {
115142ec9aeSEd Tanous     BMCWEB_LOG_DEBUG << "setup redfish route";
116142ec9aeSEd Tanous 
117142ec9aeSEd Tanous     // Section 7.4 of the redfish spec "Redfish Services shall process the
118142ec9aeSEd Tanous     // [OData-Version header] in the following table as defined by the HTTP 1.1
119142ec9aeSEd Tanous     // specification..."
120142ec9aeSEd Tanous     // Required to pass redfish-protocol-validator REQ_HEADERS_ODATA_VERSION
121142ec9aeSEd Tanous     std::string_view odataHeader = req.getHeaderValue("OData-Version");
122142ec9aeSEd Tanous     if (!odataHeader.empty() && odataHeader != "4.0")
123142ec9aeSEd Tanous     {
1243ba00073SCarson Labrado         messages::preconditionFailed(asyncResp->res);
125142ec9aeSEd Tanous         return false;
126142ec9aeSEd Tanous     }
127142ec9aeSEd Tanous 
1283ba00073SCarson Labrado     asyncResp->res.addHeader("OData-Version", "4.0");
129c02a74f8SEd Tanous 
130f4c99e70SEd Tanous     std::optional<query_param::Query> queryOpt =
131*39662a3bSEd Tanous         query_param::parseParameters(req.url().params(), asyncResp->res);
132f4c99e70SEd Tanous     if (queryOpt == std::nullopt)
133f4c99e70SEd Tanous     {
134f4c99e70SEd Tanous         return false;
135f4c99e70SEd Tanous     }
136f4c99e70SEd Tanous 
1372d6cb56bSEd Tanous     if (!handleIfMatch(app, req, asyncResp))
1382d6cb56bSEd Tanous     {
1392d6cb56bSEd Tanous         return false;
1402d6cb56bSEd Tanous     }
1412d6cb56bSEd Tanous 
14205916cefSCarson Labrado     bool needToCallHandlers = true;
14305916cefSCarson Labrado 
14405916cefSCarson Labrado #ifdef BMCWEB_ENABLE_REDFISH_AGGREGATION
14505916cefSCarson Labrado     needToCallHandlers = RedfishAggregator::getInstance().beginAggregation(
14605916cefSCarson Labrado                              req, asyncResp) == Result::LocalHandle;
14705916cefSCarson Labrado 
14805916cefSCarson Labrado     // If the request should be forwarded to a satellite BMC then we don't want
14905916cefSCarson Labrado     // to write anything to the asyncResp since it will get overwritten later.
15005916cefSCarson Labrado #endif
15105916cefSCarson Labrado 
1527cf436c9SEd Tanous     // If this isn't a get, no need to do anything with parameters
1537cf436c9SEd Tanous     if (req.method() != boost::beast::http::verb::get)
1547cf436c9SEd Tanous     {
15505916cefSCarson Labrado         return needToCallHandlers;
1567cf436c9SEd Tanous     }
1577cf436c9SEd Tanous 
158a6b9125fSNan Zhou     delegated = query_param::delegate(queryCapabilities, *queryOpt);
159f4c99e70SEd Tanous     std::function<void(crow::Response&)> handler =
1603ba00073SCarson Labrado         asyncResp->res.releaseCompleteRequestHandler();
161827c4902SNan Zhou 
1623ba00073SCarson Labrado     asyncResp->res.setCompleteRequestHandler(
163f4c99e70SEd Tanous         [&app, handler(std::move(handler)),
164827c4902SNan Zhou          query{std::move(*queryOpt)}](crow::Response& resIn) mutable {
1658a592810SEd Tanous         processAllParams(app, query, handler, resIn);
166f4c99e70SEd Tanous     });
167827c4902SNan Zhou 
16805916cefSCarson Labrado     return needToCallHandlers;
169f4c99e70SEd Tanous }
170a6b9125fSNan Zhou 
171a6b9125fSNan Zhou // Sets up the Redfish Route. All parameters are handled by the default handler.
1723ba00073SCarson Labrado [[nodiscard]] inline bool
1733ba00073SCarson Labrado     setUpRedfishRoute(crow::App& app, const crow::Request& req,
1743ba00073SCarson Labrado                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
175a6b9125fSNan Zhou {
176a6b9125fSNan Zhou     // This route |delegated| is never used
177a6b9125fSNan Zhou     query_param::Query delegated;
1783ba00073SCarson Labrado     return setUpRedfishRouteWithDelegation(app, req, asyncResp, delegated,
179a6b9125fSNan Zhou                                            query_param::QueryCapabilities{});
180a6b9125fSNan Zhou }
181f4c99e70SEd Tanous } // namespace redfish
182