xref: /openbmc/bmcweb/redfish-core/include/query.hpp (revision 3ba0007367777144f474fdf99439ae8c03633486)
1f4c99e70SEd Tanous #pragma once
2f4c99e70SEd Tanous 
3f4c99e70SEd Tanous #include "utils/query_param.hpp"
4f4c99e70SEd Tanous 
5f4c99e70SEd Tanous #include <bmcweb_config.h>
6f4c99e70SEd Tanous 
7f4c99e70SEd Tanous namespace redfish
8f4c99e70SEd Tanous {
9f4c99e70SEd Tanous 
10a6b9125fSNan Zhou // Sets up the Redfish Route and delegates some of the query parameter
11a6b9125fSNan Zhou // processing. |queryCapabilities| stores which query parameters will be
12a6b9125fSNan Zhou // handled by redfish-core/lib codes, then default query parameter handler won't
13a6b9125fSNan Zhou // process these parameters.
14a6b9125fSNan Zhou [[nodiscard]] inline bool setUpRedfishRouteWithDelegation(
15*3ba00073SCarson Labrado     crow::App& app, const crow::Request& req,
16*3ba00073SCarson Labrado     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
17a6b9125fSNan Zhou     query_param::Query& delegated,
18a6b9125fSNan Zhou     const query_param::QueryCapabilities& queryCapabilities)
19f4c99e70SEd Tanous {
20142ec9aeSEd Tanous     BMCWEB_LOG_DEBUG << "setup redfish route";
21142ec9aeSEd Tanous 
22142ec9aeSEd Tanous     // Section 7.4 of the redfish spec "Redfish Services shall process the
23142ec9aeSEd Tanous     // [OData-Version header] in the following table as defined by the HTTP 1.1
24142ec9aeSEd Tanous     // specification..."
25142ec9aeSEd Tanous     // Required to pass redfish-protocol-validator REQ_HEADERS_ODATA_VERSION
26142ec9aeSEd Tanous     std::string_view odataHeader = req.getHeaderValue("OData-Version");
27142ec9aeSEd Tanous     if (!odataHeader.empty() && odataHeader != "4.0")
28142ec9aeSEd Tanous     {
29*3ba00073SCarson Labrado         messages::preconditionFailed(asyncResp->res);
30142ec9aeSEd Tanous         return false;
31142ec9aeSEd Tanous     }
32142ec9aeSEd Tanous 
33*3ba00073SCarson Labrado     asyncResp->res.addHeader("OData-Version", "4.0");
34c02a74f8SEd Tanous 
35f4c99e70SEd Tanous     std::optional<query_param::Query> queryOpt =
36*3ba00073SCarson Labrado         query_param::parseParameters(req.urlView.params(), asyncResp->res);
37f4c99e70SEd Tanous     if (queryOpt == std::nullopt)
38f4c99e70SEd Tanous     {
39f4c99e70SEd Tanous         return false;
40f4c99e70SEd Tanous     }
41f4c99e70SEd Tanous 
427cf436c9SEd Tanous     // If this isn't a get, no need to do anything with parameters
437cf436c9SEd Tanous     if (req.method() != boost::beast::http::verb::get)
447cf436c9SEd Tanous     {
457cf436c9SEd Tanous         return true;
467cf436c9SEd Tanous     }
477cf436c9SEd Tanous 
48a6b9125fSNan Zhou     delegated = query_param::delegate(queryCapabilities, *queryOpt);
49f4c99e70SEd Tanous     std::function<void(crow::Response&)> handler =
50*3ba00073SCarson Labrado         asyncResp->res.releaseCompleteRequestHandler();
51*3ba00073SCarson Labrado     asyncResp->res.setCompleteRequestHandler(
52f4c99e70SEd Tanous         [&app, handler(std::move(handler)),
53f4c99e70SEd Tanous          query{*queryOpt}](crow::Response& res) mutable {
547cf436c9SEd Tanous         processAllParams(app, query, handler, res);
55f4c99e70SEd Tanous     });
56f4c99e70SEd Tanous     return true;
57f4c99e70SEd Tanous }
58a6b9125fSNan Zhou 
59a6b9125fSNan Zhou // Sets up the Redfish Route. All parameters are handled by the default handler.
60*3ba00073SCarson Labrado [[nodiscard]] inline bool
61*3ba00073SCarson Labrado     setUpRedfishRoute(crow::App& app, const crow::Request& req,
62*3ba00073SCarson Labrado                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
63a6b9125fSNan Zhou {
64a6b9125fSNan Zhou     // This route |delegated| is never used
65a6b9125fSNan Zhou     query_param::Query delegated;
66*3ba00073SCarson Labrado     return setUpRedfishRouteWithDelegation(app, req, asyncResp, delegated,
67a6b9125fSNan Zhou                                            query_param::QueryCapabilities{});
68a6b9125fSNan Zhou }
69f4c99e70SEd Tanous } // namespace redfish
70