xref: /openbmc/bmcweb/redfish-core/include/query.hpp (revision 827c4902835d3175a36d90808a51304f744203ce)
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 
30f4c99e70SEd Tanous namespace redfish
31f4c99e70SEd Tanous {
32f4c99e70SEd Tanous 
33a6b9125fSNan Zhou // Sets up the Redfish Route and delegates some of the query parameter
34a6b9125fSNan Zhou // processing. |queryCapabilities| stores which query parameters will be
35a6b9125fSNan Zhou // handled by redfish-core/lib codes, then default query parameter handler won't
36a6b9125fSNan Zhou // process these parameters.
37a6b9125fSNan Zhou [[nodiscard]] inline bool setUpRedfishRouteWithDelegation(
383ba00073SCarson Labrado     crow::App& app, const crow::Request& req,
393ba00073SCarson Labrado     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
40a6b9125fSNan Zhou     query_param::Query& delegated,
41a6b9125fSNan Zhou     const query_param::QueryCapabilities& queryCapabilities)
42f4c99e70SEd Tanous {
43142ec9aeSEd Tanous     BMCWEB_LOG_DEBUG << "setup redfish route";
44142ec9aeSEd Tanous 
45142ec9aeSEd Tanous     // Section 7.4 of the redfish spec "Redfish Services shall process the
46142ec9aeSEd Tanous     // [OData-Version header] in the following table as defined by the HTTP 1.1
47142ec9aeSEd Tanous     // specification..."
48142ec9aeSEd Tanous     // Required to pass redfish-protocol-validator REQ_HEADERS_ODATA_VERSION
49142ec9aeSEd Tanous     std::string_view odataHeader = req.getHeaderValue("OData-Version");
50142ec9aeSEd Tanous     if (!odataHeader.empty() && odataHeader != "4.0")
51142ec9aeSEd Tanous     {
523ba00073SCarson Labrado         messages::preconditionFailed(asyncResp->res);
53142ec9aeSEd Tanous         return false;
54142ec9aeSEd Tanous     }
55142ec9aeSEd Tanous 
563ba00073SCarson Labrado     asyncResp->res.addHeader("OData-Version", "4.0");
57c02a74f8SEd Tanous 
58f4c99e70SEd Tanous     std::optional<query_param::Query> queryOpt =
593ba00073SCarson Labrado         query_param::parseParameters(req.urlView.params(), asyncResp->res);
60f4c99e70SEd Tanous     if (queryOpt == std::nullopt)
61f4c99e70SEd Tanous     {
62f4c99e70SEd Tanous         return false;
63f4c99e70SEd Tanous     }
64f4c99e70SEd Tanous 
657cf436c9SEd Tanous     // If this isn't a get, no need to do anything with parameters
667cf436c9SEd Tanous     if (req.method() != boost::beast::http::verb::get)
677cf436c9SEd Tanous     {
687cf436c9SEd Tanous         return true;
697cf436c9SEd Tanous     }
707cf436c9SEd Tanous 
71a6b9125fSNan Zhou     delegated = query_param::delegate(queryCapabilities, *queryOpt);
72f4c99e70SEd Tanous     std::function<void(crow::Response&)> handler =
733ba00073SCarson Labrado         asyncResp->res.releaseCompleteRequestHandler();
74*827c4902SNan Zhou 
753ba00073SCarson Labrado     asyncResp->res.setCompleteRequestHandler(
76f4c99e70SEd Tanous         [&app, handler(std::move(handler)),
77*827c4902SNan Zhou          query{std::move(*queryOpt)}](crow::Response& resIn) mutable {
788a592810SEd Tanous         processAllParams(app, query, handler, resIn);
79f4c99e70SEd Tanous     });
80*827c4902SNan Zhou 
81f4c99e70SEd Tanous     return true;
82f4c99e70SEd Tanous }
83a6b9125fSNan Zhou 
84a6b9125fSNan Zhou // Sets up the Redfish Route. All parameters are handled by the default handler.
853ba00073SCarson Labrado [[nodiscard]] inline bool
863ba00073SCarson Labrado     setUpRedfishRoute(crow::App& app, const crow::Request& req,
873ba00073SCarson Labrado                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
88a6b9125fSNan Zhou {
89a6b9125fSNan Zhou     // This route |delegated| is never used
90a6b9125fSNan Zhou     query_param::Query delegated;
913ba00073SCarson Labrado     return setUpRedfishRouteWithDelegation(app, req, asyncResp, delegated,
92a6b9125fSNan Zhou                                            query_param::QueryCapabilities{});
93a6b9125fSNan Zhou }
94f4c99e70SEd Tanous } // namespace redfish
95