blob: 29ae22b745ecf004f5b788fa570bdac4e2c3c3cc [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:
zhanghch0577726382021-10-21 14:07:57 +0800400 std::function<void(crow::websocket::Connection&)> openHandler;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700401 std::function<void(crow::websocket::Connection&, const std::string&, bool)>
402 messageHandler;
403 std::function<void(crow::websocket::Connection&, const std::string&)>
404 closeHandler;
405 std::function<void(crow::websocket::Connection&)> errorHandler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700406};
407
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500408template <typename T>
409struct RuleParameterTraits
Ed Tanous1abe55e2018-09-05 08:30:59 -0700410{
411 using self_t = T;
412 WebSocketRule& websocket()
413 {
Ed Tanous271584a2019-07-09 16:24:22 -0700414 self_t* self = static_cast<self_t*>(this);
415 WebSocketRule* p = new WebSocketRule(self->rule);
416 self->ruleToUpgrade.reset(p);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700417 return *p;
418 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700419
Ed Tanousf23b7292020-10-15 09:41:17 -0700420 self_t& name(const std::string_view name) noexcept
Ed Tanous1abe55e2018-09-05 08:30:59 -0700421 {
Ed Tanous271584a2019-07-09 16:24:22 -0700422 self_t* self = static_cast<self_t*>(this);
Ed Tanousf23b7292020-10-15 09:41:17 -0700423 self->nameStr = name;
Ed Tanous271584a2019-07-09 16:24:22 -0700424 return *self;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700425 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700426
Ed Tanous1abe55e2018-09-05 08:30:59 -0700427 self_t& methods(boost::beast::http::verb method)
428 {
Ed Tanous271584a2019-07-09 16:24:22 -0700429 self_t* self = static_cast<self_t*>(this);
430 self->methodsBitfield = 1U << static_cast<size_t>(method);
431 return *self;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700432 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700433
Ed Tanous1abe55e2018-09-05 08:30:59 -0700434 template <typename... MethodArgs>
Ed Tanous81ce6092020-12-17 16:54:55 +0000435 self_t& methods(boost::beast::http::verb method, MethodArgs... argsMethod)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700436 {
Ed Tanous271584a2019-07-09 16:24:22 -0700437 self_t* self = static_cast<self_t*>(this);
Ed Tanous81ce6092020-12-17 16:54:55 +0000438 methods(argsMethod...);
Ed Tanous271584a2019-07-09 16:24:22 -0700439 self->methodsBitfield |= 1U << static_cast<size_t>(method);
440 return *self;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700441 }
Tanousf00032d2018-11-05 01:18:10 -0300442
Ed Tanous432a8902021-06-14 15:28:56 -0700443 self_t& privileges(
444 const std::initializer_list<std::initializer_list<const char*>>& p)
Tanousf00032d2018-11-05 01:18:10 -0300445 {
Ed Tanous271584a2019-07-09 16:24:22 -0700446 self_t* self = static_cast<self_t*>(this);
Ed Tanous432a8902021-06-14 15:28:56 -0700447 for (const std::initializer_list<const char*>& privilege : p)
Tanousf00032d2018-11-05 01:18:10 -0300448 {
Ed Tanous271584a2019-07-09 16:24:22 -0700449 self->privilegesSet.emplace_back(privilege);
Tanousf00032d2018-11-05 01:18:10 -0300450 }
Ed Tanous271584a2019-07-09 16:24:22 -0700451 return *self;
Tanousf00032d2018-11-05 01:18:10 -0300452 }
Ed Tanoused398212021-06-09 17:05:54 -0700453
454 template <size_t N, typename... MethodArgs>
455 self_t& privileges(const std::array<redfish::Privileges, N>& p)
456 {
457 self_t* self = static_cast<self_t*>(this);
458 for (const redfish::Privileges& privilege : p)
459 {
460 self->privilegesSet.emplace_back(privilege);
461 }
462 return *self;
463 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700464};
465
Ed Tanous1abe55e2018-09-05 08:30:59 -0700466class DynamicRule : public BaseRule, public RuleParameterTraits<DynamicRule>
467{
468 public:
Ed Tanousf23b7292020-10-15 09:41:17 -0700469 DynamicRule(const std::string& ruleIn) : BaseRule(ruleIn)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500470 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700471
Ed Tanous1abe55e2018-09-05 08:30:59 -0700472 void validate() override
473 {
474 if (!erasedHandler)
475 {
476 throw std::runtime_error(nameStr + (!nameStr.empty() ? ": " : "") +
477 "no handler for url " + rule);
478 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700479 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700480
zhanghch058d1b46d2021-04-01 11:18:24 +0800481 void handle(const Request& req,
482 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700483 const RoutingParams& params) override
484 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800485 erasedHandler(req, asyncResp, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700486 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700487
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500488 template <typename Func>
489 void operator()(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700490 {
491 using function_t = utility::function_traits<Func>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700492 erasedHandler =
Ed Tanous988403c2020-08-24 11:29:49 -0700493 wrap(std::move(f),
494 std::make_integer_sequence<unsigned, function_t::arity>{});
Ed Tanous1abe55e2018-09-05 08:30:59 -0700495 }
496
497 // enable_if Arg1 == request && Arg2 == Response
Gunnar Mills6be0e402020-07-08 13:21:51 -0500498 // enable_if Arg1 == request && Arg2 != response
Ed Tanous1abe55e2018-09-05 08:30:59 -0700499 // enable_if Arg1 != request
500
501 template <typename Func, unsigned... Indices>
zhanghch058d1b46d2021-04-01 11:18:24 +0800502 std::function<void(const Request&,
503 const std::shared_ptr<bmcweb::AsyncResp>&,
504 const RoutingParams&)>
Ed Tanous104f09c2022-01-25 09:56:04 -0800505 wrap(Func f, std::integer_sequence<unsigned, Indices...> /*is*/)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700506 {
Ed Tanous988403c2020-08-24 11:29:49 -0700507 using function_t = crow::utility::function_traits<Func>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700508
509 if (!black_magic::isParameterTagCompatible(
Ed Tanous988403c2020-08-24 11:29:49 -0700510 black_magic::getParameterTag(rule.c_str()),
511 black_magic::computeParameterTagFromArgsList<
Ed Tanous1abe55e2018-09-05 08:30:59 -0700512 typename function_t::template arg<Indices>...>::value))
513 {
514 throw std::runtime_error("routeDynamic: Handler type is mismatched "
515 "with URL parameters: " +
516 rule);
517 }
518 auto ret = detail::routing_handler_call_helper::Wrapped<
519 Func, typename function_t::template arg<Indices>...>();
520 ret.template set<typename function_t::template arg<Indices>...>(
521 std::move(f));
522 return ret;
523 }
524
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500525 template <typename Func>
526 void operator()(std::string name, Func&& f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700527 {
528 nameStr = std::move(name);
529 (*this).template operator()<Func>(std::forward(f));
530 }
531
532 private:
zhanghch058d1b46d2021-04-01 11:18:24 +0800533 std::function<void(const Request&,
534 const std::shared_ptr<bmcweb::AsyncResp>&,
535 const RoutingParams&)>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700536 erasedHandler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700537};
538
539template <typename... Args>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500540class TaggedRule :
541 public BaseRule,
542 public RuleParameterTraits<TaggedRule<Args...>>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700543{
544 public:
545 using self_t = TaggedRule<Args...>;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700546
Ed Tanousf23b7292020-10-15 09:41:17 -0700547 TaggedRule(const std::string& ruleIn) : BaseRule(ruleIn)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500548 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700549
Ed Tanous1abe55e2018-09-05 08:30:59 -0700550 void validate() override
551 {
552 if (!handler)
553 {
554 throw std::runtime_error(nameStr + (!nameStr.empty() ? ": " : "") +
555 "no handler for url " + rule);
556 }
557 }
558
559 template <typename Func>
560 typename std::enable_if<
561 black_magic::CallHelper<Func, black_magic::S<Args...>>::value,
562 void>::type
563 operator()(Func&& f)
564 {
565 static_assert(
566 black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
567 black_magic::CallHelper<
568 Func, black_magic::S<crow::Request, Args...>>::value,
569 "Handler type is mismatched with URL parameters");
570 static_assert(
571 !std::is_same<void, decltype(f(std::declval<Args>()...))>::value,
572 "Handler function cannot have void return type; valid return "
573 "types: "
Gunnar Mills6be0e402020-07-08 13:21:51 -0500574 "string, int, crow::response, nlohmann::json");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700575
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800576 handler = [f = std::forward<Func>(f)](
zhanghch058d1b46d2021-04-01 11:18:24 +0800577 const Request&,
578 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
579 Args... args) { asyncResp->res.result(f(args...)); };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700580 }
581
582 template <typename Func>
583 typename std::enable_if<
584 !black_magic::CallHelper<Func, black_magic::S<Args...>>::value &&
Ed Tanous7045c8d2017-04-03 10:04:37 -0700585 black_magic::CallHelper<
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700586 Func, black_magic::S<crow::Request, Args...>>::value,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700587 void>::type
588 operator()(Func&& f)
589 {
590 static_assert(
591 black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
592 black_magic::CallHelper<
593 Func, black_magic::S<crow::Request, Args...>>::value,
594 "Handler type is mismatched with URL parameters");
595 static_assert(
596 !std::is_same<void, decltype(f(std::declval<crow::Request>(),
597 std::declval<Args>()...))>::value,
598 "Handler function cannot have void return type; valid return "
599 "types: "
Gunnar Mills6be0e402020-07-08 13:21:51 -0500600 "string, int, crow::response,nlohmann::json");
Ed Tanous7045c8d2017-04-03 10:04:37 -0700601
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800602 handler = [f = std::forward<Func>(f)](
zhanghch058d1b46d2021-04-01 11:18:24 +0800603 const crow::Request& req,
604 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
605 Args... args) { asyncResp->res.result(f(req, args...)); };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700606 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700607
Ed Tanous1abe55e2018-09-05 08:30:59 -0700608 template <typename Func>
609 typename std::enable_if<
610 !black_magic::CallHelper<Func, black_magic::S<Args...>>::value &&
611 !black_magic::CallHelper<
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700612 Func, black_magic::S<crow::Request, Args...>>::value,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700613 void>::type
614 operator()(Func&& f)
615 {
616 static_assert(
617 black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
618 black_magic::CallHelper<
619 Func, black_magic::S<crow::Request, Args...>>::value ||
620 black_magic::CallHelper<
zhanghch058d1b46d2021-04-01 11:18:24 +0800621 Func, black_magic::S<crow::Request,
622 std::shared_ptr<bmcweb::AsyncResp>&,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700623 Args...>>::value,
624 "Handler type is mismatched with URL parameters");
625 static_assert(
zhanghch058d1b46d2021-04-01 11:18:24 +0800626 std::is_same<
627 void,
628 decltype(f(std::declval<crow::Request>(),
629 std::declval<std::shared_ptr<bmcweb::AsyncResp>&>(),
630 std::declval<Args>()...))>::value,
Tanousf00032d2018-11-05 01:18:10 -0300631 "Handler function with response argument should have void "
632 "return "
Ed Tanous1abe55e2018-09-05 08:30:59 -0700633 "type");
Ed Tanous7045c8d2017-04-03 10:04:37 -0700634
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800635 handler = std::forward<Func>(f);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700636 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700637
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500638 template <typename Func>
Ed Tanousf23b7292020-10-15 09:41:17 -0700639 void operator()(const std::string_view name, Func&& f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700640 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700641 nameStr = name;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700642 (*this).template operator()<Func>(std::forward(f));
643 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700644
zhanghch058d1b46d2021-04-01 11:18:24 +0800645 void handle(const Request& req,
646 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700647 const RoutingParams& params) override
648 {
649 detail::routing_handler_call_helper::Call<
650 detail::routing_handler_call_helper::CallParams<decltype(handler)>,
651 0, 0, 0, 0, black_magic::S<Args...>, black_magic::S<>>()(
652 detail::routing_handler_call_helper::CallParams<decltype(handler)>{
zhanghch058d1b46d2021-04-01 11:18:24 +0800653 handler, params, req, asyncResp});
Ed Tanous1abe55e2018-09-05 08:30:59 -0700654 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700655
Ed Tanous1abe55e2018-09-05 08:30:59 -0700656 private:
zhanghch058d1b46d2021-04-01 11:18:24 +0800657 std::function<void(const crow::Request&,
658 const std::shared_ptr<bmcweb::AsyncResp>&, Args...)>
659 handler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700660};
661
Ed Tanous1abe55e2018-09-05 08:30:59 -0700662class Trie
663{
664 public:
665 struct Node
666 {
667 unsigned ruleIndex{};
Ed Tanous271584a2019-07-09 16:24:22 -0700668 std::array<size_t, static_cast<size_t>(ParamType::MAX)>
669 paramChildrens{};
Ed Tanousa94ac612022-02-22 11:13:24 -0800670 using ChildMap = boost::container::flat_map<
671 std::string, unsigned, std::less<>,
672 std::vector<std::pair<std::string, unsigned>>>;
673 ChildMap children;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700674
Ed Tanous1abe55e2018-09-05 08:30:59 -0700675 bool isSimpleNode() const
676 {
Ed Tanouse662eae2022-01-25 10:39:19 -0800677 return ruleIndex == 0 &&
678 std::all_of(std::begin(paramChildrens),
679 std::end(paramChildrens),
680 [](size_t x) { return x == 0U; });
Ed Tanous7045c8d2017-04-03 10:04:37 -0700681 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700682 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700683
Ed Tanous1abe55e2018-09-05 08:30:59 -0700684 Trie() : nodes(1)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500685 {}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700686
687 private:
688 void optimizeNode(Node* node)
689 {
Ed Tanous271584a2019-07-09 16:24:22 -0700690 for (size_t x : node->paramChildrens)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700691 {
Ed Tanousdbb59d42022-01-25 11:09:55 -0800692 if (x == 0U)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700693 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700694 continue;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700695 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700696 Node* child = &nodes[x];
697 optimizeNode(child);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700698 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700699 if (node->children.empty())
Ed Tanous3174e4d2020-10-07 11:41:22 -0700700 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700701 return;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700702 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700703 bool mergeWithChild = true;
Ed Tanousa94ac612022-02-22 11:13:24 -0800704 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700705 {
706 Node* child = &nodes[kv.second];
707 if (!child->isSimpleNode())
708 {
709 mergeWithChild = false;
710 break;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700711 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700712 }
713 if (mergeWithChild)
714 {
Ed Tanousa94ac612022-02-22 11:13:24 -0800715 Node::ChildMap merged;
716 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700717 {
718 Node* child = &nodes[kv.second];
Ed Tanousa94ac612022-02-22 11:13:24 -0800719 for (const Node::ChildMap::value_type& childKv :
Tanousf00032d2018-11-05 01:18:10 -0300720 child->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700721 {
722 merged[kv.first + childKv.first] = childKv.second;
723 }
724 }
725 node->children = std::move(merged);
726 optimizeNode(node);
727 }
728 else
729 {
Ed Tanousa94ac612022-02-22 11:13:24 -0800730 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700731 {
732 Node* child = &nodes[kv.second];
733 optimizeNode(child);
734 }
735 }
736 }
737
738 void optimize()
739 {
740 optimizeNode(head());
741 }
742
743 public:
744 void validate()
745 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700746 optimize();
747 }
748
Ed Tanous81ce6092020-12-17 16:54:55 +0000749 void findRouteIndexes(const std::string& reqUrl,
750 std::vector<unsigned>& routeIndexes,
Tanousf00032d2018-11-05 01:18:10 -0300751 const Node* node = nullptr, unsigned pos = 0) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700752 {
753 if (node == nullptr)
754 {
755 node = head();
756 }
Ed Tanousa94ac612022-02-22 11:13:24 -0800757 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700758 {
759 const std::string& fragment = kv.first;
760 const Node* child = &nodes[kv.second];
Ed Tanous81ce6092020-12-17 16:54:55 +0000761 if (pos >= reqUrl.size())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700762 {
763 if (child->ruleIndex != 0 && fragment != "/")
764 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000765 routeIndexes.push_back(child->ruleIndex);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700766 }
Ed Tanous81ce6092020-12-17 16:54:55 +0000767 findRouteIndexes(reqUrl, routeIndexes, child,
Ed Tanous271584a2019-07-09 16:24:22 -0700768 static_cast<unsigned>(pos + fragment.size()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700769 }
770 else
771 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000772 if (reqUrl.compare(pos, fragment.size(), fragment) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700773 {
Ed Tanous271584a2019-07-09 16:24:22 -0700774 findRouteIndexes(
Ed Tanous81ce6092020-12-17 16:54:55 +0000775 reqUrl, routeIndexes, child,
Ed Tanous271584a2019-07-09 16:24:22 -0700776 static_cast<unsigned>(pos + fragment.size()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700777 }
778 }
779 }
780 }
781
782 std::pair<unsigned, RoutingParams>
Ed Tanous81ce6092020-12-17 16:54:55 +0000783 find(const std::string_view reqUrl, const Node* node = nullptr,
Ed Tanous271584a2019-07-09 16:24:22 -0700784 size_t pos = 0, RoutingParams* params = nullptr) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700785 {
786 RoutingParams empty;
787 if (params == nullptr)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700788 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700789 params = &empty;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700790 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700791
792 unsigned found{};
793 RoutingParams matchParams;
794
795 if (node == nullptr)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700796 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700797 node = head();
Ed Tanous3174e4d2020-10-07 11:41:22 -0700798 }
Ed Tanous81ce6092020-12-17 16:54:55 +0000799 if (pos == reqUrl.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -0700800 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700801 return {node->ruleIndex, *params};
Ed Tanous3174e4d2020-10-07 11:41:22 -0700802 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700803
804 auto updateFound =
805 [&found, &matchParams](std::pair<unsigned, RoutingParams>& ret) {
Ed Tanouse662eae2022-01-25 10:39:19 -0800806 if (ret.first != 0U && (found == 0U || found > ret.first))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700807 {
808 found = ret.first;
809 matchParams = std::move(ret.second);
810 }
811 };
812
Ed Tanouse662eae2022-01-25 10:39:19 -0800813 if (node->paramChildrens[static_cast<size_t>(ParamType::INT)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700814 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000815 char c = reqUrl[pos];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700816 if ((c >= '0' && c <= '9') || c == '+' || c == '-')
817 {
Ed Tanous543f4402022-01-06 13:12:53 -0800818 char* eptr = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700819 errno = 0;
820 long long int value =
Ed Tanous81ce6092020-12-17 16:54:55 +0000821 std::strtoll(reqUrl.data() + pos, &eptr, 10);
822 if (errno != ERANGE && eptr != reqUrl.data() + pos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700823 {
824 params->intParams.push_back(value);
Ed Tanous81ce6092020-12-17 16:54:55 +0000825 std::pair<unsigned, RoutingParams> ret =
826 find(reqUrl,
827 &nodes[node->paramChildrens[static_cast<size_t>(
828 ParamType::INT)]],
829 static_cast<size_t>(eptr - reqUrl.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700830 updateFound(ret);
831 params->intParams.pop_back();
832 }
833 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700834 }
835
Ed Tanouse662eae2022-01-25 10:39:19 -0800836 if (node->paramChildrens[static_cast<size_t>(ParamType::UINT)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700837 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000838 char c = reqUrl[pos];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700839 if ((c >= '0' && c <= '9') || c == '+')
840 {
Ed Tanous543f4402022-01-06 13:12:53 -0800841 char* eptr = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700842 errno = 0;
843 unsigned long long int value =
Ed Tanous81ce6092020-12-17 16:54:55 +0000844 std::strtoull(reqUrl.data() + pos, &eptr, 10);
845 if (errno != ERANGE && eptr != reqUrl.data() + pos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700846 {
847 params->uintParams.push_back(value);
Ed Tanous81ce6092020-12-17 16:54:55 +0000848 std::pair<unsigned, RoutingParams> ret =
849 find(reqUrl,
850 &nodes[node->paramChildrens[static_cast<size_t>(
851 ParamType::UINT)]],
852 static_cast<size_t>(eptr - reqUrl.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700853 updateFound(ret);
854 params->uintParams.pop_back();
855 }
856 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700857 }
858
Ed Tanouse662eae2022-01-25 10:39:19 -0800859 if (node->paramChildrens[static_cast<size_t>(ParamType::DOUBLE)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700860 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000861 char c = reqUrl[pos];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700862 if ((c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')
863 {
Ed Tanous543f4402022-01-06 13:12:53 -0800864 char* eptr = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700865 errno = 0;
Ed Tanous81ce6092020-12-17 16:54:55 +0000866 double value = std::strtod(reqUrl.data() + pos, &eptr);
867 if (errno != ERANGE && eptr != reqUrl.data() + pos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700868 {
869 params->doubleParams.push_back(value);
Ed Tanous81ce6092020-12-17 16:54:55 +0000870 std::pair<unsigned, RoutingParams> ret =
871 find(reqUrl,
872 &nodes[node->paramChildrens[static_cast<size_t>(
873 ParamType::DOUBLE)]],
874 static_cast<size_t>(eptr - reqUrl.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700875 updateFound(ret);
876 params->doubleParams.pop_back();
877 }
878 }
879 }
880
Ed Tanouse662eae2022-01-25 10:39:19 -0800881 if (node->paramChildrens[static_cast<size_t>(ParamType::STRING)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700882 {
Ed Tanousb01bf292019-03-25 19:25:26 +0000883 size_t epos = pos;
Ed Tanous81ce6092020-12-17 16:54:55 +0000884 for (; epos < reqUrl.size(); epos++)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700885 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000886 if (reqUrl[epos] == '/')
Ed Tanous3174e4d2020-10-07 11:41:22 -0700887 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700888 break;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700889 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700890 }
891
892 if (epos != pos)
893 {
894 params->stringParams.emplace_back(
Ed Tanous81ce6092020-12-17 16:54:55 +0000895 reqUrl.substr(pos, epos - pos));
Tanousf00032d2018-11-05 01:18:10 -0300896 std::pair<unsigned, RoutingParams> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000897 find(reqUrl,
Ed Tanous271584a2019-07-09 16:24:22 -0700898 &nodes[node->paramChildrens[static_cast<size_t>(
899 ParamType::STRING)]],
Ed Tanousb01bf292019-03-25 19:25:26 +0000900 epos, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700901 updateFound(ret);
902 params->stringParams.pop_back();
903 }
904 }
905
Ed Tanouse662eae2022-01-25 10:39:19 -0800906 if (node->paramChildrens[static_cast<size_t>(ParamType::PATH)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700907 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000908 size_t epos = reqUrl.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700909
910 if (epos != pos)
911 {
912 params->stringParams.emplace_back(
Ed Tanous81ce6092020-12-17 16:54:55 +0000913 reqUrl.substr(pos, epos - pos));
Ed Tanous271584a2019-07-09 16:24:22 -0700914 std::pair<unsigned, RoutingParams> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000915 find(reqUrl,
Ed Tanous271584a2019-07-09 16:24:22 -0700916 &nodes[node->paramChildrens[static_cast<size_t>(
917 ParamType::PATH)]],
918 epos, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700919 updateFound(ret);
920 params->stringParams.pop_back();
921 }
922 }
923
Ed Tanousa94ac612022-02-22 11:13:24 -0800924 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700925 {
926 const std::string& fragment = kv.first;
927 const Node* child = &nodes[kv.second];
928
Ed Tanous81ce6092020-12-17 16:54:55 +0000929 if (reqUrl.compare(pos, fragment.size(), fragment) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700930 {
Tanousf00032d2018-11-05 01:18:10 -0300931 std::pair<unsigned, RoutingParams> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000932 find(reqUrl, child, pos + fragment.size(), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700933 updateFound(ret);
934 }
935 }
936
937 return {found, matchParams};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700938 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700939
940 void add(const std::string& url, unsigned ruleIndex)
941 {
Ed Tanous271584a2019-07-09 16:24:22 -0700942 size_t idx = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700943
944 for (unsigned i = 0; i < url.size(); i++)
945 {
946 char c = url[i];
947 if (c == '<')
948 {
Tanousf00032d2018-11-05 01:18:10 -0300949 const static std::array<std::pair<ParamType, std::string>, 7>
950 paramTraits = {{
951 {ParamType::INT, "<int>"},
952 {ParamType::UINT, "<uint>"},
953 {ParamType::DOUBLE, "<float>"},
954 {ParamType::DOUBLE, "<double>"},
955 {ParamType::STRING, "<str>"},
956 {ParamType::STRING, "<string>"},
957 {ParamType::PATH, "<path>"},
958 }};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700959
Tanousf00032d2018-11-05 01:18:10 -0300960 for (const std::pair<ParamType, std::string>& x : paramTraits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700961 {
Tanousf00032d2018-11-05 01:18:10 -0300962 if (url.compare(i, x.second.size(), x.second) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700963 {
Ed Tanous271584a2019-07-09 16:24:22 -0700964 size_t index = static_cast<size_t>(x.first);
Ed Tanouse662eae2022-01-25 10:39:19 -0800965 if (nodes[idx].paramChildrens[index] == 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700966 {
Tanousf00032d2018-11-05 01:18:10 -0300967 unsigned newNodeIdx = newNode();
Ed Tanous271584a2019-07-09 16:24:22 -0700968 nodes[idx].paramChildrens[index] = newNodeIdx;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700969 }
Ed Tanous271584a2019-07-09 16:24:22 -0700970 idx = nodes[idx].paramChildrens[index];
971 i += static_cast<unsigned>(x.second.size());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700972 break;
973 }
974 }
975
976 i--;
977 }
978 else
979 {
980 std::string piece(&c, 1);
Ed Tanouse662eae2022-01-25 10:39:19 -0800981 if (nodes[idx].children.count(piece) == 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700982 {
Tanousf00032d2018-11-05 01:18:10 -0300983 unsigned newNodeIdx = newNode();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700984 nodes[idx].children.emplace(piece, newNodeIdx);
985 }
986 idx = nodes[idx].children[piece];
987 }
988 }
Ed Tanouse662eae2022-01-25 10:39:19 -0800989 if (nodes[idx].ruleIndex != 0U)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700990 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700991 throw std::runtime_error("handler already exists for " + url);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700992 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700993 nodes[idx].ruleIndex = ruleIndex;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700994 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700995
Ed Tanous1abe55e2018-09-05 08:30:59 -0700996 private:
Ed Tanous271584a2019-07-09 16:24:22 -0700997 void debugNodePrint(Node* n, size_t level)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700998 {
Ed Tanous271584a2019-07-09 16:24:22 -0700999 for (size_t i = 0; i < static_cast<size_t>(ParamType::MAX); i++)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001000 {
Ed Tanouse662eae2022-01-25 10:39:19 -08001001 if (n->paramChildrens[i] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001002 {
1003 BMCWEB_LOG_DEBUG << std::string(
Ed Tanous271584a2019-07-09 16:24:22 -07001004 2U * level, ' ') /*<< "("<<n->paramChildrens[i]<<") "*/;
1005 switch (static_cast<ParamType>(i))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001006 {
1007 case ParamType::INT:
1008 BMCWEB_LOG_DEBUG << "<int>";
1009 break;
1010 case ParamType::UINT:
1011 BMCWEB_LOG_DEBUG << "<uint>";
1012 break;
1013 case ParamType::DOUBLE:
1014 BMCWEB_LOG_DEBUG << "<float>";
1015 break;
1016 case ParamType::STRING:
1017 BMCWEB_LOG_DEBUG << "<str>";
1018 break;
1019 case ParamType::PATH:
1020 BMCWEB_LOG_DEBUG << "<path>";
1021 break;
Ed Tanous23a21a12020-07-25 04:45:05 +00001022 case ParamType::MAX:
Ed Tanous1abe55e2018-09-05 08:30:59 -07001023 BMCWEB_LOG_DEBUG << "<ERROR>";
1024 break;
1025 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001026
Ed Tanous1abe55e2018-09-05 08:30:59 -07001027 debugNodePrint(&nodes[n->paramChildrens[i]], level + 1);
1028 }
1029 }
Ed Tanousa94ac612022-02-22 11:13:24 -08001030 for (const Node::ChildMap::value_type& kv : n->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001031 {
1032 BMCWEB_LOG_DEBUG
Ed Tanous271584a2019-07-09 16:24:22 -07001033 << std::string(2U * level, ' ') /*<< "(" << kv.second << ") "*/
Ed Tanous1abe55e2018-09-05 08:30:59 -07001034 << kv.first;
1035 debugNodePrint(&nodes[kv.second], level + 1);
1036 }
1037 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001038
Ed Tanous1abe55e2018-09-05 08:30:59 -07001039 public:
1040 void debugPrint()
1041 {
Ed Tanous271584a2019-07-09 16:24:22 -07001042 debugNodePrint(head(), 0U);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001043 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001044
Ed Tanous1abe55e2018-09-05 08:30:59 -07001045 private:
1046 const Node* head() const
1047 {
1048 return &nodes.front();
1049 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001050
Ed Tanous1abe55e2018-09-05 08:30:59 -07001051 Node* head()
1052 {
1053 return &nodes.front();
1054 }
1055
1056 unsigned newNode()
1057 {
1058 nodes.resize(nodes.size() + 1);
Ed Tanous271584a2019-07-09 16:24:22 -07001059 return static_cast<unsigned>(nodes.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001060 }
1061
1062 std::vector<Node> nodes;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001063};
1064
Ed Tanous1abe55e2018-09-05 08:30:59 -07001065class Router
1066{
1067 public:
Ed Tanous0c0084a2019-10-24 15:57:51 -07001068 Router() = default;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001069
Ed Tanous1abe55e2018-09-05 08:30:59 -07001070 DynamicRule& newRuleDynamic(const std::string& rule)
1071 {
1072 std::unique_ptr<DynamicRule> ruleObject =
1073 std::make_unique<DynamicRule>(rule);
1074 DynamicRule* ptr = ruleObject.get();
Tanousf00032d2018-11-05 01:18:10 -03001075 allRules.emplace_back(std::move(ruleObject));
Ed Tanous7045c8d2017-04-03 10:04:37 -07001076
Ed Tanous1abe55e2018-09-05 08:30:59 -07001077 return *ptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001078 }
1079
Ed Tanous1abe55e2018-09-05 08:30:59 -07001080 template <uint64_t N>
1081 typename black_magic::Arguments<N>::type::template rebind<TaggedRule>&
1082 newRuleTagged(const std::string& rule)
1083 {
1084 using RuleT = typename black_magic::Arguments<N>::type::template rebind<
1085 TaggedRule>;
1086 std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule);
1087 RuleT* ptr = ruleObject.get();
Tanousf00032d2018-11-05 01:18:10 -03001088 allRules.emplace_back(std::move(ruleObject));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001089
1090 return *ptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001091 }
1092
Tanousf00032d2018-11-05 01:18:10 -03001093 void internalAddRuleObject(const std::string& rule, BaseRule* ruleObject)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001094 {
Tanousf00032d2018-11-05 01:18:10 -03001095 if (ruleObject == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001096 {
Tanousf00032d2018-11-05 01:18:10 -03001097 return;
1098 }
Ed Tanous888880a2020-08-24 13:48:50 -07001099 for (size_t method = 0, methodBit = 1; method < maxHttpVerbCount;
Ed Tanous2c70f802020-09-28 14:29:23 -07001100 method++, methodBit <<= 1)
Tanousf00032d2018-11-05 01:18:10 -03001101 {
Ed Tanouse662eae2022-01-25 10:39:19 -08001102 if ((ruleObject->methodsBitfield & methodBit) > 0U)
Tanousf00032d2018-11-05 01:18:10 -03001103 {
1104 perMethods[method].rules.emplace_back(ruleObject);
1105 perMethods[method].trie.add(
Ed Tanous271584a2019-07-09 16:24:22 -07001106 rule, static_cast<unsigned>(
1107 perMethods[method].rules.size() - 1U));
Tanousf00032d2018-11-05 01:18:10 -03001108 // directory case:
1109 // request to `/about' url matches `/about/' rule
1110 if (rule.size() > 2 && rule.back() == '/')
1111 {
1112 perMethods[method].trie.add(
1113 rule.substr(0, rule.size() - 1),
Ed Tanous271584a2019-07-09 16:24:22 -07001114 static_cast<unsigned>(perMethods[method].rules.size() -
1115 1));
Tanousf00032d2018-11-05 01:18:10 -03001116 }
1117 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001118 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001119 }
1120
Ed Tanous1abe55e2018-09-05 08:30:59 -07001121 void validate()
1122 {
Tanousf00032d2018-11-05 01:18:10 -03001123 for (std::unique_ptr<BaseRule>& rule : allRules)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001124 {
1125 if (rule)
1126 {
Tanousf00032d2018-11-05 01:18:10 -03001127 std::unique_ptr<BaseRule> upgraded = rule->upgrade();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001128 if (upgraded)
Ed Tanous3174e4d2020-10-07 11:41:22 -07001129 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001130 rule = std::move(upgraded);
Ed Tanous3174e4d2020-10-07 11:41:22 -07001131 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001132 rule->validate();
Tanousf00032d2018-11-05 01:18:10 -03001133 internalAddRuleObject(rule->rule, rule.get());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001134 }
1135 }
Tanousf00032d2018-11-05 01:18:10 -03001136 for (PerMethod& perMethod : perMethods)
1137 {
1138 perMethod.trie.validate();
1139 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001140 }
1141
Ed Tanous1abe55e2018-09-05 08:30:59 -07001142 template <typename Adaptor>
1143 void handleUpgrade(const Request& req, Response& res, Adaptor&& adaptor)
1144 {
Ed Tanous271584a2019-07-09 16:24:22 -07001145 if (static_cast<size_t>(req.method()) >= perMethods.size())
Ed Tanous888880a2020-08-24 13:48:50 -07001146 {
1147 res.result(boost::beast::http::status::not_found);
1148 res.end();
Tanousf00032d2018-11-05 01:18:10 -03001149 return;
Ed Tanous888880a2020-08-24 13:48:50 -07001150 }
Tanousf00032d2018-11-05 01:18:10 -03001151
Ed Tanous271584a2019-07-09 16:24:22 -07001152 PerMethod& perMethod = perMethods[static_cast<size_t>(req.method())];
Tanousf00032d2018-11-05 01:18:10 -03001153 Trie& trie = perMethod.trie;
1154 std::vector<BaseRule*>& rules = perMethod.rules;
1155
1156 const std::pair<unsigned, RoutingParams>& found = trie.find(req.url);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001157 unsigned ruleIndex = found.first;
Ed Tanouse662eae2022-01-25 10:39:19 -08001158 if (ruleIndex == 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001159 {
1160 BMCWEB_LOG_DEBUG << "Cannot match rules " << req.url;
Ed Tanousde5c9f32019-03-26 09:17:55 -07001161 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001162 res.end();
1163 return;
1164 }
1165
1166 if (ruleIndex >= rules.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -07001167 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001168 throw std::runtime_error("Trie internal structure corrupted!");
Ed Tanous3174e4d2020-10-07 11:41:22 -07001169 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001170
Ed Tanous271584a2019-07-09 16:24:22 -07001171 if ((rules[ruleIndex]->getMethods() &
1172 (1U << static_cast<size_t>(req.method()))) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001173 {
1174 BMCWEB_LOG_DEBUG << "Rule found but method mismatch: " << req.url
1175 << " with " << req.methodString() << "("
Ed Tanous271584a2019-07-09 16:24:22 -07001176 << static_cast<uint32_t>(req.method()) << ") / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001177 << rules[ruleIndex]->getMethods();
Ed Tanousde5c9f32019-03-26 09:17:55 -07001178 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001179 res.end();
1180 return;
1181 }
1182
1183 BMCWEB_LOG_DEBUG << "Matched rule (upgrade) '" << rules[ruleIndex]->rule
Ed Tanous271584a2019-07-09 16:24:22 -07001184 << "' " << static_cast<uint32_t>(req.method()) << " / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001185 << rules[ruleIndex]->getMethods();
1186
1187 // any uncaught exceptions become 500s
1188 try
1189 {
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001190 rules[ruleIndex]->handleUpgrade(req, res,
1191 std::forward<Adaptor>(adaptor));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001192 }
Patrick Williamsc5967042021-10-06 12:39:54 -05001193 catch (const std::exception& e)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001194 {
1195 BMCWEB_LOG_ERROR << "An uncaught exception occurred: " << e.what();
Ed Tanousde5c9f32019-03-26 09:17:55 -07001196 res.result(boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001197 res.end();
1198 return;
1199 }
1200 catch (...)
1201 {
1202 BMCWEB_LOG_ERROR
1203 << "An uncaught exception occurred. The type was unknown "
1204 "so no information was available.";
Ed Tanousde5c9f32019-03-26 09:17:55 -07001205 res.result(boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001206 res.end();
1207 return;
1208 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001209 }
1210
zhanghch058d1b46d2021-04-01 11:18:24 +08001211 void handle(Request& req,
1212 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001213 {
Ed Tanous271584a2019-07-09 16:24:22 -07001214 if (static_cast<size_t>(req.method()) >= perMethods.size())
Ed Tanous888880a2020-08-24 13:48:50 -07001215 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001216 asyncResp->res.result(boost::beast::http::status::not_found);
Tanousf00032d2018-11-05 01:18:10 -03001217 return;
Ed Tanous888880a2020-08-24 13:48:50 -07001218 }
Ed Tanous271584a2019-07-09 16:24:22 -07001219 PerMethod& perMethod = perMethods[static_cast<size_t>(req.method())];
Tanousf00032d2018-11-05 01:18:10 -03001220 Trie& trie = perMethod.trie;
1221 std::vector<BaseRule*>& rules = perMethod.rules;
1222
1223 const std::pair<unsigned, RoutingParams>& found = trie.find(req.url);
Ed Tanous7045c8d2017-04-03 10:04:37 -07001224
Ed Tanous1abe55e2018-09-05 08:30:59 -07001225 unsigned ruleIndex = found.first;
1226
Ed Tanouse662eae2022-01-25 10:39:19 -08001227 if (ruleIndex == 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001228 {
Ed Tanous2634dcd2019-03-26 09:28:06 -07001229 // Check to see if this url exists at any verb
1230 for (const PerMethod& p : perMethods)
1231 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001232 const std::pair<unsigned, RoutingParams>& found2 =
Ed Tanous2634dcd2019-03-26 09:28:06 -07001233 p.trie.find(req.url);
Ed Tanous23a21a12020-07-25 04:45:05 +00001234 if (found2.first > 0)
Ed Tanous2634dcd2019-03-26 09:28:06 -07001235 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001236 asyncResp->res.result(
1237 boost::beast::http::status::method_not_allowed);
Ed Tanous2634dcd2019-03-26 09:28:06 -07001238 return;
1239 }
1240 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001241 BMCWEB_LOG_DEBUG << "Cannot match rules " << req.url;
zhanghch058d1b46d2021-04-01 11:18:24 +08001242 asyncResp->res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001243 return;
1244 }
1245
1246 if (ruleIndex >= rules.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -07001247 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001248 throw std::runtime_error("Trie internal structure corrupted!");
Ed Tanous3174e4d2020-10-07 11:41:22 -07001249 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001250
Ed Tanous271584a2019-07-09 16:24:22 -07001251 if ((rules[ruleIndex]->getMethods() &
1252 (1U << static_cast<uint32_t>(req.method()))) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001253 {
1254 BMCWEB_LOG_DEBUG << "Rule found but method mismatch: " << req.url
1255 << " with " << req.methodString() << "("
Ed Tanous271584a2019-07-09 16:24:22 -07001256 << static_cast<uint32_t>(req.method()) << ") / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001257 << rules[ruleIndex]->getMethods();
zhanghch058d1b46d2021-04-01 11:18:24 +08001258 asyncResp->res.result(
1259 boost::beast::http::status::method_not_allowed);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001260 return;
1261 }
1262
1263 BMCWEB_LOG_DEBUG << "Matched rule '" << rules[ruleIndex]->rule << "' "
Ed Tanous271584a2019-07-09 16:24:22 -07001264 << static_cast<uint32_t>(req.method()) << " / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001265 << rules[ruleIndex]->getMethods();
1266
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001267 if (req.session == nullptr)
James Feist7166bf02019-12-10 16:52:14 +00001268 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001269 rules[ruleIndex]->handle(req, asyncResp, found.second);
James Feist7166bf02019-12-10 16:52:14 +00001270 return;
1271 }
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001272
1273 crow::connections::systemBus->async_method_call(
Ed Tanous168e20c2021-12-13 14:39:53 -08001274 [&req, asyncResp, &rules, ruleIndex,
1275 found](const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -08001276 const dbus::utility::DBusPropertiesMap& userInfoMap) {
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001277 if (ec)
1278 {
1279 BMCWEB_LOG_ERROR << "GetUserInfo failed...";
zhanghch058d1b46d2021-04-01 11:18:24 +08001280 asyncResp->res.result(
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001281 boost::beast::http::status::internal_server_error);
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001282 return;
1283 }
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001284 std::string userRole{};
Ed Tanousb9d36b42022-02-26 21:42:46 -08001285 const bool* remoteUser = nullptr;
1286 std::optional<bool> passwordExpired;
1287
1288 for (const auto& userInfo : userInfoMap)
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001289 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001290 if (userInfo.first == "UserPrivilege")
1291 {
1292 const std::string* userRolePtr =
1293 std::get_if<std::string>(&userInfo.second);
1294 if (userRolePtr == nullptr)
1295 {
1296 continue;
1297 }
1298 userRole = *userRolePtr;
1299 BMCWEB_LOG_DEBUG
1300 << "userName = " << req.session->username
1301 << " userRole = " << *userRolePtr;
1302 }
1303 else if (userInfo.first == "RemoteUser")
1304 {
1305 remoteUser = std::get_if<bool>(&userInfo.second);
1306 }
1307 else if (userInfo.first == "UserPasswordExpired")
1308 {
1309 const bool* passwordExpiredPtr =
1310 std::get_if<bool>(&userInfo.second);
1311 if (passwordExpiredPtr == nullptr)
1312 {
1313 continue;
1314 }
1315 passwordExpired = *passwordExpiredPtr;
1316 }
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001317 }
1318
Ed Tanousb9d36b42022-02-26 21:42:46 -08001319 if (remoteUser == nullptr)
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001320 {
1321 BMCWEB_LOG_ERROR
1322 << "RemoteUser property missing or wrong type";
zhanghch058d1b46d2021-04-01 11:18:24 +08001323 asyncResp->res.result(
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001324 boost::beast::http::status::internal_server_error);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001325 return;
1326 }
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001327
Ed Tanousb9d36b42022-02-26 21:42:46 -08001328 if (passwordExpired == std::nullopt)
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001329 {
Ed Tanousb9d36b42022-02-26 21:42:46 -08001330 if (!*remoteUser)
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001331 {
1332 BMCWEB_LOG_ERROR
1333 << "UserPasswordExpired property is expected for"
1334 " local user but is missing or wrong type";
zhanghch058d1b46d2021-04-01 11:18:24 +08001335 asyncResp->res.result(
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001336 boost::beast::http::status::internal_server_error);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001337 return;
1338 }
Ed Tanousb9d36b42022-02-26 21:42:46 -08001339 passwordExpired = false;
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001340 }
1341
Ed Tanous23a21a12020-07-25 04:45:05 +00001342 // Get the userprivileges from the role
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001343 redfish::Privileges userPrivileges =
1344 redfish::getUserPrivileges(userRole);
1345
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001346 // Set isConfigureSelfOnly based on D-Bus results. This
1347 // ignores the results from both pamAuthenticateUser and the
1348 // value from any previous use of this session.
Ed Tanousb9d36b42022-02-26 21:42:46 -08001349 req.session->isConfigureSelfOnly = *passwordExpired;
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001350
Ed Tanous23a21a12020-07-25 04:45:05 +00001351 // Modifyprivileges if isConfigureSelfOnly.
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001352 if (req.session->isConfigureSelfOnly)
1353 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001354 // Remove allprivileges except ConfigureSelf
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001355 userPrivileges = userPrivileges.intersection(
1356 redfish::Privileges{"ConfigureSelf"});
1357 BMCWEB_LOG_DEBUG << "Operation limited to ConfigureSelf";
1358 }
1359
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001360 if (!rules[ruleIndex]->checkPrivileges(userPrivileges))
1361 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001362 asyncResp->res.result(
1363 boost::beast::http::status::forbidden);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001364 if (req.session->isConfigureSelfOnly)
1365 {
1366 redfish::messages::passwordChangeRequired(
zhanghch058d1b46d2021-04-01 11:18:24 +08001367 asyncResp->res,
Ed Tanousace85d62021-10-26 12:45:59 -07001368 crow::utility::urlFromPieces(
1369 "redfish", "v1", "AccountService", "Accounts",
1370 req.session->username));
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001371 }
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001372 return;
1373 }
1374
1375 req.userRole = userRole;
zhanghch058d1b46d2021-04-01 11:18:24 +08001376 rules[ruleIndex]->handle(req, asyncResp, found.second);
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001377 },
1378 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1379 "xyz.openbmc_project.User.Manager", "GetUserInfo",
1380 req.session->username);
Ed Tanous7045c8d2017-04-03 10:04:37 -07001381 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001382
Ed Tanous1abe55e2018-09-05 08:30:59 -07001383 void debugPrint()
1384 {
Ed Tanous271584a2019-07-09 16:24:22 -07001385 for (size_t i = 0; i < perMethods.size(); i++)
Tanousf00032d2018-11-05 01:18:10 -03001386 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001387 BMCWEB_LOG_DEBUG << boost::beast::http::to_string(
1388 static_cast<boost::beast::http::verb>(i));
Tanousf00032d2018-11-05 01:18:10 -03001389 perMethods[i].trie.debugPrint();
1390 }
Ed Tanous3dac7492017-08-02 13:46:20 -07001391 }
Ed Tanousb4a7bfa2017-04-04 17:23:00 -07001392
Ed Tanous1abe55e2018-09-05 08:30:59 -07001393 std::vector<const std::string*> getRoutes(const std::string& parent)
1394 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001395 std::vector<const std::string*> ret;
Tanousf00032d2018-11-05 01:18:10 -03001396
1397 for (const PerMethod& pm : perMethods)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001398 {
Tanousf00032d2018-11-05 01:18:10 -03001399 std::vector<unsigned> x;
1400 pm.trie.findRouteIndexes(parent, x);
1401 for (unsigned index : x)
1402 {
1403 ret.push_back(&pm.rules[index]->rule);
1404 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001405 }
1406 return ret;
1407 }
1408
1409 private:
Tanousf00032d2018-11-05 01:18:10 -03001410 struct PerMethod
1411 {
1412 std::vector<BaseRule*> rules;
1413 Trie trie;
Ed Tanous313a3c22022-03-14 09:27:38 -07001414 // rule index 0 has special meaning; preallocate it to avoid
Tanousf00032d2018-11-05 01:18:10 -03001415 // duplication.
Ed Tanous313a3c22022-03-14 09:27:38 -07001416 PerMethod() : rules(1)
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001417 {}
Tanousf00032d2018-11-05 01:18:10 -03001418 };
Ed Tanous888880a2020-08-24 13:48:50 -07001419
1420 const static size_t maxHttpVerbCount =
Ed Tanouscc090442020-10-07 08:20:50 -07001421 static_cast<size_t>(boost::beast::http::verb::unlink);
Ed Tanous888880a2020-08-24 13:48:50 -07001422
Tanousf00032d2018-11-05 01:18:10 -03001423 std::array<PerMethod, maxHttpVerbCount> perMethods;
1424 std::vector<std::unique_ptr<BaseRule>> allRules;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001425};
Ed Tanous1abe55e2018-09-05 08:30:59 -07001426} // namespace crow