blob: 4dd03c622cfa19c504d6317fea949702bff23440 [file] [log] [blame]
Ed Tanousf4c99e72021-10-04 17:02:43 -07001#pragma once
2
3#include "utils/query_param.hpp"
4
5#include <bmcweb_config.h>
6
7namespace redfish
8{
9
Nan Zhoua6b91252022-04-04 13:10:40 -070010// 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)
Ed Tanousf4c99e72021-10-04 17:02:43 -070018{
Ed Tanous142ec9a2022-03-24 18:20:45 -070019 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
Ed Tanousf4c99e72021-10-04 17:02:43 -070032 // If query parameters aren't enabled, do nothing.
33 if constexpr (!bmcwebInsecureEnableQueryParams)
34 {
35 return true;
36 }
37 std::optional<query_param::Query> queryOpt =
38 query_param::parseParameters(req.urlView.params(), res);
39 if (queryOpt == std::nullopt)
40 {
41 return false;
42 }
43
Ed Tanous7cf436c2022-03-22 23:53:51 -070044 // If this isn't a get, no need to do anything with parameters
45 if (req.method() != boost::beast::http::verb::get)
46 {
47 return true;
48 }
49
Nan Zhoua6b91252022-04-04 13:10:40 -070050 delegated = query_param::delegate(queryCapabilities, *queryOpt);
Ed Tanousf4c99e72021-10-04 17:02:43 -070051 std::function<void(crow::Response&)> handler =
52 res.releaseCompleteRequestHandler();
Ed Tanousf4c99e72021-10-04 17:02:43 -070053 res.setCompleteRequestHandler(
54 [&app, handler(std::move(handler)),
55 query{*queryOpt}](crow::Response& res) mutable {
Ed Tanous7cf436c2022-03-22 23:53:51 -070056 processAllParams(app, query, handler, res);
Ed Tanousf4c99e72021-10-04 17:02:43 -070057 });
58 return true;
59}
Nan Zhoua6b91252022-04-04 13:10:40 -070060
61// Sets up the Redfish Route. All parameters are handled by the default handler.
62[[nodiscard]] inline bool setUpRedfishRoute(crow::App& app,
63 const crow::Request& req,
64 crow::Response& res)
65{
66 // This route |delegated| is never used
67 query_param::Query delegated;
68 return setUpRedfishRouteWithDelegation(app, req, res, delegated,
69 query_param::QueryCapabilities{});
70}
Ed Tanousf4c99e72021-10-04 17:02:43 -070071} // namespace redfish