Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include "utils/query_param.hpp" |
| 4 | |
| 5 | #include <bmcweb_config.h> |
| 6 | |
| 7 | namespace redfish |
| 8 | { |
| 9 | |
| 10 | [[nodiscard]] inline bool setUpRedfishRoute(crow::App& app, |
| 11 | const crow::Request& req, |
| 12 | crow::Response& res) |
| 13 | { |
Ed Tanous | 142ec9a | 2022-03-24 18:20:45 -0700 | [diff] [blame^] | 14 | 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 Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 27 | // 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 Tanous | 7cf436c | 2022-03-22 23:53:51 -0700 | [diff] [blame] | 39 | // 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 Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 45 | 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 Tanous | 7cf436c | 2022-03-22 23:53:51 -0700 | [diff] [blame] | 51 | processAllParams(app, query, handler, res); |
Ed Tanous | f4c99e7 | 2021-10-04 17:02:43 -0700 | [diff] [blame] | 52 | }); |
| 53 | return true; |
| 54 | } |
| 55 | } // namespace redfish |