blob: 7db370d9fb1fc62520a9b3b3d3fc90988fe6cd3a [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
10[[nodiscard]] inline bool setUpRedfishRoute(crow::App& app,
11 const crow::Request& req,
12 crow::Response& res)
13{
Ed Tanous142ec9a2022-03-24 18:20:45 -070014 BMCWEB_LOG_DEBUG << "setup redfish route";
15
16 // Section 7.4 of the redfish spec "Redfish Services shall process the
17 // [OData-Version header] in the following table as defined by the HTTP 1.1
18 // specification..."
19 // Required to pass redfish-protocol-validator REQ_HEADERS_ODATA_VERSION
20 std::string_view odataHeader = req.getHeaderValue("OData-Version");
21 if (!odataHeader.empty() && odataHeader != "4.0")
22 {
23 messages::preconditionFailed(res);
24 return false;
25 }
26
Ed Tanousf4c99e72021-10-04 17:02:43 -070027 // If query parameters aren't enabled, do nothing.
28 if constexpr (!bmcwebInsecureEnableQueryParams)
29 {
30 return true;
31 }
32 std::optional<query_param::Query> queryOpt =
33 query_param::parseParameters(req.urlView.params(), res);
34 if (queryOpt == std::nullopt)
35 {
36 return false;
37 }
38
Ed Tanous7cf436c2022-03-22 23:53:51 -070039 // If this isn't a get, no need to do anything with parameters
40 if (req.method() != boost::beast::http::verb::get)
41 {
42 return true;
43 }
44
Ed Tanousf4c99e72021-10-04 17:02:43 -070045 std::function<void(crow::Response&)> handler =
46 res.releaseCompleteRequestHandler();
47
48 res.setCompleteRequestHandler(
49 [&app, handler(std::move(handler)),
50 query{*queryOpt}](crow::Response& res) mutable {
Ed Tanous7cf436c2022-03-22 23:53:51 -070051 processAllParams(app, query, handler, res);
Ed Tanousf4c99e72021-10-04 17:02:43 -070052 });
53 return true;
54}
55} // namespace redfish