xref: /openbmc/bmcweb/redfish-core/include/query.hpp (revision 142ec9aeb70c23a1aa98aa90d472ff11a76529ee)
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 
10f4c99e70SEd Tanous [[nodiscard]] inline bool setUpRedfishRoute(crow::App& app,
11f4c99e70SEd Tanous                                             const crow::Request& req,
12f4c99e70SEd Tanous                                             crow::Response& res)
13f4c99e70SEd Tanous {
14*142ec9aeSEd Tanous     BMCWEB_LOG_DEBUG << "setup redfish route";
15*142ec9aeSEd Tanous 
16*142ec9aeSEd Tanous     // Section 7.4 of the redfish spec "Redfish Services shall process the
17*142ec9aeSEd Tanous     // [OData-Version header] in the following table as defined by the HTTP 1.1
18*142ec9aeSEd Tanous     // specification..."
19*142ec9aeSEd Tanous     // Required to pass redfish-protocol-validator REQ_HEADERS_ODATA_VERSION
20*142ec9aeSEd Tanous     std::string_view odataHeader = req.getHeaderValue("OData-Version");
21*142ec9aeSEd Tanous     if (!odataHeader.empty() && odataHeader != "4.0")
22*142ec9aeSEd Tanous     {
23*142ec9aeSEd Tanous         messages::preconditionFailed(res);
24*142ec9aeSEd Tanous         return false;
25*142ec9aeSEd Tanous     }
26*142ec9aeSEd Tanous 
27f4c99e70SEd Tanous     // If query parameters aren't enabled, do nothing.
28f4c99e70SEd Tanous     if constexpr (!bmcwebInsecureEnableQueryParams)
29f4c99e70SEd Tanous     {
30f4c99e70SEd Tanous         return true;
31f4c99e70SEd Tanous     }
32f4c99e70SEd Tanous     std::optional<query_param::Query> queryOpt =
33f4c99e70SEd Tanous         query_param::parseParameters(req.urlView.params(), res);
34f4c99e70SEd Tanous     if (queryOpt == std::nullopt)
35f4c99e70SEd Tanous     {
36f4c99e70SEd Tanous         return false;
37f4c99e70SEd Tanous     }
38f4c99e70SEd Tanous 
397cf436c9SEd Tanous     // If this isn't a get, no need to do anything with parameters
407cf436c9SEd Tanous     if (req.method() != boost::beast::http::verb::get)
417cf436c9SEd Tanous     {
427cf436c9SEd Tanous         return true;
437cf436c9SEd Tanous     }
447cf436c9SEd Tanous 
45f4c99e70SEd Tanous     std::function<void(crow::Response&)> handler =
46f4c99e70SEd Tanous         res.releaseCompleteRequestHandler();
47f4c99e70SEd Tanous 
48f4c99e70SEd Tanous     res.setCompleteRequestHandler(
49f4c99e70SEd Tanous         [&app, handler(std::move(handler)),
50f4c99e70SEd Tanous          query{*queryOpt}](crow::Response& res) mutable {
517cf436c9SEd Tanous             processAllParams(app, query, handler, res);
52f4c99e70SEd Tanous         });
53f4c99e70SEd Tanous     return true;
54f4c99e70SEd Tanous }
55f4c99e70SEd Tanous } // namespace redfish
56