blob: af6269e233b5b95fb17279a64feba4bb3b976700 [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"
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06004#include "error_messages.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -07005#include "http_request.hpp"
6#include "http_response.hpp"
7#include "logging.hpp"
Tanousf00032d2018-11-05 01:18:10 -03008#include "privileges.hpp"
Ratan Gupta6f359562019-04-03 10:39:08 +05309#include "sessions.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -070010#include "utility.hpp"
11#include "websocket.hpp"
Ed Tanous1abe55e2018-09-05 08:30:59 -070012
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +020013#include <async_resp.hpp>
Tanousf00032d2018-11-05 01:18:10 -030014#include <boost/container/flat_map.hpp>
15#include <boost/container/small_vector.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070016#include <boost/lexical_cast.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050017
Ed Tanouse0d918b2018-03-27 17:41:04 -070018#include <cerrno>
Ed Tanous7045c8d2017-04-03 10:04:37 -070019#include <cstdint>
Ed Tanouse0d918b2018-03-27 17:41:04 -070020#include <cstdlib>
Ed Tanous3dac7492017-08-02 13:46:20 -070021#include <limits>
Ed Tanous7045c8d2017-04-03 10:04:37 -070022#include <memory>
23#include <tuple>
Ed Tanous7045c8d2017-04-03 10:04:37 -070024#include <utility>
25#include <vector>
Ed Tanous9140a672017-04-24 17:01:32 -070026
Ed Tanous1abe55e2018-09-05 08:30:59 -070027namespace crow
28{
Tanousf00032d2018-11-05 01:18:10 -030029
Ed Tanous1abe55e2018-09-05 08:30:59 -070030class BaseRule
31{
32 public:
Ed Tanousf23b7292020-10-15 09:41:17 -070033 BaseRule(const std::string& thisRule) : rule(thisRule)
Gunnar Mills1214b7e2020-06-04 10:11:30 -050034 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -070035
Ed Tanous0c0084a2019-10-24 15:57:51 -070036 virtual ~BaseRule() = default;
Ed Tanous7045c8d2017-04-03 10:04:37 -070037
Ed Tanous1abe55e2018-09-05 08:30:59 -070038 virtual void validate() = 0;
39 std::unique_ptr<BaseRule> upgrade()
40 {
41 if (ruleToUpgrade)
Ed Tanous3174e4d2020-10-07 11:41:22 -070042 {
Ed Tanous1abe55e2018-09-05 08:30:59 -070043 return std::move(ruleToUpgrade);
Ed Tanous3174e4d2020-10-07 11:41:22 -070044 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070045 return {};
46 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070047
zhanghch058d1b46d2021-04-01 11:18:24 +080048 virtual void handle(const Request&,
49 const std::shared_ptr<bmcweb::AsyncResp>&,
50 const RoutingParams&) = 0;
Ed Tanousceac6f72018-12-02 11:58:47 -080051 virtual void handleUpgrade(const Request&, Response& res,
52 boost::asio::ip::tcp::socket&&)
Ed Tanous1abe55e2018-09-05 08:30:59 -070053 {
Ed Tanousde5c9f32019-03-26 09:17:55 -070054 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -070055 res.end();
56 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -070057#ifdef BMCWEB_ENABLE_SSL
Ed Tanousceac6f72018-12-02 11:58:47 -080058 virtual void
59 handleUpgrade(const Request&, Response& res,
60 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>&&)
Ed Tanous1abe55e2018-09-05 08:30:59 -070061 {
Ed Tanousde5c9f32019-03-26 09:17:55 -070062 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -070063 res.end();
64 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070065#endif
66
Ed Tanous271584a2019-07-09 16:24:22 -070067 size_t getMethods()
Ed Tanous1abe55e2018-09-05 08:30:59 -070068 {
69 return methodsBitfield;
70 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070071
Tanousf00032d2018-11-05 01:18:10 -030072 bool checkPrivileges(const redfish::Privileges& userPrivileges)
73 {
74 // If there are no privileges assigned, assume no privileges
75 // required
76 if (privilegesSet.empty())
77 {
78 return true;
79 }
80
81 for (const redfish::Privileges& requiredPrivileges : privilegesSet)
82 {
83 if (userPrivileges.isSupersetOf(requiredPrivileges))
84 {
85 return true;
86 }
87 }
88 return false;
89 }
90
Ed Tanous271584a2019-07-09 16:24:22 -070091 size_t methodsBitfield{
92 1 << static_cast<size_t>(boost::beast::http::verb::get)};
Ed Tanous7045c8d2017-04-03 10:04:37 -070093
Tanousf00032d2018-11-05 01:18:10 -030094 std::vector<redfish::Privileges> privilegesSet;
95
Ed Tanous1abe55e2018-09-05 08:30:59 -070096 std::string rule;
97 std::string nameStr;
Ed Tanous7045c8d2017-04-03 10:04:37 -070098
Ed Tanous1abe55e2018-09-05 08:30:59 -070099 std::unique_ptr<BaseRule> ruleToUpgrade;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700100
Ed Tanous1abe55e2018-09-05 08:30:59 -0700101 friend class Router;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500102 template <typename T>
103 friend struct RuleParameterTraits;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700104};
105
Ed Tanous1abe55e2018-09-05 08:30:59 -0700106namespace detail
107{
108namespace routing_handler_call_helper
109{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500110template <typename T, int Pos>
111struct CallPair
Ed Tanous1abe55e2018-09-05 08:30:59 -0700112{
113 using type = T;
114 static const int pos = Pos;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700115};
116
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500117template <typename H1>
118struct CallParams
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119{
120 H1& handler;
121 const RoutingParams& params;
122 const Request& req;
zhanghch058d1b46d2021-04-01 11:18:24 +0800123 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700124};
125
126template <typename F, int NInt, int NUint, int NDouble, int NString,
127 typename S1, typename S2>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700128struct Call
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500129{};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700130
131template <typename F, int NInt, int NUint, int NDouble, int NString,
132 typename... Args1, typename... Args2>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700133struct Call<F, NInt, NUint, NDouble, NString, black_magic::S<int64_t, Args1...>,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700134 black_magic::S<Args2...>>
135{
136 void operator()(F cparams)
137 {
138 using pushed = typename black_magic::S<Args2...>::template push_back<
139 CallPair<int64_t, NInt>>;
140 Call<F, NInt + 1, NUint, NDouble, NString, black_magic::S<Args1...>,
141 pushed>()(cparams);
142 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700143};
144
145template <typename F, int NInt, int NUint, int NDouble, int NString,
146 typename... Args1, typename... Args2>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700147struct Call<F, NInt, NUint, NDouble, NString,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700148 black_magic::S<uint64_t, Args1...>, black_magic::S<Args2...>>
149{
150 void operator()(F cparams)
151 {
152 using pushed = typename black_magic::S<Args2...>::template push_back<
153 CallPair<uint64_t, NUint>>;
154 Call<F, NInt, NUint + 1, NDouble, NString, black_magic::S<Args1...>,
155 pushed>()(cparams);
156 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700157};
158
159template <typename F, int NInt, int NUint, int NDouble, int NString,
160 typename... Args1, typename... Args2>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700161struct Call<F, NInt, NUint, NDouble, NString, black_magic::S<double, Args1...>,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700162 black_magic::S<Args2...>>
163{
164 void operator()(F cparams)
165 {
166 using pushed = typename black_magic::S<Args2...>::template push_back<
167 CallPair<double, NDouble>>;
168 Call<F, NInt, NUint, NDouble + 1, NString, black_magic::S<Args1...>,
169 pushed>()(cparams);
170 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700171};
172
173template <typename F, int NInt, int NUint, int NDouble, int NString,
174 typename... Args1, typename... Args2>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700175struct Call<F, NInt, NUint, NDouble, NString,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700176 black_magic::S<std::string, Args1...>, black_magic::S<Args2...>>
177{
178 void operator()(F cparams)
179 {
180 using pushed = typename black_magic::S<Args2...>::template push_back<
181 CallPair<std::string, NString>>;
182 Call<F, NInt, NUint, NDouble, NString + 1, black_magic::S<Args1...>,
183 pushed>()(cparams);
184 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700185};
186
187template <typename F, int NInt, int NUint, int NDouble, int NString,
188 typename... Args1>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700189struct Call<F, NInt, NUint, NDouble, NString, black_magic::S<>,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700190 black_magic::S<Args1...>>
191{
192 void operator()(F cparams)
193 {
194 cparams.handler(
zhanghch058d1b46d2021-04-01 11:18:24 +0800195 cparams.req, cparams.asyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700196 cparams.params.template get<typename Args1::type>(Args1::pos)...);
197 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700198};
199
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500200template <typename Func, typename... ArgsWrapped>
201struct Wrapped
Ed Tanous1abe55e2018-09-05 08:30:59 -0700202{
203 template <typename... Args>
204 void set(
205 Func f,
206 typename std::enable_if<
207 !std::is_same<
208 typename std::tuple_element<0, std::tuple<Args..., void>>::type,
209 const Request&>::value,
210 int>::type = 0)
211 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800212 handler = [f = std::move(f)](
213 const Request&,
214 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
215 Args... args) { asyncResp->res.result(f(args...)); };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700216 }
217
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500218 template <typename Req, typename... Args>
219 struct ReqHandlerWrapper
Ed Tanous1abe55e2018-09-05 08:30:59 -0700220 {
Ed Tanous23a21a12020-07-25 04:45:05 +0000221 ReqHandlerWrapper(Func fIn) : f(std::move(fIn))
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500222 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700223
zhanghch058d1b46d2021-04-01 11:18:24 +0800224 void operator()(const Request& req,
225 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
226 Args... args)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700227 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800228 asyncResp->res.result(f(req, args...));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700229 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700230
Ed Tanous1abe55e2018-09-05 08:30:59 -0700231 Func f;
232 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700233
Ed Tanous1abe55e2018-09-05 08:30:59 -0700234 template <typename... Args>
235 void set(
236 Func f,
237 typename std::enable_if<
238 std::is_same<
239 typename std::tuple_element<0, std::tuple<Args..., void>>::type,
240 const Request&>::value &&
241 !std::is_same<typename std::tuple_element<
242 1, std::tuple<Args..., void, void>>::type,
zhanghch058d1b46d2021-04-01 11:18:24 +0800243 const std::shared_ptr<bmcweb::AsyncResp>&>::value,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700244 int>::type = 0)
245 {
246 handler = ReqHandlerWrapper<Args...>(std::move(f));
247 /*handler = (
248 [f = std::move(f)]
249 (const Request& req, Response& res, Args... args){
Ed Tanousde5c9f32019-03-26 09:17:55 -0700250 res.result(f(req, args...));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700251 res.end();
252 });*/
253 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700254
Ed Tanous1abe55e2018-09-05 08:30:59 -0700255 template <typename... Args>
256 void set(
257 Func f,
258 typename std::enable_if<
259 std::is_same<
260 typename std::tuple_element<0, std::tuple<Args..., void>>::type,
261 const Request&>::value &&
262 std::is_same<typename std::tuple_element<
263 1, std::tuple<Args..., void, void>>::type,
zhanghch058d1b46d2021-04-01 11:18:24 +0800264 const std::shared_ptr<bmcweb::AsyncResp>&>::value,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700265 int>::type = 0)
266 {
267 handler = std::move(f);
268 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700269
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500270 template <typename... Args>
271 struct HandlerTypeHelper
Ed Tanous1abe55e2018-09-05 08:30:59 -0700272 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800273 using type = std::function<void(
274 const crow::Request&, const std::shared_ptr<bmcweb::AsyncResp>&,
275 Args...)>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700276 using args_type =
Ed Tanous988403c2020-08-24 11:29:49 -0700277 black_magic::S<typename black_magic::PromoteT<Args>...>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700278 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700279
Ed Tanous1abe55e2018-09-05 08:30:59 -0700280 template <typename... Args>
281 struct HandlerTypeHelper<const Request&, Args...>
282 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800283 using type = std::function<void(
284 const crow::Request&, const std::shared_ptr<bmcweb::AsyncResp>&,
285 Args...)>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700286 using args_type =
Ed Tanous988403c2020-08-24 11:29:49 -0700287 black_magic::S<typename black_magic::PromoteT<Args>...>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700288 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700289
Ed Tanous1abe55e2018-09-05 08:30:59 -0700290 template <typename... Args>
zhanghch058d1b46d2021-04-01 11:18:24 +0800291 struct HandlerTypeHelper<const Request&,
292 const std::shared_ptr<bmcweb::AsyncResp>&, Args...>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700293 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800294 using type = std::function<void(
295 const crow::Request&, const std::shared_ptr<bmcweb::AsyncResp>&,
296 Args...)>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700297 using args_type =
Ed Tanous988403c2020-08-24 11:29:49 -0700298 black_magic::S<typename black_magic::PromoteT<Args>...>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700299 };
300
301 typename HandlerTypeHelper<ArgsWrapped...>::type handler;
302
zhanghch058d1b46d2021-04-01 11:18:24 +0800303 void operator()(const Request& req,
304 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700305 const RoutingParams& params)
306 {
307 detail::routing_handler_call_helper::Call<
308 detail::routing_handler_call_helper::CallParams<decltype(handler)>,
309 0, 0, 0, 0, typename HandlerTypeHelper<ArgsWrapped...>::args_type,
310 black_magic::S<>>()(
311 detail::routing_handler_call_helper::CallParams<decltype(handler)>{
zhanghch058d1b46d2021-04-01 11:18:24 +0800312 handler, params, req, asyncResp});
Ed Tanous1abe55e2018-09-05 08:30:59 -0700313 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700314};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700315} // namespace routing_handler_call_helper
316} // namespace detail
Ed Tanous7045c8d2017-04-03 10:04:37 -0700317
Ed Tanous1abe55e2018-09-05 08:30:59 -0700318class WebSocketRule : public BaseRule
319{
320 using self_t = WebSocketRule;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700321
Ed Tanous1abe55e2018-09-05 08:30:59 -0700322 public:
Ed Tanousf23b7292020-10-15 09:41:17 -0700323 WebSocketRule(const std::string& ruleIn) : BaseRule(ruleIn)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500324 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700325
Ed Tanous1abe55e2018-09-05 08:30:59 -0700326 void validate() override
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500327 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700328
zhanghch058d1b46d2021-04-01 11:18:24 +0800329 void handle(const Request&,
330 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
331 const RoutingParams&) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700332 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800333 asyncResp->res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700334 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700335
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000336 void handleUpgrade(const Request& req, Response&,
Ed Tanousceac6f72018-12-02 11:58:47 -0800337 boost::asio::ip::tcp::socket&& adaptor) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700338 {
Ratan Gupta02453b12019-10-22 14:43:36 +0530339 std::shared_ptr<
340 crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>>
341 myConnection = std::make_shared<
342 crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>>(
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000343 req, std::move(adaptor), openHandler, messageHandler,
Ratan Gupta02453b12019-10-22 14:43:36 +0530344 closeHandler, errorHandler);
345 myConnection->start();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700346 }
347#ifdef BMCWEB_ENABLE_SSL
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000348 void handleUpgrade(const Request& req, Response&,
Ed Tanousceac6f72018-12-02 11:58:47 -0800349 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>&&
350 adaptor) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700351 {
Ed Tanousceac6f72018-12-02 11:58:47 -0800352 std::shared_ptr<crow::websocket::ConnectionImpl<
353 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>>
354 myConnection = std::make_shared<crow::websocket::ConnectionImpl<
355 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>>(
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000356 req, std::move(adaptor), openHandler, messageHandler,
Ed Tanousceac6f72018-12-02 11:58:47 -0800357 closeHandler, errorHandler);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700358 myConnection->start();
359 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700360#endif
361
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500362 template <typename Func>
363 self_t& onopen(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700364 {
365 openHandler = f;
366 return *this;
367 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700368
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500369 template <typename Func>
370 self_t& onmessage(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700371 {
372 messageHandler = 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& onclose(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700378 {
379 closeHandler = 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& onerror(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700385 {
386 errorHandler = f;
387 return *this;
388 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700389
Ed Tanous1abe55e2018-09-05 08:30:59 -0700390 protected:
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200391 std::function<void(crow::websocket::Connection&,
392 std::shared_ptr<bmcweb::AsyncResp>)>
393 openHandler;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700394 std::function<void(crow::websocket::Connection&, const std::string&, bool)>
395 messageHandler;
396 std::function<void(crow::websocket::Connection&, const std::string&)>
397 closeHandler;
398 std::function<void(crow::websocket::Connection&)> errorHandler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700399};
400
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500401template <typename T>
402struct RuleParameterTraits
Ed Tanous1abe55e2018-09-05 08:30:59 -0700403{
404 using self_t = T;
405 WebSocketRule& websocket()
406 {
Ed Tanous271584a2019-07-09 16:24:22 -0700407 self_t* self = static_cast<self_t*>(this);
408 WebSocketRule* p = new WebSocketRule(self->rule);
409 self->ruleToUpgrade.reset(p);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700410 return *p;
411 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700412
Ed Tanousf23b7292020-10-15 09:41:17 -0700413 self_t& name(const std::string_view name) noexcept
Ed Tanous1abe55e2018-09-05 08:30:59 -0700414 {
Ed Tanous271584a2019-07-09 16:24:22 -0700415 self_t* self = static_cast<self_t*>(this);
Ed Tanousf23b7292020-10-15 09:41:17 -0700416 self->nameStr = name;
Ed Tanous271584a2019-07-09 16:24:22 -0700417 return *self;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700418 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700419
Ed Tanous1abe55e2018-09-05 08:30:59 -0700420 self_t& methods(boost::beast::http::verb method)
421 {
Ed Tanous271584a2019-07-09 16:24:22 -0700422 self_t* self = static_cast<self_t*>(this);
423 self->methodsBitfield = 1U << static_cast<size_t>(method);
424 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 template <typename... MethodArgs>
Ed Tanous81ce6092020-12-17 16:54:55 +0000428 self_t& methods(boost::beast::http::verb method, MethodArgs... argsMethod)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700429 {
Ed Tanous271584a2019-07-09 16:24:22 -0700430 self_t* self = static_cast<self_t*>(this);
Ed Tanous81ce6092020-12-17 16:54:55 +0000431 methods(argsMethod...);
Ed Tanous271584a2019-07-09 16:24:22 -0700432 self->methodsBitfield |= 1U << static_cast<size_t>(method);
433 return *self;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700434 }
Tanousf00032d2018-11-05 01:18:10 -0300435
Ed Tanous432a8902021-06-14 15:28:56 -0700436 self_t& privileges(
437 const std::initializer_list<std::initializer_list<const char*>>& p)
Tanousf00032d2018-11-05 01:18:10 -0300438 {
Ed Tanous271584a2019-07-09 16:24:22 -0700439 self_t* self = static_cast<self_t*>(this);
Ed Tanous432a8902021-06-14 15:28:56 -0700440 for (const std::initializer_list<const char*>& privilege : p)
Tanousf00032d2018-11-05 01:18:10 -0300441 {
Ed Tanous271584a2019-07-09 16:24:22 -0700442 self->privilegesSet.emplace_back(privilege);
Tanousf00032d2018-11-05 01:18:10 -0300443 }
Ed Tanous271584a2019-07-09 16:24:22 -0700444 return *self;
Tanousf00032d2018-11-05 01:18:10 -0300445 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700446};
447
Ed Tanous1abe55e2018-09-05 08:30:59 -0700448class DynamicRule : public BaseRule, public RuleParameterTraits<DynamicRule>
449{
450 public:
Ed Tanousf23b7292020-10-15 09:41:17 -0700451 DynamicRule(const std::string& ruleIn) : BaseRule(ruleIn)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500452 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700453
Ed Tanous1abe55e2018-09-05 08:30:59 -0700454 void validate() override
455 {
456 if (!erasedHandler)
457 {
458 throw std::runtime_error(nameStr + (!nameStr.empty() ? ": " : "") +
459 "no handler for url " + rule);
460 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700461 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700462
zhanghch058d1b46d2021-04-01 11:18:24 +0800463 void handle(const Request& req,
464 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700465 const RoutingParams& params) override
466 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800467 erasedHandler(req, asyncResp, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700468 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700469
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500470 template <typename Func>
471 void operator()(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700472 {
473 using function_t = utility::function_traits<Func>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700474 erasedHandler =
Ed Tanous988403c2020-08-24 11:29:49 -0700475 wrap(std::move(f),
476 std::make_integer_sequence<unsigned, function_t::arity>{});
Ed Tanous1abe55e2018-09-05 08:30:59 -0700477 }
478
479 // enable_if Arg1 == request && Arg2 == Response
Gunnar Mills6be0e402020-07-08 13:21:51 -0500480 // enable_if Arg1 == request && Arg2 != response
Ed Tanous1abe55e2018-09-05 08:30:59 -0700481 // enable_if Arg1 != request
482
483 template <typename Func, unsigned... Indices>
zhanghch058d1b46d2021-04-01 11:18:24 +0800484 std::function<void(const Request&,
485 const std::shared_ptr<bmcweb::AsyncResp>&,
486 const RoutingParams&)>
Ed Tanous988403c2020-08-24 11:29:49 -0700487 wrap(Func f, std::integer_sequence<unsigned, Indices...>)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700488 {
Ed Tanous988403c2020-08-24 11:29:49 -0700489 using function_t = crow::utility::function_traits<Func>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700490
491 if (!black_magic::isParameterTagCompatible(
Ed Tanous988403c2020-08-24 11:29:49 -0700492 black_magic::getParameterTag(rule.c_str()),
493 black_magic::computeParameterTagFromArgsList<
Ed Tanous1abe55e2018-09-05 08:30:59 -0700494 typename function_t::template arg<Indices>...>::value))
495 {
496 throw std::runtime_error("routeDynamic: Handler type is mismatched "
497 "with URL parameters: " +
498 rule);
499 }
500 auto ret = detail::routing_handler_call_helper::Wrapped<
501 Func, typename function_t::template arg<Indices>...>();
502 ret.template set<typename function_t::template arg<Indices>...>(
503 std::move(f));
504 return ret;
505 }
506
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500507 template <typename Func>
508 void operator()(std::string name, Func&& f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700509 {
510 nameStr = std::move(name);
511 (*this).template operator()<Func>(std::forward(f));
512 }
513
514 private:
zhanghch058d1b46d2021-04-01 11:18:24 +0800515 std::function<void(const Request&,
516 const std::shared_ptr<bmcweb::AsyncResp>&,
517 const RoutingParams&)>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700518 erasedHandler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700519};
520
521template <typename... Args>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500522class TaggedRule :
523 public BaseRule,
524 public RuleParameterTraits<TaggedRule<Args...>>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700525{
526 public:
527 using self_t = TaggedRule<Args...>;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700528
Ed Tanousf23b7292020-10-15 09:41:17 -0700529 TaggedRule(const std::string& ruleIn) : BaseRule(ruleIn)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500530 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700531
Ed Tanous1abe55e2018-09-05 08:30:59 -0700532 void validate() override
533 {
534 if (!handler)
535 {
536 throw std::runtime_error(nameStr + (!nameStr.empty() ? ": " : "") +
537 "no handler for url " + rule);
538 }
539 }
540
541 template <typename Func>
542 typename std::enable_if<
543 black_magic::CallHelper<Func, black_magic::S<Args...>>::value,
544 void>::type
545 operator()(Func&& f)
546 {
547 static_assert(
548 black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
549 black_magic::CallHelper<
550 Func, black_magic::S<crow::Request, Args...>>::value,
551 "Handler type is mismatched with URL parameters");
552 static_assert(
553 !std::is_same<void, decltype(f(std::declval<Args>()...))>::value,
554 "Handler function cannot have void return type; valid return "
555 "types: "
Gunnar Mills6be0e402020-07-08 13:21:51 -0500556 "string, int, crow::response, nlohmann::json");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700557
zhanghch058d1b46d2021-04-01 11:18:24 +0800558 handler = [f = std::move(f)](
559 const Request&,
560 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
561 Args... args) { asyncResp->res.result(f(args...)); };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700562 }
563
564 template <typename Func>
565 typename std::enable_if<
566 !black_magic::CallHelper<Func, black_magic::S<Args...>>::value &&
Ed Tanous7045c8d2017-04-03 10:04:37 -0700567 black_magic::CallHelper<
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700568 Func, black_magic::S<crow::Request, Args...>>::value,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700569 void>::type
570 operator()(Func&& f)
571 {
572 static_assert(
573 black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
574 black_magic::CallHelper<
575 Func, black_magic::S<crow::Request, Args...>>::value,
576 "Handler type is mismatched with URL parameters");
577 static_assert(
578 !std::is_same<void, decltype(f(std::declval<crow::Request>(),
579 std::declval<Args>()...))>::value,
580 "Handler function cannot have void return type; valid return "
581 "types: "
Gunnar Mills6be0e402020-07-08 13:21:51 -0500582 "string, int, crow::response,nlohmann::json");
Ed Tanous7045c8d2017-04-03 10:04:37 -0700583
zhanghch058d1b46d2021-04-01 11:18:24 +0800584 handler = [f = std::move(f)](
585 const crow::Request& req,
586 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
587 Args... args) { asyncResp->res.result(f(req, args...)); };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700588 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700589
Ed Tanous1abe55e2018-09-05 08:30:59 -0700590 template <typename Func>
591 typename std::enable_if<
592 !black_magic::CallHelper<Func, black_magic::S<Args...>>::value &&
593 !black_magic::CallHelper<
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700594 Func, black_magic::S<crow::Request, Args...>>::value,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700595 void>::type
596 operator()(Func&& f)
597 {
598 static_assert(
599 black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
600 black_magic::CallHelper<
601 Func, black_magic::S<crow::Request, Args...>>::value ||
602 black_magic::CallHelper<
zhanghch058d1b46d2021-04-01 11:18:24 +0800603 Func, black_magic::S<crow::Request,
604 std::shared_ptr<bmcweb::AsyncResp>&,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700605 Args...>>::value,
606 "Handler type is mismatched with URL parameters");
607 static_assert(
zhanghch058d1b46d2021-04-01 11:18:24 +0800608 std::is_same<
609 void,
610 decltype(f(std::declval<crow::Request>(),
611 std::declval<std::shared_ptr<bmcweb::AsyncResp>&>(),
612 std::declval<Args>()...))>::value,
Tanousf00032d2018-11-05 01:18:10 -0300613 "Handler function with response argument should have void "
614 "return "
Ed Tanous1abe55e2018-09-05 08:30:59 -0700615 "type");
Ed Tanous7045c8d2017-04-03 10:04:37 -0700616
Ed Tanous1abe55e2018-09-05 08:30:59 -0700617 handler = std::move(f);
618 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700619
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500620 template <typename Func>
Ed Tanousf23b7292020-10-15 09:41:17 -0700621 void operator()(const std::string_view name, Func&& f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700622 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700623 nameStr = name;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700624 (*this).template operator()<Func>(std::forward(f));
625 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700626
zhanghch058d1b46d2021-04-01 11:18:24 +0800627 void handle(const Request& req,
628 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700629 const RoutingParams& params) override
630 {
631 detail::routing_handler_call_helper::Call<
632 detail::routing_handler_call_helper::CallParams<decltype(handler)>,
633 0, 0, 0, 0, black_magic::S<Args...>, black_magic::S<>>()(
634 detail::routing_handler_call_helper::CallParams<decltype(handler)>{
zhanghch058d1b46d2021-04-01 11:18:24 +0800635 handler, params, req, asyncResp});
Ed Tanous1abe55e2018-09-05 08:30:59 -0700636 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700637
Ed Tanous1abe55e2018-09-05 08:30:59 -0700638 private:
zhanghch058d1b46d2021-04-01 11:18:24 +0800639 std::function<void(const crow::Request&,
640 const std::shared_ptr<bmcweb::AsyncResp>&, Args...)>
641 handler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700642};
643
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700644const int ruleSpecialRedirectSlash = 1;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700645
Ed Tanous1abe55e2018-09-05 08:30:59 -0700646class Trie
647{
648 public:
649 struct Node
650 {
651 unsigned ruleIndex{};
Ed Tanous271584a2019-07-09 16:24:22 -0700652 std::array<size_t, static_cast<size_t>(ParamType::MAX)>
653 paramChildrens{};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700654 boost::container::flat_map<std::string, unsigned> children;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700655
Ed Tanous1abe55e2018-09-05 08:30:59 -0700656 bool isSimpleNode() const
657 {
658 return !ruleIndex && std::all_of(std::begin(paramChildrens),
659 std::end(paramChildrens),
Ed Tanous271584a2019-07-09 16:24:22 -0700660 [](size_t x) { return !x; });
Ed Tanous7045c8d2017-04-03 10:04:37 -0700661 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700662 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700663
Ed Tanous1abe55e2018-09-05 08:30:59 -0700664 Trie() : nodes(1)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500665 {}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700666
667 private:
668 void optimizeNode(Node* node)
669 {
Ed Tanous271584a2019-07-09 16:24:22 -0700670 for (size_t x : node->paramChildrens)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700671 {
672 if (!x)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700673 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700674 continue;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700675 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700676 Node* child = &nodes[x];
677 optimizeNode(child);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700678 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700679 if (node->children.empty())
Ed Tanous3174e4d2020-10-07 11:41:22 -0700680 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700681 return;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700682 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700683 bool mergeWithChild = true;
Tanousf00032d2018-11-05 01:18:10 -0300684 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700685 {
686 Node* child = &nodes[kv.second];
687 if (!child->isSimpleNode())
688 {
689 mergeWithChild = false;
690 break;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700691 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700692 }
693 if (mergeWithChild)
694 {
695 decltype(node->children) merged;
Tanousf00032d2018-11-05 01:18:10 -0300696 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700697 {
698 Node* child = &nodes[kv.second];
Tanousf00032d2018-11-05 01:18:10 -0300699 for (const std::pair<std::string, unsigned>& childKv :
700 child->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700701 {
702 merged[kv.first + childKv.first] = childKv.second;
703 }
704 }
705 node->children = std::move(merged);
706 optimizeNode(node);
707 }
708 else
709 {
Tanousf00032d2018-11-05 01:18:10 -0300710 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700711 {
712 Node* child = &nodes[kv.second];
713 optimizeNode(child);
714 }
715 }
716 }
717
718 void optimize()
719 {
720 optimizeNode(head());
721 }
722
723 public:
724 void validate()
725 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700726 optimize();
727 }
728
Ed Tanous81ce6092020-12-17 16:54:55 +0000729 void findRouteIndexes(const std::string& reqUrl,
730 std::vector<unsigned>& routeIndexes,
Tanousf00032d2018-11-05 01:18:10 -0300731 const Node* node = nullptr, unsigned pos = 0) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700732 {
733 if (node == nullptr)
734 {
735 node = head();
736 }
Tanousf00032d2018-11-05 01:18:10 -0300737 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700738 {
739 const std::string& fragment = kv.first;
740 const Node* child = &nodes[kv.second];
Ed Tanous81ce6092020-12-17 16:54:55 +0000741 if (pos >= reqUrl.size())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700742 {
743 if (child->ruleIndex != 0 && fragment != "/")
744 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000745 routeIndexes.push_back(child->ruleIndex);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700746 }
Ed Tanous81ce6092020-12-17 16:54:55 +0000747 findRouteIndexes(reqUrl, routeIndexes, child,
Ed Tanous271584a2019-07-09 16:24:22 -0700748 static_cast<unsigned>(pos + fragment.size()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700749 }
750 else
751 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000752 if (reqUrl.compare(pos, fragment.size(), fragment) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700753 {
Ed Tanous271584a2019-07-09 16:24:22 -0700754 findRouteIndexes(
Ed Tanous81ce6092020-12-17 16:54:55 +0000755 reqUrl, routeIndexes, child,
Ed Tanous271584a2019-07-09 16:24:22 -0700756 static_cast<unsigned>(pos + fragment.size()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700757 }
758 }
759 }
760 }
761
762 std::pair<unsigned, RoutingParams>
Ed Tanous81ce6092020-12-17 16:54:55 +0000763 find(const std::string_view reqUrl, const Node* node = nullptr,
Ed Tanous271584a2019-07-09 16:24:22 -0700764 size_t pos = 0, RoutingParams* params = nullptr) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700765 {
766 RoutingParams empty;
767 if (params == nullptr)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700768 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700769 params = &empty;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700770 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700771
772 unsigned found{};
773 RoutingParams matchParams;
774
775 if (node == nullptr)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700776 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700777 node = head();
Ed Tanous3174e4d2020-10-07 11:41:22 -0700778 }
Ed Tanous81ce6092020-12-17 16:54:55 +0000779 if (pos == reqUrl.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -0700780 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700781 return {node->ruleIndex, *params};
Ed Tanous3174e4d2020-10-07 11:41:22 -0700782 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700783
784 auto updateFound =
785 [&found, &matchParams](std::pair<unsigned, RoutingParams>& ret) {
786 if (ret.first && (!found || found > ret.first))
787 {
788 found = ret.first;
789 matchParams = std::move(ret.second);
790 }
791 };
792
Ed Tanous271584a2019-07-09 16:24:22 -0700793 if (node->paramChildrens[static_cast<size_t>(ParamType::INT)])
Ed Tanous1abe55e2018-09-05 08:30:59 -0700794 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000795 char c = reqUrl[pos];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700796 if ((c >= '0' && c <= '9') || c == '+' || c == '-')
797 {
798 char* eptr;
799 errno = 0;
800 long long int value =
Ed Tanous81ce6092020-12-17 16:54:55 +0000801 std::strtoll(reqUrl.data() + pos, &eptr, 10);
802 if (errno != ERANGE && eptr != reqUrl.data() + pos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700803 {
804 params->intParams.push_back(value);
Ed Tanous81ce6092020-12-17 16:54:55 +0000805 std::pair<unsigned, RoutingParams> ret =
806 find(reqUrl,
807 &nodes[node->paramChildrens[static_cast<size_t>(
808 ParamType::INT)]],
809 static_cast<size_t>(eptr - reqUrl.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700810 updateFound(ret);
811 params->intParams.pop_back();
812 }
813 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700814 }
815
Ed Tanous271584a2019-07-09 16:24:22 -0700816 if (node->paramChildrens[static_cast<size_t>(ParamType::UINT)])
Ed Tanous1abe55e2018-09-05 08:30:59 -0700817 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000818 char c = reqUrl[pos];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700819 if ((c >= '0' && c <= '9') || c == '+')
820 {
821 char* eptr;
822 errno = 0;
823 unsigned long long int value =
Ed Tanous81ce6092020-12-17 16:54:55 +0000824 std::strtoull(reqUrl.data() + pos, &eptr, 10);
825 if (errno != ERANGE && eptr != reqUrl.data() + pos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700826 {
827 params->uintParams.push_back(value);
Ed Tanous81ce6092020-12-17 16:54:55 +0000828 std::pair<unsigned, RoutingParams> ret =
829 find(reqUrl,
830 &nodes[node->paramChildrens[static_cast<size_t>(
831 ParamType::UINT)]],
832 static_cast<size_t>(eptr - reqUrl.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700833 updateFound(ret);
834 params->uintParams.pop_back();
835 }
836 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700837 }
838
Ed Tanous271584a2019-07-09 16:24:22 -0700839 if (node->paramChildrens[static_cast<size_t>(ParamType::DOUBLE)])
Ed Tanous1abe55e2018-09-05 08:30:59 -0700840 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000841 char c = reqUrl[pos];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700842 if ((c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')
843 {
844 char* eptr;
845 errno = 0;
Ed Tanous81ce6092020-12-17 16:54:55 +0000846 double value = std::strtod(reqUrl.data() + pos, &eptr);
847 if (errno != ERANGE && eptr != reqUrl.data() + pos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700848 {
849 params->doubleParams.push_back(value);
Ed Tanous81ce6092020-12-17 16:54:55 +0000850 std::pair<unsigned, RoutingParams> ret =
851 find(reqUrl,
852 &nodes[node->paramChildrens[static_cast<size_t>(
853 ParamType::DOUBLE)]],
854 static_cast<size_t>(eptr - reqUrl.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700855 updateFound(ret);
856 params->doubleParams.pop_back();
857 }
858 }
859 }
860
Ed Tanous271584a2019-07-09 16:24:22 -0700861 if (node->paramChildrens[static_cast<size_t>(ParamType::STRING)])
Ed Tanous1abe55e2018-09-05 08:30:59 -0700862 {
Ed Tanousb01bf292019-03-25 19:25:26 +0000863 size_t epos = pos;
Ed Tanous81ce6092020-12-17 16:54:55 +0000864 for (; epos < reqUrl.size(); epos++)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700865 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000866 if (reqUrl[epos] == '/')
Ed Tanous3174e4d2020-10-07 11:41:22 -0700867 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700868 break;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700869 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700870 }
871
872 if (epos != pos)
873 {
874 params->stringParams.emplace_back(
Ed Tanous81ce6092020-12-17 16:54:55 +0000875 reqUrl.substr(pos, epos - pos));
Tanousf00032d2018-11-05 01:18:10 -0300876 std::pair<unsigned, RoutingParams> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000877 find(reqUrl,
Ed Tanous271584a2019-07-09 16:24:22 -0700878 &nodes[node->paramChildrens[static_cast<size_t>(
879 ParamType::STRING)]],
Ed Tanousb01bf292019-03-25 19:25:26 +0000880 epos, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700881 updateFound(ret);
882 params->stringParams.pop_back();
883 }
884 }
885
Ed Tanous271584a2019-07-09 16:24:22 -0700886 if (node->paramChildrens[static_cast<size_t>(ParamType::PATH)])
Ed Tanous1abe55e2018-09-05 08:30:59 -0700887 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000888 size_t epos = reqUrl.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700889
890 if (epos != pos)
891 {
892 params->stringParams.emplace_back(
Ed Tanous81ce6092020-12-17 16:54:55 +0000893 reqUrl.substr(pos, epos - pos));
Ed Tanous271584a2019-07-09 16:24:22 -0700894 std::pair<unsigned, RoutingParams> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000895 find(reqUrl,
Ed Tanous271584a2019-07-09 16:24:22 -0700896 &nodes[node->paramChildrens[static_cast<size_t>(
897 ParamType::PATH)]],
898 epos, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700899 updateFound(ret);
900 params->stringParams.pop_back();
901 }
902 }
903
Tanousf00032d2018-11-05 01:18:10 -0300904 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700905 {
906 const std::string& fragment = kv.first;
907 const Node* child = &nodes[kv.second];
908
Ed Tanous81ce6092020-12-17 16:54:55 +0000909 if (reqUrl.compare(pos, fragment.size(), fragment) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700910 {
Tanousf00032d2018-11-05 01:18:10 -0300911 std::pair<unsigned, RoutingParams> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000912 find(reqUrl, child, pos + fragment.size(), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700913 updateFound(ret);
914 }
915 }
916
917 return {found, matchParams};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700918 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700919
920 void add(const std::string& url, unsigned ruleIndex)
921 {
Ed Tanous271584a2019-07-09 16:24:22 -0700922 size_t idx = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700923
924 for (unsigned i = 0; i < url.size(); i++)
925 {
926 char c = url[i];
927 if (c == '<')
928 {
Tanousf00032d2018-11-05 01:18:10 -0300929 const static std::array<std::pair<ParamType, std::string>, 7>
930 paramTraits = {{
931 {ParamType::INT, "<int>"},
932 {ParamType::UINT, "<uint>"},
933 {ParamType::DOUBLE, "<float>"},
934 {ParamType::DOUBLE, "<double>"},
935 {ParamType::STRING, "<str>"},
936 {ParamType::STRING, "<string>"},
937 {ParamType::PATH, "<path>"},
938 }};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700939
Tanousf00032d2018-11-05 01:18:10 -0300940 for (const std::pair<ParamType, std::string>& x : paramTraits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700941 {
Tanousf00032d2018-11-05 01:18:10 -0300942 if (url.compare(i, x.second.size(), x.second) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700943 {
Ed Tanous271584a2019-07-09 16:24:22 -0700944 size_t index = static_cast<size_t>(x.first);
945 if (!nodes[idx].paramChildrens[index])
Ed Tanous1abe55e2018-09-05 08:30:59 -0700946 {
Tanousf00032d2018-11-05 01:18:10 -0300947 unsigned newNodeIdx = newNode();
Ed Tanous271584a2019-07-09 16:24:22 -0700948 nodes[idx].paramChildrens[index] = newNodeIdx;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700949 }
Ed Tanous271584a2019-07-09 16:24:22 -0700950 idx = nodes[idx].paramChildrens[index];
951 i += static_cast<unsigned>(x.second.size());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700952 break;
953 }
954 }
955
956 i--;
957 }
958 else
959 {
960 std::string piece(&c, 1);
961 if (!nodes[idx].children.count(piece))
962 {
Tanousf00032d2018-11-05 01:18:10 -0300963 unsigned newNodeIdx = newNode();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700964 nodes[idx].children.emplace(piece, newNodeIdx);
965 }
966 idx = nodes[idx].children[piece];
967 }
968 }
969 if (nodes[idx].ruleIndex)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700970 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700971 throw std::runtime_error("handler already exists for " + url);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700972 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700973 nodes[idx].ruleIndex = ruleIndex;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700974 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700975
Ed Tanous1abe55e2018-09-05 08:30:59 -0700976 private:
Ed Tanous271584a2019-07-09 16:24:22 -0700977 void debugNodePrint(Node* n, size_t level)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700978 {
Ed Tanous271584a2019-07-09 16:24:22 -0700979 for (size_t i = 0; i < static_cast<size_t>(ParamType::MAX); i++)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700980 {
981 if (n->paramChildrens[i])
982 {
983 BMCWEB_LOG_DEBUG << std::string(
Ed Tanous271584a2019-07-09 16:24:22 -0700984 2U * level, ' ') /*<< "("<<n->paramChildrens[i]<<") "*/;
985 switch (static_cast<ParamType>(i))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700986 {
987 case ParamType::INT:
988 BMCWEB_LOG_DEBUG << "<int>";
989 break;
990 case ParamType::UINT:
991 BMCWEB_LOG_DEBUG << "<uint>";
992 break;
993 case ParamType::DOUBLE:
994 BMCWEB_LOG_DEBUG << "<float>";
995 break;
996 case ParamType::STRING:
997 BMCWEB_LOG_DEBUG << "<str>";
998 break;
999 case ParamType::PATH:
1000 BMCWEB_LOG_DEBUG << "<path>";
1001 break;
Ed Tanous23a21a12020-07-25 04:45:05 +00001002 case ParamType::MAX:
Ed Tanous1abe55e2018-09-05 08:30:59 -07001003 BMCWEB_LOG_DEBUG << "<ERROR>";
1004 break;
1005 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001006
Ed Tanous1abe55e2018-09-05 08:30:59 -07001007 debugNodePrint(&nodes[n->paramChildrens[i]], level + 1);
1008 }
1009 }
Tanousf00032d2018-11-05 01:18:10 -03001010 for (const std::pair<std::string, unsigned>& kv : n->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001011 {
1012 BMCWEB_LOG_DEBUG
Ed Tanous271584a2019-07-09 16:24:22 -07001013 << std::string(2U * level, ' ') /*<< "(" << kv.second << ") "*/
Ed Tanous1abe55e2018-09-05 08:30:59 -07001014 << kv.first;
1015 debugNodePrint(&nodes[kv.second], level + 1);
1016 }
1017 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001018
Ed Tanous1abe55e2018-09-05 08:30:59 -07001019 public:
1020 void debugPrint()
1021 {
Ed Tanous271584a2019-07-09 16:24:22 -07001022 debugNodePrint(head(), 0U);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001023 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001024
Ed Tanous1abe55e2018-09-05 08:30:59 -07001025 private:
1026 const Node* head() const
1027 {
1028 return &nodes.front();
1029 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001030
Ed Tanous1abe55e2018-09-05 08:30:59 -07001031 Node* head()
1032 {
1033 return &nodes.front();
1034 }
1035
1036 unsigned newNode()
1037 {
1038 nodes.resize(nodes.size() + 1);
Ed Tanous271584a2019-07-09 16:24:22 -07001039 return static_cast<unsigned>(nodes.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001040 }
1041
1042 std::vector<Node> nodes;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001043};
1044
Ed Tanous1abe55e2018-09-05 08:30:59 -07001045class Router
1046{
1047 public:
Ed Tanous0c0084a2019-10-24 15:57:51 -07001048 Router() = default;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001049
Ed Tanous1abe55e2018-09-05 08:30:59 -07001050 DynamicRule& newRuleDynamic(const std::string& rule)
1051 {
1052 std::unique_ptr<DynamicRule> ruleObject =
1053 std::make_unique<DynamicRule>(rule);
1054 DynamicRule* ptr = ruleObject.get();
Tanousf00032d2018-11-05 01:18:10 -03001055 allRules.emplace_back(std::move(ruleObject));
Ed Tanous7045c8d2017-04-03 10:04:37 -07001056
Ed Tanous1abe55e2018-09-05 08:30:59 -07001057 return *ptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001058 }
1059
Ed Tanous1abe55e2018-09-05 08:30:59 -07001060 template <uint64_t N>
1061 typename black_magic::Arguments<N>::type::template rebind<TaggedRule>&
1062 newRuleTagged(const std::string& rule)
1063 {
1064 using RuleT = typename black_magic::Arguments<N>::type::template rebind<
1065 TaggedRule>;
1066 std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule);
1067 RuleT* ptr = ruleObject.get();
Tanousf00032d2018-11-05 01:18:10 -03001068 allRules.emplace_back(std::move(ruleObject));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001069
1070 return *ptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001071 }
1072
Tanousf00032d2018-11-05 01:18:10 -03001073 void internalAddRuleObject(const std::string& rule, BaseRule* ruleObject)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001074 {
Tanousf00032d2018-11-05 01:18:10 -03001075 if (ruleObject == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001076 {
Tanousf00032d2018-11-05 01:18:10 -03001077 return;
1078 }
Ed Tanous888880a2020-08-24 13:48:50 -07001079 for (size_t method = 0, methodBit = 1; method < maxHttpVerbCount;
Ed Tanous2c70f802020-09-28 14:29:23 -07001080 method++, methodBit <<= 1)
Tanousf00032d2018-11-05 01:18:10 -03001081 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001082 if (ruleObject->methodsBitfield & methodBit)
Tanousf00032d2018-11-05 01:18:10 -03001083 {
1084 perMethods[method].rules.emplace_back(ruleObject);
1085 perMethods[method].trie.add(
Ed Tanous271584a2019-07-09 16:24:22 -07001086 rule, static_cast<unsigned>(
1087 perMethods[method].rules.size() - 1U));
Tanousf00032d2018-11-05 01:18:10 -03001088 // directory case:
1089 // request to `/about' url matches `/about/' rule
1090 if (rule.size() > 2 && rule.back() == '/')
1091 {
1092 perMethods[method].trie.add(
1093 rule.substr(0, rule.size() - 1),
Ed Tanous271584a2019-07-09 16:24:22 -07001094 static_cast<unsigned>(perMethods[method].rules.size() -
1095 1));
Tanousf00032d2018-11-05 01:18:10 -03001096 }
1097 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001098 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001099 }
1100
Ed Tanous1abe55e2018-09-05 08:30:59 -07001101 void validate()
1102 {
Tanousf00032d2018-11-05 01:18:10 -03001103 for (std::unique_ptr<BaseRule>& rule : allRules)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001104 {
1105 if (rule)
1106 {
Tanousf00032d2018-11-05 01:18:10 -03001107 std::unique_ptr<BaseRule> upgraded = rule->upgrade();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001108 if (upgraded)
Ed Tanous3174e4d2020-10-07 11:41:22 -07001109 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001110 rule = std::move(upgraded);
Ed Tanous3174e4d2020-10-07 11:41:22 -07001111 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001112 rule->validate();
Tanousf00032d2018-11-05 01:18:10 -03001113 internalAddRuleObject(rule->rule, rule.get());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001114 }
1115 }
Tanousf00032d2018-11-05 01:18:10 -03001116 for (PerMethod& perMethod : perMethods)
1117 {
1118 perMethod.trie.validate();
1119 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001120 }
1121
Ed Tanous1abe55e2018-09-05 08:30:59 -07001122 template <typename Adaptor>
1123 void handleUpgrade(const Request& req, Response& res, Adaptor&& adaptor)
1124 {
Ed Tanous271584a2019-07-09 16:24:22 -07001125 if (static_cast<size_t>(req.method()) >= perMethods.size())
Ed Tanous888880a2020-08-24 13:48:50 -07001126 {
1127 res.result(boost::beast::http::status::not_found);
1128 res.end();
Tanousf00032d2018-11-05 01:18:10 -03001129 return;
Ed Tanous888880a2020-08-24 13:48:50 -07001130 }
Tanousf00032d2018-11-05 01:18:10 -03001131
Ed Tanous271584a2019-07-09 16:24:22 -07001132 PerMethod& perMethod = perMethods[static_cast<size_t>(req.method())];
Tanousf00032d2018-11-05 01:18:10 -03001133 Trie& trie = perMethod.trie;
1134 std::vector<BaseRule*>& rules = perMethod.rules;
1135
1136 const std::pair<unsigned, RoutingParams>& found = trie.find(req.url);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001137 unsigned ruleIndex = found.first;
1138 if (!ruleIndex)
1139 {
1140 BMCWEB_LOG_DEBUG << "Cannot match rules " << req.url;
Ed Tanousde5c9f32019-03-26 09:17:55 -07001141 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001142 res.end();
1143 return;
1144 }
1145
1146 if (ruleIndex >= rules.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -07001147 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001148 throw std::runtime_error("Trie internal structure corrupted!");
Ed Tanous3174e4d2020-10-07 11:41:22 -07001149 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001150
1151 if (ruleIndex == ruleSpecialRedirectSlash)
1152 {
1153 BMCWEB_LOG_INFO << "Redirecting to a url with trailing slash: "
1154 << req.url;
Ed Tanousde5c9f32019-03-26 09:17:55 -07001155 res.result(boost::beast::http::status::moved_permanently);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001156
1157 // TODO absolute url building
1158 if (req.getHeaderValue("Host").empty())
1159 {
1160 res.addHeader("Location", std::string(req.url) + "/");
1161 }
1162 else
1163 {
1164 res.addHeader(
1165 "Location",
1166 req.isSecure
1167 ? "https://"
1168 : "http://" + std::string(req.getHeaderValue("Host")) +
1169 std::string(req.url) + "/");
1170 }
1171 res.end();
1172 return;
1173 }
1174
Ed Tanous271584a2019-07-09 16:24:22 -07001175 if ((rules[ruleIndex]->getMethods() &
1176 (1U << static_cast<size_t>(req.method()))) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001177 {
1178 BMCWEB_LOG_DEBUG << "Rule found but method mismatch: " << req.url
1179 << " with " << req.methodString() << "("
Ed Tanous271584a2019-07-09 16:24:22 -07001180 << static_cast<uint32_t>(req.method()) << ") / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001181 << rules[ruleIndex]->getMethods();
Ed Tanousde5c9f32019-03-26 09:17:55 -07001182 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001183 res.end();
1184 return;
1185 }
1186
1187 BMCWEB_LOG_DEBUG << "Matched rule (upgrade) '" << rules[ruleIndex]->rule
Ed Tanous271584a2019-07-09 16:24:22 -07001188 << "' " << static_cast<uint32_t>(req.method()) << " / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001189 << rules[ruleIndex]->getMethods();
1190
1191 // any uncaught exceptions become 500s
1192 try
1193 {
1194 rules[ruleIndex]->handleUpgrade(req, res, std::move(adaptor));
1195 }
1196 catch (std::exception& e)
1197 {
1198 BMCWEB_LOG_ERROR << "An uncaught exception occurred: " << e.what();
Ed Tanousde5c9f32019-03-26 09:17:55 -07001199 res.result(boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001200 res.end();
1201 return;
1202 }
1203 catch (...)
1204 {
1205 BMCWEB_LOG_ERROR
1206 << "An uncaught exception occurred. The type was unknown "
1207 "so no information was available.";
Ed Tanousde5c9f32019-03-26 09:17:55 -07001208 res.result(boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001209 res.end();
1210 return;
1211 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001212 }
1213
zhanghch058d1b46d2021-04-01 11:18:24 +08001214 void handle(Request& req,
1215 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001216 {
Ed Tanous271584a2019-07-09 16:24:22 -07001217 if (static_cast<size_t>(req.method()) >= perMethods.size())
Ed Tanous888880a2020-08-24 13:48:50 -07001218 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001219 asyncResp->res.result(boost::beast::http::status::not_found);
Tanousf00032d2018-11-05 01:18:10 -03001220 return;
Ed Tanous888880a2020-08-24 13:48:50 -07001221 }
Ed Tanous271584a2019-07-09 16:24:22 -07001222 PerMethod& perMethod = perMethods[static_cast<size_t>(req.method())];
Tanousf00032d2018-11-05 01:18:10 -03001223 Trie& trie = perMethod.trie;
1224 std::vector<BaseRule*>& rules = perMethod.rules;
1225
1226 const std::pair<unsigned, RoutingParams>& found = trie.find(req.url);
Ed Tanous7045c8d2017-04-03 10:04:37 -07001227
Ed Tanous1abe55e2018-09-05 08:30:59 -07001228 unsigned ruleIndex = found.first;
1229
1230 if (!ruleIndex)
1231 {
Ed Tanous2634dcd2019-03-26 09:28:06 -07001232 // Check to see if this url exists at any verb
1233 for (const PerMethod& p : perMethods)
1234 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001235 const std::pair<unsigned, RoutingParams>& found2 =
Ed Tanous2634dcd2019-03-26 09:28:06 -07001236 p.trie.find(req.url);
Ed Tanous23a21a12020-07-25 04:45:05 +00001237 if (found2.first > 0)
Ed Tanous2634dcd2019-03-26 09:28:06 -07001238 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001239 asyncResp->res.result(
1240 boost::beast::http::status::method_not_allowed);
Ed Tanous2634dcd2019-03-26 09:28:06 -07001241 return;
1242 }
1243 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001244 BMCWEB_LOG_DEBUG << "Cannot match rules " << req.url;
zhanghch058d1b46d2021-04-01 11:18:24 +08001245 asyncResp->res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001246 return;
1247 }
1248
1249 if (ruleIndex >= rules.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -07001250 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001251 throw std::runtime_error("Trie internal structure corrupted!");
Ed Tanous3174e4d2020-10-07 11:41:22 -07001252 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001253
1254 if (ruleIndex == ruleSpecialRedirectSlash)
1255 {
1256 BMCWEB_LOG_INFO << "Redirecting to a url with trailing slash: "
1257 << req.url;
zhanghch058d1b46d2021-04-01 11:18:24 +08001258 asyncResp->res.result(
1259 boost::beast::http::status::moved_permanently);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001260
1261 // TODO absolute url building
1262 if (req.getHeaderValue("Host").empty())
1263 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001264 asyncResp->res.addHeader("Location",
1265 std::string(req.url) + "/");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001266 }
1267 else
1268 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001269 asyncResp->res.addHeader(
1270 "Location", (req.isSecure ? "https://" : "http://") +
1271 std::string(req.getHeaderValue("Host")) +
1272 std::string(req.url) + "/");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001273 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001274 return;
1275 }
1276
Ed Tanous271584a2019-07-09 16:24:22 -07001277 if ((rules[ruleIndex]->getMethods() &
1278 (1U << static_cast<uint32_t>(req.method()))) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001279 {
1280 BMCWEB_LOG_DEBUG << "Rule found but method mismatch: " << req.url
1281 << " with " << req.methodString() << "("
Ed Tanous271584a2019-07-09 16:24:22 -07001282 << static_cast<uint32_t>(req.method()) << ") / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001283 << rules[ruleIndex]->getMethods();
zhanghch058d1b46d2021-04-01 11:18:24 +08001284 asyncResp->res.result(
1285 boost::beast::http::status::method_not_allowed);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001286 return;
1287 }
1288
1289 BMCWEB_LOG_DEBUG << "Matched rule '" << rules[ruleIndex]->rule << "' "
Ed Tanous271584a2019-07-09 16:24:22 -07001290 << static_cast<uint32_t>(req.method()) << " / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001291 << rules[ruleIndex]->getMethods();
1292
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001293 if (req.session == nullptr)
James Feist7166bf02019-12-10 16:52:14 +00001294 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001295 rules[ruleIndex]->handle(req, asyncResp, found.second);
James Feist7166bf02019-12-10 16:52:14 +00001296 return;
1297 }
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001298
1299 crow::connections::systemBus->async_method_call(
zhanghch058d1b46d2021-04-01 11:18:24 +08001300 [&req, asyncResp, &rules, ruleIndex, found](
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001301 const boost::system::error_code ec,
1302 std::map<std::string, std::variant<bool, std::string,
1303 std::vector<std::string>>>
1304 userInfo) {
1305 if (ec)
1306 {
1307 BMCWEB_LOG_ERROR << "GetUserInfo failed...";
zhanghch058d1b46d2021-04-01 11:18:24 +08001308 asyncResp->res.result(
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001309 boost::beast::http::status::internal_server_error);
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001310 return;
1311 }
1312
1313 const std::string* userRolePtr = nullptr;
1314 auto userInfoIter = userInfo.find("UserPrivilege");
1315 if (userInfoIter != userInfo.end())
1316 {
1317 userRolePtr =
1318 std::get_if<std::string>(&userInfoIter->second);
1319 }
1320
1321 std::string userRole{};
1322 if (userRolePtr != nullptr)
1323 {
1324 userRole = *userRolePtr;
1325 BMCWEB_LOG_DEBUG << "userName = " << req.session->username
1326 << " userRole = " << *userRolePtr;
1327 }
1328
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001329 bool* remoteUserPtr = nullptr;
1330 auto remoteUserIter = userInfo.find("RemoteUser");
1331 if (remoteUserIter != userInfo.end())
1332 {
1333 remoteUserPtr = std::get_if<bool>(&remoteUserIter->second);
1334 }
1335 if (remoteUserPtr == nullptr)
1336 {
1337 BMCWEB_LOG_ERROR
1338 << "RemoteUser property missing or wrong type";
zhanghch058d1b46d2021-04-01 11:18:24 +08001339 asyncResp->res.result(
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001340 boost::beast::http::status::internal_server_error);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001341 return;
1342 }
1343 bool remoteUser = *remoteUserPtr;
1344
1345 bool passwordExpired = false; // default for remote user
1346 if (!remoteUser)
1347 {
1348 bool* passwordExpiredPtr = nullptr;
1349 auto passwordExpiredIter =
1350 userInfo.find("UserPasswordExpired");
1351 if (passwordExpiredIter != userInfo.end())
1352 {
1353 passwordExpiredPtr =
1354 std::get_if<bool>(&passwordExpiredIter->second);
1355 }
1356 if (passwordExpiredPtr != nullptr)
1357 {
1358 passwordExpired = *passwordExpiredPtr;
1359 }
1360 else
1361 {
1362 BMCWEB_LOG_ERROR
1363 << "UserPasswordExpired property is expected for"
1364 " local user but is missing or wrong type";
zhanghch058d1b46d2021-04-01 11:18:24 +08001365 asyncResp->res.result(
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001366 boost::beast::http::status::internal_server_error);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001367 return;
1368 }
1369 }
1370
Ed Tanous23a21a12020-07-25 04:45:05 +00001371 // Get the userprivileges from the role
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001372 redfish::Privileges userPrivileges =
1373 redfish::getUserPrivileges(userRole);
1374
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001375 // Set isConfigureSelfOnly based on D-Bus results. This
1376 // ignores the results from both pamAuthenticateUser and the
1377 // value from any previous use of this session.
1378 req.session->isConfigureSelfOnly = passwordExpired;
1379
Ed Tanous23a21a12020-07-25 04:45:05 +00001380 // Modifyprivileges if isConfigureSelfOnly.
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001381 if (req.session->isConfigureSelfOnly)
1382 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001383 // Remove allprivileges except ConfigureSelf
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001384 userPrivileges = userPrivileges.intersection(
1385 redfish::Privileges{"ConfigureSelf"});
1386 BMCWEB_LOG_DEBUG << "Operation limited to ConfigureSelf";
1387 }
1388
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001389 if (!rules[ruleIndex]->checkPrivileges(userPrivileges))
1390 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001391 asyncResp->res.result(
1392 boost::beast::http::status::forbidden);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001393 if (req.session->isConfigureSelfOnly)
1394 {
1395 redfish::messages::passwordChangeRequired(
zhanghch058d1b46d2021-04-01 11:18:24 +08001396 asyncResp->res,
1397 "/redfish/v1/AccountService/Accounts/" +
1398 req.session->username);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001399 }
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001400 return;
1401 }
1402
1403 req.userRole = userRole;
zhanghch058d1b46d2021-04-01 11:18:24 +08001404 rules[ruleIndex]->handle(req, asyncResp, found.second);
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001405 },
1406 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1407 "xyz.openbmc_project.User.Manager", "GetUserInfo",
1408 req.session->username);
Ed Tanous7045c8d2017-04-03 10:04:37 -07001409 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001410
Ed Tanous1abe55e2018-09-05 08:30:59 -07001411 void debugPrint()
1412 {
Ed Tanous271584a2019-07-09 16:24:22 -07001413 for (size_t i = 0; i < perMethods.size(); i++)
Tanousf00032d2018-11-05 01:18:10 -03001414 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001415 BMCWEB_LOG_DEBUG << boost::beast::http::to_string(
1416 static_cast<boost::beast::http::verb>(i));
Tanousf00032d2018-11-05 01:18:10 -03001417 perMethods[i].trie.debugPrint();
1418 }
Ed Tanous3dac7492017-08-02 13:46:20 -07001419 }
Ed Tanousb4a7bfa2017-04-04 17:23:00 -07001420
Ed Tanous1abe55e2018-09-05 08:30:59 -07001421 std::vector<const std::string*> getRoutes(const std::string& parent)
1422 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001423 std::vector<const std::string*> ret;
Tanousf00032d2018-11-05 01:18:10 -03001424
1425 for (const PerMethod& pm : perMethods)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001426 {
Tanousf00032d2018-11-05 01:18:10 -03001427 std::vector<unsigned> x;
1428 pm.trie.findRouteIndexes(parent, x);
1429 for (unsigned index : x)
1430 {
1431 ret.push_back(&pm.rules[index]->rule);
1432 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001433 }
1434 return ret;
1435 }
1436
1437 private:
Tanousf00032d2018-11-05 01:18:10 -03001438 struct PerMethod
1439 {
1440 std::vector<BaseRule*> rules;
1441 Trie trie;
1442 // rule index 0, 1 has special meaning; preallocate it to avoid
1443 // duplication.
1444 PerMethod() : rules(2)
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001445 {}
Tanousf00032d2018-11-05 01:18:10 -03001446 };
Ed Tanous888880a2020-08-24 13:48:50 -07001447
1448 const static size_t maxHttpVerbCount =
Ed Tanouscc090442020-10-07 08:20:50 -07001449 static_cast<size_t>(boost::beast::http::verb::unlink);
Ed Tanous888880a2020-08-24 13:48:50 -07001450
Tanousf00032d2018-11-05 01:18:10 -03001451 std::array<PerMethod, maxHttpVerbCount> perMethods;
1452 std::vector<std::unique_ptr<BaseRule>> allRules;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001453};
Ed Tanous1abe55e2018-09-05 08:30:59 -07001454} // namespace crow