blob: e41ce936f88241af61a7a00cb83446a85ed286d9 [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
2
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08003#include "async_resp.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -07004#include "common.hpp"
Ed Tanous168e20c2021-12-13 14:39:53 -08005#include "dbus_utility.hpp"
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06006#include "error_messages.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -07007#include "http_request.hpp"
8#include "http_response.hpp"
9#include "logging.hpp"
Tanousf00032d2018-11-05 01:18:10 -030010#include "privileges.hpp"
Ratan Gupta6f359562019-04-03 10:39:08 +053011#include "sessions.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -070012#include "utility.hpp"
Ed Tanous3d183202023-03-10 09:21:58 -080013#include "utils/dbus_utils.hpp"
Ed Tanous2c9efc32022-07-31 22:08:26 -070014#include "verb.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -070015#include "websocket.hpp"
Ed Tanous1abe55e2018-09-05 08:30:59 -070016
Ed Tanous88a03c52022-03-14 10:16:07 -070017#include <boost/beast/ssl/ssl_stream.hpp>
Tanousf00032d2018-11-05 01:18:10 -030018#include <boost/container/flat_map.hpp>
Ed Tanous3d183202023-03-10 09:21:58 -080019#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050020
Ed Tanouse0d918b2018-03-27 17:41:04 -070021#include <cerrno>
Ed Tanous7045c8d2017-04-03 10:04:37 -070022#include <cstdint>
Ed Tanouse0d918b2018-03-27 17:41:04 -070023#include <cstdlib>
Ed Tanous3dac7492017-08-02 13:46:20 -070024#include <limits>
Ed Tanous7045c8d2017-04-03 10:04:37 -070025#include <memory>
Ed Tanous2c9efc32022-07-31 22:08:26 -070026#include <optional>
Ed Tanous7045c8d2017-04-03 10:04:37 -070027#include <tuple>
Ed Tanous7045c8d2017-04-03 10:04:37 -070028#include <utility>
29#include <vector>
Ed Tanous9140a672017-04-24 17:01:32 -070030
Ed Tanous1abe55e2018-09-05 08:30:59 -070031namespace crow
32{
Tanousf00032d2018-11-05 01:18:10 -030033
Ed Tanous1abe55e2018-09-05 08:30:59 -070034class BaseRule
35{
36 public:
Ed Tanous4e23a442022-06-06 09:57:26 -070037 explicit BaseRule(const std::string& thisRule) : rule(thisRule)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050038 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070039
Ed Tanous0c0084a2019-10-24 15:57:51 -070040 virtual ~BaseRule() = default;
Ed Tanous7045c8d2017-04-03 10:04:37 -070041
Ed Tanousecd6a3a2022-01-07 09:18:40 -080042 BaseRule(const BaseRule&) = delete;
43 BaseRule(BaseRule&&) = delete;
44 BaseRule& operator=(const BaseRule&) = delete;
45 BaseRule& operator=(const BaseRule&&) = delete;
46
Ed Tanous1abe55e2018-09-05 08:30:59 -070047 virtual void validate() = 0;
48 std::unique_ptr<BaseRule> upgrade()
49 {
50 if (ruleToUpgrade)
Ed Tanous3174e4d2020-10-07 11:41:22 -070051 {
Ed Tanous1abe55e2018-09-05 08:30:59 -070052 return std::move(ruleToUpgrade);
Ed Tanous3174e4d2020-10-07 11:41:22 -070053 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070054 return {};
55 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070056
Ed Tanous104f09c2022-01-25 09:56:04 -080057 virtual void handle(const Request& /*req*/,
zhanghch058d1b46d2021-04-01 11:18:24 +080058 const std::shared_ptr<bmcweb::AsyncResp>&,
59 const RoutingParams&) = 0;
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +053060#ifndef BMCWEB_ENABLE_SSL
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +053061 virtual void
62 handleUpgrade(const Request& /*req*/,
63 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
64 boost::asio::ip::tcp::socket&& /*adaptor*/)
Ed Tanous1abe55e2018-09-05 08:30:59 -070065 {
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +053066 asyncResp->res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -070067 }
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +053068#else
Ed Tanous104f09c2022-01-25 09:56:04 -080069 virtual void handleUpgrade(
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +053070 const Request& /*req*/,
71 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous104f09c2022-01-25 09:56:04 -080072 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>&& /*adaptor*/)
Ed Tanous1abe55e2018-09-05 08:30:59 -070073 {
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +053074 asyncResp->res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -070075 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070076#endif
77
Ed Tanous9eb808c2022-01-25 10:19:23 -080078 size_t getMethods() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070079 {
80 return methodsBitfield;
81 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070082
Tanousf00032d2018-11-05 01:18:10 -030083 bool checkPrivileges(const redfish::Privileges& userPrivileges)
84 {
85 // If there are no privileges assigned, assume no privileges
86 // required
87 if (privilegesSet.empty())
88 {
89 return true;
90 }
91
92 for (const redfish::Privileges& requiredPrivileges : privilegesSet)
93 {
94 if (userPrivileges.isSupersetOf(requiredPrivileges))
95 {
96 return true;
97 }
98 }
99 return false;
100 }
101
Ed Tanous2c9efc32022-07-31 22:08:26 -0700102 size_t methodsBitfield{1 << static_cast<size_t>(HttpVerb::Get)};
Ed Tanous44e45182022-07-26 16:47:23 -0700103 static_assert(std::numeric_limits<decltype(methodsBitfield)>::digits >
Ed Tanous759cf102022-07-31 16:36:52 -0700104 methodNotAllowedIndex,
Ed Tanous44e45182022-07-26 16:47:23 -0700105 "Not enough bits to store bitfield");
Ed Tanous7045c8d2017-04-03 10:04:37 -0700106
Tanousf00032d2018-11-05 01:18:10 -0300107 std::vector<redfish::Privileges> privilegesSet;
108
Ed Tanous1abe55e2018-09-05 08:30:59 -0700109 std::string rule;
110 std::string nameStr;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700111
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112 std::unique_ptr<BaseRule> ruleToUpgrade;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700113
Ed Tanous1abe55e2018-09-05 08:30:59 -0700114 friend class Router;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500115 template <typename T>
116 friend struct RuleParameterTraits;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700117};
118
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119namespace detail
120{
121namespace routing_handler_call_helper
122{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500123template <typename T, int Pos>
124struct CallPair
Ed Tanous1abe55e2018-09-05 08:30:59 -0700125{
126 using type = T;
127 static const int pos = Pos;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700128};
129
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500130template <typename H1>
131struct CallParams
Ed Tanous1abe55e2018-09-05 08:30:59 -0700132{
133 H1& handler;
134 const RoutingParams& params;
135 const Request& req;
zhanghch058d1b46d2021-04-01 11:18:24 +0800136 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700137};
138
139template <typename F, int NInt, int NUint, int NDouble, int NString,
140 typename S1, typename S2>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700141struct Call
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500142{};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700143
144template <typename F, int NInt, int NUint, int NDouble, int NString,
145 typename... Args1, typename... Args2>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700146struct Call<F, NInt, NUint, NDouble, NString, black_magic::S<int64_t, Args1...>,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700147 black_magic::S<Args2...>>
148{
149 void operator()(F cparams)
150 {
151 using pushed = typename black_magic::S<Args2...>::template push_back<
152 CallPair<int64_t, NInt>>;
153 Call<F, NInt + 1, NUint, NDouble, NString, black_magic::S<Args1...>,
154 pushed>()(cparams);
155 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700156};
157
158template <typename F, int NInt, int NUint, int NDouble, int NString,
159 typename... Args1, typename... Args2>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700160struct Call<F, NInt, NUint, NDouble, NString,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700161 black_magic::S<uint64_t, Args1...>, black_magic::S<Args2...>>
162{
163 void operator()(F cparams)
164 {
165 using pushed = typename black_magic::S<Args2...>::template push_back<
166 CallPair<uint64_t, NUint>>;
167 Call<F, NInt, NUint + 1, NDouble, NString, black_magic::S<Args1...>,
168 pushed>()(cparams);
169 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700170};
171
172template <typename F, int NInt, int NUint, int NDouble, int NString,
173 typename... Args1, typename... Args2>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700174struct Call<F, NInt, NUint, NDouble, NString, black_magic::S<double, Args1...>,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700175 black_magic::S<Args2...>>
176{
177 void operator()(F cparams)
178 {
179 using pushed = typename black_magic::S<Args2...>::template push_back<
180 CallPair<double, NDouble>>;
181 Call<F, NInt, NUint, NDouble + 1, NString, black_magic::S<Args1...>,
182 pushed>()(cparams);
183 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700184};
185
186template <typename F, int NInt, int NUint, int NDouble, int NString,
187 typename... Args1, typename... Args2>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700188struct Call<F, NInt, NUint, NDouble, NString,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700189 black_magic::S<std::string, Args1...>, black_magic::S<Args2...>>
190{
191 void operator()(F cparams)
192 {
193 using pushed = typename black_magic::S<Args2...>::template push_back<
194 CallPair<std::string, NString>>;
195 Call<F, NInt, NUint, NDouble, NString + 1, black_magic::S<Args1...>,
196 pushed>()(cparams);
197 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700198};
199
200template <typename F, int NInt, int NUint, int NDouble, int NString,
201 typename... Args1>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700202struct Call<F, NInt, NUint, NDouble, NString, black_magic::S<>,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700203 black_magic::S<Args1...>>
204{
205 void operator()(F cparams)
206 {
207 cparams.handler(
zhanghch058d1b46d2021-04-01 11:18:24 +0800208 cparams.req, cparams.asyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700209 cparams.params.template get<typename Args1::type>(Args1::pos)...);
210 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700211};
212
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500213template <typename Func, typename... ArgsWrapped>
214struct Wrapped
Ed Tanous1abe55e2018-09-05 08:30:59 -0700215{
216 template <typename... Args>
217 void set(
218 Func f,
219 typename std::enable_if<
220 !std::is_same<
221 typename std::tuple_element<0, std::tuple<Args..., void>>::type,
222 const Request&>::value,
Ed Tanous104f09c2022-01-25 09:56:04 -0800223 int>::type /*enable*/
224 = 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700225 {
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800226 handler = [f = std::forward<Func>(f)](
zhanghch058d1b46d2021-04-01 11:18:24 +0800227 const Request&,
228 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
229 Args... args) { asyncResp->res.result(f(args...)); };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700230 }
231
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500232 template <typename Req, typename... Args>
233 struct ReqHandlerWrapper
Ed Tanous1abe55e2018-09-05 08:30:59 -0700234 {
Ed Tanous4e23a442022-06-06 09:57:26 -0700235 explicit ReqHandlerWrapper(Func fIn) : f(std::move(fIn))
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500236 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700237
zhanghch058d1b46d2021-04-01 11:18:24 +0800238 void operator()(const Request& req,
239 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
240 Args... args)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700241 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800242 asyncResp->res.result(f(req, args...));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700243 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700244
Ed Tanous1abe55e2018-09-05 08:30:59 -0700245 Func f;
246 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700247
Ed Tanous1abe55e2018-09-05 08:30:59 -0700248 template <typename... Args>
249 void set(
250 Func f,
251 typename std::enable_if<
252 std::is_same<
253 typename std::tuple_element<0, std::tuple<Args..., void>>::type,
254 const Request&>::value &&
255 !std::is_same<typename std::tuple_element<
256 1, std::tuple<Args..., void, void>>::type,
zhanghch058d1b46d2021-04-01 11:18:24 +0800257 const std::shared_ptr<bmcweb::AsyncResp>&>::value,
Ed Tanous104f09c2022-01-25 09:56:04 -0800258 int>::type /*enable*/
259 = 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700260 {
261 handler = ReqHandlerWrapper<Args...>(std::move(f));
262 /*handler = (
263 [f = std::move(f)]
264 (const Request& req, Response& res, Args... args){
Ed Tanousde5c9f32019-03-26 09:17:55 -0700265 res.result(f(req, args...));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700266 res.end();
267 });*/
268 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700269
Ed Tanous1abe55e2018-09-05 08:30:59 -0700270 template <typename... Args>
271 void set(
272 Func f,
273 typename std::enable_if<
274 std::is_same<
275 typename std::tuple_element<0, std::tuple<Args..., void>>::type,
276 const Request&>::value &&
277 std::is_same<typename std::tuple_element<
278 1, std::tuple<Args..., void, void>>::type,
zhanghch058d1b46d2021-04-01 11:18:24 +0800279 const std::shared_ptr<bmcweb::AsyncResp>&>::value,
Ed Tanous104f09c2022-01-25 09:56:04 -0800280 int>::type /*enable*/
281 = 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700282 {
283 handler = std::move(f);
284 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700285
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500286 template <typename... Args>
287 struct HandlerTypeHelper
Ed Tanous1abe55e2018-09-05 08:30:59 -0700288 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800289 using type = std::function<void(
Ed Tanous104f09c2022-01-25 09:56:04 -0800290 const crow::Request& /*req*/,
291 const std::shared_ptr<bmcweb::AsyncResp>&, Args...)>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700292 using args_type =
Ed Tanous988403c2020-08-24 11:29:49 -0700293 black_magic::S<typename black_magic::PromoteT<Args>...>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700294 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700295
Ed Tanous1abe55e2018-09-05 08:30:59 -0700296 template <typename... Args>
297 struct HandlerTypeHelper<const Request&, Args...>
298 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800299 using type = std::function<void(
Ed Tanous104f09c2022-01-25 09:56:04 -0800300 const crow::Request& /*req*/,
301 const std::shared_ptr<bmcweb::AsyncResp>&, Args...)>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700302 using args_type =
Ed Tanous988403c2020-08-24 11:29:49 -0700303 black_magic::S<typename black_magic::PromoteT<Args>...>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700304 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700305
Ed Tanous1abe55e2018-09-05 08:30:59 -0700306 template <typename... Args>
zhanghch058d1b46d2021-04-01 11:18:24 +0800307 struct HandlerTypeHelper<const Request&,
308 const std::shared_ptr<bmcweb::AsyncResp>&, Args...>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700309 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800310 using type = std::function<void(
Ed Tanous104f09c2022-01-25 09:56:04 -0800311 const crow::Request& /*req*/,
312 const std::shared_ptr<bmcweb::AsyncResp>&, Args...)>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700313 using args_type =
Ed Tanous988403c2020-08-24 11:29:49 -0700314 black_magic::S<typename black_magic::PromoteT<Args>...>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700315 };
316
317 typename HandlerTypeHelper<ArgsWrapped...>::type handler;
318
zhanghch058d1b46d2021-04-01 11:18:24 +0800319 void operator()(const Request& req,
320 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700321 const RoutingParams& params)
322 {
323 detail::routing_handler_call_helper::Call<
324 detail::routing_handler_call_helper::CallParams<decltype(handler)>,
325 0, 0, 0, 0, typename HandlerTypeHelper<ArgsWrapped...>::args_type,
326 black_magic::S<>>()(
327 detail::routing_handler_call_helper::CallParams<decltype(handler)>{
zhanghch058d1b46d2021-04-01 11:18:24 +0800328 handler, params, req, asyncResp});
Ed Tanous1abe55e2018-09-05 08:30:59 -0700329 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700330};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700331} // namespace routing_handler_call_helper
332} // namespace detail
Ed Tanous7045c8d2017-04-03 10:04:37 -0700333
Ed Tanous1abe55e2018-09-05 08:30:59 -0700334class WebSocketRule : public BaseRule
335{
336 using self_t = WebSocketRule;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700337
Ed Tanous1abe55e2018-09-05 08:30:59 -0700338 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700339 explicit WebSocketRule(const std::string& ruleIn) : BaseRule(ruleIn)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500340 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700341
Ed Tanous1abe55e2018-09-05 08:30:59 -0700342 void validate() override
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500343 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700344
Ed Tanous104f09c2022-01-25 09:56:04 -0800345 void handle(const Request& /*req*/,
zhanghch058d1b46d2021-04-01 11:18:24 +0800346 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous104f09c2022-01-25 09:56:04 -0800347 const RoutingParams& /*params*/) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700348 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800349 asyncResp->res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700350 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700351
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +0530352#ifndef BMCWEB_ENABLE_SSL
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +0530353 void handleUpgrade(const Request& req,
354 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
Ed Tanousceac6f72018-12-02 11:58:47 -0800355 boost::asio::ip::tcp::socket&& adaptor) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700356 {
Nan Zhou93c02022022-02-24 18:21:07 -0800357 BMCWEB_LOG_DEBUG << "Websocket handles upgrade";
Ratan Gupta02453b12019-10-22 14:43:36 +0530358 std::shared_ptr<
359 crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>>
360 myConnection = std::make_shared<
361 crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>>(
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000362 req, std::move(adaptor), openHandler, messageHandler,
Ratan Gupta02453b12019-10-22 14:43:36 +0530363 closeHandler, errorHandler);
364 myConnection->start();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700365 }
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +0530366#else
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +0530367 void handleUpgrade(const Request& req,
368 const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/,
Ed Tanousceac6f72018-12-02 11:58:47 -0800369 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>&&
370 adaptor) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700371 {
Nan Zhou93c02022022-02-24 18:21:07 -0800372 BMCWEB_LOG_DEBUG << "Websocket handles upgrade";
Ed Tanousceac6f72018-12-02 11:58:47 -0800373 std::shared_ptr<crow::websocket::ConnectionImpl<
374 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>>
375 myConnection = std::make_shared<crow::websocket::ConnectionImpl<
376 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>>(
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000377 req, std::move(adaptor), openHandler, messageHandler,
Ed Tanousceac6f72018-12-02 11:58:47 -0800378 closeHandler, errorHandler);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700379 myConnection->start();
380 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700381#endif
382
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500383 template <typename Func>
384 self_t& onopen(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700385 {
386 openHandler = f;
387 return *this;
388 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700389
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500390 template <typename Func>
391 self_t& onmessage(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700392 {
393 messageHandler = f;
394 return *this;
395 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700396
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500397 template <typename Func>
398 self_t& onclose(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700399 {
400 closeHandler = f;
401 return *this;
402 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700403
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500404 template <typename Func>
405 self_t& onerror(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700406 {
407 errorHandler = f;
408 return *this;
409 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700410
Ed Tanous1abe55e2018-09-05 08:30:59 -0700411 protected:
zhanghch0577726382021-10-21 14:07:57 +0800412 std::function<void(crow::websocket::Connection&)> openHandler;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700413 std::function<void(crow::websocket::Connection&, const std::string&, bool)>
414 messageHandler;
415 std::function<void(crow::websocket::Connection&, const std::string&)>
416 closeHandler;
417 std::function<void(crow::websocket::Connection&)> errorHandler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700418};
419
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500420template <typename T>
421struct RuleParameterTraits
Ed Tanous1abe55e2018-09-05 08:30:59 -0700422{
423 using self_t = T;
424 WebSocketRule& websocket()
425 {
Ed Tanous271584a2019-07-09 16:24:22 -0700426 self_t* self = static_cast<self_t*>(this);
427 WebSocketRule* p = new WebSocketRule(self->rule);
428 self->ruleToUpgrade.reset(p);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700429 return *p;
430 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700431
Ed Tanous26ccae32023-02-16 10:28:44 -0800432 self_t& name(std::string_view name) noexcept
Ed Tanous1abe55e2018-09-05 08:30:59 -0700433 {
Ed Tanous271584a2019-07-09 16:24:22 -0700434 self_t* self = static_cast<self_t*>(this);
Ed Tanousf23b7292020-10-15 09:41:17 -0700435 self->nameStr = name;
Ed Tanous271584a2019-07-09 16:24:22 -0700436 return *self;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700437 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700438
Ed Tanous1abe55e2018-09-05 08:30:59 -0700439 self_t& methods(boost::beast::http::verb method)
440 {
Ed Tanous271584a2019-07-09 16:24:22 -0700441 self_t* self = static_cast<self_t*>(this);
Ed Tanous2c9efc32022-07-31 22:08:26 -0700442 std::optional<HttpVerb> verb = httpVerbFromBoost(method);
443 if (verb)
444 {
445 self->methodsBitfield = 1U << static_cast<size_t>(*verb);
446 }
Ed Tanous271584a2019-07-09 16:24:22 -0700447 return *self;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700448 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700449
Ed Tanous1abe55e2018-09-05 08:30:59 -0700450 template <typename... MethodArgs>
Ed Tanous81ce6092020-12-17 16:54:55 +0000451 self_t& methods(boost::beast::http::verb method, MethodArgs... argsMethod)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700452 {
Ed Tanous271584a2019-07-09 16:24:22 -0700453 self_t* self = static_cast<self_t*>(this);
Ed Tanous81ce6092020-12-17 16:54:55 +0000454 methods(argsMethod...);
Ed Tanous2c9efc32022-07-31 22:08:26 -0700455 std::optional<HttpVerb> verb = httpVerbFromBoost(method);
456 if (verb)
457 {
458 self->methodsBitfield |= 1U << static_cast<size_t>(*verb);
459 }
Ed Tanous271584a2019-07-09 16:24:22 -0700460 return *self;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700461 }
Tanousf00032d2018-11-05 01:18:10 -0300462
Ed Tanous44e45182022-07-26 16:47:23 -0700463 self_t& notFound()
464 {
465 self_t* self = static_cast<self_t*>(this);
466 self->methodsBitfield = 1U << notFoundIndex;
467 return *self;
468 }
469
Ed Tanous759cf102022-07-31 16:36:52 -0700470 self_t& methodNotAllowed()
471 {
472 self_t* self = static_cast<self_t*>(this);
473 self->methodsBitfield = 1U << methodNotAllowedIndex;
474 return *self;
475 }
476
Ed Tanous432a8902021-06-14 15:28:56 -0700477 self_t& privileges(
478 const std::initializer_list<std::initializer_list<const char*>>& p)
Tanousf00032d2018-11-05 01:18:10 -0300479 {
Ed Tanous271584a2019-07-09 16:24:22 -0700480 self_t* self = static_cast<self_t*>(this);
Ed Tanous432a8902021-06-14 15:28:56 -0700481 for (const std::initializer_list<const char*>& privilege : p)
Tanousf00032d2018-11-05 01:18:10 -0300482 {
Ed Tanous271584a2019-07-09 16:24:22 -0700483 self->privilegesSet.emplace_back(privilege);
Tanousf00032d2018-11-05 01:18:10 -0300484 }
Ed Tanous271584a2019-07-09 16:24:22 -0700485 return *self;
Tanousf00032d2018-11-05 01:18:10 -0300486 }
Ed Tanoused398212021-06-09 17:05:54 -0700487
488 template <size_t N, typename... MethodArgs>
489 self_t& privileges(const std::array<redfish::Privileges, N>& p)
490 {
491 self_t* self = static_cast<self_t*>(this);
492 for (const redfish::Privileges& privilege : p)
493 {
494 self->privilegesSet.emplace_back(privilege);
495 }
496 return *self;
497 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700498};
499
Ed Tanous1abe55e2018-09-05 08:30:59 -0700500class DynamicRule : public BaseRule, public RuleParameterTraits<DynamicRule>
501{
502 public:
Ed Tanous4e23a442022-06-06 09:57:26 -0700503 explicit DynamicRule(const std::string& ruleIn) : BaseRule(ruleIn)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500504 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700505
Ed Tanous1abe55e2018-09-05 08:30:59 -0700506 void validate() override
507 {
508 if (!erasedHandler)
509 {
510 throw std::runtime_error(nameStr + (!nameStr.empty() ? ": " : "") +
511 "no handler for url " + rule);
512 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700513 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700514
zhanghch058d1b46d2021-04-01 11:18:24 +0800515 void handle(const Request& req,
516 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700517 const RoutingParams& params) override
518 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800519 erasedHandler(req, asyncResp, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700520 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700521
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500522 template <typename Func>
523 void operator()(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700524 {
Ed Tanousc867a832022-03-10 14:17:00 -0800525 using boost::callable_traits::args_t;
526 constexpr size_t arity = std::tuple_size<args_t<Func>>::value;
527 constexpr auto is = std::make_integer_sequence<unsigned, arity>{};
528 erasedHandler = wrap(std::move(f), is);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700529 }
530
531 // enable_if Arg1 == request && Arg2 == Response
Gunnar Mills6be0e402020-07-08 13:21:51 -0500532 // enable_if Arg1 == request && Arg2 != response
Ed Tanous1abe55e2018-09-05 08:30:59 -0700533 // enable_if Arg1 != request
534
535 template <typename Func, unsigned... Indices>
zhanghch058d1b46d2021-04-01 11:18:24 +0800536 std::function<void(const Request&,
537 const std::shared_ptr<bmcweb::AsyncResp>&,
538 const RoutingParams&)>
Ed Tanous104f09c2022-01-25 09:56:04 -0800539 wrap(Func f, std::integer_sequence<unsigned, Indices...> /*is*/)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700540 {
Ed Tanousc867a832022-03-10 14:17:00 -0800541 using function_t = crow::utility::FunctionTraits<Func>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700542
543 if (!black_magic::isParameterTagCompatible(
Ed Tanous988403c2020-08-24 11:29:49 -0700544 black_magic::getParameterTag(rule.c_str()),
545 black_magic::computeParameterTagFromArgsList<
Ed Tanous1abe55e2018-09-05 08:30:59 -0700546 typename function_t::template arg<Indices>...>::value))
547 {
548 throw std::runtime_error("routeDynamic: Handler type is mismatched "
549 "with URL parameters: " +
550 rule);
551 }
552 auto ret = detail::routing_handler_call_helper::Wrapped<
553 Func, typename function_t::template arg<Indices>...>();
554 ret.template set<typename function_t::template arg<Indices>...>(
555 std::move(f));
556 return ret;
557 }
558
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500559 template <typename Func>
560 void operator()(std::string name, Func&& f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700561 {
562 nameStr = std::move(name);
563 (*this).template operator()<Func>(std::forward(f));
564 }
565
566 private:
zhanghch058d1b46d2021-04-01 11:18:24 +0800567 std::function<void(const Request&,
568 const std::shared_ptr<bmcweb::AsyncResp>&,
569 const RoutingParams&)>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700570 erasedHandler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700571};
572
573template <typename... Args>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500574class TaggedRule :
575 public BaseRule,
576 public RuleParameterTraits<TaggedRule<Args...>>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700577{
578 public:
579 using self_t = TaggedRule<Args...>;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700580
Ed Tanous4e23a442022-06-06 09:57:26 -0700581 explicit TaggedRule(const std::string& ruleIn) : BaseRule(ruleIn)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500582 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700583
Ed Tanous1abe55e2018-09-05 08:30:59 -0700584 void validate() override
585 {
586 if (!handler)
587 {
588 throw std::runtime_error(nameStr + (!nameStr.empty() ? ": " : "") +
589 "no handler for url " + rule);
590 }
591 }
592
593 template <typename Func>
594 typename std::enable_if<
595 black_magic::CallHelper<Func, black_magic::S<Args...>>::value,
596 void>::type
597 operator()(Func&& f)
598 {
599 static_assert(
600 black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
601 black_magic::CallHelper<
602 Func, black_magic::S<crow::Request, Args...>>::value,
603 "Handler type is mismatched with URL parameters");
604 static_assert(
605 !std::is_same<void, decltype(f(std::declval<Args>()...))>::value,
606 "Handler function cannot have void return type; valid return "
607 "types: "
Gunnar Mills6be0e402020-07-08 13:21:51 -0500608 "string, int, crow::response, nlohmann::json");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700609
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800610 handler = [f = std::forward<Func>(f)](
zhanghch058d1b46d2021-04-01 11:18:24 +0800611 const Request&,
612 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
613 Args... args) { asyncResp->res.result(f(args...)); };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700614 }
615
616 template <typename Func>
617 typename std::enable_if<
618 !black_magic::CallHelper<Func, black_magic::S<Args...>>::value &&
Ed Tanous7045c8d2017-04-03 10:04:37 -0700619 black_magic::CallHelper<
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700620 Func, black_magic::S<crow::Request, Args...>>::value,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700621 void>::type
622 operator()(Func&& f)
623 {
624 static_assert(
625 black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
626 black_magic::CallHelper<
627 Func, black_magic::S<crow::Request, Args...>>::value,
628 "Handler type is mismatched with URL parameters");
629 static_assert(
630 !std::is_same<void, decltype(f(std::declval<crow::Request>(),
631 std::declval<Args>()...))>::value,
632 "Handler function cannot have void return type; valid return "
633 "types: "
Gunnar Mills6be0e402020-07-08 13:21:51 -0500634 "string, int, crow::response,nlohmann::json");
Ed Tanous7045c8d2017-04-03 10:04:37 -0700635
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800636 handler = [f = std::forward<Func>(f)](
zhanghch058d1b46d2021-04-01 11:18:24 +0800637 const crow::Request& req,
638 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
639 Args... args) { asyncResp->res.result(f(req, args...)); };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700640 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700641
Ed Tanous1abe55e2018-09-05 08:30:59 -0700642 template <typename Func>
643 typename std::enable_if<
644 !black_magic::CallHelper<Func, black_magic::S<Args...>>::value &&
645 !black_magic::CallHelper<
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700646 Func, black_magic::S<crow::Request, Args...>>::value,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700647 void>::type
648 operator()(Func&& f)
649 {
650 static_assert(
651 black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
652 black_magic::CallHelper<
653 Func, black_magic::S<crow::Request, Args...>>::value ||
654 black_magic::CallHelper<
zhanghch058d1b46d2021-04-01 11:18:24 +0800655 Func, black_magic::S<crow::Request,
656 std::shared_ptr<bmcweb::AsyncResp>&,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700657 Args...>>::value,
658 "Handler type is mismatched with URL parameters");
659 static_assert(
zhanghch058d1b46d2021-04-01 11:18:24 +0800660 std::is_same<
661 void,
662 decltype(f(std::declval<crow::Request>(),
663 std::declval<std::shared_ptr<bmcweb::AsyncResp>&>(),
664 std::declval<Args>()...))>::value,
Tanousf00032d2018-11-05 01:18:10 -0300665 "Handler function with response argument should have void "
666 "return "
Ed Tanous1abe55e2018-09-05 08:30:59 -0700667 "type");
Ed Tanous7045c8d2017-04-03 10:04:37 -0700668
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800669 handler = std::forward<Func>(f);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700670 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700671
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500672 template <typename Func>
Ed Tanous26ccae32023-02-16 10:28:44 -0800673 void operator()(std::string_view name, Func&& f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700674 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700675 nameStr = name;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700676 (*this).template operator()<Func>(std::forward(f));
677 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700678
zhanghch058d1b46d2021-04-01 11:18:24 +0800679 void handle(const Request& req,
680 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700681 const RoutingParams& params) override
682 {
683 detail::routing_handler_call_helper::Call<
684 detail::routing_handler_call_helper::CallParams<decltype(handler)>,
685 0, 0, 0, 0, black_magic::S<Args...>, black_magic::S<>>()(
686 detail::routing_handler_call_helper::CallParams<decltype(handler)>{
zhanghch058d1b46d2021-04-01 11:18:24 +0800687 handler, params, req, asyncResp});
Ed Tanous1abe55e2018-09-05 08:30:59 -0700688 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700689
Ed Tanous1abe55e2018-09-05 08:30:59 -0700690 private:
zhanghch058d1b46d2021-04-01 11:18:24 +0800691 std::function<void(const crow::Request&,
692 const std::shared_ptr<bmcweb::AsyncResp>&, Args...)>
693 handler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700694};
695
Ed Tanous1abe55e2018-09-05 08:30:59 -0700696class Trie
697{
698 public:
699 struct Node
700 {
701 unsigned ruleIndex{};
Ed Tanous271584a2019-07-09 16:24:22 -0700702 std::array<size_t, static_cast<size_t>(ParamType::MAX)>
703 paramChildrens{};
Ed Tanousa94ac612022-02-22 11:13:24 -0800704 using ChildMap = boost::container::flat_map<
705 std::string, unsigned, std::less<>,
706 std::vector<std::pair<std::string, unsigned>>>;
707 ChildMap children;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700708
Ed Tanous1abe55e2018-09-05 08:30:59 -0700709 bool isSimpleNode() const
710 {
Ed Tanouse662eae2022-01-25 10:39:19 -0800711 return ruleIndex == 0 &&
712 std::all_of(std::begin(paramChildrens),
713 std::end(paramChildrens),
714 [](size_t x) { return x == 0U; });
Ed Tanous7045c8d2017-04-03 10:04:37 -0700715 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700716 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700717
Ed Tanous1abe55e2018-09-05 08:30:59 -0700718 Trie() : nodes(1)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500719 {}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700720
721 private:
722 void optimizeNode(Node* node)
723 {
Ed Tanous271584a2019-07-09 16:24:22 -0700724 for (size_t x : node->paramChildrens)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700725 {
Ed Tanousdbb59d42022-01-25 11:09:55 -0800726 if (x == 0U)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700727 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700728 continue;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700729 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700730 Node* child = &nodes[x];
731 optimizeNode(child);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700732 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700733 if (node->children.empty())
Ed Tanous3174e4d2020-10-07 11:41:22 -0700734 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700735 return;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700736 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700737 bool mergeWithChild = true;
Ed Tanousa94ac612022-02-22 11:13:24 -0800738 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700739 {
740 Node* child = &nodes[kv.second];
741 if (!child->isSimpleNode())
742 {
743 mergeWithChild = false;
744 break;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700745 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700746 }
747 if (mergeWithChild)
748 {
Ed Tanousa94ac612022-02-22 11:13:24 -0800749 Node::ChildMap merged;
750 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700751 {
752 Node* child = &nodes[kv.second];
Ed Tanousa94ac612022-02-22 11:13:24 -0800753 for (const Node::ChildMap::value_type& childKv :
Tanousf00032d2018-11-05 01:18:10 -0300754 child->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700755 {
756 merged[kv.first + childKv.first] = childKv.second;
757 }
758 }
759 node->children = std::move(merged);
760 optimizeNode(node);
761 }
762 else
763 {
Ed Tanousa94ac612022-02-22 11:13:24 -0800764 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700765 {
766 Node* child = &nodes[kv.second];
767 optimizeNode(child);
768 }
769 }
770 }
771
772 void optimize()
773 {
774 optimizeNode(head());
775 }
776
777 public:
778 void validate()
779 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700780 optimize();
781 }
782
Ed Tanous81ce6092020-12-17 16:54:55 +0000783 void findRouteIndexes(const std::string& reqUrl,
784 std::vector<unsigned>& routeIndexes,
Tanousf00032d2018-11-05 01:18:10 -0300785 const Node* node = nullptr, unsigned pos = 0) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700786 {
787 if (node == nullptr)
788 {
789 node = head();
790 }
Ed Tanousa94ac612022-02-22 11:13:24 -0800791 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700792 {
793 const std::string& fragment = kv.first;
794 const Node* child = &nodes[kv.second];
Ed Tanous81ce6092020-12-17 16:54:55 +0000795 if (pos >= reqUrl.size())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700796 {
797 if (child->ruleIndex != 0 && fragment != "/")
798 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000799 routeIndexes.push_back(child->ruleIndex);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700800 }
Ed Tanous81ce6092020-12-17 16:54:55 +0000801 findRouteIndexes(reqUrl, routeIndexes, child,
Ed Tanous271584a2019-07-09 16:24:22 -0700802 static_cast<unsigned>(pos + fragment.size()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700803 }
804 else
805 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000806 if (reqUrl.compare(pos, fragment.size(), fragment) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700807 {
Ed Tanous271584a2019-07-09 16:24:22 -0700808 findRouteIndexes(
Ed Tanous81ce6092020-12-17 16:54:55 +0000809 reqUrl, routeIndexes, child,
Ed Tanous271584a2019-07-09 16:24:22 -0700810 static_cast<unsigned>(pos + fragment.size()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700811 }
812 }
813 }
814 }
815
816 std::pair<unsigned, RoutingParams>
Ed Tanous26ccae32023-02-16 10:28:44 -0800817 find(std::string_view reqUrl, const Node* node = nullptr,
Ed Tanous271584a2019-07-09 16:24:22 -0700818 size_t pos = 0, RoutingParams* params = nullptr) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700819 {
820 RoutingParams empty;
821 if (params == nullptr)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700822 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700823 params = &empty;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700824 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700825
826 unsigned found{};
827 RoutingParams matchParams;
828
829 if (node == nullptr)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700830 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700831 node = head();
Ed Tanous3174e4d2020-10-07 11:41:22 -0700832 }
Ed Tanous81ce6092020-12-17 16:54:55 +0000833 if (pos == reqUrl.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -0700834 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700835 return {node->ruleIndex, *params};
Ed Tanous3174e4d2020-10-07 11:41:22 -0700836 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700837
838 auto updateFound =
839 [&found, &matchParams](std::pair<unsigned, RoutingParams>& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700840 if (ret.first != 0U && (found == 0U || found > ret.first))
841 {
842 found = ret.first;
843 matchParams = std::move(ret.second);
844 }
845 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700846
Ed Tanouse662eae2022-01-25 10:39:19 -0800847 if (node->paramChildrens[static_cast<size_t>(ParamType::INT)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700848 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000849 char c = reqUrl[pos];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700850 if ((c >= '0' && c <= '9') || c == '+' || c == '-')
851 {
Ed Tanous543f4402022-01-06 13:12:53 -0800852 char* eptr = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700853 errno = 0;
854 long long int value =
Ed Tanous81ce6092020-12-17 16:54:55 +0000855 std::strtoll(reqUrl.data() + pos, &eptr, 10);
856 if (errno != ERANGE && eptr != reqUrl.data() + pos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700857 {
858 params->intParams.push_back(value);
Ed Tanous81ce6092020-12-17 16:54:55 +0000859 std::pair<unsigned, RoutingParams> ret =
860 find(reqUrl,
861 &nodes[node->paramChildrens[static_cast<size_t>(
862 ParamType::INT)]],
863 static_cast<size_t>(eptr - reqUrl.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700864 updateFound(ret);
865 params->intParams.pop_back();
866 }
867 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700868 }
869
Ed Tanouse662eae2022-01-25 10:39:19 -0800870 if (node->paramChildrens[static_cast<size_t>(ParamType::UINT)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700871 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000872 char c = reqUrl[pos];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700873 if ((c >= '0' && c <= '9') || c == '+')
874 {
Ed Tanous543f4402022-01-06 13:12:53 -0800875 char* eptr = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700876 errno = 0;
877 unsigned long long int value =
Ed Tanous81ce6092020-12-17 16:54:55 +0000878 std::strtoull(reqUrl.data() + pos, &eptr, 10);
879 if (errno != ERANGE && eptr != reqUrl.data() + pos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700880 {
881 params->uintParams.push_back(value);
Ed Tanous81ce6092020-12-17 16:54:55 +0000882 std::pair<unsigned, RoutingParams> ret =
883 find(reqUrl,
884 &nodes[node->paramChildrens[static_cast<size_t>(
885 ParamType::UINT)]],
886 static_cast<size_t>(eptr - reqUrl.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700887 updateFound(ret);
888 params->uintParams.pop_back();
889 }
890 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700891 }
892
Ed Tanouse662eae2022-01-25 10:39:19 -0800893 if (node->paramChildrens[static_cast<size_t>(ParamType::DOUBLE)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700894 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000895 char c = reqUrl[pos];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700896 if ((c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')
897 {
Ed Tanous543f4402022-01-06 13:12:53 -0800898 char* eptr = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700899 errno = 0;
Ed Tanous81ce6092020-12-17 16:54:55 +0000900 double value = std::strtod(reqUrl.data() + pos, &eptr);
901 if (errno != ERANGE && eptr != reqUrl.data() + pos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700902 {
903 params->doubleParams.push_back(value);
Ed Tanous81ce6092020-12-17 16:54:55 +0000904 std::pair<unsigned, RoutingParams> ret =
905 find(reqUrl,
906 &nodes[node->paramChildrens[static_cast<size_t>(
907 ParamType::DOUBLE)]],
908 static_cast<size_t>(eptr - reqUrl.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700909 updateFound(ret);
910 params->doubleParams.pop_back();
911 }
912 }
913 }
914
Ed Tanouse662eae2022-01-25 10:39:19 -0800915 if (node->paramChildrens[static_cast<size_t>(ParamType::STRING)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700916 {
Ed Tanousb01bf292019-03-25 19:25:26 +0000917 size_t epos = pos;
Ed Tanous81ce6092020-12-17 16:54:55 +0000918 for (; epos < reqUrl.size(); epos++)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700919 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000920 if (reqUrl[epos] == '/')
Ed Tanous3174e4d2020-10-07 11:41:22 -0700921 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700922 break;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700923 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700924 }
925
926 if (epos != pos)
927 {
928 params->stringParams.emplace_back(
Ed Tanous81ce6092020-12-17 16:54:55 +0000929 reqUrl.substr(pos, epos - pos));
Tanousf00032d2018-11-05 01:18:10 -0300930 std::pair<unsigned, RoutingParams> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000931 find(reqUrl,
Ed Tanous271584a2019-07-09 16:24:22 -0700932 &nodes[node->paramChildrens[static_cast<size_t>(
933 ParamType::STRING)]],
Ed Tanousb01bf292019-03-25 19:25:26 +0000934 epos, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700935 updateFound(ret);
936 params->stringParams.pop_back();
937 }
938 }
939
Ed Tanouse662eae2022-01-25 10:39:19 -0800940 if (node->paramChildrens[static_cast<size_t>(ParamType::PATH)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700941 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000942 size_t epos = reqUrl.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700943
944 if (epos != pos)
945 {
946 params->stringParams.emplace_back(
Ed Tanous81ce6092020-12-17 16:54:55 +0000947 reqUrl.substr(pos, epos - pos));
Ed Tanous271584a2019-07-09 16:24:22 -0700948 std::pair<unsigned, RoutingParams> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000949 find(reqUrl,
Ed Tanous271584a2019-07-09 16:24:22 -0700950 &nodes[node->paramChildrens[static_cast<size_t>(
951 ParamType::PATH)]],
952 epos, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700953 updateFound(ret);
954 params->stringParams.pop_back();
955 }
956 }
957
Ed Tanousa94ac612022-02-22 11:13:24 -0800958 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700959 {
960 const std::string& fragment = kv.first;
961 const Node* child = &nodes[kv.second];
962
Ed Tanous81ce6092020-12-17 16:54:55 +0000963 if (reqUrl.compare(pos, fragment.size(), fragment) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700964 {
Tanousf00032d2018-11-05 01:18:10 -0300965 std::pair<unsigned, RoutingParams> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000966 find(reqUrl, child, pos + fragment.size(), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700967 updateFound(ret);
968 }
969 }
970
971 return {found, matchParams};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700972 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700973
974 void add(const std::string& url, unsigned ruleIndex)
975 {
Ed Tanous271584a2019-07-09 16:24:22 -0700976 size_t idx = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700977
978 for (unsigned i = 0; i < url.size(); i++)
979 {
980 char c = url[i];
981 if (c == '<')
982 {
Tanousf00032d2018-11-05 01:18:10 -0300983 const static std::array<std::pair<ParamType, std::string>, 7>
984 paramTraits = {{
985 {ParamType::INT, "<int>"},
986 {ParamType::UINT, "<uint>"},
987 {ParamType::DOUBLE, "<float>"},
988 {ParamType::DOUBLE, "<double>"},
989 {ParamType::STRING, "<str>"},
990 {ParamType::STRING, "<string>"},
991 {ParamType::PATH, "<path>"},
992 }};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700993
Tanousf00032d2018-11-05 01:18:10 -0300994 for (const std::pair<ParamType, std::string>& x : paramTraits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700995 {
Tanousf00032d2018-11-05 01:18:10 -0300996 if (url.compare(i, x.second.size(), x.second) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700997 {
Ed Tanous271584a2019-07-09 16:24:22 -0700998 size_t index = static_cast<size_t>(x.first);
Ed Tanouse662eae2022-01-25 10:39:19 -0800999 if (nodes[idx].paramChildrens[index] == 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001000 {
Tanousf00032d2018-11-05 01:18:10 -03001001 unsigned newNodeIdx = newNode();
Ed Tanous271584a2019-07-09 16:24:22 -07001002 nodes[idx].paramChildrens[index] = newNodeIdx;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001003 }
Ed Tanous271584a2019-07-09 16:24:22 -07001004 idx = nodes[idx].paramChildrens[index];
1005 i += static_cast<unsigned>(x.second.size());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001006 break;
1007 }
1008 }
1009
1010 i--;
1011 }
1012 else
1013 {
1014 std::string piece(&c, 1);
Ed Tanouse662eae2022-01-25 10:39:19 -08001015 if (nodes[idx].children.count(piece) == 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001016 {
Tanousf00032d2018-11-05 01:18:10 -03001017 unsigned newNodeIdx = newNode();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001018 nodes[idx].children.emplace(piece, newNodeIdx);
1019 }
1020 idx = nodes[idx].children[piece];
1021 }
1022 }
Ed Tanouse662eae2022-01-25 10:39:19 -08001023 if (nodes[idx].ruleIndex != 0U)
Ed Tanous3174e4d2020-10-07 11:41:22 -07001024 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001025 throw std::runtime_error("handler already exists for " + url);
Ed Tanous3174e4d2020-10-07 11:41:22 -07001026 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001027 nodes[idx].ruleIndex = ruleIndex;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001028 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001029
Ed Tanous1abe55e2018-09-05 08:30:59 -07001030 private:
Ed Tanous271584a2019-07-09 16:24:22 -07001031 void debugNodePrint(Node* n, size_t level)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001032 {
Ed Tanous271584a2019-07-09 16:24:22 -07001033 for (size_t i = 0; i < static_cast<size_t>(ParamType::MAX); i++)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001034 {
Ed Tanouse662eae2022-01-25 10:39:19 -08001035 if (n->paramChildrens[i] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001036 {
1037 BMCWEB_LOG_DEBUG << std::string(
Ed Tanous271584a2019-07-09 16:24:22 -07001038 2U * level, ' ') /*<< "("<<n->paramChildrens[i]<<") "*/;
1039 switch (static_cast<ParamType>(i))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001040 {
1041 case ParamType::INT:
1042 BMCWEB_LOG_DEBUG << "<int>";
1043 break;
1044 case ParamType::UINT:
1045 BMCWEB_LOG_DEBUG << "<uint>";
1046 break;
1047 case ParamType::DOUBLE:
1048 BMCWEB_LOG_DEBUG << "<float>";
1049 break;
1050 case ParamType::STRING:
1051 BMCWEB_LOG_DEBUG << "<str>";
1052 break;
1053 case ParamType::PATH:
1054 BMCWEB_LOG_DEBUG << "<path>";
1055 break;
Ed Tanous23a21a12020-07-25 04:45:05 +00001056 case ParamType::MAX:
Ed Tanous1abe55e2018-09-05 08:30:59 -07001057 BMCWEB_LOG_DEBUG << "<ERROR>";
1058 break;
1059 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001060
Ed Tanous1abe55e2018-09-05 08:30:59 -07001061 debugNodePrint(&nodes[n->paramChildrens[i]], level + 1);
1062 }
1063 }
Ed Tanousa94ac612022-02-22 11:13:24 -08001064 for (const Node::ChildMap::value_type& kv : n->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001065 {
1066 BMCWEB_LOG_DEBUG
Ed Tanous271584a2019-07-09 16:24:22 -07001067 << std::string(2U * level, ' ') /*<< "(" << kv.second << ") "*/
Ed Tanous1abe55e2018-09-05 08:30:59 -07001068 << kv.first;
1069 debugNodePrint(&nodes[kv.second], level + 1);
1070 }
1071 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001072
Ed Tanous1abe55e2018-09-05 08:30:59 -07001073 public:
1074 void debugPrint()
1075 {
Ed Tanous271584a2019-07-09 16:24:22 -07001076 debugNodePrint(head(), 0U);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001077 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001078
Ed Tanous1abe55e2018-09-05 08:30:59 -07001079 private:
1080 const Node* head() const
1081 {
1082 return &nodes.front();
1083 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001084
Ed Tanous1abe55e2018-09-05 08:30:59 -07001085 Node* head()
1086 {
1087 return &nodes.front();
1088 }
1089
1090 unsigned newNode()
1091 {
1092 nodes.resize(nodes.size() + 1);
Ed Tanous271584a2019-07-09 16:24:22 -07001093 return static_cast<unsigned>(nodes.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001094 }
1095
1096 std::vector<Node> nodes;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001097};
1098
Ed Tanous1abe55e2018-09-05 08:30:59 -07001099class Router
1100{
1101 public:
Ed Tanous0c0084a2019-10-24 15:57:51 -07001102 Router() = default;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001103
Ed Tanous1abe55e2018-09-05 08:30:59 -07001104 DynamicRule& newRuleDynamic(const std::string& rule)
1105 {
1106 std::unique_ptr<DynamicRule> ruleObject =
1107 std::make_unique<DynamicRule>(rule);
1108 DynamicRule* ptr = ruleObject.get();
Tanousf00032d2018-11-05 01:18:10 -03001109 allRules.emplace_back(std::move(ruleObject));
Ed Tanous7045c8d2017-04-03 10:04:37 -07001110
Ed Tanous1abe55e2018-09-05 08:30:59 -07001111 return *ptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001112 }
1113
Ed Tanous1abe55e2018-09-05 08:30:59 -07001114 template <uint64_t N>
1115 typename black_magic::Arguments<N>::type::template rebind<TaggedRule>&
1116 newRuleTagged(const std::string& rule)
1117 {
1118 using RuleT = typename black_magic::Arguments<N>::type::template rebind<
1119 TaggedRule>;
1120 std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule);
1121 RuleT* ptr = ruleObject.get();
Tanousf00032d2018-11-05 01:18:10 -03001122 allRules.emplace_back(std::move(ruleObject));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001123
1124 return *ptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001125 }
1126
Tanousf00032d2018-11-05 01:18:10 -03001127 void internalAddRuleObject(const std::string& rule, BaseRule* ruleObject)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001128 {
Tanousf00032d2018-11-05 01:18:10 -03001129 if (ruleObject == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001130 {
Tanousf00032d2018-11-05 01:18:10 -03001131 return;
1132 }
Ed Tanous759cf102022-07-31 16:36:52 -07001133 for (size_t method = 0, methodBit = 1; method <= methodNotAllowedIndex;
Ed Tanous2c70f802020-09-28 14:29:23 -07001134 method++, methodBit <<= 1)
Tanousf00032d2018-11-05 01:18:10 -03001135 {
Ed Tanouse662eae2022-01-25 10:39:19 -08001136 if ((ruleObject->methodsBitfield & methodBit) > 0U)
Tanousf00032d2018-11-05 01:18:10 -03001137 {
1138 perMethods[method].rules.emplace_back(ruleObject);
1139 perMethods[method].trie.add(
Ed Tanous271584a2019-07-09 16:24:22 -07001140 rule, static_cast<unsigned>(
1141 perMethods[method].rules.size() - 1U));
Tanousf00032d2018-11-05 01:18:10 -03001142 // directory case:
1143 // request to `/about' url matches `/about/' rule
1144 if (rule.size() > 2 && rule.back() == '/')
1145 {
1146 perMethods[method].trie.add(
1147 rule.substr(0, rule.size() - 1),
Ed Tanous271584a2019-07-09 16:24:22 -07001148 static_cast<unsigned>(perMethods[method].rules.size() -
1149 1));
Tanousf00032d2018-11-05 01:18:10 -03001150 }
1151 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001152 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001153 }
1154
Ed Tanous1abe55e2018-09-05 08:30:59 -07001155 void validate()
1156 {
Tanousf00032d2018-11-05 01:18:10 -03001157 for (std::unique_ptr<BaseRule>& rule : allRules)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001158 {
1159 if (rule)
1160 {
Tanousf00032d2018-11-05 01:18:10 -03001161 std::unique_ptr<BaseRule> upgraded = rule->upgrade();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001162 if (upgraded)
Ed Tanous3174e4d2020-10-07 11:41:22 -07001163 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001164 rule = std::move(upgraded);
Ed Tanous3174e4d2020-10-07 11:41:22 -07001165 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001166 rule->validate();
Tanousf00032d2018-11-05 01:18:10 -03001167 internalAddRuleObject(rule->rule, rule.get());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001168 }
1169 }
Tanousf00032d2018-11-05 01:18:10 -03001170 for (PerMethod& perMethod : perMethods)
1171 {
1172 perMethod.trie.validate();
1173 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001174 }
1175
Ed Tanous44e45182022-07-26 16:47:23 -07001176 struct FindRoute
1177 {
1178 BaseRule* rule = nullptr;
1179 RoutingParams params;
1180 };
1181
1182 struct FindRouteResponse
1183 {
1184 std::string allowHeader;
1185 FindRoute route;
1186 };
1187
Ed Tanous759cf102022-07-31 16:36:52 -07001188 FindRoute findRouteByIndex(std::string_view url, size_t index) const
1189 {
1190 FindRoute route;
1191 if (index >= perMethods.size())
1192 {
1193 BMCWEB_LOG_CRITICAL << "Bad index???";
1194 return route;
1195 }
1196 const PerMethod& perMethod = perMethods[index];
1197 std::pair<unsigned, RoutingParams> found = perMethod.trie.find(url);
1198 if (found.first >= perMethod.rules.size())
1199 {
1200 throw std::runtime_error("Trie internal structure corrupted!");
1201 }
1202 // Found a 404 route, switch that in
1203 if (found.first != 0U)
1204 {
1205 route.rule = perMethod.rules[found.first];
1206 route.params = std::move(found.second);
1207 }
1208 return route;
1209 }
1210
1211 FindRouteResponse findRoute(Request& req) const
Ed Tanous44e45182022-07-26 16:47:23 -07001212 {
1213 FindRouteResponse findRoute;
1214
Ed Tanous2c9efc32022-07-31 22:08:26 -07001215 std::optional<HttpVerb> verb = httpVerbFromBoost(req.method());
1216 if (!verb)
1217 {
1218 return findRoute;
1219 }
1220 size_t reqMethodIndex = static_cast<size_t>(*verb);
Ed Tanous44e45182022-07-26 16:47:23 -07001221 // Check to see if this url exists at any verb
1222 for (size_t perMethodIndex = 0; perMethodIndex <= maxVerbIndex;
1223 perMethodIndex++)
1224 {
1225 // Make sure it's safe to deference the array at that index
1226 static_assert(maxVerbIndex <
1227 std::tuple_size_v<decltype(perMethods)>);
Ed Tanous39662a32023-02-06 15:09:46 -08001228 FindRoute route =
1229 findRouteByIndex(req.url().encoded_path(), perMethodIndex);
Ed Tanous759cf102022-07-31 16:36:52 -07001230 if (route.rule == nullptr)
Ed Tanous44e45182022-07-26 16:47:23 -07001231 {
1232 continue;
1233 }
1234 if (!findRoute.allowHeader.empty())
1235 {
1236 findRoute.allowHeader += ", ";
1237 }
Ed Tanous2c9efc32022-07-31 22:08:26 -07001238 HttpVerb thisVerb = static_cast<HttpVerb>(perMethodIndex);
1239 findRoute.allowHeader += httpVerbToString(thisVerb);
Ed Tanous44e45182022-07-26 16:47:23 -07001240 if (perMethodIndex == reqMethodIndex)
1241 {
Ed Tanous759cf102022-07-31 16:36:52 -07001242 findRoute.route = route;
Ed Tanous44e45182022-07-26 16:47:23 -07001243 }
1244 }
1245 return findRoute;
1246 }
1247
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +05301248 static bool isUserPrivileged(
1249 Request& req, const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1250 BaseRule& rule, const dbus::utility::DBusPropertiesMap& userInfoMap)
Ed Tanouse1f5c162023-03-10 09:02:51 -08001251 {
Ed Tanous3d183202023-03-10 09:21:58 -08001252 std::string userRole{};
1253 const std::string* userRolePtr = nullptr;
1254 const bool* remoteUser = nullptr;
1255 const bool* passwordExpired = nullptr;
1256
1257 const bool success = sdbusplus::unpackPropertiesNoThrow(
1258 redfish::dbus_utils::UnpackErrorPrinter(), userInfoMap,
1259 "UserPrivilege", userRolePtr, "RemoteUser", remoteUser,
1260 "UserPasswordExpired", passwordExpired);
1261
1262 if (!success)
Ed Tanouse1f5c162023-03-10 09:02:51 -08001263 {
Ed Tanous3d183202023-03-10 09:21:58 -08001264 asyncResp->res.result(
1265 boost::beast::http::status::internal_server_error);
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +05301266 return false;
Ed Tanous3d183202023-03-10 09:21:58 -08001267 }
1268
1269 if (userRolePtr != nullptr)
1270 {
1271 userRole = *userRolePtr;
1272 BMCWEB_LOG_DEBUG << "userName = " << req.session->username
1273 << " userRole = " << *userRolePtr;
Ed Tanouse1f5c162023-03-10 09:02:51 -08001274 }
1275
1276 if (remoteUser == nullptr)
1277 {
1278 BMCWEB_LOG_ERROR << "RemoteUser property missing or wrong type";
1279 asyncResp->res.result(
1280 boost::beast::http::status::internal_server_error);
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +05301281 return false;
Ed Tanouse1f5c162023-03-10 09:02:51 -08001282 }
Ed Tanous3d183202023-03-10 09:21:58 -08001283 bool expired = false;
1284 if (passwordExpired == nullptr)
Ed Tanouse1f5c162023-03-10 09:02:51 -08001285 {
1286 if (!*remoteUser)
1287 {
1288 BMCWEB_LOG_ERROR
1289 << "UserPasswordExpired property is expected for"
1290 " local user but is missing or wrong type";
1291 asyncResp->res.result(
1292 boost::beast::http::status::internal_server_error);
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +05301293 return false;
Ed Tanouse1f5c162023-03-10 09:02:51 -08001294 }
Ed Tanous3d183202023-03-10 09:21:58 -08001295 }
1296 else
1297 {
1298 expired = *passwordExpired;
Ed Tanouse1f5c162023-03-10 09:02:51 -08001299 }
1300
1301 // Get the user's privileges from the role
1302 redfish::Privileges userPrivileges =
1303 redfish::getUserPrivileges(userRole);
1304
1305 // Set isConfigureSelfOnly based on D-Bus results. This
1306 // ignores the results from both pamAuthenticateUser and the
1307 // value from any previous use of this session.
Ed Tanous3d183202023-03-10 09:21:58 -08001308 req.session->isConfigureSelfOnly = expired;
Ed Tanouse1f5c162023-03-10 09:02:51 -08001309
1310 // Modify privileges if isConfigureSelfOnly.
1311 if (req.session->isConfigureSelfOnly)
1312 {
1313 // Remove all privileges except ConfigureSelf
1314 userPrivileges = userPrivileges.intersection(
1315 redfish::Privileges{"ConfigureSelf"});
1316 BMCWEB_LOG_DEBUG << "Operation limited to ConfigureSelf";
1317 }
1318
1319 if (!rule.checkPrivileges(userPrivileges))
1320 {
1321 asyncResp->res.result(boost::beast::http::status::forbidden);
1322 if (req.session->isConfigureSelfOnly)
1323 {
1324 redfish::messages::passwordChangeRequired(
1325 asyncResp->res, crow::utility::urlFromPieces(
1326 "redfish", "v1", "AccountService",
1327 "Accounts", req.session->username));
1328 }
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +05301329 return false;
Ed Tanouse1f5c162023-03-10 09:02:51 -08001330 }
1331
1332 req.userRole = userRole;
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +05301333
1334 return true;
1335 }
1336
1337 template <typename CallbackFn>
Jonathan Domand3c8ce62023-03-21 18:17:06 -07001338 void afterGetUserInfo(Request& req,
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +05301339 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1340 BaseRule& rule, CallbackFn&& callback,
1341 const boost::system::error_code& ec,
1342 const dbus::utility::DBusPropertiesMap& userInfoMap)
1343 {
1344 if (ec)
1345 {
1346 BMCWEB_LOG_ERROR << "GetUserInfo failed...";
1347 asyncResp->res.result(
1348 boost::beast::http::status::internal_server_error);
1349 return;
1350 }
1351
1352 if (!Router::isUserPrivileged(req, asyncResp, rule, userInfoMap))
1353 {
1354 // User is not privileged
1355 BMCWEB_LOG_ERROR << "Insufficient Privilege";
1356 asyncResp->res.result(boost::beast::http::status::forbidden);
1357 return;
1358 }
Jonathan Domand3c8ce62023-03-21 18:17:06 -07001359 callback(req);
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +05301360 }
1361
1362 template <typename CallbackFn>
Jonathan Domand3c8ce62023-03-21 18:17:06 -07001363 void validatePrivilege(Request& req,
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +05301364 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1365 BaseRule& rule, CallbackFn&& callback)
1366 {
Ed Tanous915d2d42023-03-15 13:09:34 -07001367 if (req.session == nullptr)
1368 {
1369 return;
1370 }
1371 std::string username = req.session->username;
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +05301372 crow::connections::systemBus->async_method_call(
Jonathan Domand3c8ce62023-03-21 18:17:06 -07001373 [this, &req, asyncResp, &rule,
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +05301374 callback(std::forward<CallbackFn>(callback))](
1375 const boost::system::error_code& ec,
1376 const dbus::utility::DBusPropertiesMap& userInfoMap) mutable {
Jonathan Domand3c8ce62023-03-21 18:17:06 -07001377 afterGetUserInfo(req, asyncResp, rule,
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +05301378 std::forward<CallbackFn>(callback), ec,
1379 userInfoMap);
1380 },
1381 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
Ed Tanous915d2d42023-03-15 13:09:34 -07001382 "xyz.openbmc_project.User.Manager", "GetUserInfo", username);
Ed Tanouse1f5c162023-03-10 09:02:51 -08001383 }
1384
Ed Tanous1abe55e2018-09-05 08:30:59 -07001385 template <typename Adaptor>
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +05301386 void handleUpgrade(Request& req,
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +05301387 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1388 Adaptor&& adaptor)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001389 {
Ed Tanous2c9efc32022-07-31 22:08:26 -07001390 std::optional<HttpVerb> verb = httpVerbFromBoost(req.method());
1391 if (!verb || static_cast<size_t>(*verb) >= perMethods.size())
Ed Tanous888880a2020-08-24 13:48:50 -07001392 {
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +05301393 asyncResp->res.result(boost::beast::http::status::not_found);
Tanousf00032d2018-11-05 01:18:10 -03001394 return;
Ed Tanous888880a2020-08-24 13:48:50 -07001395 }
Ed Tanous2c9efc32022-07-31 22:08:26 -07001396 PerMethod& perMethod = perMethods[static_cast<size_t>(*verb)];
Tanousf00032d2018-11-05 01:18:10 -03001397 Trie& trie = perMethod.trie;
1398 std::vector<BaseRule*>& rules = perMethod.rules;
1399
Ed Tanous39662a32023-02-06 15:09:46 -08001400 const std::pair<unsigned, RoutingParams>& found =
1401 trie.find(req.url().encoded_path());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001402 unsigned ruleIndex = found.first;
Ed Tanouse662eae2022-01-25 10:39:19 -08001403 if (ruleIndex == 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001404 {
Ed Tanous39662a32023-02-06 15:09:46 -08001405 BMCWEB_LOG_DEBUG << "Cannot match rules "
1406 << req.url().encoded_path();
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +05301407 asyncResp->res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001408 return;
1409 }
1410
1411 if (ruleIndex >= rules.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -07001412 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001413 throw std::runtime_error("Trie internal structure corrupted!");
Ed Tanous3174e4d2020-10-07 11:41:22 -07001414 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001415
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +05301416 BaseRule& rule = *rules[ruleIndex];
1417 size_t methods = rule.getMethods();
1418 if ((methods & (1U << static_cast<size_t>(*verb))) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001419 {
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +05301420 BMCWEB_LOG_DEBUG
1421 << "Rule found but method mismatch: "
1422 << req.url().encoded_path() << " with " << req.methodString()
1423 << "(" << static_cast<uint32_t>(*verb) << ") / " << methods;
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +05301424 asyncResp->res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001425 return;
1426 }
1427
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +05301428 BMCWEB_LOG_DEBUG << "Matched rule (upgrade) '" << rule.rule << "' "
1429 << static_cast<uint32_t>(*verb) << " / " << methods;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001430
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +05301431 // TODO(ed) This should be able to use std::bind_front, but it doesn't
1432 // appear to work with the std::move on adaptor.
Ed Tanous915d2d42023-03-15 13:09:34 -07001433 validatePrivilege(
Jonathan Domand3c8ce62023-03-21 18:17:06 -07001434 req, asyncResp, rule,
Ed Tanous915d2d42023-03-15 13:09:34 -07001435 [&rule, asyncResp, adaptor(std::forward<Adaptor>(adaptor))](
Jonathan Domand3c8ce62023-03-21 18:17:06 -07001436 Request& thisReq) mutable {
Ed Tanous915d2d42023-03-15 13:09:34 -07001437 rule.handleUpgrade(thisReq, asyncResp, std::move(adaptor));
1438 });
Ed Tanous7045c8d2017-04-03 10:04:37 -07001439 }
1440
zhanghch058d1b46d2021-04-01 11:18:24 +08001441 void handle(Request& req,
1442 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001443 {
Ed Tanous2c9efc32022-07-31 22:08:26 -07001444 std::optional<HttpVerb> verb = httpVerbFromBoost(req.method());
1445 if (!verb || static_cast<size_t>(*verb) >= perMethods.size())
Ed Tanous888880a2020-08-24 13:48:50 -07001446 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001447 asyncResp->res.result(boost::beast::http::status::not_found);
Tanousf00032d2018-11-05 01:18:10 -03001448 return;
Ed Tanous888880a2020-08-24 13:48:50 -07001449 }
Ed Tanous44e45182022-07-26 16:47:23 -07001450
1451 FindRouteResponse foundRoute = findRoute(req);
1452
Ed Tanous759cf102022-07-31 16:36:52 -07001453 if (foundRoute.route.rule == nullptr)
Ed Tanous88a03c52022-03-14 10:16:07 -07001454 {
Ed Tanous759cf102022-07-31 16:36:52 -07001455 // Couldn't find a normal route with any verb, try looking for a 404
1456 // route
1457 if (foundRoute.allowHeader.empty())
Ed Tanous44e45182022-07-26 16:47:23 -07001458 {
Ed Tanous39662a32023-02-06 15:09:46 -08001459 foundRoute.route =
1460 findRouteByIndex(req.url().encoded_path(), notFoundIndex);
Ed Tanous759cf102022-07-31 16:36:52 -07001461 }
1462 else
1463 {
1464 // See if we have a method not allowed (405) handler
Ed Tanous39662a32023-02-06 15:09:46 -08001465 foundRoute.route = findRouteByIndex(req.url().encoded_path(),
1466 methodNotAllowedIndex);
Ed Tanous44e45182022-07-26 16:47:23 -07001467 }
1468 }
Ed Tanous759cf102022-07-31 16:36:52 -07001469
1470 // Fill in the allow header if it's valid
1471 if (!foundRoute.allowHeader.empty())
Ed Tanous44e45182022-07-26 16:47:23 -07001472 {
Ed Tanous759cf102022-07-31 16:36:52 -07001473
Ed Tanous88a03c52022-03-14 10:16:07 -07001474 asyncResp->res.addHeader(boost::beast::http::field::allow,
Ed Tanous44e45182022-07-26 16:47:23 -07001475 foundRoute.allowHeader);
Ed Tanous88a03c52022-03-14 10:16:07 -07001476 }
Tanousf00032d2018-11-05 01:18:10 -03001477
Ed Tanous44e45182022-07-26 16:47:23 -07001478 // If we couldn't find a real route or a 404 route, return a generic
1479 // response
1480 if (foundRoute.route.rule == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001481 {
Ed Tanous44e45182022-07-26 16:47:23 -07001482 if (foundRoute.allowHeader.empty())
1483 {
1484 asyncResp->res.result(boost::beast::http::status::not_found);
1485 }
1486 else
Ed Tanous2634dcd2019-03-26 09:28:06 -07001487 {
Ed Tanous88a03c52022-03-14 10:16:07 -07001488 asyncResp->res.result(
1489 boost::beast::http::status::method_not_allowed);
Ed Tanous2634dcd2019-03-26 09:28:06 -07001490 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001491 return;
1492 }
1493
Ed Tanous44e45182022-07-26 16:47:23 -07001494 BaseRule& rule = *foundRoute.route.rule;
1495 RoutingParams params = std::move(foundRoute.route.params);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001496
Ed Tanous44e45182022-07-26 16:47:23 -07001497 BMCWEB_LOG_DEBUG << "Matched rule '" << rule.rule << "' "
Snehalatha Venkatesh1c99da02022-12-27 06:45:35 +00001498 << static_cast<uint32_t>(*verb) << " / "
Ed Tanous44e45182022-07-26 16:47:23 -07001499 << rule.getMethods();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001500
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001501 if (req.session == nullptr)
James Feist7166bf02019-12-10 16:52:14 +00001502 {
Ed Tanous44e45182022-07-26 16:47:23 -07001503 rule.handle(req, asyncResp, params);
James Feist7166bf02019-12-10 16:52:14 +00001504 return;
1505 }
Jonathan Domand3c8ce62023-03-21 18:17:06 -07001506 validatePrivilege(req, asyncResp, rule,
1507 [&rule, asyncResp, params](Request& thisReq) mutable {
Ed Tanous915d2d42023-03-15 13:09:34 -07001508 rule.handle(thisReq, asyncResp, params);
Jonathan Domand3c8ce62023-03-21 18:17:06 -07001509 });
Ed Tanous7045c8d2017-04-03 10:04:37 -07001510 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001511
Ed Tanous1abe55e2018-09-05 08:30:59 -07001512 void debugPrint()
1513 {
Ed Tanous271584a2019-07-09 16:24:22 -07001514 for (size_t i = 0; i < perMethods.size(); i++)
Tanousf00032d2018-11-05 01:18:10 -03001515 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001516 BMCWEB_LOG_DEBUG << boost::beast::http::to_string(
1517 static_cast<boost::beast::http::verb>(i));
Tanousf00032d2018-11-05 01:18:10 -03001518 perMethods[i].trie.debugPrint();
1519 }
Ed Tanous3dac7492017-08-02 13:46:20 -07001520 }
Ed Tanousb4a7bfa2017-04-04 17:23:00 -07001521
Ed Tanous1abe55e2018-09-05 08:30:59 -07001522 std::vector<const std::string*> getRoutes(const std::string& parent)
1523 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001524 std::vector<const std::string*> ret;
Tanousf00032d2018-11-05 01:18:10 -03001525
1526 for (const PerMethod& pm : perMethods)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001527 {
Tanousf00032d2018-11-05 01:18:10 -03001528 std::vector<unsigned> x;
1529 pm.trie.findRouteIndexes(parent, x);
1530 for (unsigned index : x)
1531 {
1532 ret.push_back(&pm.rules[index]->rule);
1533 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001534 }
1535 return ret;
1536 }
1537
1538 private:
Tanousf00032d2018-11-05 01:18:10 -03001539 struct PerMethod
1540 {
1541 std::vector<BaseRule*> rules;
1542 Trie trie;
Ed Tanous313a3c22022-03-14 09:27:38 -07001543 // rule index 0 has special meaning; preallocate it to avoid
Tanousf00032d2018-11-05 01:18:10 -03001544 // duplication.
Ed Tanous313a3c22022-03-14 09:27:38 -07001545 PerMethod() : rules(1)
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001546 {}
Tanousf00032d2018-11-05 01:18:10 -03001547 };
Ed Tanous888880a2020-08-24 13:48:50 -07001548
Ed Tanous759cf102022-07-31 16:36:52 -07001549 std::array<PerMethod, methodNotAllowedIndex + 1> perMethods;
Tanousf00032d2018-11-05 01:18:10 -03001550 std::vector<std::unique_ptr<BaseRule>> allRules;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001551};
Ed Tanous1abe55e2018-09-05 08:30:59 -07001552} // namespace crow