1 #pragma once
2 
3 #include "utils/query_param.hpp"
4 
5 #include <bmcweb_config.h>
6 
7 namespace redfish
8 {
9 
10 // Sets up the Redfish Route and delegates some of the query parameter
11 // processing. |queryCapabilities| stores which query parameters will be
12 // handled by redfish-core/lib codes, then default query parameter handler won't
13 // process these parameters.
14 [[nodiscard]] inline bool setUpRedfishRouteWithDelegation(
15     crow::App& app, const crow::Request& req, crow::Response& res,
16     query_param::Query& delegated,
17     const query_param::QueryCapabilities& queryCapabilities)
18 {
19     BMCWEB_LOG_DEBUG << "setup redfish route";
20 
21     // Section 7.4 of the redfish spec "Redfish Services shall process the
22     // [OData-Version header] in the following table as defined by the HTTP 1.1
23     // specification..."
24     // Required to pass redfish-protocol-validator REQ_HEADERS_ODATA_VERSION
25     std::string_view odataHeader = req.getHeaderValue("OData-Version");
26     if (!odataHeader.empty() && odataHeader != "4.0")
27     {
28         messages::preconditionFailed(res);
29         return false;
30     }
31 
32     res.addHeader("OData-Version", "4.0");
33 
34     std::optional<query_param::Query> queryOpt =
35         query_param::parseParameters(req.urlView.params(), res);
36     if (queryOpt == std::nullopt)
37     {
38         return false;
39     }
40 
41     // If this isn't a get, no need to do anything with parameters
42     if (req.method() != boost::beast::http::verb::get)
43     {
44         return true;
45     }
46 
47     delegated = query_param::delegate(queryCapabilities, *queryOpt);
48     std::function<void(crow::Response&)> handler =
49         res.releaseCompleteRequestHandler();
50     res.setCompleteRequestHandler(
51         [&app, handler(std::move(handler)),
52          query{*queryOpt}](crow::Response& res) mutable {
53             processAllParams(app, query, handler, res);
54         });
55     return true;
56 }
57 
58 // Sets up the Redfish Route. All parameters are handled by the default handler.
59 [[nodiscard]] inline bool setUpRedfishRoute(crow::App& app,
60                                             const crow::Request& req,
61                                             crow::Response& res)
62 {
63     // This route |delegated| is never used
64     query_param::Query delegated;
65     return setUpRedfishRouteWithDelegation(app, req, res, delegated,
66                                            query_param::QueryCapabilities{});
67 }
68 } // namespace redfish
69