xref: /openbmc/bmcweb/redfish-core/include/query.hpp (revision e796c262ff093032a23d25b04daef0a1e4794791)
1f4c99e70SEd Tanous #pragma once
2f4c99e70SEd Tanous 
3*e796c262SNan Zhou #include "bmcweb_config.h"
4*e796c262SNan Zhou 
5*e796c262SNan Zhou #include "app.hpp"
6*e796c262SNan Zhou #include "async_resp.hpp"
7*e796c262SNan Zhou #include "error_messages.hpp"
8*e796c262SNan Zhou #include "http_request.hpp"
9*e796c262SNan Zhou #include "http_response.hpp"
10*e796c262SNan Zhou #include "logging.hpp"
11f4c99e70SEd Tanous #include "utils/query_param.hpp"
12f4c99e70SEd Tanous 
13*e796c262SNan Zhou #include <boost/beast/http/verb.hpp>
14*e796c262SNan Zhou #include <boost/url/params_view.hpp>
15*e796c262SNan Zhou #include <boost/url/url_view.hpp>
16*e796c262SNan Zhou 
17*e796c262SNan Zhou #include <functional>
18*e796c262SNan Zhou #include <memory>
19*e796c262SNan Zhou #include <new>
20*e796c262SNan Zhou #include <optional>
21*e796c262SNan Zhou #include <string>
22*e796c262SNan Zhou #include <string_view>
23*e796c262SNan Zhou #include <type_traits>
24*e796c262SNan Zhou #include <utility>
25*e796c262SNan Zhou 
26*e796c262SNan Zhou // IWYU pragma: no_forward_declare crow::App
27*e796c262SNan Zhou // IWYU pragma: no_include <boost/url/impl/params_view.hpp>
28*e796c262SNan 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();
743ba00073SCarson Labrado     asyncResp->res.setCompleteRequestHandler(
75f4c99e70SEd Tanous         [&app, handler(std::move(handler)),
768a592810SEd Tanous          query{*queryOpt}](crow::Response& resIn) mutable {
778a592810SEd Tanous         processAllParams(app, query, handler, resIn);
78f4c99e70SEd Tanous     });
79f4c99e70SEd Tanous     return true;
80f4c99e70SEd Tanous }
81a6b9125fSNan Zhou 
82a6b9125fSNan Zhou // Sets up the Redfish Route. All parameters are handled by the default handler.
833ba00073SCarson Labrado [[nodiscard]] inline bool
843ba00073SCarson Labrado     setUpRedfishRoute(crow::App& app, const crow::Request& req,
853ba00073SCarson Labrado                       const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
86a6b9125fSNan Zhou {
87a6b9125fSNan Zhou     // This route |delegated| is never used
88a6b9125fSNan Zhou     query_param::Query delegated;
893ba00073SCarson Labrado     return setUpRedfishRouteWithDelegation(app, req, asyncResp, delegated,
90a6b9125fSNan Zhou                                            query_param::QueryCapabilities{});
91a6b9125fSNan Zhou }
92f4c99e70SEd Tanous } // namespace redfish
93