blob: 5b819c582c72f6a0fac7815f248c5923f5faaf78 [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
2
Ed Tanous04e438c2020-10-03 08:06:26 -07003#include "common.hpp"
Ed Tanous168e20c2021-12-13 14:39:53 -08004#include "dbus_utility.hpp"
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06005#include "error_messages.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -07006#include "http_request.hpp"
7#include "http_response.hpp"
8#include "logging.hpp"
Tanousf00032d2018-11-05 01:18:10 -03009#include "privileges.hpp"
Ratan Gupta6f359562019-04-03 10:39:08 +053010#include "sessions.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -070011#include "utility.hpp"
12#include "websocket.hpp"
Ed Tanous1abe55e2018-09-05 08:30:59 -070013
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020014#include <async_resp.hpp>
Tanousf00032d2018-11-05 01:18:10 -030015#include <boost/container/flat_map.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050016
Ed Tanouse0d918b2018-03-27 17:41:04 -070017#include <cerrno>
Ed Tanous7045c8d2017-04-03 10:04:37 -070018#include <cstdint>
Ed Tanouse0d918b2018-03-27 17:41:04 -070019#include <cstdlib>
Ed Tanous3dac7492017-08-02 13:46:20 -070020#include <limits>
Ed Tanous7045c8d2017-04-03 10:04:37 -070021#include <memory>
22#include <tuple>
Ed Tanous7045c8d2017-04-03 10:04:37 -070023#include <utility>
24#include <vector>
Ed Tanous9140a672017-04-24 17:01:32 -070025
Ed Tanous1abe55e2018-09-05 08:30:59 -070026namespace crow
27{
Tanousf00032d2018-11-05 01:18:10 -030028
Ed Tanous1abe55e2018-09-05 08:30:59 -070029class BaseRule
30{
31 public:
Ed Tanousf23b7292020-10-15 09:41:17 -070032 BaseRule(const std::string& thisRule) : rule(thisRule)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050033 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070034
Ed Tanous0c0084a2019-10-24 15:57:51 -070035 virtual ~BaseRule() = default;
Ed Tanous7045c8d2017-04-03 10:04:37 -070036
Ed Tanousecd6a3a2022-01-07 09:18:40 -080037 BaseRule(const BaseRule&) = delete;
38 BaseRule(BaseRule&&) = delete;
39 BaseRule& operator=(const BaseRule&) = delete;
40 BaseRule& operator=(const BaseRule&&) = delete;
41
Ed Tanous1abe55e2018-09-05 08:30:59 -070042 virtual void validate() = 0;
43 std::unique_ptr<BaseRule> upgrade()
44 {
45 if (ruleToUpgrade)
Ed Tanous3174e4d2020-10-07 11:41:22 -070046 {
Ed Tanous1abe55e2018-09-05 08:30:59 -070047 return std::move(ruleToUpgrade);
Ed Tanous3174e4d2020-10-07 11:41:22 -070048 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070049 return {};
50 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070051
Ed Tanous104f09c2022-01-25 09:56:04 -080052 virtual void handle(const Request& /*req*/,
zhanghch058d1b46d2021-04-01 11:18:24 +080053 const std::shared_ptr<bmcweb::AsyncResp>&,
54 const RoutingParams&) = 0;
Ed Tanous104f09c2022-01-25 09:56:04 -080055 virtual void handleUpgrade(const Request& /*req*/, Response& res,
56 boost::asio::ip::tcp::socket&& /*adaptor*/)
Ed Tanous1abe55e2018-09-05 08:30:59 -070057 {
Ed Tanousde5c9f32019-03-26 09:17:55 -070058 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -070059 res.end();
60 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -070061#ifdef BMCWEB_ENABLE_SSL
Ed Tanous104f09c2022-01-25 09:56:04 -080062 virtual void handleUpgrade(
63 const Request& /*req*/, Response& res,
64 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>&& /*adaptor*/)
Ed Tanous1abe55e2018-09-05 08:30:59 -070065 {
Ed Tanousde5c9f32019-03-26 09:17:55 -070066 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -070067 res.end();
68 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070069#endif
70
Ed Tanous9eb808c2022-01-25 10:19:23 -080071 size_t getMethods() const
Ed Tanous1abe55e2018-09-05 08:30:59 -070072 {
73 return methodsBitfield;
74 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070075
Tanousf00032d2018-11-05 01:18:10 -030076 bool checkPrivileges(const redfish::Privileges& userPrivileges)
77 {
78 // If there are no privileges assigned, assume no privileges
79 // required
80 if (privilegesSet.empty())
81 {
82 return true;
83 }
84
85 for (const redfish::Privileges& requiredPrivileges : privilegesSet)
86 {
87 if (userPrivileges.isSupersetOf(requiredPrivileges))
88 {
89 return true;
90 }
91 }
92 return false;
93 }
94
Ed Tanous271584a2019-07-09 16:24:22 -070095 size_t methodsBitfield{
96 1 << static_cast<size_t>(boost::beast::http::verb::get)};
Ed Tanous7045c8d2017-04-03 10:04:37 -070097
Tanousf00032d2018-11-05 01:18:10 -030098 std::vector<redfish::Privileges> privilegesSet;
99
Ed Tanous1abe55e2018-09-05 08:30:59 -0700100 std::string rule;
101 std::string nameStr;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700102
Ed Tanous1abe55e2018-09-05 08:30:59 -0700103 std::unique_ptr<BaseRule> ruleToUpgrade;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700104
Ed Tanous1abe55e2018-09-05 08:30:59 -0700105 friend class Router;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500106 template <typename T>
107 friend struct RuleParameterTraits;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700108};
109
Ed Tanous1abe55e2018-09-05 08:30:59 -0700110namespace detail
111{
112namespace routing_handler_call_helper
113{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500114template <typename T, int Pos>
115struct CallPair
Ed Tanous1abe55e2018-09-05 08:30:59 -0700116{
117 using type = T;
118 static const int pos = Pos;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700119};
120
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500121template <typename H1>
122struct CallParams
Ed Tanous1abe55e2018-09-05 08:30:59 -0700123{
124 H1& handler;
125 const RoutingParams& params;
126 const Request& req;
zhanghch058d1b46d2021-04-01 11:18:24 +0800127 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700128};
129
130template <typename F, int NInt, int NUint, int NDouble, int NString,
131 typename S1, typename S2>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700132struct Call
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500133{};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700134
135template <typename F, int NInt, int NUint, int NDouble, int NString,
136 typename... Args1, typename... Args2>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700137struct Call<F, NInt, NUint, NDouble, NString, black_magic::S<int64_t, Args1...>,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700138 black_magic::S<Args2...>>
139{
140 void operator()(F cparams)
141 {
142 using pushed = typename black_magic::S<Args2...>::template push_back<
143 CallPair<int64_t, NInt>>;
144 Call<F, NInt + 1, NUint, NDouble, NString, black_magic::S<Args1...>,
145 pushed>()(cparams);
146 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700147};
148
149template <typename F, int NInt, int NUint, int NDouble, int NString,
150 typename... Args1, typename... Args2>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700151struct Call<F, NInt, NUint, NDouble, NString,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700152 black_magic::S<uint64_t, Args1...>, black_magic::S<Args2...>>
153{
154 void operator()(F cparams)
155 {
156 using pushed = typename black_magic::S<Args2...>::template push_back<
157 CallPair<uint64_t, NUint>>;
158 Call<F, NInt, NUint + 1, NDouble, NString, black_magic::S<Args1...>,
159 pushed>()(cparams);
160 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700161};
162
163template <typename F, int NInt, int NUint, int NDouble, int NString,
164 typename... Args1, typename... Args2>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700165struct Call<F, NInt, NUint, NDouble, NString, black_magic::S<double, Args1...>,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700166 black_magic::S<Args2...>>
167{
168 void operator()(F cparams)
169 {
170 using pushed = typename black_magic::S<Args2...>::template push_back<
171 CallPair<double, NDouble>>;
172 Call<F, NInt, NUint, NDouble + 1, NString, black_magic::S<Args1...>,
173 pushed>()(cparams);
174 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700175};
176
177template <typename F, int NInt, int NUint, int NDouble, int NString,
178 typename... Args1, typename... Args2>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700179struct Call<F, NInt, NUint, NDouble, NString,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700180 black_magic::S<std::string, Args1...>, black_magic::S<Args2...>>
181{
182 void operator()(F cparams)
183 {
184 using pushed = typename black_magic::S<Args2...>::template push_back<
185 CallPair<std::string, NString>>;
186 Call<F, NInt, NUint, NDouble, NString + 1, black_magic::S<Args1...>,
187 pushed>()(cparams);
188 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700189};
190
191template <typename F, int NInt, int NUint, int NDouble, int NString,
192 typename... Args1>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700193struct Call<F, NInt, NUint, NDouble, NString, black_magic::S<>,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700194 black_magic::S<Args1...>>
195{
196 void operator()(F cparams)
197 {
198 cparams.handler(
zhanghch058d1b46d2021-04-01 11:18:24 +0800199 cparams.req, cparams.asyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700200 cparams.params.template get<typename Args1::type>(Args1::pos)...);
201 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700202};
203
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500204template <typename Func, typename... ArgsWrapped>
205struct Wrapped
Ed Tanous1abe55e2018-09-05 08:30:59 -0700206{
207 template <typename... Args>
208 void set(
209 Func f,
210 typename std::enable_if<
211 !std::is_same<
212 typename std::tuple_element<0, std::tuple<Args..., void>>::type,
213 const Request&>::value,
Ed Tanous104f09c2022-01-25 09:56:04 -0800214 int>::type /*enable*/
215 = 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700216 {
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800217 handler = [f = std::forward<Func>(f)](
zhanghch058d1b46d2021-04-01 11:18:24 +0800218 const Request&,
219 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
220 Args... args) { asyncResp->res.result(f(args...)); };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700221 }
222
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500223 template <typename Req, typename... Args>
224 struct ReqHandlerWrapper
Ed Tanous1abe55e2018-09-05 08:30:59 -0700225 {
Ed Tanous23a21a12020-07-25 04:45:05 +0000226 ReqHandlerWrapper(Func fIn) : f(std::move(fIn))
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500227 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700228
zhanghch058d1b46d2021-04-01 11:18:24 +0800229 void operator()(const Request& req,
230 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
231 Args... args)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700232 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800233 asyncResp->res.result(f(req, args...));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700234 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700235
Ed Tanous1abe55e2018-09-05 08:30:59 -0700236 Func f;
237 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700238
Ed Tanous1abe55e2018-09-05 08:30:59 -0700239 template <typename... Args>
240 void set(
241 Func f,
242 typename std::enable_if<
243 std::is_same<
244 typename std::tuple_element<0, std::tuple<Args..., void>>::type,
245 const Request&>::value &&
246 !std::is_same<typename std::tuple_element<
247 1, std::tuple<Args..., void, void>>::type,
zhanghch058d1b46d2021-04-01 11:18:24 +0800248 const std::shared_ptr<bmcweb::AsyncResp>&>::value,
Ed Tanous104f09c2022-01-25 09:56:04 -0800249 int>::type /*enable*/
250 = 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700251 {
252 handler = ReqHandlerWrapper<Args...>(std::move(f));
253 /*handler = (
254 [f = std::move(f)]
255 (const Request& req, Response& res, Args... args){
Ed Tanousde5c9f32019-03-26 09:17:55 -0700256 res.result(f(req, args...));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700257 res.end();
258 });*/
259 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700260
Ed Tanous1abe55e2018-09-05 08:30:59 -0700261 template <typename... Args>
262 void set(
263 Func f,
264 typename std::enable_if<
265 std::is_same<
266 typename std::tuple_element<0, std::tuple<Args..., void>>::type,
267 const Request&>::value &&
268 std::is_same<typename std::tuple_element<
269 1, std::tuple<Args..., void, void>>::type,
zhanghch058d1b46d2021-04-01 11:18:24 +0800270 const std::shared_ptr<bmcweb::AsyncResp>&>::value,
Ed Tanous104f09c2022-01-25 09:56:04 -0800271 int>::type /*enable*/
272 = 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700273 {
274 handler = std::move(f);
275 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700276
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500277 template <typename... Args>
278 struct HandlerTypeHelper
Ed Tanous1abe55e2018-09-05 08:30:59 -0700279 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800280 using type = std::function<void(
Ed Tanous104f09c2022-01-25 09:56:04 -0800281 const crow::Request& /*req*/,
282 const std::shared_ptr<bmcweb::AsyncResp>&, Args...)>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700283 using args_type =
Ed Tanous988403c2020-08-24 11:29:49 -0700284 black_magic::S<typename black_magic::PromoteT<Args>...>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700285 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700286
Ed Tanous1abe55e2018-09-05 08:30:59 -0700287 template <typename... Args>
288 struct HandlerTypeHelper<const Request&, Args...>
289 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800290 using type = std::function<void(
Ed Tanous104f09c2022-01-25 09:56:04 -0800291 const crow::Request& /*req*/,
292 const std::shared_ptr<bmcweb::AsyncResp>&, Args...)>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700293 using args_type =
Ed Tanous988403c2020-08-24 11:29:49 -0700294 black_magic::S<typename black_magic::PromoteT<Args>...>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700295 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700296
Ed Tanous1abe55e2018-09-05 08:30:59 -0700297 template <typename... Args>
zhanghch058d1b46d2021-04-01 11:18:24 +0800298 struct HandlerTypeHelper<const Request&,
299 const std::shared_ptr<bmcweb::AsyncResp>&, Args...>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700300 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800301 using type = std::function<void(
Ed Tanous104f09c2022-01-25 09:56:04 -0800302 const crow::Request& /*req*/,
303 const std::shared_ptr<bmcweb::AsyncResp>&, Args...)>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700304 using args_type =
Ed Tanous988403c2020-08-24 11:29:49 -0700305 black_magic::S<typename black_magic::PromoteT<Args>...>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700306 };
307
308 typename HandlerTypeHelper<ArgsWrapped...>::type handler;
309
zhanghch058d1b46d2021-04-01 11:18:24 +0800310 void operator()(const Request& req,
311 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700312 const RoutingParams& params)
313 {
314 detail::routing_handler_call_helper::Call<
315 detail::routing_handler_call_helper::CallParams<decltype(handler)>,
316 0, 0, 0, 0, typename HandlerTypeHelper<ArgsWrapped...>::args_type,
317 black_magic::S<>>()(
318 detail::routing_handler_call_helper::CallParams<decltype(handler)>{
zhanghch058d1b46d2021-04-01 11:18:24 +0800319 handler, params, req, asyncResp});
Ed Tanous1abe55e2018-09-05 08:30:59 -0700320 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700321};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700322} // namespace routing_handler_call_helper
323} // namespace detail
Ed Tanous7045c8d2017-04-03 10:04:37 -0700324
Ed Tanous1abe55e2018-09-05 08:30:59 -0700325class WebSocketRule : public BaseRule
326{
327 using self_t = WebSocketRule;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700328
Ed Tanous1abe55e2018-09-05 08:30:59 -0700329 public:
Ed Tanousf23b7292020-10-15 09:41:17 -0700330 WebSocketRule(const std::string& ruleIn) : BaseRule(ruleIn)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500331 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700332
Ed Tanous1abe55e2018-09-05 08:30:59 -0700333 void validate() override
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500334 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700335
Ed Tanous104f09c2022-01-25 09:56:04 -0800336 void handle(const Request& /*req*/,
zhanghch058d1b46d2021-04-01 11:18:24 +0800337 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous104f09c2022-01-25 09:56:04 -0800338 const RoutingParams& /*params*/) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700339 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800340 asyncResp->res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700341 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700342
Ed Tanous104f09c2022-01-25 09:56:04 -0800343 void handleUpgrade(const Request& req, Response& /*res*/,
Ed Tanousceac6f72018-12-02 11:58:47 -0800344 boost::asio::ip::tcp::socket&& adaptor) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700345 {
Nan Zhou93c02022022-02-24 18:21:07 -0800346 BMCWEB_LOG_DEBUG << "Websocket handles upgrade";
Ratan Gupta02453b12019-10-22 14:43:36 +0530347 std::shared_ptr<
348 crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>>
349 myConnection = std::make_shared<
350 crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>>(
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000351 req, std::move(adaptor), openHandler, messageHandler,
Ratan Gupta02453b12019-10-22 14:43:36 +0530352 closeHandler, errorHandler);
353 myConnection->start();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700354 }
355#ifdef BMCWEB_ENABLE_SSL
Ed Tanous104f09c2022-01-25 09:56:04 -0800356 void handleUpgrade(const Request& req, Response& /*res*/,
Ed Tanousceac6f72018-12-02 11:58:47 -0800357 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>&&
358 adaptor) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700359 {
Nan Zhou93c02022022-02-24 18:21:07 -0800360 BMCWEB_LOG_DEBUG << "Websocket handles upgrade";
Ed Tanousceac6f72018-12-02 11:58:47 -0800361 std::shared_ptr<crow::websocket::ConnectionImpl<
362 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>>
363 myConnection = std::make_shared<crow::websocket::ConnectionImpl<
364 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>>(
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000365 req, std::move(adaptor), openHandler, messageHandler,
Ed Tanousceac6f72018-12-02 11:58:47 -0800366 closeHandler, errorHandler);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700367 myConnection->start();
368 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700369#endif
370
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500371 template <typename Func>
372 self_t& onopen(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700373 {
374 openHandler = f;
375 return *this;
376 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700377
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500378 template <typename Func>
379 self_t& onmessage(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700380 {
381 messageHandler = f;
382 return *this;
383 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700384
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500385 template <typename Func>
386 self_t& onclose(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700387 {
388 closeHandler = f;
389 return *this;
390 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700391
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500392 template <typename Func>
393 self_t& onerror(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700394 {
395 errorHandler = f;
396 return *this;
397 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700398
Ed Tanous1abe55e2018-09-05 08:30:59 -0700399 protected:
Gunnar Millsccd584f2021-11-16 11:36:33 -0600400 std::function<void(crow::websocket::Connection&,
401 std::shared_ptr<bmcweb::AsyncResp>)>
402 openHandler;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700403 std::function<void(crow::websocket::Connection&, const std::string&, bool)>
404 messageHandler;
405 std::function<void(crow::websocket::Connection&, const std::string&)>
406 closeHandler;
407 std::function<void(crow::websocket::Connection&)> errorHandler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700408};
409
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500410template <typename T>
411struct RuleParameterTraits
Ed Tanous1abe55e2018-09-05 08:30:59 -0700412{
413 using self_t = T;
414 WebSocketRule& websocket()
415 {
Ed Tanous271584a2019-07-09 16:24:22 -0700416 self_t* self = static_cast<self_t*>(this);
417 WebSocketRule* p = new WebSocketRule(self->rule);
418 self->ruleToUpgrade.reset(p);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700419 return *p;
420 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700421
Ed Tanousf23b7292020-10-15 09:41:17 -0700422 self_t& name(const std::string_view name) noexcept
Ed Tanous1abe55e2018-09-05 08:30:59 -0700423 {
Ed Tanous271584a2019-07-09 16:24:22 -0700424 self_t* self = static_cast<self_t*>(this);
Ed Tanousf23b7292020-10-15 09:41:17 -0700425 self->nameStr = name;
Ed Tanous271584a2019-07-09 16:24:22 -0700426 return *self;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700427 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700428
Ed Tanous1abe55e2018-09-05 08:30:59 -0700429 self_t& methods(boost::beast::http::verb method)
430 {
Ed Tanous271584a2019-07-09 16:24:22 -0700431 self_t* self = static_cast<self_t*>(this);
432 self->methodsBitfield = 1U << static_cast<size_t>(method);
433 return *self;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700434 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700435
Ed Tanous1abe55e2018-09-05 08:30:59 -0700436 template <typename... MethodArgs>
Ed Tanous81ce6092020-12-17 16:54:55 +0000437 self_t& methods(boost::beast::http::verb method, MethodArgs... argsMethod)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700438 {
Ed Tanous271584a2019-07-09 16:24:22 -0700439 self_t* self = static_cast<self_t*>(this);
Ed Tanous81ce6092020-12-17 16:54:55 +0000440 methods(argsMethod...);
Ed Tanous271584a2019-07-09 16:24:22 -0700441 self->methodsBitfield |= 1U << static_cast<size_t>(method);
442 return *self;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700443 }
Tanousf00032d2018-11-05 01:18:10 -0300444
Ed Tanous432a8902021-06-14 15:28:56 -0700445 self_t& privileges(
446 const std::initializer_list<std::initializer_list<const char*>>& p)
Tanousf00032d2018-11-05 01:18:10 -0300447 {
Ed Tanous271584a2019-07-09 16:24:22 -0700448 self_t* self = static_cast<self_t*>(this);
Ed Tanous432a8902021-06-14 15:28:56 -0700449 for (const std::initializer_list<const char*>& privilege : p)
Tanousf00032d2018-11-05 01:18:10 -0300450 {
Ed Tanous271584a2019-07-09 16:24:22 -0700451 self->privilegesSet.emplace_back(privilege);
Tanousf00032d2018-11-05 01:18:10 -0300452 }
Ed Tanous271584a2019-07-09 16:24:22 -0700453 return *self;
Tanousf00032d2018-11-05 01:18:10 -0300454 }
Ed Tanoused398212021-06-09 17:05:54 -0700455
456 template <size_t N, typename... MethodArgs>
457 self_t& privileges(const std::array<redfish::Privileges, N>& p)
458 {
459 self_t* self = static_cast<self_t*>(this);
460 for (const redfish::Privileges& privilege : p)
461 {
462 self->privilegesSet.emplace_back(privilege);
463 }
464 return *self;
465 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700466};
467
Ed Tanous1abe55e2018-09-05 08:30:59 -0700468class DynamicRule : public BaseRule, public RuleParameterTraits<DynamicRule>
469{
470 public:
Ed Tanousf23b7292020-10-15 09:41:17 -0700471 DynamicRule(const std::string& ruleIn) : BaseRule(ruleIn)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500472 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700473
Ed Tanous1abe55e2018-09-05 08:30:59 -0700474 void validate() override
475 {
476 if (!erasedHandler)
477 {
478 throw std::runtime_error(nameStr + (!nameStr.empty() ? ": " : "") +
479 "no handler for url " + rule);
480 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700481 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700482
zhanghch058d1b46d2021-04-01 11:18:24 +0800483 void handle(const Request& req,
484 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700485 const RoutingParams& params) override
486 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800487 erasedHandler(req, asyncResp, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700488 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700489
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500490 template <typename Func>
491 void operator()(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700492 {
493 using function_t = utility::function_traits<Func>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700494 erasedHandler =
Ed Tanous988403c2020-08-24 11:29:49 -0700495 wrap(std::move(f),
496 std::make_integer_sequence<unsigned, function_t::arity>{});
Ed Tanous1abe55e2018-09-05 08:30:59 -0700497 }
498
499 // enable_if Arg1 == request && Arg2 == Response
Gunnar Mills6be0e402020-07-08 13:21:51 -0500500 // enable_if Arg1 == request && Arg2 != response
Ed Tanous1abe55e2018-09-05 08:30:59 -0700501 // enable_if Arg1 != request
502
503 template <typename Func, unsigned... Indices>
zhanghch058d1b46d2021-04-01 11:18:24 +0800504 std::function<void(const Request&,
505 const std::shared_ptr<bmcweb::AsyncResp>&,
506 const RoutingParams&)>
Ed Tanous104f09c2022-01-25 09:56:04 -0800507 wrap(Func f, std::integer_sequence<unsigned, Indices...> /*is*/)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700508 {
Ed Tanous988403c2020-08-24 11:29:49 -0700509 using function_t = crow::utility::function_traits<Func>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700510
511 if (!black_magic::isParameterTagCompatible(
Ed Tanous988403c2020-08-24 11:29:49 -0700512 black_magic::getParameterTag(rule.c_str()),
513 black_magic::computeParameterTagFromArgsList<
Ed Tanous1abe55e2018-09-05 08:30:59 -0700514 typename function_t::template arg<Indices>...>::value))
515 {
516 throw std::runtime_error("routeDynamic: Handler type is mismatched "
517 "with URL parameters: " +
518 rule);
519 }
520 auto ret = detail::routing_handler_call_helper::Wrapped<
521 Func, typename function_t::template arg<Indices>...>();
522 ret.template set<typename function_t::template arg<Indices>...>(
523 std::move(f));
524 return ret;
525 }
526
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500527 template <typename Func>
528 void operator()(std::string name, Func&& f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700529 {
530 nameStr = std::move(name);
531 (*this).template operator()<Func>(std::forward(f));
532 }
533
534 private:
zhanghch058d1b46d2021-04-01 11:18:24 +0800535 std::function<void(const Request&,
536 const std::shared_ptr<bmcweb::AsyncResp>&,
537 const RoutingParams&)>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700538 erasedHandler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700539};
540
541template <typename... Args>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500542class TaggedRule :
543 public BaseRule,
544 public RuleParameterTraits<TaggedRule<Args...>>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700545{
546 public:
547 using self_t = TaggedRule<Args...>;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700548
Ed Tanousf23b7292020-10-15 09:41:17 -0700549 TaggedRule(const std::string& ruleIn) : BaseRule(ruleIn)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500550 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700551
Ed Tanous1abe55e2018-09-05 08:30:59 -0700552 void validate() override
553 {
554 if (!handler)
555 {
556 throw std::runtime_error(nameStr + (!nameStr.empty() ? ": " : "") +
557 "no handler for url " + rule);
558 }
559 }
560
561 template <typename Func>
562 typename std::enable_if<
563 black_magic::CallHelper<Func, black_magic::S<Args...>>::value,
564 void>::type
565 operator()(Func&& f)
566 {
567 static_assert(
568 black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
569 black_magic::CallHelper<
570 Func, black_magic::S<crow::Request, Args...>>::value,
571 "Handler type is mismatched with URL parameters");
572 static_assert(
573 !std::is_same<void, decltype(f(std::declval<Args>()...))>::value,
574 "Handler function cannot have void return type; valid return "
575 "types: "
Gunnar Mills6be0e402020-07-08 13:21:51 -0500576 "string, int, crow::response, nlohmann::json");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700577
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800578 handler = [f = std::forward<Func>(f)](
zhanghch058d1b46d2021-04-01 11:18:24 +0800579 const Request&,
580 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
581 Args... args) { asyncResp->res.result(f(args...)); };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700582 }
583
584 template <typename Func>
585 typename std::enable_if<
586 !black_magic::CallHelper<Func, black_magic::S<Args...>>::value &&
Ed Tanous7045c8d2017-04-03 10:04:37 -0700587 black_magic::CallHelper<
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700588 Func, black_magic::S<crow::Request, Args...>>::value,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700589 void>::type
590 operator()(Func&& f)
591 {
592 static_assert(
593 black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
594 black_magic::CallHelper<
595 Func, black_magic::S<crow::Request, Args...>>::value,
596 "Handler type is mismatched with URL parameters");
597 static_assert(
598 !std::is_same<void, decltype(f(std::declval<crow::Request>(),
599 std::declval<Args>()...))>::value,
600 "Handler function cannot have void return type; valid return "
601 "types: "
Gunnar Mills6be0e402020-07-08 13:21:51 -0500602 "string, int, crow::response,nlohmann::json");
Ed Tanous7045c8d2017-04-03 10:04:37 -0700603
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800604 handler = [f = std::forward<Func>(f)](
zhanghch058d1b46d2021-04-01 11:18:24 +0800605 const crow::Request& req,
606 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
607 Args... args) { asyncResp->res.result(f(req, args...)); };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700608 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700609
Ed Tanous1abe55e2018-09-05 08:30:59 -0700610 template <typename Func>
611 typename std::enable_if<
612 !black_magic::CallHelper<Func, black_magic::S<Args...>>::value &&
613 !black_magic::CallHelper<
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700614 Func, black_magic::S<crow::Request, Args...>>::value,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700615 void>::type
616 operator()(Func&& f)
617 {
618 static_assert(
619 black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
620 black_magic::CallHelper<
621 Func, black_magic::S<crow::Request, Args...>>::value ||
622 black_magic::CallHelper<
zhanghch058d1b46d2021-04-01 11:18:24 +0800623 Func, black_magic::S<crow::Request,
624 std::shared_ptr<bmcweb::AsyncResp>&,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700625 Args...>>::value,
626 "Handler type is mismatched with URL parameters");
627 static_assert(
zhanghch058d1b46d2021-04-01 11:18:24 +0800628 std::is_same<
629 void,
630 decltype(f(std::declval<crow::Request>(),
631 std::declval<std::shared_ptr<bmcweb::AsyncResp>&>(),
632 std::declval<Args>()...))>::value,
Tanousf00032d2018-11-05 01:18:10 -0300633 "Handler function with response argument should have void "
634 "return "
Ed Tanous1abe55e2018-09-05 08:30:59 -0700635 "type");
Ed Tanous7045c8d2017-04-03 10:04:37 -0700636
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800637 handler = std::forward<Func>(f);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700638 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700639
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500640 template <typename Func>
Ed Tanousf23b7292020-10-15 09:41:17 -0700641 void operator()(const std::string_view name, Func&& f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700642 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700643 nameStr = name;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700644 (*this).template operator()<Func>(std::forward(f));
645 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700646
zhanghch058d1b46d2021-04-01 11:18:24 +0800647 void handle(const Request& req,
648 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700649 const RoutingParams& params) override
650 {
651 detail::routing_handler_call_helper::Call<
652 detail::routing_handler_call_helper::CallParams<decltype(handler)>,
653 0, 0, 0, 0, black_magic::S<Args...>, black_magic::S<>>()(
654 detail::routing_handler_call_helper::CallParams<decltype(handler)>{
zhanghch058d1b46d2021-04-01 11:18:24 +0800655 handler, params, req, asyncResp});
Ed Tanous1abe55e2018-09-05 08:30:59 -0700656 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700657
Ed Tanous1abe55e2018-09-05 08:30:59 -0700658 private:
zhanghch058d1b46d2021-04-01 11:18:24 +0800659 std::function<void(const crow::Request&,
660 const std::shared_ptr<bmcweb::AsyncResp>&, Args...)>
661 handler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700662};
663
Ed Tanous1abe55e2018-09-05 08:30:59 -0700664class Trie
665{
666 public:
667 struct Node
668 {
669 unsigned ruleIndex{};
Ed Tanous271584a2019-07-09 16:24:22 -0700670 std::array<size_t, static_cast<size_t>(ParamType::MAX)>
671 paramChildrens{};
Ed Tanousa94ac612022-02-22 11:13:24 -0800672 using ChildMap = boost::container::flat_map<
673 std::string, unsigned, std::less<>,
674 std::vector<std::pair<std::string, unsigned>>>;
675 ChildMap children;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700676
Ed Tanous1abe55e2018-09-05 08:30:59 -0700677 bool isSimpleNode() const
678 {
Ed Tanouse662eae2022-01-25 10:39:19 -0800679 return ruleIndex == 0 &&
680 std::all_of(std::begin(paramChildrens),
681 std::end(paramChildrens),
682 [](size_t x) { return x == 0U; });
Ed Tanous7045c8d2017-04-03 10:04:37 -0700683 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700684 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700685
Ed Tanous1abe55e2018-09-05 08:30:59 -0700686 Trie() : nodes(1)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500687 {}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700688
689 private:
690 void optimizeNode(Node* node)
691 {
Ed Tanous271584a2019-07-09 16:24:22 -0700692 for (size_t x : node->paramChildrens)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700693 {
Ed Tanousdbb59d42022-01-25 11:09:55 -0800694 if (x == 0U)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700695 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700696 continue;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700697 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700698 Node* child = &nodes[x];
699 optimizeNode(child);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700700 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700701 if (node->children.empty())
Ed Tanous3174e4d2020-10-07 11:41:22 -0700702 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700703 return;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700704 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700705 bool mergeWithChild = true;
Ed Tanousa94ac612022-02-22 11:13:24 -0800706 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700707 {
708 Node* child = &nodes[kv.second];
709 if (!child->isSimpleNode())
710 {
711 mergeWithChild = false;
712 break;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700713 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700714 }
715 if (mergeWithChild)
716 {
Ed Tanousa94ac612022-02-22 11:13:24 -0800717 Node::ChildMap merged;
718 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700719 {
720 Node* child = &nodes[kv.second];
Ed Tanousa94ac612022-02-22 11:13:24 -0800721 for (const Node::ChildMap::value_type& childKv :
Tanousf00032d2018-11-05 01:18:10 -0300722 child->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700723 {
724 merged[kv.first + childKv.first] = childKv.second;
725 }
726 }
727 node->children = std::move(merged);
728 optimizeNode(node);
729 }
730 else
731 {
Ed Tanousa94ac612022-02-22 11:13:24 -0800732 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700733 {
734 Node* child = &nodes[kv.second];
735 optimizeNode(child);
736 }
737 }
738 }
739
740 void optimize()
741 {
742 optimizeNode(head());
743 }
744
745 public:
746 void validate()
747 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700748 optimize();
749 }
750
Ed Tanous81ce6092020-12-17 16:54:55 +0000751 void findRouteIndexes(const std::string& reqUrl,
752 std::vector<unsigned>& routeIndexes,
Tanousf00032d2018-11-05 01:18:10 -0300753 const Node* node = nullptr, unsigned pos = 0) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700754 {
755 if (node == nullptr)
756 {
757 node = head();
758 }
Ed Tanousa94ac612022-02-22 11:13:24 -0800759 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700760 {
761 const std::string& fragment = kv.first;
762 const Node* child = &nodes[kv.second];
Ed Tanous81ce6092020-12-17 16:54:55 +0000763 if (pos >= reqUrl.size())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700764 {
765 if (child->ruleIndex != 0 && fragment != "/")
766 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000767 routeIndexes.push_back(child->ruleIndex);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700768 }
Ed Tanous81ce6092020-12-17 16:54:55 +0000769 findRouteIndexes(reqUrl, routeIndexes, child,
Ed Tanous271584a2019-07-09 16:24:22 -0700770 static_cast<unsigned>(pos + fragment.size()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700771 }
772 else
773 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000774 if (reqUrl.compare(pos, fragment.size(), fragment) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700775 {
Ed Tanous271584a2019-07-09 16:24:22 -0700776 findRouteIndexes(
Ed Tanous81ce6092020-12-17 16:54:55 +0000777 reqUrl, routeIndexes, child,
Ed Tanous271584a2019-07-09 16:24:22 -0700778 static_cast<unsigned>(pos + fragment.size()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700779 }
780 }
781 }
782 }
783
784 std::pair<unsigned, RoutingParams>
Ed Tanous81ce6092020-12-17 16:54:55 +0000785 find(const std::string_view reqUrl, const Node* node = nullptr,
Ed Tanous271584a2019-07-09 16:24:22 -0700786 size_t pos = 0, RoutingParams* params = nullptr) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700787 {
788 RoutingParams empty;
789 if (params == nullptr)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700790 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700791 params = &empty;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700792 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700793
794 unsigned found{};
795 RoutingParams matchParams;
796
797 if (node == nullptr)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700798 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700799 node = head();
Ed Tanous3174e4d2020-10-07 11:41:22 -0700800 }
Ed Tanous81ce6092020-12-17 16:54:55 +0000801 if (pos == reqUrl.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -0700802 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700803 return {node->ruleIndex, *params};
Ed Tanous3174e4d2020-10-07 11:41:22 -0700804 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700805
806 auto updateFound =
807 [&found, &matchParams](std::pair<unsigned, RoutingParams>& ret) {
Ed Tanouse662eae2022-01-25 10:39:19 -0800808 if (ret.first != 0U && (found == 0U || found > ret.first))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700809 {
810 found = ret.first;
811 matchParams = std::move(ret.second);
812 }
813 };
814
Ed Tanouse662eae2022-01-25 10:39:19 -0800815 if (node->paramChildrens[static_cast<size_t>(ParamType::INT)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700816 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000817 char c = reqUrl[pos];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700818 if ((c >= '0' && c <= '9') || c == '+' || c == '-')
819 {
Ed Tanous543f4402022-01-06 13:12:53 -0800820 char* eptr = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700821 errno = 0;
822 long long int value =
Ed Tanous81ce6092020-12-17 16:54:55 +0000823 std::strtoll(reqUrl.data() + pos, &eptr, 10);
824 if (errno != ERANGE && eptr != reqUrl.data() + pos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700825 {
826 params->intParams.push_back(value);
Ed Tanous81ce6092020-12-17 16:54:55 +0000827 std::pair<unsigned, RoutingParams> ret =
828 find(reqUrl,
829 &nodes[node->paramChildrens[static_cast<size_t>(
830 ParamType::INT)]],
831 static_cast<size_t>(eptr - reqUrl.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700832 updateFound(ret);
833 params->intParams.pop_back();
834 }
835 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700836 }
837
Ed Tanouse662eae2022-01-25 10:39:19 -0800838 if (node->paramChildrens[static_cast<size_t>(ParamType::UINT)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700839 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000840 char c = reqUrl[pos];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700841 if ((c >= '0' && c <= '9') || c == '+')
842 {
Ed Tanous543f4402022-01-06 13:12:53 -0800843 char* eptr = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700844 errno = 0;
845 unsigned long long int value =
Ed Tanous81ce6092020-12-17 16:54:55 +0000846 std::strtoull(reqUrl.data() + pos, &eptr, 10);
847 if (errno != ERANGE && eptr != reqUrl.data() + pos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700848 {
849 params->uintParams.push_back(value);
Ed Tanous81ce6092020-12-17 16:54:55 +0000850 std::pair<unsigned, RoutingParams> ret =
851 find(reqUrl,
852 &nodes[node->paramChildrens[static_cast<size_t>(
853 ParamType::UINT)]],
854 static_cast<size_t>(eptr - reqUrl.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700855 updateFound(ret);
856 params->uintParams.pop_back();
857 }
858 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700859 }
860
Ed Tanouse662eae2022-01-25 10:39:19 -0800861 if (node->paramChildrens[static_cast<size_t>(ParamType::DOUBLE)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700862 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000863 char c = reqUrl[pos];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700864 if ((c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')
865 {
Ed Tanous543f4402022-01-06 13:12:53 -0800866 char* eptr = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700867 errno = 0;
Ed Tanous81ce6092020-12-17 16:54:55 +0000868 double value = std::strtod(reqUrl.data() + pos, &eptr);
869 if (errno != ERANGE && eptr != reqUrl.data() + pos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700870 {
871 params->doubleParams.push_back(value);
Ed Tanous81ce6092020-12-17 16:54:55 +0000872 std::pair<unsigned, RoutingParams> ret =
873 find(reqUrl,
874 &nodes[node->paramChildrens[static_cast<size_t>(
875 ParamType::DOUBLE)]],
876 static_cast<size_t>(eptr - reqUrl.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700877 updateFound(ret);
878 params->doubleParams.pop_back();
879 }
880 }
881 }
882
Ed Tanouse662eae2022-01-25 10:39:19 -0800883 if (node->paramChildrens[static_cast<size_t>(ParamType::STRING)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700884 {
Ed Tanousb01bf292019-03-25 19:25:26 +0000885 size_t epos = pos;
Ed Tanous81ce6092020-12-17 16:54:55 +0000886 for (; epos < reqUrl.size(); epos++)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700887 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000888 if (reqUrl[epos] == '/')
Ed Tanous3174e4d2020-10-07 11:41:22 -0700889 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700890 break;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700891 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700892 }
893
894 if (epos != pos)
895 {
896 params->stringParams.emplace_back(
Ed Tanous81ce6092020-12-17 16:54:55 +0000897 reqUrl.substr(pos, epos - pos));
Tanousf00032d2018-11-05 01:18:10 -0300898 std::pair<unsigned, RoutingParams> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000899 find(reqUrl,
Ed Tanous271584a2019-07-09 16:24:22 -0700900 &nodes[node->paramChildrens[static_cast<size_t>(
901 ParamType::STRING)]],
Ed Tanousb01bf292019-03-25 19:25:26 +0000902 epos, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700903 updateFound(ret);
904 params->stringParams.pop_back();
905 }
906 }
907
Ed Tanouse662eae2022-01-25 10:39:19 -0800908 if (node->paramChildrens[static_cast<size_t>(ParamType::PATH)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700909 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000910 size_t epos = reqUrl.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700911
912 if (epos != pos)
913 {
914 params->stringParams.emplace_back(
Ed Tanous81ce6092020-12-17 16:54:55 +0000915 reqUrl.substr(pos, epos - pos));
Ed Tanous271584a2019-07-09 16:24:22 -0700916 std::pair<unsigned, RoutingParams> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000917 find(reqUrl,
Ed Tanous271584a2019-07-09 16:24:22 -0700918 &nodes[node->paramChildrens[static_cast<size_t>(
919 ParamType::PATH)]],
920 epos, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700921 updateFound(ret);
922 params->stringParams.pop_back();
923 }
924 }
925
Ed Tanousa94ac612022-02-22 11:13:24 -0800926 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700927 {
928 const std::string& fragment = kv.first;
929 const Node* child = &nodes[kv.second];
930
Ed Tanous81ce6092020-12-17 16:54:55 +0000931 if (reqUrl.compare(pos, fragment.size(), fragment) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700932 {
Tanousf00032d2018-11-05 01:18:10 -0300933 std::pair<unsigned, RoutingParams> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000934 find(reqUrl, child, pos + fragment.size(), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700935 updateFound(ret);
936 }
937 }
938
939 return {found, matchParams};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700940 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700941
942 void add(const std::string& url, unsigned ruleIndex)
943 {
Ed Tanous271584a2019-07-09 16:24:22 -0700944 size_t idx = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700945
946 for (unsigned i = 0; i < url.size(); i++)
947 {
948 char c = url[i];
949 if (c == '<')
950 {
Tanousf00032d2018-11-05 01:18:10 -0300951 const static std::array<std::pair<ParamType, std::string>, 7>
952 paramTraits = {{
953 {ParamType::INT, "<int>"},
954 {ParamType::UINT, "<uint>"},
955 {ParamType::DOUBLE, "<float>"},
956 {ParamType::DOUBLE, "<double>"},
957 {ParamType::STRING, "<str>"},
958 {ParamType::STRING, "<string>"},
959 {ParamType::PATH, "<path>"},
960 }};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700961
Tanousf00032d2018-11-05 01:18:10 -0300962 for (const std::pair<ParamType, std::string>& x : paramTraits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700963 {
Tanousf00032d2018-11-05 01:18:10 -0300964 if (url.compare(i, x.second.size(), x.second) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700965 {
Ed Tanous271584a2019-07-09 16:24:22 -0700966 size_t index = static_cast<size_t>(x.first);
Ed Tanouse662eae2022-01-25 10:39:19 -0800967 if (nodes[idx].paramChildrens[index] == 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700968 {
Tanousf00032d2018-11-05 01:18:10 -0300969 unsigned newNodeIdx = newNode();
Ed Tanous271584a2019-07-09 16:24:22 -0700970 nodes[idx].paramChildrens[index] = newNodeIdx;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700971 }
Ed Tanous271584a2019-07-09 16:24:22 -0700972 idx = nodes[idx].paramChildrens[index];
973 i += static_cast<unsigned>(x.second.size());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700974 break;
975 }
976 }
977
978 i--;
979 }
980 else
981 {
982 std::string piece(&c, 1);
Ed Tanouse662eae2022-01-25 10:39:19 -0800983 if (nodes[idx].children.count(piece) == 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700984 {
Tanousf00032d2018-11-05 01:18:10 -0300985 unsigned newNodeIdx = newNode();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700986 nodes[idx].children.emplace(piece, newNodeIdx);
987 }
988 idx = nodes[idx].children[piece];
989 }
990 }
Ed Tanouse662eae2022-01-25 10:39:19 -0800991 if (nodes[idx].ruleIndex != 0U)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700992 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700993 throw std::runtime_error("handler already exists for " + url);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700994 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700995 nodes[idx].ruleIndex = ruleIndex;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700996 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700997
Ed Tanous1abe55e2018-09-05 08:30:59 -0700998 private:
Ed Tanous271584a2019-07-09 16:24:22 -0700999 void debugNodePrint(Node* n, size_t level)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001000 {
Ed Tanous271584a2019-07-09 16:24:22 -07001001 for (size_t i = 0; i < static_cast<size_t>(ParamType::MAX); i++)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001002 {
Ed Tanouse662eae2022-01-25 10:39:19 -08001003 if (n->paramChildrens[i] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001004 {
1005 BMCWEB_LOG_DEBUG << std::string(
Ed Tanous271584a2019-07-09 16:24:22 -07001006 2U * level, ' ') /*<< "("<<n->paramChildrens[i]<<") "*/;
1007 switch (static_cast<ParamType>(i))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001008 {
1009 case ParamType::INT:
1010 BMCWEB_LOG_DEBUG << "<int>";
1011 break;
1012 case ParamType::UINT:
1013 BMCWEB_LOG_DEBUG << "<uint>";
1014 break;
1015 case ParamType::DOUBLE:
1016 BMCWEB_LOG_DEBUG << "<float>";
1017 break;
1018 case ParamType::STRING:
1019 BMCWEB_LOG_DEBUG << "<str>";
1020 break;
1021 case ParamType::PATH:
1022 BMCWEB_LOG_DEBUG << "<path>";
1023 break;
Ed Tanous23a21a12020-07-25 04:45:05 +00001024 case ParamType::MAX:
Ed Tanous1abe55e2018-09-05 08:30:59 -07001025 BMCWEB_LOG_DEBUG << "<ERROR>";
1026 break;
1027 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001028
Ed Tanous1abe55e2018-09-05 08:30:59 -07001029 debugNodePrint(&nodes[n->paramChildrens[i]], level + 1);
1030 }
1031 }
Ed Tanousa94ac612022-02-22 11:13:24 -08001032 for (const Node::ChildMap::value_type& kv : n->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001033 {
1034 BMCWEB_LOG_DEBUG
Ed Tanous271584a2019-07-09 16:24:22 -07001035 << std::string(2U * level, ' ') /*<< "(" << kv.second << ") "*/
Ed Tanous1abe55e2018-09-05 08:30:59 -07001036 << kv.first;
1037 debugNodePrint(&nodes[kv.second], level + 1);
1038 }
1039 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001040
Ed Tanous1abe55e2018-09-05 08:30:59 -07001041 public:
1042 void debugPrint()
1043 {
Ed Tanous271584a2019-07-09 16:24:22 -07001044 debugNodePrint(head(), 0U);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001045 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001046
Ed Tanous1abe55e2018-09-05 08:30:59 -07001047 private:
1048 const Node* head() const
1049 {
1050 return &nodes.front();
1051 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001052
Ed Tanous1abe55e2018-09-05 08:30:59 -07001053 Node* head()
1054 {
1055 return &nodes.front();
1056 }
1057
1058 unsigned newNode()
1059 {
1060 nodes.resize(nodes.size() + 1);
Ed Tanous271584a2019-07-09 16:24:22 -07001061 return static_cast<unsigned>(nodes.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001062 }
1063
1064 std::vector<Node> nodes;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001065};
1066
Ed Tanous1abe55e2018-09-05 08:30:59 -07001067class Router
1068{
1069 public:
Ed Tanous0c0084a2019-10-24 15:57:51 -07001070 Router() = default;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001071
Ed Tanous1abe55e2018-09-05 08:30:59 -07001072 DynamicRule& newRuleDynamic(const std::string& rule)
1073 {
1074 std::unique_ptr<DynamicRule> ruleObject =
1075 std::make_unique<DynamicRule>(rule);
1076 DynamicRule* ptr = ruleObject.get();
Tanousf00032d2018-11-05 01:18:10 -03001077 allRules.emplace_back(std::move(ruleObject));
Ed Tanous7045c8d2017-04-03 10:04:37 -07001078
Ed Tanous1abe55e2018-09-05 08:30:59 -07001079 return *ptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001080 }
1081
Ed Tanous1abe55e2018-09-05 08:30:59 -07001082 template <uint64_t N>
1083 typename black_magic::Arguments<N>::type::template rebind<TaggedRule>&
1084 newRuleTagged(const std::string& rule)
1085 {
1086 using RuleT = typename black_magic::Arguments<N>::type::template rebind<
1087 TaggedRule>;
1088 std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule);
1089 RuleT* ptr = ruleObject.get();
Tanousf00032d2018-11-05 01:18:10 -03001090 allRules.emplace_back(std::move(ruleObject));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001091
1092 return *ptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001093 }
1094
Tanousf00032d2018-11-05 01:18:10 -03001095 void internalAddRuleObject(const std::string& rule, BaseRule* ruleObject)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001096 {
Tanousf00032d2018-11-05 01:18:10 -03001097 if (ruleObject == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001098 {
Tanousf00032d2018-11-05 01:18:10 -03001099 return;
1100 }
Ed Tanous888880a2020-08-24 13:48:50 -07001101 for (size_t method = 0, methodBit = 1; method < maxHttpVerbCount;
Ed Tanous2c70f802020-09-28 14:29:23 -07001102 method++, methodBit <<= 1)
Tanousf00032d2018-11-05 01:18:10 -03001103 {
Ed Tanouse662eae2022-01-25 10:39:19 -08001104 if ((ruleObject->methodsBitfield & methodBit) > 0U)
Tanousf00032d2018-11-05 01:18:10 -03001105 {
1106 perMethods[method].rules.emplace_back(ruleObject);
1107 perMethods[method].trie.add(
Ed Tanous271584a2019-07-09 16:24:22 -07001108 rule, static_cast<unsigned>(
1109 perMethods[method].rules.size() - 1U));
Tanousf00032d2018-11-05 01:18:10 -03001110 // directory case:
1111 // request to `/about' url matches `/about/' rule
1112 if (rule.size() > 2 && rule.back() == '/')
1113 {
1114 perMethods[method].trie.add(
1115 rule.substr(0, rule.size() - 1),
Ed Tanous271584a2019-07-09 16:24:22 -07001116 static_cast<unsigned>(perMethods[method].rules.size() -
1117 1));
Tanousf00032d2018-11-05 01:18:10 -03001118 }
1119 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001120 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001121 }
1122
Ed Tanous1abe55e2018-09-05 08:30:59 -07001123 void validate()
1124 {
Tanousf00032d2018-11-05 01:18:10 -03001125 for (std::unique_ptr<BaseRule>& rule : allRules)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001126 {
1127 if (rule)
1128 {
Tanousf00032d2018-11-05 01:18:10 -03001129 std::unique_ptr<BaseRule> upgraded = rule->upgrade();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001130 if (upgraded)
Ed Tanous3174e4d2020-10-07 11:41:22 -07001131 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001132 rule = std::move(upgraded);
Ed Tanous3174e4d2020-10-07 11:41:22 -07001133 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001134 rule->validate();
Tanousf00032d2018-11-05 01:18:10 -03001135 internalAddRuleObject(rule->rule, rule.get());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001136 }
1137 }
Tanousf00032d2018-11-05 01:18:10 -03001138 for (PerMethod& perMethod : perMethods)
1139 {
1140 perMethod.trie.validate();
1141 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001142 }
1143
Ed Tanous1abe55e2018-09-05 08:30:59 -07001144 template <typename Adaptor>
1145 void handleUpgrade(const Request& req, Response& res, Adaptor&& adaptor)
1146 {
Ed Tanous271584a2019-07-09 16:24:22 -07001147 if (static_cast<size_t>(req.method()) >= perMethods.size())
Ed Tanous888880a2020-08-24 13:48:50 -07001148 {
1149 res.result(boost::beast::http::status::not_found);
1150 res.end();
Tanousf00032d2018-11-05 01:18:10 -03001151 return;
Ed Tanous888880a2020-08-24 13:48:50 -07001152 }
Tanousf00032d2018-11-05 01:18:10 -03001153
Ed Tanous271584a2019-07-09 16:24:22 -07001154 PerMethod& perMethod = perMethods[static_cast<size_t>(req.method())];
Tanousf00032d2018-11-05 01:18:10 -03001155 Trie& trie = perMethod.trie;
1156 std::vector<BaseRule*>& rules = perMethod.rules;
1157
1158 const std::pair<unsigned, RoutingParams>& found = trie.find(req.url);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001159 unsigned ruleIndex = found.first;
Ed Tanouse662eae2022-01-25 10:39:19 -08001160 if (ruleIndex == 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001161 {
1162 BMCWEB_LOG_DEBUG << "Cannot match rules " << req.url;
Ed Tanousde5c9f32019-03-26 09:17:55 -07001163 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001164 res.end();
1165 return;
1166 }
1167
1168 if (ruleIndex >= rules.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -07001169 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001170 throw std::runtime_error("Trie internal structure corrupted!");
Ed Tanous3174e4d2020-10-07 11:41:22 -07001171 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001172
Ed Tanous271584a2019-07-09 16:24:22 -07001173 if ((rules[ruleIndex]->getMethods() &
1174 (1U << static_cast<size_t>(req.method()))) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001175 {
1176 BMCWEB_LOG_DEBUG << "Rule found but method mismatch: " << req.url
1177 << " with " << req.methodString() << "("
Ed Tanous271584a2019-07-09 16:24:22 -07001178 << static_cast<uint32_t>(req.method()) << ") / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001179 << rules[ruleIndex]->getMethods();
Ed Tanousde5c9f32019-03-26 09:17:55 -07001180 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001181 res.end();
1182 return;
1183 }
1184
1185 BMCWEB_LOG_DEBUG << "Matched rule (upgrade) '" << rules[ruleIndex]->rule
Ed Tanous271584a2019-07-09 16:24:22 -07001186 << "' " << static_cast<uint32_t>(req.method()) << " / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001187 << rules[ruleIndex]->getMethods();
1188
1189 // any uncaught exceptions become 500s
1190 try
1191 {
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001192 rules[ruleIndex]->handleUpgrade(req, res,
1193 std::forward<Adaptor>(adaptor));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001194 }
Patrick Williamsc5967042021-10-06 12:39:54 -05001195 catch (const std::exception& e)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001196 {
1197 BMCWEB_LOG_ERROR << "An uncaught exception occurred: " << e.what();
Ed Tanousde5c9f32019-03-26 09:17:55 -07001198 res.result(boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001199 res.end();
1200 return;
1201 }
1202 catch (...)
1203 {
1204 BMCWEB_LOG_ERROR
1205 << "An uncaught exception occurred. The type was unknown "
1206 "so no information was available.";
Ed Tanousde5c9f32019-03-26 09:17:55 -07001207 res.result(boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001208 res.end();
1209 return;
1210 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001211 }
1212
zhanghch058d1b46d2021-04-01 11:18:24 +08001213 void handle(Request& req,
1214 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001215 {
Ed Tanous271584a2019-07-09 16:24:22 -07001216 if (static_cast<size_t>(req.method()) >= perMethods.size())
Ed Tanous888880a2020-08-24 13:48:50 -07001217 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001218 asyncResp->res.result(boost::beast::http::status::not_found);
Tanousf00032d2018-11-05 01:18:10 -03001219 return;
Ed Tanous888880a2020-08-24 13:48:50 -07001220 }
Ed Tanous271584a2019-07-09 16:24:22 -07001221 PerMethod& perMethod = perMethods[static_cast<size_t>(req.method())];
Tanousf00032d2018-11-05 01:18:10 -03001222 Trie& trie = perMethod.trie;
1223 std::vector<BaseRule*>& rules = perMethod.rules;
1224
1225 const std::pair<unsigned, RoutingParams>& found = trie.find(req.url);
Ed Tanous7045c8d2017-04-03 10:04:37 -07001226
Ed Tanous1abe55e2018-09-05 08:30:59 -07001227 unsigned ruleIndex = found.first;
1228
Ed Tanouse662eae2022-01-25 10:39:19 -08001229 if (ruleIndex == 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001230 {
Ed Tanous2634dcd2019-03-26 09:28:06 -07001231 // Check to see if this url exists at any verb
1232 for (const PerMethod& p : perMethods)
1233 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001234 const std::pair<unsigned, RoutingParams>& found2 =
Ed Tanous2634dcd2019-03-26 09:28:06 -07001235 p.trie.find(req.url);
Ed Tanous23a21a12020-07-25 04:45:05 +00001236 if (found2.first > 0)
Ed Tanous2634dcd2019-03-26 09:28:06 -07001237 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001238 asyncResp->res.result(
1239 boost::beast::http::status::method_not_allowed);
Ed Tanous2634dcd2019-03-26 09:28:06 -07001240 return;
1241 }
1242 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001243 BMCWEB_LOG_DEBUG << "Cannot match rules " << req.url;
zhanghch058d1b46d2021-04-01 11:18:24 +08001244 asyncResp->res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001245 return;
1246 }
1247
1248 if (ruleIndex >= rules.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -07001249 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001250 throw std::runtime_error("Trie internal structure corrupted!");
Ed Tanous3174e4d2020-10-07 11:41:22 -07001251 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001252
Ed Tanous271584a2019-07-09 16:24:22 -07001253 if ((rules[ruleIndex]->getMethods() &
1254 (1U << static_cast<uint32_t>(req.method()))) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001255 {
1256 BMCWEB_LOG_DEBUG << "Rule found but method mismatch: " << req.url
1257 << " with " << req.methodString() << "("
Ed Tanous271584a2019-07-09 16:24:22 -07001258 << static_cast<uint32_t>(req.method()) << ") / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001259 << rules[ruleIndex]->getMethods();
zhanghch058d1b46d2021-04-01 11:18:24 +08001260 asyncResp->res.result(
1261 boost::beast::http::status::method_not_allowed);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001262 return;
1263 }
1264
1265 BMCWEB_LOG_DEBUG << "Matched rule '" << rules[ruleIndex]->rule << "' "
Ed Tanous271584a2019-07-09 16:24:22 -07001266 << static_cast<uint32_t>(req.method()) << " / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001267 << rules[ruleIndex]->getMethods();
1268
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001269 if (req.session == nullptr)
James Feist7166bf02019-12-10 16:52:14 +00001270 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001271 rules[ruleIndex]->handle(req, asyncResp, found.second);
James Feist7166bf02019-12-10 16:52:14 +00001272 return;
1273 }
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001274
1275 crow::connections::systemBus->async_method_call(
Ed Tanous168e20c2021-12-13 14:39:53 -08001276 [&req, asyncResp, &rules, ruleIndex,
1277 found](const boost::system::error_code ec,
1278 const std::map<std::string, dbus::utility::DbusVariantType>&
1279 userInfo) {
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001280 if (ec)
1281 {
1282 BMCWEB_LOG_ERROR << "GetUserInfo failed...";
zhanghch058d1b46d2021-04-01 11:18:24 +08001283 asyncResp->res.result(
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001284 boost::beast::http::status::internal_server_error);
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001285 return;
1286 }
1287
1288 const std::string* userRolePtr = nullptr;
1289 auto userInfoIter = userInfo.find("UserPrivilege");
1290 if (userInfoIter != userInfo.end())
1291 {
1292 userRolePtr =
1293 std::get_if<std::string>(&userInfoIter->second);
1294 }
1295
1296 std::string userRole{};
1297 if (userRolePtr != nullptr)
1298 {
1299 userRole = *userRolePtr;
1300 BMCWEB_LOG_DEBUG << "userName = " << req.session->username
1301 << " userRole = " << *userRolePtr;
1302 }
1303
Ed Tanous5dc924d2021-12-20 09:17:28 -08001304 const bool* remoteUserPtr = nullptr;
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001305 auto remoteUserIter = userInfo.find("RemoteUser");
1306 if (remoteUserIter != userInfo.end())
1307 {
1308 remoteUserPtr = std::get_if<bool>(&remoteUserIter->second);
1309 }
1310 if (remoteUserPtr == nullptr)
1311 {
1312 BMCWEB_LOG_ERROR
1313 << "RemoteUser property missing or wrong type";
zhanghch058d1b46d2021-04-01 11:18:24 +08001314 asyncResp->res.result(
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001315 boost::beast::http::status::internal_server_error);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001316 return;
1317 }
1318 bool remoteUser = *remoteUserPtr;
1319
1320 bool passwordExpired = false; // default for remote user
1321 if (!remoteUser)
1322 {
Ed Tanous5dc924d2021-12-20 09:17:28 -08001323 const bool* passwordExpiredPtr = nullptr;
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001324 auto passwordExpiredIter =
1325 userInfo.find("UserPasswordExpired");
1326 if (passwordExpiredIter != userInfo.end())
1327 {
1328 passwordExpiredPtr =
1329 std::get_if<bool>(&passwordExpiredIter->second);
1330 }
1331 if (passwordExpiredPtr != nullptr)
1332 {
1333 passwordExpired = *passwordExpiredPtr;
1334 }
1335 else
1336 {
1337 BMCWEB_LOG_ERROR
1338 << "UserPasswordExpired property is expected for"
1339 " local user but is missing or wrong type";
zhanghch058d1b46d2021-04-01 11:18:24 +08001340 asyncResp->res.result(
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001341 boost::beast::http::status::internal_server_error);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001342 return;
1343 }
1344 }
1345
Ed Tanous23a21a12020-07-25 04:45:05 +00001346 // Get the userprivileges from the role
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001347 redfish::Privileges userPrivileges =
1348 redfish::getUserPrivileges(userRole);
1349
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001350 // Set isConfigureSelfOnly based on D-Bus results. This
1351 // ignores the results from both pamAuthenticateUser and the
1352 // value from any previous use of this session.
1353 req.session->isConfigureSelfOnly = passwordExpired;
1354
Ed Tanous23a21a12020-07-25 04:45:05 +00001355 // Modifyprivileges if isConfigureSelfOnly.
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001356 if (req.session->isConfigureSelfOnly)
1357 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001358 // Remove allprivileges except ConfigureSelf
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001359 userPrivileges = userPrivileges.intersection(
1360 redfish::Privileges{"ConfigureSelf"});
1361 BMCWEB_LOG_DEBUG << "Operation limited to ConfigureSelf";
1362 }
1363
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001364 if (!rules[ruleIndex]->checkPrivileges(userPrivileges))
1365 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001366 asyncResp->res.result(
1367 boost::beast::http::status::forbidden);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001368 if (req.session->isConfigureSelfOnly)
1369 {
1370 redfish::messages::passwordChangeRequired(
zhanghch058d1b46d2021-04-01 11:18:24 +08001371 asyncResp->res,
Ed Tanousace85d62021-10-26 12:45:59 -07001372 crow::utility::urlFromPieces(
1373 "redfish", "v1", "AccountService", "Accounts",
1374 req.session->username));
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001375 }
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001376 return;
1377 }
1378
1379 req.userRole = userRole;
zhanghch058d1b46d2021-04-01 11:18:24 +08001380 rules[ruleIndex]->handle(req, asyncResp, found.second);
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001381 },
1382 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1383 "xyz.openbmc_project.User.Manager", "GetUserInfo",
1384 req.session->username);
Ed Tanous7045c8d2017-04-03 10:04:37 -07001385 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001386
Ed Tanous1abe55e2018-09-05 08:30:59 -07001387 void debugPrint()
1388 {
Ed Tanous271584a2019-07-09 16:24:22 -07001389 for (size_t i = 0; i < perMethods.size(); i++)
Tanousf00032d2018-11-05 01:18:10 -03001390 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001391 BMCWEB_LOG_DEBUG << boost::beast::http::to_string(
1392 static_cast<boost::beast::http::verb>(i));
Tanousf00032d2018-11-05 01:18:10 -03001393 perMethods[i].trie.debugPrint();
1394 }
Ed Tanous3dac7492017-08-02 13:46:20 -07001395 }
Ed Tanousb4a7bfa2017-04-04 17:23:00 -07001396
Ed Tanous1abe55e2018-09-05 08:30:59 -07001397 std::vector<const std::string*> getRoutes(const std::string& parent)
1398 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001399 std::vector<const std::string*> ret;
Tanousf00032d2018-11-05 01:18:10 -03001400
1401 for (const PerMethod& pm : perMethods)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001402 {
Tanousf00032d2018-11-05 01:18:10 -03001403 std::vector<unsigned> x;
1404 pm.trie.findRouteIndexes(parent, x);
1405 for (unsigned index : x)
1406 {
1407 ret.push_back(&pm.rules[index]->rule);
1408 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001409 }
1410 return ret;
1411 }
1412
1413 private:
Tanousf00032d2018-11-05 01:18:10 -03001414 struct PerMethod
1415 {
1416 std::vector<BaseRule*> rules;
1417 Trie trie;
Ed Tanous313a3c22022-03-14 09:27:38 -07001418 // rule index 0 has special meaning; preallocate it to avoid
Tanousf00032d2018-11-05 01:18:10 -03001419 // duplication.
Ed Tanous313a3c22022-03-14 09:27:38 -07001420 PerMethod() : rules(1)
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001421 {}
Tanousf00032d2018-11-05 01:18:10 -03001422 };
Ed Tanous888880a2020-08-24 13:48:50 -07001423
1424 const static size_t maxHttpVerbCount =
Ed Tanouscc090442020-10-07 08:20:50 -07001425 static_cast<size_t>(boost::beast::http::verb::unlink);
Ed Tanous888880a2020-08-24 13:48:50 -07001426
Tanousf00032d2018-11-05 01:18:10 -03001427 std::array<PerMethod, maxHttpVerbCount> perMethods;
1428 std::vector<std::unique_ptr<BaseRule>> allRules;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001429};
Ed Tanous1abe55e2018-09-05 08:30:59 -07001430} // namespace crow