blob: 8c4fa2ff313724be67f74ceff95e9f1852d16889 [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 {
Ratan Gupta02453b12019-10-22 14:43:36 +0530346 std::shared_ptr<
347 crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>>
348 myConnection = std::make_shared<
349 crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>>(
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000350 req, std::move(adaptor), openHandler, messageHandler,
Ratan Gupta02453b12019-10-22 14:43:36 +0530351 closeHandler, errorHandler);
352 myConnection->start();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700353 }
354#ifdef BMCWEB_ENABLE_SSL
Ed Tanous104f09c2022-01-25 09:56:04 -0800355 void handleUpgrade(const Request& req, Response& /*res*/,
Ed Tanousceac6f72018-12-02 11:58:47 -0800356 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>&&
357 adaptor) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700358 {
Ed Tanousceac6f72018-12-02 11:58:47 -0800359 std::shared_ptr<crow::websocket::ConnectionImpl<
360 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>>
361 myConnection = std::make_shared<crow::websocket::ConnectionImpl<
362 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>>(
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000363 req, std::move(adaptor), openHandler, messageHandler,
Ed Tanousceac6f72018-12-02 11:58:47 -0800364 closeHandler, errorHandler);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700365 myConnection->start();
366 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700367#endif
368
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500369 template <typename Func>
370 self_t& onopen(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700371 {
372 openHandler = f;
373 return *this;
374 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700375
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500376 template <typename Func>
377 self_t& onmessage(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700378 {
379 messageHandler = f;
380 return *this;
381 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700382
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500383 template <typename Func>
384 self_t& onclose(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700385 {
386 closeHandler = f;
387 return *this;
388 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700389
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500390 template <typename Func>
391 self_t& onerror(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700392 {
393 errorHandler = f;
394 return *this;
395 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700396
Ed Tanous1abe55e2018-09-05 08:30:59 -0700397 protected:
Gunnar Millsccd584f2021-11-16 11:36:33 -0600398 std::function<void(crow::websocket::Connection&,
399 std::shared_ptr<bmcweb::AsyncResp>)>
400 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 Tanous55c7b7a2018-05-22 15:27:24 -0700662const int ruleSpecialRedirectSlash = 1;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700663
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 Tanous1abe55e2018-09-05 08:30:59 -0700672 boost::container::flat_map<std::string, unsigned> children;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700673
Ed Tanous1abe55e2018-09-05 08:30:59 -0700674 bool isSimpleNode() const
675 {
Ed Tanouse662eae2022-01-25 10:39:19 -0800676 return ruleIndex == 0 &&
677 std::all_of(std::begin(paramChildrens),
678 std::end(paramChildrens),
679 [](size_t x) { return x == 0U; });
Ed Tanous7045c8d2017-04-03 10:04:37 -0700680 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700681 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700682
Ed Tanous1abe55e2018-09-05 08:30:59 -0700683 Trie() : nodes(1)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500684 {}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700685
686 private:
687 void optimizeNode(Node* node)
688 {
Ed Tanous271584a2019-07-09 16:24:22 -0700689 for (size_t x : node->paramChildrens)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700690 {
Ed Tanousdbb59d42022-01-25 11:09:55 -0800691 if (x == 0U)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700692 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700693 continue;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700694 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700695 Node* child = &nodes[x];
696 optimizeNode(child);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700697 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700698 if (node->children.empty())
Ed Tanous3174e4d2020-10-07 11:41:22 -0700699 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700700 return;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700701 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700702 bool mergeWithChild = true;
Tanousf00032d2018-11-05 01:18:10 -0300703 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700704 {
705 Node* child = &nodes[kv.second];
706 if (!child->isSimpleNode())
707 {
708 mergeWithChild = false;
709 break;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700710 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700711 }
712 if (mergeWithChild)
713 {
714 decltype(node->children) merged;
Tanousf00032d2018-11-05 01:18:10 -0300715 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700716 {
717 Node* child = &nodes[kv.second];
Tanousf00032d2018-11-05 01:18:10 -0300718 for (const std::pair<std::string, unsigned>& childKv :
719 child->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700720 {
721 merged[kv.first + childKv.first] = childKv.second;
722 }
723 }
724 node->children = std::move(merged);
725 optimizeNode(node);
726 }
727 else
728 {
Tanousf00032d2018-11-05 01:18:10 -0300729 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700730 {
731 Node* child = &nodes[kv.second];
732 optimizeNode(child);
733 }
734 }
735 }
736
737 void optimize()
738 {
739 optimizeNode(head());
740 }
741
742 public:
743 void validate()
744 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700745 optimize();
746 }
747
Ed Tanous81ce6092020-12-17 16:54:55 +0000748 void findRouteIndexes(const std::string& reqUrl,
749 std::vector<unsigned>& routeIndexes,
Tanousf00032d2018-11-05 01:18:10 -0300750 const Node* node = nullptr, unsigned pos = 0) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700751 {
752 if (node == nullptr)
753 {
754 node = head();
755 }
Tanousf00032d2018-11-05 01:18:10 -0300756 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700757 {
758 const std::string& fragment = kv.first;
759 const Node* child = &nodes[kv.second];
Ed Tanous81ce6092020-12-17 16:54:55 +0000760 if (pos >= reqUrl.size())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700761 {
762 if (child->ruleIndex != 0 && fragment != "/")
763 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000764 routeIndexes.push_back(child->ruleIndex);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700765 }
Ed Tanous81ce6092020-12-17 16:54:55 +0000766 findRouteIndexes(reqUrl, routeIndexes, child,
Ed Tanous271584a2019-07-09 16:24:22 -0700767 static_cast<unsigned>(pos + fragment.size()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700768 }
769 else
770 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000771 if (reqUrl.compare(pos, fragment.size(), fragment) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700772 {
Ed Tanous271584a2019-07-09 16:24:22 -0700773 findRouteIndexes(
Ed Tanous81ce6092020-12-17 16:54:55 +0000774 reqUrl, routeIndexes, child,
Ed Tanous271584a2019-07-09 16:24:22 -0700775 static_cast<unsigned>(pos + fragment.size()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700776 }
777 }
778 }
779 }
780
781 std::pair<unsigned, RoutingParams>
Ed Tanous81ce6092020-12-17 16:54:55 +0000782 find(const std::string_view reqUrl, const Node* node = nullptr,
Ed Tanous271584a2019-07-09 16:24:22 -0700783 size_t pos = 0, RoutingParams* params = nullptr) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700784 {
785 RoutingParams empty;
786 if (params == nullptr)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700787 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700788 params = &empty;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700789 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700790
791 unsigned found{};
792 RoutingParams matchParams;
793
794 if (node == nullptr)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700795 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700796 node = head();
Ed Tanous3174e4d2020-10-07 11:41:22 -0700797 }
Ed Tanous81ce6092020-12-17 16:54:55 +0000798 if (pos == reqUrl.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -0700799 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700800 return {node->ruleIndex, *params};
Ed Tanous3174e4d2020-10-07 11:41:22 -0700801 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700802
803 auto updateFound =
804 [&found, &matchParams](std::pair<unsigned, RoutingParams>& ret) {
Ed Tanouse662eae2022-01-25 10:39:19 -0800805 if (ret.first != 0U && (found == 0U || found > ret.first))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700806 {
807 found = ret.first;
808 matchParams = std::move(ret.second);
809 }
810 };
811
Ed Tanouse662eae2022-01-25 10:39:19 -0800812 if (node->paramChildrens[static_cast<size_t>(ParamType::INT)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700813 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000814 char c = reqUrl[pos];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700815 if ((c >= '0' && c <= '9') || c == '+' || c == '-')
816 {
Ed Tanous543f4402022-01-06 13:12:53 -0800817 char* eptr = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700818 errno = 0;
819 long long int value =
Ed Tanous81ce6092020-12-17 16:54:55 +0000820 std::strtoll(reqUrl.data() + pos, &eptr, 10);
821 if (errno != ERANGE && eptr != reqUrl.data() + pos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700822 {
823 params->intParams.push_back(value);
Ed Tanous81ce6092020-12-17 16:54:55 +0000824 std::pair<unsigned, RoutingParams> ret =
825 find(reqUrl,
826 &nodes[node->paramChildrens[static_cast<size_t>(
827 ParamType::INT)]],
828 static_cast<size_t>(eptr - reqUrl.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700829 updateFound(ret);
830 params->intParams.pop_back();
831 }
832 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700833 }
834
Ed Tanouse662eae2022-01-25 10:39:19 -0800835 if (node->paramChildrens[static_cast<size_t>(ParamType::UINT)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700836 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000837 char c = reqUrl[pos];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700838 if ((c >= '0' && c <= '9') || c == '+')
839 {
Ed Tanous543f4402022-01-06 13:12:53 -0800840 char* eptr = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700841 errno = 0;
842 unsigned long long int value =
Ed Tanous81ce6092020-12-17 16:54:55 +0000843 std::strtoull(reqUrl.data() + pos, &eptr, 10);
844 if (errno != ERANGE && eptr != reqUrl.data() + pos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700845 {
846 params->uintParams.push_back(value);
Ed Tanous81ce6092020-12-17 16:54:55 +0000847 std::pair<unsigned, RoutingParams> ret =
848 find(reqUrl,
849 &nodes[node->paramChildrens[static_cast<size_t>(
850 ParamType::UINT)]],
851 static_cast<size_t>(eptr - reqUrl.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700852 updateFound(ret);
853 params->uintParams.pop_back();
854 }
855 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700856 }
857
Ed Tanouse662eae2022-01-25 10:39:19 -0800858 if (node->paramChildrens[static_cast<size_t>(ParamType::DOUBLE)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700859 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000860 char c = reqUrl[pos];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700861 if ((c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')
862 {
Ed Tanous543f4402022-01-06 13:12:53 -0800863 char* eptr = nullptr;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700864 errno = 0;
Ed Tanous81ce6092020-12-17 16:54:55 +0000865 double value = std::strtod(reqUrl.data() + pos, &eptr);
866 if (errno != ERANGE && eptr != reqUrl.data() + pos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700867 {
868 params->doubleParams.push_back(value);
Ed Tanous81ce6092020-12-17 16:54:55 +0000869 std::pair<unsigned, RoutingParams> ret =
870 find(reqUrl,
871 &nodes[node->paramChildrens[static_cast<size_t>(
872 ParamType::DOUBLE)]],
873 static_cast<size_t>(eptr - reqUrl.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700874 updateFound(ret);
875 params->doubleParams.pop_back();
876 }
877 }
878 }
879
Ed Tanouse662eae2022-01-25 10:39:19 -0800880 if (node->paramChildrens[static_cast<size_t>(ParamType::STRING)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700881 {
Ed Tanousb01bf292019-03-25 19:25:26 +0000882 size_t epos = pos;
Ed Tanous81ce6092020-12-17 16:54:55 +0000883 for (; epos < reqUrl.size(); epos++)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700884 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000885 if (reqUrl[epos] == '/')
Ed Tanous3174e4d2020-10-07 11:41:22 -0700886 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700887 break;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700888 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700889 }
890
891 if (epos != pos)
892 {
893 params->stringParams.emplace_back(
Ed Tanous81ce6092020-12-17 16:54:55 +0000894 reqUrl.substr(pos, epos - pos));
Tanousf00032d2018-11-05 01:18:10 -0300895 std::pair<unsigned, RoutingParams> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000896 find(reqUrl,
Ed Tanous271584a2019-07-09 16:24:22 -0700897 &nodes[node->paramChildrens[static_cast<size_t>(
898 ParamType::STRING)]],
Ed Tanousb01bf292019-03-25 19:25:26 +0000899 epos, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700900 updateFound(ret);
901 params->stringParams.pop_back();
902 }
903 }
904
Ed Tanouse662eae2022-01-25 10:39:19 -0800905 if (node->paramChildrens[static_cast<size_t>(ParamType::PATH)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700906 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000907 size_t epos = reqUrl.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700908
909 if (epos != pos)
910 {
911 params->stringParams.emplace_back(
Ed Tanous81ce6092020-12-17 16:54:55 +0000912 reqUrl.substr(pos, epos - pos));
Ed Tanous271584a2019-07-09 16:24:22 -0700913 std::pair<unsigned, RoutingParams> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000914 find(reqUrl,
Ed Tanous271584a2019-07-09 16:24:22 -0700915 &nodes[node->paramChildrens[static_cast<size_t>(
916 ParamType::PATH)]],
917 epos, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700918 updateFound(ret);
919 params->stringParams.pop_back();
920 }
921 }
922
Tanousf00032d2018-11-05 01:18:10 -0300923 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700924 {
925 const std::string& fragment = kv.first;
926 const Node* child = &nodes[kv.second];
927
Ed Tanous81ce6092020-12-17 16:54:55 +0000928 if (reqUrl.compare(pos, fragment.size(), fragment) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700929 {
Tanousf00032d2018-11-05 01:18:10 -0300930 std::pair<unsigned, RoutingParams> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000931 find(reqUrl, child, pos + fragment.size(), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700932 updateFound(ret);
933 }
934 }
935
936 return {found, matchParams};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700937 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700938
939 void add(const std::string& url, unsigned ruleIndex)
940 {
Ed Tanous271584a2019-07-09 16:24:22 -0700941 size_t idx = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700942
943 for (unsigned i = 0; i < url.size(); i++)
944 {
945 char c = url[i];
946 if (c == '<')
947 {
Tanousf00032d2018-11-05 01:18:10 -0300948 const static std::array<std::pair<ParamType, std::string>, 7>
949 paramTraits = {{
950 {ParamType::INT, "<int>"},
951 {ParamType::UINT, "<uint>"},
952 {ParamType::DOUBLE, "<float>"},
953 {ParamType::DOUBLE, "<double>"},
954 {ParamType::STRING, "<str>"},
955 {ParamType::STRING, "<string>"},
956 {ParamType::PATH, "<path>"},
957 }};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700958
Tanousf00032d2018-11-05 01:18:10 -0300959 for (const std::pair<ParamType, std::string>& x : paramTraits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700960 {
Tanousf00032d2018-11-05 01:18:10 -0300961 if (url.compare(i, x.second.size(), x.second) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700962 {
Ed Tanous271584a2019-07-09 16:24:22 -0700963 size_t index = static_cast<size_t>(x.first);
Ed Tanouse662eae2022-01-25 10:39:19 -0800964 if (nodes[idx].paramChildrens[index] == 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700965 {
Tanousf00032d2018-11-05 01:18:10 -0300966 unsigned newNodeIdx = newNode();
Ed Tanous271584a2019-07-09 16:24:22 -0700967 nodes[idx].paramChildrens[index] = newNodeIdx;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700968 }
Ed Tanous271584a2019-07-09 16:24:22 -0700969 idx = nodes[idx].paramChildrens[index];
970 i += static_cast<unsigned>(x.second.size());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700971 break;
972 }
973 }
974
975 i--;
976 }
977 else
978 {
979 std::string piece(&c, 1);
Ed Tanouse662eae2022-01-25 10:39:19 -0800980 if (nodes[idx].children.count(piece) == 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700981 {
Tanousf00032d2018-11-05 01:18:10 -0300982 unsigned newNodeIdx = newNode();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700983 nodes[idx].children.emplace(piece, newNodeIdx);
984 }
985 idx = nodes[idx].children[piece];
986 }
987 }
Ed Tanouse662eae2022-01-25 10:39:19 -0800988 if (nodes[idx].ruleIndex != 0U)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700989 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700990 throw std::runtime_error("handler already exists for " + url);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700991 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700992 nodes[idx].ruleIndex = ruleIndex;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700993 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700994
Ed Tanous1abe55e2018-09-05 08:30:59 -0700995 private:
Ed Tanous271584a2019-07-09 16:24:22 -0700996 void debugNodePrint(Node* n, size_t level)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700997 {
Ed Tanous271584a2019-07-09 16:24:22 -0700998 for (size_t i = 0; i < static_cast<size_t>(ParamType::MAX); i++)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700999 {
Ed Tanouse662eae2022-01-25 10:39:19 -08001000 if (n->paramChildrens[i] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001001 {
1002 BMCWEB_LOG_DEBUG << std::string(
Ed Tanous271584a2019-07-09 16:24:22 -07001003 2U * level, ' ') /*<< "("<<n->paramChildrens[i]<<") "*/;
1004 switch (static_cast<ParamType>(i))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001005 {
1006 case ParamType::INT:
1007 BMCWEB_LOG_DEBUG << "<int>";
1008 break;
1009 case ParamType::UINT:
1010 BMCWEB_LOG_DEBUG << "<uint>";
1011 break;
1012 case ParamType::DOUBLE:
1013 BMCWEB_LOG_DEBUG << "<float>";
1014 break;
1015 case ParamType::STRING:
1016 BMCWEB_LOG_DEBUG << "<str>";
1017 break;
1018 case ParamType::PATH:
1019 BMCWEB_LOG_DEBUG << "<path>";
1020 break;
Ed Tanous23a21a12020-07-25 04:45:05 +00001021 case ParamType::MAX:
Ed Tanous1abe55e2018-09-05 08:30:59 -07001022 BMCWEB_LOG_DEBUG << "<ERROR>";
1023 break;
1024 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001025
Ed Tanous1abe55e2018-09-05 08:30:59 -07001026 debugNodePrint(&nodes[n->paramChildrens[i]], level + 1);
1027 }
1028 }
Tanousf00032d2018-11-05 01:18:10 -03001029 for (const std::pair<std::string, unsigned>& kv : n->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001030 {
1031 BMCWEB_LOG_DEBUG
Ed Tanous271584a2019-07-09 16:24:22 -07001032 << std::string(2U * level, ' ') /*<< "(" << kv.second << ") "*/
Ed Tanous1abe55e2018-09-05 08:30:59 -07001033 << kv.first;
1034 debugNodePrint(&nodes[kv.second], level + 1);
1035 }
1036 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001037
Ed Tanous1abe55e2018-09-05 08:30:59 -07001038 public:
1039 void debugPrint()
1040 {
Ed Tanous271584a2019-07-09 16:24:22 -07001041 debugNodePrint(head(), 0U);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001042 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001043
Ed Tanous1abe55e2018-09-05 08:30:59 -07001044 private:
1045 const Node* head() const
1046 {
1047 return &nodes.front();
1048 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001049
Ed Tanous1abe55e2018-09-05 08:30:59 -07001050 Node* head()
1051 {
1052 return &nodes.front();
1053 }
1054
1055 unsigned newNode()
1056 {
1057 nodes.resize(nodes.size() + 1);
Ed Tanous271584a2019-07-09 16:24:22 -07001058 return static_cast<unsigned>(nodes.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001059 }
1060
1061 std::vector<Node> nodes;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001062};
1063
Ed Tanous1abe55e2018-09-05 08:30:59 -07001064class Router
1065{
1066 public:
Ed Tanous0c0084a2019-10-24 15:57:51 -07001067 Router() = default;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001068
Ed Tanous1abe55e2018-09-05 08:30:59 -07001069 DynamicRule& newRuleDynamic(const std::string& rule)
1070 {
1071 std::unique_ptr<DynamicRule> ruleObject =
1072 std::make_unique<DynamicRule>(rule);
1073 DynamicRule* ptr = ruleObject.get();
Tanousf00032d2018-11-05 01:18:10 -03001074 allRules.emplace_back(std::move(ruleObject));
Ed Tanous7045c8d2017-04-03 10:04:37 -07001075
Ed Tanous1abe55e2018-09-05 08:30:59 -07001076 return *ptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001077 }
1078
Ed Tanous1abe55e2018-09-05 08:30:59 -07001079 template <uint64_t N>
1080 typename black_magic::Arguments<N>::type::template rebind<TaggedRule>&
1081 newRuleTagged(const std::string& rule)
1082 {
1083 using RuleT = typename black_magic::Arguments<N>::type::template rebind<
1084 TaggedRule>;
1085 std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule);
1086 RuleT* ptr = ruleObject.get();
Tanousf00032d2018-11-05 01:18:10 -03001087 allRules.emplace_back(std::move(ruleObject));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001088
1089 return *ptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001090 }
1091
Tanousf00032d2018-11-05 01:18:10 -03001092 void internalAddRuleObject(const std::string& rule, BaseRule* ruleObject)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001093 {
Tanousf00032d2018-11-05 01:18:10 -03001094 if (ruleObject == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001095 {
Tanousf00032d2018-11-05 01:18:10 -03001096 return;
1097 }
Ed Tanous888880a2020-08-24 13:48:50 -07001098 for (size_t method = 0, methodBit = 1; method < maxHttpVerbCount;
Ed Tanous2c70f802020-09-28 14:29:23 -07001099 method++, methodBit <<= 1)
Tanousf00032d2018-11-05 01:18:10 -03001100 {
Ed Tanouse662eae2022-01-25 10:39:19 -08001101 if ((ruleObject->methodsBitfield & methodBit) > 0U)
Tanousf00032d2018-11-05 01:18:10 -03001102 {
1103 perMethods[method].rules.emplace_back(ruleObject);
1104 perMethods[method].trie.add(
Ed Tanous271584a2019-07-09 16:24:22 -07001105 rule, static_cast<unsigned>(
1106 perMethods[method].rules.size() - 1U));
Tanousf00032d2018-11-05 01:18:10 -03001107 // directory case:
1108 // request to `/about' url matches `/about/' rule
1109 if (rule.size() > 2 && rule.back() == '/')
1110 {
1111 perMethods[method].trie.add(
1112 rule.substr(0, rule.size() - 1),
Ed Tanous271584a2019-07-09 16:24:22 -07001113 static_cast<unsigned>(perMethods[method].rules.size() -
1114 1));
Tanousf00032d2018-11-05 01:18:10 -03001115 }
1116 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001117 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001118 }
1119
Ed Tanous1abe55e2018-09-05 08:30:59 -07001120 void validate()
1121 {
Tanousf00032d2018-11-05 01:18:10 -03001122 for (std::unique_ptr<BaseRule>& rule : allRules)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001123 {
1124 if (rule)
1125 {
Tanousf00032d2018-11-05 01:18:10 -03001126 std::unique_ptr<BaseRule> upgraded = rule->upgrade();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001127 if (upgraded)
Ed Tanous3174e4d2020-10-07 11:41:22 -07001128 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001129 rule = std::move(upgraded);
Ed Tanous3174e4d2020-10-07 11:41:22 -07001130 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001131 rule->validate();
Tanousf00032d2018-11-05 01:18:10 -03001132 internalAddRuleObject(rule->rule, rule.get());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001133 }
1134 }
Tanousf00032d2018-11-05 01:18:10 -03001135 for (PerMethod& perMethod : perMethods)
1136 {
1137 perMethod.trie.validate();
1138 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001139 }
1140
Ed Tanous1abe55e2018-09-05 08:30:59 -07001141 template <typename Adaptor>
1142 void handleUpgrade(const Request& req, Response& res, Adaptor&& adaptor)
1143 {
Ed Tanous271584a2019-07-09 16:24:22 -07001144 if (static_cast<size_t>(req.method()) >= perMethods.size())
Ed Tanous888880a2020-08-24 13:48:50 -07001145 {
1146 res.result(boost::beast::http::status::not_found);
1147 res.end();
Tanousf00032d2018-11-05 01:18:10 -03001148 return;
Ed Tanous888880a2020-08-24 13:48:50 -07001149 }
Tanousf00032d2018-11-05 01:18:10 -03001150
Ed Tanous271584a2019-07-09 16:24:22 -07001151 PerMethod& perMethod = perMethods[static_cast<size_t>(req.method())];
Tanousf00032d2018-11-05 01:18:10 -03001152 Trie& trie = perMethod.trie;
1153 std::vector<BaseRule*>& rules = perMethod.rules;
1154
1155 const std::pair<unsigned, RoutingParams>& found = trie.find(req.url);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001156 unsigned ruleIndex = found.first;
Ed Tanouse662eae2022-01-25 10:39:19 -08001157 if (ruleIndex == 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001158 {
1159 BMCWEB_LOG_DEBUG << "Cannot match rules " << req.url;
Ed Tanousde5c9f32019-03-26 09:17:55 -07001160 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001161 res.end();
1162 return;
1163 }
1164
1165 if (ruleIndex >= rules.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -07001166 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001167 throw std::runtime_error("Trie internal structure corrupted!");
Ed Tanous3174e4d2020-10-07 11:41:22 -07001168 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001169
1170 if (ruleIndex == ruleSpecialRedirectSlash)
1171 {
1172 BMCWEB_LOG_INFO << "Redirecting to a url with trailing slash: "
1173 << req.url;
Ed Tanousde5c9f32019-03-26 09:17:55 -07001174 res.result(boost::beast::http::status::moved_permanently);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001175
1176 // TODO absolute url building
1177 if (req.getHeaderValue("Host").empty())
1178 {
1179 res.addHeader("Location", std::string(req.url) + "/");
1180 }
1181 else
1182 {
1183 res.addHeader(
1184 "Location",
1185 req.isSecure
1186 ? "https://"
1187 : "http://" + std::string(req.getHeaderValue("Host")) +
1188 std::string(req.url) + "/");
1189 }
1190 res.end();
1191 return;
1192 }
1193
Ed Tanous271584a2019-07-09 16:24:22 -07001194 if ((rules[ruleIndex]->getMethods() &
1195 (1U << static_cast<size_t>(req.method()))) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001196 {
1197 BMCWEB_LOG_DEBUG << "Rule found but method mismatch: " << req.url
1198 << " with " << req.methodString() << "("
Ed Tanous271584a2019-07-09 16:24:22 -07001199 << static_cast<uint32_t>(req.method()) << ") / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001200 << rules[ruleIndex]->getMethods();
Ed Tanousde5c9f32019-03-26 09:17:55 -07001201 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001202 res.end();
1203 return;
1204 }
1205
1206 BMCWEB_LOG_DEBUG << "Matched rule (upgrade) '" << rules[ruleIndex]->rule
Ed Tanous271584a2019-07-09 16:24:22 -07001207 << "' " << static_cast<uint32_t>(req.method()) << " / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001208 << rules[ruleIndex]->getMethods();
1209
1210 // any uncaught exceptions become 500s
1211 try
1212 {
Ed Tanousf94c4ec2022-01-06 12:44:41 -08001213 rules[ruleIndex]->handleUpgrade(req, res,
1214 std::forward<Adaptor>(adaptor));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001215 }
Patrick Williamsc5967042021-10-06 12:39:54 -05001216 catch (const std::exception& e)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001217 {
1218 BMCWEB_LOG_ERROR << "An uncaught exception occurred: " << e.what();
Ed Tanousde5c9f32019-03-26 09:17:55 -07001219 res.result(boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001220 res.end();
1221 return;
1222 }
1223 catch (...)
1224 {
1225 BMCWEB_LOG_ERROR
1226 << "An uncaught exception occurred. The type was unknown "
1227 "so no information was available.";
Ed Tanousde5c9f32019-03-26 09:17:55 -07001228 res.result(boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001229 res.end();
1230 return;
1231 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001232 }
1233
zhanghch058d1b46d2021-04-01 11:18:24 +08001234 void handle(Request& req,
1235 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001236 {
Ed Tanous271584a2019-07-09 16:24:22 -07001237 if (static_cast<size_t>(req.method()) >= perMethods.size())
Ed Tanous888880a2020-08-24 13:48:50 -07001238 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001239 asyncResp->res.result(boost::beast::http::status::not_found);
Tanousf00032d2018-11-05 01:18:10 -03001240 return;
Ed Tanous888880a2020-08-24 13:48:50 -07001241 }
Ed Tanous271584a2019-07-09 16:24:22 -07001242 PerMethod& perMethod = perMethods[static_cast<size_t>(req.method())];
Tanousf00032d2018-11-05 01:18:10 -03001243 Trie& trie = perMethod.trie;
1244 std::vector<BaseRule*>& rules = perMethod.rules;
1245
1246 const std::pair<unsigned, RoutingParams>& found = trie.find(req.url);
Ed Tanous7045c8d2017-04-03 10:04:37 -07001247
Ed Tanous1abe55e2018-09-05 08:30:59 -07001248 unsigned ruleIndex = found.first;
1249
Ed Tanouse662eae2022-01-25 10:39:19 -08001250 if (ruleIndex == 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001251 {
Ed Tanous2634dcd2019-03-26 09:28:06 -07001252 // Check to see if this url exists at any verb
1253 for (const PerMethod& p : perMethods)
1254 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001255 const std::pair<unsigned, RoutingParams>& found2 =
Ed Tanous2634dcd2019-03-26 09:28:06 -07001256 p.trie.find(req.url);
Ed Tanous23a21a12020-07-25 04:45:05 +00001257 if (found2.first > 0)
Ed Tanous2634dcd2019-03-26 09:28:06 -07001258 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001259 asyncResp->res.result(
1260 boost::beast::http::status::method_not_allowed);
Ed Tanous2634dcd2019-03-26 09:28:06 -07001261 return;
1262 }
1263 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001264 BMCWEB_LOG_DEBUG << "Cannot match rules " << req.url;
zhanghch058d1b46d2021-04-01 11:18:24 +08001265 asyncResp->res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001266 return;
1267 }
1268
1269 if (ruleIndex >= rules.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -07001270 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001271 throw std::runtime_error("Trie internal structure corrupted!");
Ed Tanous3174e4d2020-10-07 11:41:22 -07001272 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001273
1274 if (ruleIndex == ruleSpecialRedirectSlash)
1275 {
1276 BMCWEB_LOG_INFO << "Redirecting to a url with trailing slash: "
1277 << req.url;
zhanghch058d1b46d2021-04-01 11:18:24 +08001278 asyncResp->res.result(
1279 boost::beast::http::status::moved_permanently);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001280
1281 // TODO absolute url building
1282 if (req.getHeaderValue("Host").empty())
1283 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001284 asyncResp->res.addHeader("Location",
1285 std::string(req.url) + "/");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001286 }
1287 else
1288 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001289 asyncResp->res.addHeader(
1290 "Location", (req.isSecure ? "https://" : "http://") +
1291 std::string(req.getHeaderValue("Host")) +
1292 std::string(req.url) + "/");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001293 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001294 return;
1295 }
1296
Ed Tanous271584a2019-07-09 16:24:22 -07001297 if ((rules[ruleIndex]->getMethods() &
1298 (1U << static_cast<uint32_t>(req.method()))) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001299 {
1300 BMCWEB_LOG_DEBUG << "Rule found but method mismatch: " << req.url
1301 << " with " << req.methodString() << "("
Ed Tanous271584a2019-07-09 16:24:22 -07001302 << static_cast<uint32_t>(req.method()) << ") / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001303 << rules[ruleIndex]->getMethods();
zhanghch058d1b46d2021-04-01 11:18:24 +08001304 asyncResp->res.result(
1305 boost::beast::http::status::method_not_allowed);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001306 return;
1307 }
1308
1309 BMCWEB_LOG_DEBUG << "Matched rule '" << rules[ruleIndex]->rule << "' "
Ed Tanous271584a2019-07-09 16:24:22 -07001310 << static_cast<uint32_t>(req.method()) << " / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001311 << rules[ruleIndex]->getMethods();
1312
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001313 if (req.session == nullptr)
James Feist7166bf02019-12-10 16:52:14 +00001314 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001315 rules[ruleIndex]->handle(req, asyncResp, found.second);
James Feist7166bf02019-12-10 16:52:14 +00001316 return;
1317 }
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001318
1319 crow::connections::systemBus->async_method_call(
Ed Tanous168e20c2021-12-13 14:39:53 -08001320 [&req, asyncResp, &rules, ruleIndex,
1321 found](const boost::system::error_code ec,
1322 const std::map<std::string, dbus::utility::DbusVariantType>&
1323 userInfo) {
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001324 if (ec)
1325 {
1326 BMCWEB_LOG_ERROR << "GetUserInfo failed...";
zhanghch058d1b46d2021-04-01 11:18:24 +08001327 asyncResp->res.result(
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001328 boost::beast::http::status::internal_server_error);
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001329 return;
1330 }
1331
1332 const std::string* userRolePtr = nullptr;
1333 auto userInfoIter = userInfo.find("UserPrivilege");
1334 if (userInfoIter != userInfo.end())
1335 {
1336 userRolePtr =
1337 std::get_if<std::string>(&userInfoIter->second);
1338 }
1339
1340 std::string userRole{};
1341 if (userRolePtr != nullptr)
1342 {
1343 userRole = *userRolePtr;
1344 BMCWEB_LOG_DEBUG << "userName = " << req.session->username
1345 << " userRole = " << *userRolePtr;
1346 }
1347
Ed Tanous5dc924d2021-12-20 09:17:28 -08001348 const bool* remoteUserPtr = nullptr;
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001349 auto remoteUserIter = userInfo.find("RemoteUser");
1350 if (remoteUserIter != userInfo.end())
1351 {
1352 remoteUserPtr = std::get_if<bool>(&remoteUserIter->second);
1353 }
1354 if (remoteUserPtr == nullptr)
1355 {
1356 BMCWEB_LOG_ERROR
1357 << "RemoteUser property missing or wrong type";
zhanghch058d1b46d2021-04-01 11:18:24 +08001358 asyncResp->res.result(
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001359 boost::beast::http::status::internal_server_error);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001360 return;
1361 }
1362 bool remoteUser = *remoteUserPtr;
1363
1364 bool passwordExpired = false; // default for remote user
1365 if (!remoteUser)
1366 {
Ed Tanous5dc924d2021-12-20 09:17:28 -08001367 const bool* passwordExpiredPtr = nullptr;
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001368 auto passwordExpiredIter =
1369 userInfo.find("UserPasswordExpired");
1370 if (passwordExpiredIter != userInfo.end())
1371 {
1372 passwordExpiredPtr =
1373 std::get_if<bool>(&passwordExpiredIter->second);
1374 }
1375 if (passwordExpiredPtr != nullptr)
1376 {
1377 passwordExpired = *passwordExpiredPtr;
1378 }
1379 else
1380 {
1381 BMCWEB_LOG_ERROR
1382 << "UserPasswordExpired property is expected for"
1383 " local user but is missing or wrong type";
zhanghch058d1b46d2021-04-01 11:18:24 +08001384 asyncResp->res.result(
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001385 boost::beast::http::status::internal_server_error);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001386 return;
1387 }
1388 }
1389
Ed Tanous23a21a12020-07-25 04:45:05 +00001390 // Get the userprivileges from the role
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001391 redfish::Privileges userPrivileges =
1392 redfish::getUserPrivileges(userRole);
1393
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001394 // Set isConfigureSelfOnly based on D-Bus results. This
1395 // ignores the results from both pamAuthenticateUser and the
1396 // value from any previous use of this session.
1397 req.session->isConfigureSelfOnly = passwordExpired;
1398
Ed Tanous23a21a12020-07-25 04:45:05 +00001399 // Modifyprivileges if isConfigureSelfOnly.
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001400 if (req.session->isConfigureSelfOnly)
1401 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001402 // Remove allprivileges except ConfigureSelf
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001403 userPrivileges = userPrivileges.intersection(
1404 redfish::Privileges{"ConfigureSelf"});
1405 BMCWEB_LOG_DEBUG << "Operation limited to ConfigureSelf";
1406 }
1407
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001408 if (!rules[ruleIndex]->checkPrivileges(userPrivileges))
1409 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001410 asyncResp->res.result(
1411 boost::beast::http::status::forbidden);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001412 if (req.session->isConfigureSelfOnly)
1413 {
1414 redfish::messages::passwordChangeRequired(
zhanghch058d1b46d2021-04-01 11:18:24 +08001415 asyncResp->res,
1416 "/redfish/v1/AccountService/Accounts/" +
1417 req.session->username);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001418 }
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001419 return;
1420 }
1421
1422 req.userRole = userRole;
zhanghch058d1b46d2021-04-01 11:18:24 +08001423 rules[ruleIndex]->handle(req, asyncResp, found.second);
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001424 },
1425 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1426 "xyz.openbmc_project.User.Manager", "GetUserInfo",
1427 req.session->username);
Ed Tanous7045c8d2017-04-03 10:04:37 -07001428 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001429
Ed Tanous1abe55e2018-09-05 08:30:59 -07001430 void debugPrint()
1431 {
Ed Tanous271584a2019-07-09 16:24:22 -07001432 for (size_t i = 0; i < perMethods.size(); i++)
Tanousf00032d2018-11-05 01:18:10 -03001433 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001434 BMCWEB_LOG_DEBUG << boost::beast::http::to_string(
1435 static_cast<boost::beast::http::verb>(i));
Tanousf00032d2018-11-05 01:18:10 -03001436 perMethods[i].trie.debugPrint();
1437 }
Ed Tanous3dac7492017-08-02 13:46:20 -07001438 }
Ed Tanousb4a7bfa2017-04-04 17:23:00 -07001439
Ed Tanous1abe55e2018-09-05 08:30:59 -07001440 std::vector<const std::string*> getRoutes(const std::string& parent)
1441 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001442 std::vector<const std::string*> ret;
Tanousf00032d2018-11-05 01:18:10 -03001443
1444 for (const PerMethod& pm : perMethods)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001445 {
Tanousf00032d2018-11-05 01:18:10 -03001446 std::vector<unsigned> x;
1447 pm.trie.findRouteIndexes(parent, x);
1448 for (unsigned index : x)
1449 {
1450 ret.push_back(&pm.rules[index]->rule);
1451 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001452 }
1453 return ret;
1454 }
1455
1456 private:
Tanousf00032d2018-11-05 01:18:10 -03001457 struct PerMethod
1458 {
1459 std::vector<BaseRule*> rules;
1460 Trie trie;
1461 // rule index 0, 1 has special meaning; preallocate it to avoid
1462 // duplication.
1463 PerMethod() : rules(2)
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001464 {}
Tanousf00032d2018-11-05 01:18:10 -03001465 };
Ed Tanous888880a2020-08-24 13:48:50 -07001466
1467 const static size_t maxHttpVerbCount =
Ed Tanouscc090442020-10-07 08:20:50 -07001468 static_cast<size_t>(boost::beast::http::verb::unlink);
Ed Tanous888880a2020-08-24 13:48:50 -07001469
Tanousf00032d2018-11-05 01:18:10 -03001470 std::array<PerMethod, maxHttpVerbCount> perMethods;
1471 std::vector<std::unique_ptr<BaseRule>> allRules;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001472};
Ed Tanous1abe55e2018-09-05 08:30:59 -07001473} // namespace crow