blob: d9401bc69b302bda2fd65aabc2684095a482720b [file] [log] [blame]
Ed Tanousf4c99e72021-10-04 17:02:43 -07001#pragma once
2
Nan Zhoue796c262022-08-02 19:56:29 +00003#include "bmcweb_config.h"
4
5#include "app.hpp"
6#include "async_resp.hpp"
7#include "error_messages.hpp"
8#include "http_request.hpp"
9#include "http_response.hpp"
10#include "logging.hpp"
Ed Tanousf4c99e72021-10-04 17:02:43 -070011#include "utils/query_param.hpp"
12
Nan Zhoue796c262022-08-02 19:56:29 +000013#include <boost/beast/http/verb.hpp>
14#include <boost/url/params_view.hpp>
15#include <boost/url/url_view.hpp>
16
17#include <functional>
18#include <memory>
19#include <new>
20#include <optional>
21#include <string>
22#include <string_view>
23#include <type_traits>
24#include <utility>
25
26// IWYU pragma: no_forward_declare crow::App
27// IWYU pragma: no_include <boost/url/impl/params_view.hpp>
28// IWYU pragma: no_include <boost/url/impl/url_view.hpp>
Ed Tanousf4c99e72021-10-04 17:02:43 -070029
30namespace redfish
31{
32
Nan Zhoua6b91252022-04-04 13:10:40 -070033// Sets up the Redfish Route and delegates some of the query parameter
34// processing. |queryCapabilities| stores which query parameters will be
35// handled by redfish-core/lib codes, then default query parameter handler won't
36// process these parameters.
37[[nodiscard]] inline bool setUpRedfishRouteWithDelegation(
Carson Labrado3ba00072022-06-06 19:40:56 +000038 crow::App& app, const crow::Request& req,
39 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Nan Zhoua6b91252022-04-04 13:10:40 -070040 query_param::Query& delegated,
41 const query_param::QueryCapabilities& queryCapabilities)
Ed Tanousf4c99e72021-10-04 17:02:43 -070042{
Ed Tanous142ec9a2022-03-24 18:20:45 -070043 BMCWEB_LOG_DEBUG << "setup redfish route";
44
45 // Section 7.4 of the redfish spec "Redfish Services shall process the
46 // [OData-Version header] in the following table as defined by the HTTP 1.1
47 // specification..."
48 // Required to pass redfish-protocol-validator REQ_HEADERS_ODATA_VERSION
49 std::string_view odataHeader = req.getHeaderValue("OData-Version");
50 if (!odataHeader.empty() && odataHeader != "4.0")
51 {
Carson Labrado3ba00072022-06-06 19:40:56 +000052 messages::preconditionFailed(asyncResp->res);
Ed Tanous142ec9a2022-03-24 18:20:45 -070053 return false;
54 }
55
Carson Labrado3ba00072022-06-06 19:40:56 +000056 asyncResp->res.addHeader("OData-Version", "4.0");
Ed Tanousc02a74f2022-05-11 14:46:44 -070057
Ed Tanousf4c99e72021-10-04 17:02:43 -070058 std::optional<query_param::Query> queryOpt =
Carson Labrado3ba00072022-06-06 19:40:56 +000059 query_param::parseParameters(req.urlView.params(), asyncResp->res);
Ed Tanousf4c99e72021-10-04 17:02:43 -070060 if (queryOpt == std::nullopt)
61 {
62 return false;
63 }
64
Ed Tanous7cf436c2022-03-22 23:53:51 -070065 // If this isn't a get, no need to do anything with parameters
66 if (req.method() != boost::beast::http::verb::get)
67 {
68 return true;
69 }
70
Nan Zhoua6b91252022-04-04 13:10:40 -070071 delegated = query_param::delegate(queryCapabilities, *queryOpt);
Ed Tanousf4c99e72021-10-04 17:02:43 -070072 std::function<void(crow::Response&)> handler =
Carson Labrado3ba00072022-06-06 19:40:56 +000073 asyncResp->res.releaseCompleteRequestHandler();
74 asyncResp->res.setCompleteRequestHandler(
Ed Tanousf4c99e72021-10-04 17:02:43 -070075 [&app, handler(std::move(handler)),
Ed Tanous8a592812022-06-04 09:06:59 -070076 query{*queryOpt}](crow::Response& resIn) mutable {
77 processAllParams(app, query, handler, resIn);
Ed Tanous002d39b2022-05-31 08:59:27 -070078 });
Ed Tanousf4c99e72021-10-04 17:02:43 -070079 return true;
80}
Nan Zhoua6b91252022-04-04 13:10:40 -070081
82// Sets up the Redfish Route. All parameters are handled by the default handler.
Carson Labrado3ba00072022-06-06 19:40:56 +000083[[nodiscard]] inline bool
84 setUpRedfishRoute(crow::App& app, const crow::Request& req,
85 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Nan Zhoua6b91252022-04-04 13:10:40 -070086{
87 // This route |delegated| is never used
88 query_param::Query delegated;
Carson Labrado3ba00072022-06-06 19:40:56 +000089 return setUpRedfishRouteWithDelegation(app, req, asyncResp, delegated,
Nan Zhoua6b91252022-04-04 13:10:40 -070090 query_param::QueryCapabilities{});
91}
Ed Tanousf4c99e72021-10-04 17:02:43 -070092} // namespace redfish