blob: dd07523d006f3cc530654a33cf39b9ff4d3f9cb4 [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
436 template <typename... MethodArgs>
Ed Tanous23a21a12020-07-25 04:45:05 +0000437 self_t& privileges(std::initializer_list<const char*> l)
Tanousf00032d2018-11-05 01:18:10 -0300438 {
Ed Tanous271584a2019-07-09 16:24:22 -0700439 self_t* self = static_cast<self_t*>(this);
440 self->privilegesSet.emplace_back(l);
441 return *self;
Tanousf00032d2018-11-05 01:18:10 -0300442 }
443
444 template <typename... MethodArgs>
Ed Tanous23a21a12020-07-25 04:45:05 +0000445 self_t& privileges(const std::vector<redfish::Privileges>& p)
Tanousf00032d2018-11-05 01:18:10 -0300446 {
Ed Tanous271584a2019-07-09 16:24:22 -0700447 self_t* self = static_cast<self_t*>(this);
Tanousf00032d2018-11-05 01:18:10 -0300448 for (const redfish::Privileges& privilege : p)
449 {
Ed Tanous271584a2019-07-09 16:24:22 -0700450 self->privilegesSet.emplace_back(privilege);
Tanousf00032d2018-11-05 01:18:10 -0300451 }
Ed Tanous271584a2019-07-09 16:24:22 -0700452 return *self;
Tanousf00032d2018-11-05 01:18:10 -0300453 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700454};
455
Ed Tanous1abe55e2018-09-05 08:30:59 -0700456class DynamicRule : public BaseRule, public RuleParameterTraits<DynamicRule>
457{
458 public:
Ed Tanousf23b7292020-10-15 09:41:17 -0700459 DynamicRule(const std::string& ruleIn) : BaseRule(ruleIn)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500460 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700461
Ed Tanous1abe55e2018-09-05 08:30:59 -0700462 void validate() override
463 {
464 if (!erasedHandler)
465 {
466 throw std::runtime_error(nameStr + (!nameStr.empty() ? ": " : "") +
467 "no handler for url " + rule);
468 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700469 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700470
zhanghch058d1b46d2021-04-01 11:18:24 +0800471 void handle(const Request& req,
472 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700473 const RoutingParams& params) override
474 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800475 erasedHandler(req, asyncResp, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700476 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700477
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500478 template <typename Func>
479 void operator()(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700480 {
481 using function_t = utility::function_traits<Func>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700482 erasedHandler =
Ed Tanous988403c2020-08-24 11:29:49 -0700483 wrap(std::move(f),
484 std::make_integer_sequence<unsigned, function_t::arity>{});
Ed Tanous1abe55e2018-09-05 08:30:59 -0700485 }
486
487 // enable_if Arg1 == request && Arg2 == Response
Gunnar Mills6be0e402020-07-08 13:21:51 -0500488 // enable_if Arg1 == request && Arg2 != response
Ed Tanous1abe55e2018-09-05 08:30:59 -0700489 // enable_if Arg1 != request
490
491 template <typename Func, unsigned... Indices>
zhanghch058d1b46d2021-04-01 11:18:24 +0800492 std::function<void(const Request&,
493 const std::shared_ptr<bmcweb::AsyncResp>&,
494 const RoutingParams&)>
Ed Tanous988403c2020-08-24 11:29:49 -0700495 wrap(Func f, std::integer_sequence<unsigned, Indices...>)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700496 {
Ed Tanous988403c2020-08-24 11:29:49 -0700497 using function_t = crow::utility::function_traits<Func>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700498
499 if (!black_magic::isParameterTagCompatible(
Ed Tanous988403c2020-08-24 11:29:49 -0700500 black_magic::getParameterTag(rule.c_str()),
501 black_magic::computeParameterTagFromArgsList<
Ed Tanous1abe55e2018-09-05 08:30:59 -0700502 typename function_t::template arg<Indices>...>::value))
503 {
504 throw std::runtime_error("routeDynamic: Handler type is mismatched "
505 "with URL parameters: " +
506 rule);
507 }
508 auto ret = detail::routing_handler_call_helper::Wrapped<
509 Func, typename function_t::template arg<Indices>...>();
510 ret.template set<typename function_t::template arg<Indices>...>(
511 std::move(f));
512 return ret;
513 }
514
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500515 template <typename Func>
516 void operator()(std::string name, Func&& f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700517 {
518 nameStr = std::move(name);
519 (*this).template operator()<Func>(std::forward(f));
520 }
521
522 private:
zhanghch058d1b46d2021-04-01 11:18:24 +0800523 std::function<void(const Request&,
524 const std::shared_ptr<bmcweb::AsyncResp>&,
525 const RoutingParams&)>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700526 erasedHandler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700527};
528
529template <typename... Args>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500530class TaggedRule :
531 public BaseRule,
532 public RuleParameterTraits<TaggedRule<Args...>>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700533{
534 public:
535 using self_t = TaggedRule<Args...>;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700536
Ed Tanousf23b7292020-10-15 09:41:17 -0700537 TaggedRule(const std::string& ruleIn) : BaseRule(ruleIn)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500538 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700539
Ed Tanous1abe55e2018-09-05 08:30:59 -0700540 void validate() override
541 {
542 if (!handler)
543 {
544 throw std::runtime_error(nameStr + (!nameStr.empty() ? ": " : "") +
545 "no handler for url " + rule);
546 }
547 }
548
549 template <typename Func>
550 typename std::enable_if<
551 black_magic::CallHelper<Func, black_magic::S<Args...>>::value,
552 void>::type
553 operator()(Func&& f)
554 {
555 static_assert(
556 black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
557 black_magic::CallHelper<
558 Func, black_magic::S<crow::Request, Args...>>::value,
559 "Handler type is mismatched with URL parameters");
560 static_assert(
561 !std::is_same<void, decltype(f(std::declval<Args>()...))>::value,
562 "Handler function cannot have void return type; valid return "
563 "types: "
Gunnar Mills6be0e402020-07-08 13:21:51 -0500564 "string, int, crow::response, nlohmann::json");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700565
zhanghch058d1b46d2021-04-01 11:18:24 +0800566 handler = [f = std::move(f)](
567 const Request&,
568 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
569 Args... args) { asyncResp->res.result(f(args...)); };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700570 }
571
572 template <typename Func>
573 typename std::enable_if<
574 !black_magic::CallHelper<Func, black_magic::S<Args...>>::value &&
Ed Tanous7045c8d2017-04-03 10:04:37 -0700575 black_magic::CallHelper<
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700576 Func, black_magic::S<crow::Request, Args...>>::value,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700577 void>::type
578 operator()(Func&& f)
579 {
580 static_assert(
581 black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
582 black_magic::CallHelper<
583 Func, black_magic::S<crow::Request, Args...>>::value,
584 "Handler type is mismatched with URL parameters");
585 static_assert(
586 !std::is_same<void, decltype(f(std::declval<crow::Request>(),
587 std::declval<Args>()...))>::value,
588 "Handler function cannot have void return type; valid return "
589 "types: "
Gunnar Mills6be0e402020-07-08 13:21:51 -0500590 "string, int, crow::response,nlohmann::json");
Ed Tanous7045c8d2017-04-03 10:04:37 -0700591
zhanghch058d1b46d2021-04-01 11:18:24 +0800592 handler = [f = std::move(f)](
593 const crow::Request& req,
594 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
595 Args... args) { asyncResp->res.result(f(req, args...)); };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700596 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700597
Ed Tanous1abe55e2018-09-05 08:30:59 -0700598 template <typename Func>
599 typename std::enable_if<
600 !black_magic::CallHelper<Func, black_magic::S<Args...>>::value &&
601 !black_magic::CallHelper<
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700602 Func, black_magic::S<crow::Request, Args...>>::value,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700603 void>::type
604 operator()(Func&& f)
605 {
606 static_assert(
607 black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
608 black_magic::CallHelper<
609 Func, black_magic::S<crow::Request, Args...>>::value ||
610 black_magic::CallHelper<
zhanghch058d1b46d2021-04-01 11:18:24 +0800611 Func, black_magic::S<crow::Request,
612 std::shared_ptr<bmcweb::AsyncResp>&,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700613 Args...>>::value,
614 "Handler type is mismatched with URL parameters");
615 static_assert(
zhanghch058d1b46d2021-04-01 11:18:24 +0800616 std::is_same<
617 void,
618 decltype(f(std::declval<crow::Request>(),
619 std::declval<std::shared_ptr<bmcweb::AsyncResp>&>(),
620 std::declval<Args>()...))>::value,
Tanousf00032d2018-11-05 01:18:10 -0300621 "Handler function with response argument should have void "
622 "return "
Ed Tanous1abe55e2018-09-05 08:30:59 -0700623 "type");
Ed Tanous7045c8d2017-04-03 10:04:37 -0700624
Ed Tanous1abe55e2018-09-05 08:30:59 -0700625 handler = std::move(f);
626 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700627
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500628 template <typename Func>
Ed Tanousf23b7292020-10-15 09:41:17 -0700629 void operator()(const std::string_view name, Func&& f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700630 {
Ed Tanousf23b7292020-10-15 09:41:17 -0700631 nameStr = name;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700632 (*this).template operator()<Func>(std::forward(f));
633 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700634
zhanghch058d1b46d2021-04-01 11:18:24 +0800635 void handle(const Request& req,
636 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700637 const RoutingParams& params) override
638 {
639 detail::routing_handler_call_helper::Call<
640 detail::routing_handler_call_helper::CallParams<decltype(handler)>,
641 0, 0, 0, 0, black_magic::S<Args...>, black_magic::S<>>()(
642 detail::routing_handler_call_helper::CallParams<decltype(handler)>{
zhanghch058d1b46d2021-04-01 11:18:24 +0800643 handler, params, req, asyncResp});
Ed Tanous1abe55e2018-09-05 08:30:59 -0700644 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700645
Ed Tanous1abe55e2018-09-05 08:30:59 -0700646 private:
zhanghch058d1b46d2021-04-01 11:18:24 +0800647 std::function<void(const crow::Request&,
648 const std::shared_ptr<bmcweb::AsyncResp>&, Args...)>
649 handler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700650};
651
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700652const int ruleSpecialRedirectSlash = 1;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700653
Ed Tanous1abe55e2018-09-05 08:30:59 -0700654class Trie
655{
656 public:
657 struct Node
658 {
659 unsigned ruleIndex{};
Ed Tanous271584a2019-07-09 16:24:22 -0700660 std::array<size_t, static_cast<size_t>(ParamType::MAX)>
661 paramChildrens{};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700662 boost::container::flat_map<std::string, unsigned> children;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700663
Ed Tanous1abe55e2018-09-05 08:30:59 -0700664 bool isSimpleNode() const
665 {
666 return !ruleIndex && std::all_of(std::begin(paramChildrens),
667 std::end(paramChildrens),
Ed Tanous271584a2019-07-09 16:24:22 -0700668 [](size_t x) { return !x; });
Ed Tanous7045c8d2017-04-03 10:04:37 -0700669 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700670 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700671
Ed Tanous1abe55e2018-09-05 08:30:59 -0700672 Trie() : nodes(1)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500673 {}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700674
675 private:
676 void optimizeNode(Node* node)
677 {
Ed Tanous271584a2019-07-09 16:24:22 -0700678 for (size_t x : node->paramChildrens)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700679 {
680 if (!x)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700681 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700682 continue;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700683 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700684 Node* child = &nodes[x];
685 optimizeNode(child);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700686 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700687 if (node->children.empty())
Ed Tanous3174e4d2020-10-07 11:41:22 -0700688 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700689 return;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700690 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700691 bool mergeWithChild = true;
Tanousf00032d2018-11-05 01:18:10 -0300692 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700693 {
694 Node* child = &nodes[kv.second];
695 if (!child->isSimpleNode())
696 {
697 mergeWithChild = false;
698 break;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700699 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700700 }
701 if (mergeWithChild)
702 {
703 decltype(node->children) merged;
Tanousf00032d2018-11-05 01:18:10 -0300704 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700705 {
706 Node* child = &nodes[kv.second];
Tanousf00032d2018-11-05 01:18:10 -0300707 for (const std::pair<std::string, unsigned>& childKv :
708 child->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700709 {
710 merged[kv.first + childKv.first] = childKv.second;
711 }
712 }
713 node->children = std::move(merged);
714 optimizeNode(node);
715 }
716 else
717 {
Tanousf00032d2018-11-05 01:18:10 -0300718 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700719 {
720 Node* child = &nodes[kv.second];
721 optimizeNode(child);
722 }
723 }
724 }
725
726 void optimize()
727 {
728 optimizeNode(head());
729 }
730
731 public:
732 void validate()
733 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700734 optimize();
735 }
736
Ed Tanous81ce6092020-12-17 16:54:55 +0000737 void findRouteIndexes(const std::string& reqUrl,
738 std::vector<unsigned>& routeIndexes,
Tanousf00032d2018-11-05 01:18:10 -0300739 const Node* node = nullptr, unsigned pos = 0) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700740 {
741 if (node == nullptr)
742 {
743 node = head();
744 }
Tanousf00032d2018-11-05 01:18:10 -0300745 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700746 {
747 const std::string& fragment = kv.first;
748 const Node* child = &nodes[kv.second];
Ed Tanous81ce6092020-12-17 16:54:55 +0000749 if (pos >= reqUrl.size())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700750 {
751 if (child->ruleIndex != 0 && fragment != "/")
752 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000753 routeIndexes.push_back(child->ruleIndex);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700754 }
Ed Tanous81ce6092020-12-17 16:54:55 +0000755 findRouteIndexes(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 else
759 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000760 if (reqUrl.compare(pos, fragment.size(), fragment) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700761 {
Ed Tanous271584a2019-07-09 16:24:22 -0700762 findRouteIndexes(
Ed Tanous81ce6092020-12-17 16:54:55 +0000763 reqUrl, routeIndexes, child,
Ed Tanous271584a2019-07-09 16:24:22 -0700764 static_cast<unsigned>(pos + fragment.size()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700765 }
766 }
767 }
768 }
769
770 std::pair<unsigned, RoutingParams>
Ed Tanous81ce6092020-12-17 16:54:55 +0000771 find(const std::string_view reqUrl, const Node* node = nullptr,
Ed Tanous271584a2019-07-09 16:24:22 -0700772 size_t pos = 0, RoutingParams* params = nullptr) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700773 {
774 RoutingParams empty;
775 if (params == nullptr)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700776 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700777 params = &empty;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700778 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700779
780 unsigned found{};
781 RoutingParams matchParams;
782
783 if (node == nullptr)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700784 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700785 node = head();
Ed Tanous3174e4d2020-10-07 11:41:22 -0700786 }
Ed Tanous81ce6092020-12-17 16:54:55 +0000787 if (pos == reqUrl.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -0700788 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700789 return {node->ruleIndex, *params};
Ed Tanous3174e4d2020-10-07 11:41:22 -0700790 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700791
792 auto updateFound =
793 [&found, &matchParams](std::pair<unsigned, RoutingParams>& ret) {
794 if (ret.first && (!found || found > ret.first))
795 {
796 found = ret.first;
797 matchParams = std::move(ret.second);
798 }
799 };
800
Ed Tanous271584a2019-07-09 16:24:22 -0700801 if (node->paramChildrens[static_cast<size_t>(ParamType::INT)])
Ed Tanous1abe55e2018-09-05 08:30:59 -0700802 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000803 char c = reqUrl[pos];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700804 if ((c >= '0' && c <= '9') || c == '+' || c == '-')
805 {
806 char* eptr;
807 errno = 0;
808 long long int value =
Ed Tanous81ce6092020-12-17 16:54:55 +0000809 std::strtoll(reqUrl.data() + pos, &eptr, 10);
810 if (errno != ERANGE && eptr != reqUrl.data() + pos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700811 {
812 params->intParams.push_back(value);
Ed Tanous81ce6092020-12-17 16:54:55 +0000813 std::pair<unsigned, RoutingParams> ret =
814 find(reqUrl,
815 &nodes[node->paramChildrens[static_cast<size_t>(
816 ParamType::INT)]],
817 static_cast<size_t>(eptr - reqUrl.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700818 updateFound(ret);
819 params->intParams.pop_back();
820 }
821 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700822 }
823
Ed Tanous271584a2019-07-09 16:24:22 -0700824 if (node->paramChildrens[static_cast<size_t>(ParamType::UINT)])
Ed Tanous1abe55e2018-09-05 08:30:59 -0700825 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000826 char c = reqUrl[pos];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700827 if ((c >= '0' && c <= '9') || c == '+')
828 {
829 char* eptr;
830 errno = 0;
831 unsigned long long int value =
Ed Tanous81ce6092020-12-17 16:54:55 +0000832 std::strtoull(reqUrl.data() + pos, &eptr, 10);
833 if (errno != ERANGE && eptr != reqUrl.data() + pos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700834 {
835 params->uintParams.push_back(value);
Ed Tanous81ce6092020-12-17 16:54:55 +0000836 std::pair<unsigned, RoutingParams> ret =
837 find(reqUrl,
838 &nodes[node->paramChildrens[static_cast<size_t>(
839 ParamType::UINT)]],
840 static_cast<size_t>(eptr - reqUrl.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700841 updateFound(ret);
842 params->uintParams.pop_back();
843 }
844 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700845 }
846
Ed Tanous271584a2019-07-09 16:24:22 -0700847 if (node->paramChildrens[static_cast<size_t>(ParamType::DOUBLE)])
Ed Tanous1abe55e2018-09-05 08:30:59 -0700848 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000849 char c = reqUrl[pos];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700850 if ((c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')
851 {
852 char* eptr;
853 errno = 0;
Ed Tanous81ce6092020-12-17 16:54:55 +0000854 double value = std::strtod(reqUrl.data() + pos, &eptr);
855 if (errno != ERANGE && eptr != reqUrl.data() + pos)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700856 {
857 params->doubleParams.push_back(value);
Ed Tanous81ce6092020-12-17 16:54:55 +0000858 std::pair<unsigned, RoutingParams> ret =
859 find(reqUrl,
860 &nodes[node->paramChildrens[static_cast<size_t>(
861 ParamType::DOUBLE)]],
862 static_cast<size_t>(eptr - reqUrl.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700863 updateFound(ret);
864 params->doubleParams.pop_back();
865 }
866 }
867 }
868
Ed Tanous271584a2019-07-09 16:24:22 -0700869 if (node->paramChildrens[static_cast<size_t>(ParamType::STRING)])
Ed Tanous1abe55e2018-09-05 08:30:59 -0700870 {
Ed Tanousb01bf292019-03-25 19:25:26 +0000871 size_t epos = pos;
Ed Tanous81ce6092020-12-17 16:54:55 +0000872 for (; epos < reqUrl.size(); epos++)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700873 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000874 if (reqUrl[epos] == '/')
Ed Tanous3174e4d2020-10-07 11:41:22 -0700875 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700876 break;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700877 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700878 }
879
880 if (epos != pos)
881 {
882 params->stringParams.emplace_back(
Ed Tanous81ce6092020-12-17 16:54:55 +0000883 reqUrl.substr(pos, epos - pos));
Tanousf00032d2018-11-05 01:18:10 -0300884 std::pair<unsigned, RoutingParams> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000885 find(reqUrl,
Ed Tanous271584a2019-07-09 16:24:22 -0700886 &nodes[node->paramChildrens[static_cast<size_t>(
887 ParamType::STRING)]],
Ed Tanousb01bf292019-03-25 19:25:26 +0000888 epos, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700889 updateFound(ret);
890 params->stringParams.pop_back();
891 }
892 }
893
Ed Tanous271584a2019-07-09 16:24:22 -0700894 if (node->paramChildrens[static_cast<size_t>(ParamType::PATH)])
Ed Tanous1abe55e2018-09-05 08:30:59 -0700895 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000896 size_t epos = reqUrl.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700897
898 if (epos != pos)
899 {
900 params->stringParams.emplace_back(
Ed Tanous81ce6092020-12-17 16:54:55 +0000901 reqUrl.substr(pos, epos - pos));
Ed Tanous271584a2019-07-09 16:24:22 -0700902 std::pair<unsigned, RoutingParams> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000903 find(reqUrl,
Ed Tanous271584a2019-07-09 16:24:22 -0700904 &nodes[node->paramChildrens[static_cast<size_t>(
905 ParamType::PATH)]],
906 epos, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700907 updateFound(ret);
908 params->stringParams.pop_back();
909 }
910 }
911
Tanousf00032d2018-11-05 01:18:10 -0300912 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700913 {
914 const std::string& fragment = kv.first;
915 const Node* child = &nodes[kv.second];
916
Ed Tanous81ce6092020-12-17 16:54:55 +0000917 if (reqUrl.compare(pos, fragment.size(), fragment) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700918 {
Tanousf00032d2018-11-05 01:18:10 -0300919 std::pair<unsigned, RoutingParams> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000920 find(reqUrl, child, pos + fragment.size(), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700921 updateFound(ret);
922 }
923 }
924
925 return {found, matchParams};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700926 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700927
928 void add(const std::string& url, unsigned ruleIndex)
929 {
Ed Tanous271584a2019-07-09 16:24:22 -0700930 size_t idx = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700931
932 for (unsigned i = 0; i < url.size(); i++)
933 {
934 char c = url[i];
935 if (c == '<')
936 {
Tanousf00032d2018-11-05 01:18:10 -0300937 const static std::array<std::pair<ParamType, std::string>, 7>
938 paramTraits = {{
939 {ParamType::INT, "<int>"},
940 {ParamType::UINT, "<uint>"},
941 {ParamType::DOUBLE, "<float>"},
942 {ParamType::DOUBLE, "<double>"},
943 {ParamType::STRING, "<str>"},
944 {ParamType::STRING, "<string>"},
945 {ParamType::PATH, "<path>"},
946 }};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700947
Tanousf00032d2018-11-05 01:18:10 -0300948 for (const std::pair<ParamType, std::string>& x : paramTraits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700949 {
Tanousf00032d2018-11-05 01:18:10 -0300950 if (url.compare(i, x.second.size(), x.second) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700951 {
Ed Tanous271584a2019-07-09 16:24:22 -0700952 size_t index = static_cast<size_t>(x.first);
953 if (!nodes[idx].paramChildrens[index])
Ed Tanous1abe55e2018-09-05 08:30:59 -0700954 {
Tanousf00032d2018-11-05 01:18:10 -0300955 unsigned newNodeIdx = newNode();
Ed Tanous271584a2019-07-09 16:24:22 -0700956 nodes[idx].paramChildrens[index] = newNodeIdx;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700957 }
Ed Tanous271584a2019-07-09 16:24:22 -0700958 idx = nodes[idx].paramChildrens[index];
959 i += static_cast<unsigned>(x.second.size());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700960 break;
961 }
962 }
963
964 i--;
965 }
966 else
967 {
968 std::string piece(&c, 1);
969 if (!nodes[idx].children.count(piece))
970 {
Tanousf00032d2018-11-05 01:18:10 -0300971 unsigned newNodeIdx = newNode();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700972 nodes[idx].children.emplace(piece, newNodeIdx);
973 }
974 idx = nodes[idx].children[piece];
975 }
976 }
977 if (nodes[idx].ruleIndex)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700978 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700979 throw std::runtime_error("handler already exists for " + url);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700980 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700981 nodes[idx].ruleIndex = ruleIndex;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700982 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700983
Ed Tanous1abe55e2018-09-05 08:30:59 -0700984 private:
Ed Tanous271584a2019-07-09 16:24:22 -0700985 void debugNodePrint(Node* n, size_t level)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700986 {
Ed Tanous271584a2019-07-09 16:24:22 -0700987 for (size_t i = 0; i < static_cast<size_t>(ParamType::MAX); i++)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700988 {
989 if (n->paramChildrens[i])
990 {
991 BMCWEB_LOG_DEBUG << std::string(
Ed Tanous271584a2019-07-09 16:24:22 -0700992 2U * level, ' ') /*<< "("<<n->paramChildrens[i]<<") "*/;
993 switch (static_cast<ParamType>(i))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700994 {
995 case ParamType::INT:
996 BMCWEB_LOG_DEBUG << "<int>";
997 break;
998 case ParamType::UINT:
999 BMCWEB_LOG_DEBUG << "<uint>";
1000 break;
1001 case ParamType::DOUBLE:
1002 BMCWEB_LOG_DEBUG << "<float>";
1003 break;
1004 case ParamType::STRING:
1005 BMCWEB_LOG_DEBUG << "<str>";
1006 break;
1007 case ParamType::PATH:
1008 BMCWEB_LOG_DEBUG << "<path>";
1009 break;
Ed Tanous23a21a12020-07-25 04:45:05 +00001010 case ParamType::MAX:
Ed Tanous1abe55e2018-09-05 08:30:59 -07001011 BMCWEB_LOG_DEBUG << "<ERROR>";
1012 break;
1013 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001014
Ed Tanous1abe55e2018-09-05 08:30:59 -07001015 debugNodePrint(&nodes[n->paramChildrens[i]], level + 1);
1016 }
1017 }
Tanousf00032d2018-11-05 01:18:10 -03001018 for (const std::pair<std::string, unsigned>& kv : n->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001019 {
1020 BMCWEB_LOG_DEBUG
Ed Tanous271584a2019-07-09 16:24:22 -07001021 << std::string(2U * level, ' ') /*<< "(" << kv.second << ") "*/
Ed Tanous1abe55e2018-09-05 08:30:59 -07001022 << kv.first;
1023 debugNodePrint(&nodes[kv.second], level + 1);
1024 }
1025 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001026
Ed Tanous1abe55e2018-09-05 08:30:59 -07001027 public:
1028 void debugPrint()
1029 {
Ed Tanous271584a2019-07-09 16:24:22 -07001030 debugNodePrint(head(), 0U);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001031 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001032
Ed Tanous1abe55e2018-09-05 08:30:59 -07001033 private:
1034 const Node* head() const
1035 {
1036 return &nodes.front();
1037 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001038
Ed Tanous1abe55e2018-09-05 08:30:59 -07001039 Node* head()
1040 {
1041 return &nodes.front();
1042 }
1043
1044 unsigned newNode()
1045 {
1046 nodes.resize(nodes.size() + 1);
Ed Tanous271584a2019-07-09 16:24:22 -07001047 return static_cast<unsigned>(nodes.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001048 }
1049
1050 std::vector<Node> nodes;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001051};
1052
Ed Tanous1abe55e2018-09-05 08:30:59 -07001053class Router
1054{
1055 public:
Ed Tanous0c0084a2019-10-24 15:57:51 -07001056 Router() = default;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001057
Ed Tanous1abe55e2018-09-05 08:30:59 -07001058 DynamicRule& newRuleDynamic(const std::string& rule)
1059 {
1060 std::unique_ptr<DynamicRule> ruleObject =
1061 std::make_unique<DynamicRule>(rule);
1062 DynamicRule* ptr = ruleObject.get();
Tanousf00032d2018-11-05 01:18:10 -03001063 allRules.emplace_back(std::move(ruleObject));
Ed Tanous7045c8d2017-04-03 10:04:37 -07001064
Ed Tanous1abe55e2018-09-05 08:30:59 -07001065 return *ptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001066 }
1067
Ed Tanous1abe55e2018-09-05 08:30:59 -07001068 template <uint64_t N>
1069 typename black_magic::Arguments<N>::type::template rebind<TaggedRule>&
1070 newRuleTagged(const std::string& rule)
1071 {
1072 using RuleT = typename black_magic::Arguments<N>::type::template rebind<
1073 TaggedRule>;
1074 std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule);
1075 RuleT* ptr = ruleObject.get();
Tanousf00032d2018-11-05 01:18:10 -03001076 allRules.emplace_back(std::move(ruleObject));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001077
1078 return *ptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001079 }
1080
Tanousf00032d2018-11-05 01:18:10 -03001081 void internalAddRuleObject(const std::string& rule, BaseRule* ruleObject)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001082 {
Tanousf00032d2018-11-05 01:18:10 -03001083 if (ruleObject == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001084 {
Tanousf00032d2018-11-05 01:18:10 -03001085 return;
1086 }
Ed Tanous888880a2020-08-24 13:48:50 -07001087 for (size_t method = 0, methodBit = 1; method < maxHttpVerbCount;
Ed Tanous2c70f802020-09-28 14:29:23 -07001088 method++, methodBit <<= 1)
Tanousf00032d2018-11-05 01:18:10 -03001089 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001090 if (ruleObject->methodsBitfield & methodBit)
Tanousf00032d2018-11-05 01:18:10 -03001091 {
1092 perMethods[method].rules.emplace_back(ruleObject);
1093 perMethods[method].trie.add(
Ed Tanous271584a2019-07-09 16:24:22 -07001094 rule, static_cast<unsigned>(
1095 perMethods[method].rules.size() - 1U));
Tanousf00032d2018-11-05 01:18:10 -03001096 // directory case:
1097 // request to `/about' url matches `/about/' rule
1098 if (rule.size() > 2 && rule.back() == '/')
1099 {
1100 perMethods[method].trie.add(
1101 rule.substr(0, rule.size() - 1),
Ed Tanous271584a2019-07-09 16:24:22 -07001102 static_cast<unsigned>(perMethods[method].rules.size() -
1103 1));
Tanousf00032d2018-11-05 01:18:10 -03001104 }
1105 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001106 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001107 }
1108
Ed Tanous1abe55e2018-09-05 08:30:59 -07001109 void validate()
1110 {
Tanousf00032d2018-11-05 01:18:10 -03001111 for (std::unique_ptr<BaseRule>& rule : allRules)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001112 {
1113 if (rule)
1114 {
Tanousf00032d2018-11-05 01:18:10 -03001115 std::unique_ptr<BaseRule> upgraded = rule->upgrade();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001116 if (upgraded)
Ed Tanous3174e4d2020-10-07 11:41:22 -07001117 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001118 rule = std::move(upgraded);
Ed Tanous3174e4d2020-10-07 11:41:22 -07001119 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001120 rule->validate();
Tanousf00032d2018-11-05 01:18:10 -03001121 internalAddRuleObject(rule->rule, rule.get());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001122 }
1123 }
Tanousf00032d2018-11-05 01:18:10 -03001124 for (PerMethod& perMethod : perMethods)
1125 {
1126 perMethod.trie.validate();
1127 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001128 }
1129
Ed Tanous1abe55e2018-09-05 08:30:59 -07001130 template <typename Adaptor>
1131 void handleUpgrade(const Request& req, Response& res, Adaptor&& adaptor)
1132 {
Ed Tanous271584a2019-07-09 16:24:22 -07001133 if (static_cast<size_t>(req.method()) >= perMethods.size())
Ed Tanous888880a2020-08-24 13:48:50 -07001134 {
1135 res.result(boost::beast::http::status::not_found);
1136 res.end();
Tanousf00032d2018-11-05 01:18:10 -03001137 return;
Ed Tanous888880a2020-08-24 13:48:50 -07001138 }
Tanousf00032d2018-11-05 01:18:10 -03001139
Ed Tanous271584a2019-07-09 16:24:22 -07001140 PerMethod& perMethod = perMethods[static_cast<size_t>(req.method())];
Tanousf00032d2018-11-05 01:18:10 -03001141 Trie& trie = perMethod.trie;
1142 std::vector<BaseRule*>& rules = perMethod.rules;
1143
1144 const std::pair<unsigned, RoutingParams>& found = trie.find(req.url);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001145 unsigned ruleIndex = found.first;
1146 if (!ruleIndex)
1147 {
1148 BMCWEB_LOG_DEBUG << "Cannot match rules " << req.url;
Ed Tanousde5c9f32019-03-26 09:17:55 -07001149 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001150 res.end();
1151 return;
1152 }
1153
1154 if (ruleIndex >= rules.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -07001155 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001156 throw std::runtime_error("Trie internal structure corrupted!");
Ed Tanous3174e4d2020-10-07 11:41:22 -07001157 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001158
1159 if (ruleIndex == ruleSpecialRedirectSlash)
1160 {
1161 BMCWEB_LOG_INFO << "Redirecting to a url with trailing slash: "
1162 << req.url;
Ed Tanousde5c9f32019-03-26 09:17:55 -07001163 res.result(boost::beast::http::status::moved_permanently);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001164
1165 // TODO absolute url building
1166 if (req.getHeaderValue("Host").empty())
1167 {
1168 res.addHeader("Location", std::string(req.url) + "/");
1169 }
1170 else
1171 {
1172 res.addHeader(
1173 "Location",
1174 req.isSecure
1175 ? "https://"
1176 : "http://" + std::string(req.getHeaderValue("Host")) +
1177 std::string(req.url) + "/");
1178 }
1179 res.end();
1180 return;
1181 }
1182
Ed Tanous271584a2019-07-09 16:24:22 -07001183 if ((rules[ruleIndex]->getMethods() &
1184 (1U << static_cast<size_t>(req.method()))) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001185 {
1186 BMCWEB_LOG_DEBUG << "Rule found but method mismatch: " << req.url
1187 << " with " << req.methodString() << "("
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();
Ed Tanousde5c9f32019-03-26 09:17:55 -07001190 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001191 res.end();
1192 return;
1193 }
1194
1195 BMCWEB_LOG_DEBUG << "Matched rule (upgrade) '" << rules[ruleIndex]->rule
Ed Tanous271584a2019-07-09 16:24:22 -07001196 << "' " << static_cast<uint32_t>(req.method()) << " / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001197 << rules[ruleIndex]->getMethods();
1198
1199 // any uncaught exceptions become 500s
1200 try
1201 {
1202 rules[ruleIndex]->handleUpgrade(req, res, std::move(adaptor));
1203 }
1204 catch (std::exception& e)
1205 {
1206 BMCWEB_LOG_ERROR << "An uncaught exception occurred: " << e.what();
Ed Tanousde5c9f32019-03-26 09:17:55 -07001207 res.result(boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001208 res.end();
1209 return;
1210 }
1211 catch (...)
1212 {
1213 BMCWEB_LOG_ERROR
1214 << "An uncaught exception occurred. The type was unknown "
1215 "so no information was available.";
Ed Tanousde5c9f32019-03-26 09:17:55 -07001216 res.result(boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001217 res.end();
1218 return;
1219 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001220 }
1221
zhanghch058d1b46d2021-04-01 11:18:24 +08001222 void handle(Request& req,
1223 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001224 {
Ed Tanous271584a2019-07-09 16:24:22 -07001225 if (static_cast<size_t>(req.method()) >= perMethods.size())
Ed Tanous888880a2020-08-24 13:48:50 -07001226 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001227 asyncResp->res.result(boost::beast::http::status::not_found);
Tanousf00032d2018-11-05 01:18:10 -03001228 return;
Ed Tanous888880a2020-08-24 13:48:50 -07001229 }
Ed Tanous271584a2019-07-09 16:24:22 -07001230 PerMethod& perMethod = perMethods[static_cast<size_t>(req.method())];
Tanousf00032d2018-11-05 01:18:10 -03001231 Trie& trie = perMethod.trie;
1232 std::vector<BaseRule*>& rules = perMethod.rules;
1233
1234 const std::pair<unsigned, RoutingParams>& found = trie.find(req.url);
Ed Tanous7045c8d2017-04-03 10:04:37 -07001235
Ed Tanous1abe55e2018-09-05 08:30:59 -07001236 unsigned ruleIndex = found.first;
1237
1238 if (!ruleIndex)
1239 {
Ed Tanous2634dcd2019-03-26 09:28:06 -07001240 // Check to see if this url exists at any verb
1241 for (const PerMethod& p : perMethods)
1242 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001243 const std::pair<unsigned, RoutingParams>& found2 =
Ed Tanous2634dcd2019-03-26 09:28:06 -07001244 p.trie.find(req.url);
Ed Tanous23a21a12020-07-25 04:45:05 +00001245 if (found2.first > 0)
Ed Tanous2634dcd2019-03-26 09:28:06 -07001246 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001247 asyncResp->res.result(
1248 boost::beast::http::status::method_not_allowed);
Ed Tanous2634dcd2019-03-26 09:28:06 -07001249 return;
1250 }
1251 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001252 BMCWEB_LOG_DEBUG << "Cannot match rules " << req.url;
zhanghch058d1b46d2021-04-01 11:18:24 +08001253 asyncResp->res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001254 return;
1255 }
1256
1257 if (ruleIndex >= rules.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -07001258 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001259 throw std::runtime_error("Trie internal structure corrupted!");
Ed Tanous3174e4d2020-10-07 11:41:22 -07001260 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001261
1262 if (ruleIndex == ruleSpecialRedirectSlash)
1263 {
1264 BMCWEB_LOG_INFO << "Redirecting to a url with trailing slash: "
1265 << req.url;
zhanghch058d1b46d2021-04-01 11:18:24 +08001266 asyncResp->res.result(
1267 boost::beast::http::status::moved_permanently);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001268
1269 // TODO absolute url building
1270 if (req.getHeaderValue("Host").empty())
1271 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001272 asyncResp->res.addHeader("Location",
1273 std::string(req.url) + "/");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001274 }
1275 else
1276 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001277 asyncResp->res.addHeader(
1278 "Location", (req.isSecure ? "https://" : "http://") +
1279 std::string(req.getHeaderValue("Host")) +
1280 std::string(req.url) + "/");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001281 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001282 return;
1283 }
1284
Ed Tanous271584a2019-07-09 16:24:22 -07001285 if ((rules[ruleIndex]->getMethods() &
1286 (1U << static_cast<uint32_t>(req.method()))) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001287 {
1288 BMCWEB_LOG_DEBUG << "Rule found but method mismatch: " << req.url
1289 << " with " << req.methodString() << "("
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();
zhanghch058d1b46d2021-04-01 11:18:24 +08001292 asyncResp->res.result(
1293 boost::beast::http::status::method_not_allowed);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001294 return;
1295 }
1296
1297 BMCWEB_LOG_DEBUG << "Matched rule '" << rules[ruleIndex]->rule << "' "
Ed Tanous271584a2019-07-09 16:24:22 -07001298 << static_cast<uint32_t>(req.method()) << " / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001299 << rules[ruleIndex]->getMethods();
1300
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001301 if (req.session == nullptr)
James Feist7166bf02019-12-10 16:52:14 +00001302 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001303 rules[ruleIndex]->handle(req, asyncResp, found.second);
James Feist7166bf02019-12-10 16:52:14 +00001304 return;
1305 }
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001306
1307 crow::connections::systemBus->async_method_call(
zhanghch058d1b46d2021-04-01 11:18:24 +08001308 [&req, asyncResp, &rules, ruleIndex, found](
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001309 const boost::system::error_code ec,
1310 std::map<std::string, std::variant<bool, std::string,
1311 std::vector<std::string>>>
1312 userInfo) {
1313 if (ec)
1314 {
1315 BMCWEB_LOG_ERROR << "GetUserInfo failed...";
zhanghch058d1b46d2021-04-01 11:18:24 +08001316 asyncResp->res.result(
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001317 boost::beast::http::status::internal_server_error);
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001318 return;
1319 }
1320
1321 const std::string* userRolePtr = nullptr;
1322 auto userInfoIter = userInfo.find("UserPrivilege");
1323 if (userInfoIter != userInfo.end())
1324 {
1325 userRolePtr =
1326 std::get_if<std::string>(&userInfoIter->second);
1327 }
1328
1329 std::string userRole{};
1330 if (userRolePtr != nullptr)
1331 {
1332 userRole = *userRolePtr;
1333 BMCWEB_LOG_DEBUG << "userName = " << req.session->username
1334 << " userRole = " << *userRolePtr;
1335 }
1336
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001337 bool* remoteUserPtr = nullptr;
1338 auto remoteUserIter = userInfo.find("RemoteUser");
1339 if (remoteUserIter != userInfo.end())
1340 {
1341 remoteUserPtr = std::get_if<bool>(&remoteUserIter->second);
1342 }
1343 if (remoteUserPtr == nullptr)
1344 {
1345 BMCWEB_LOG_ERROR
1346 << "RemoteUser property missing or wrong type";
zhanghch058d1b46d2021-04-01 11:18:24 +08001347 asyncResp->res.result(
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001348 boost::beast::http::status::internal_server_error);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001349 return;
1350 }
1351 bool remoteUser = *remoteUserPtr;
1352
1353 bool passwordExpired = false; // default for remote user
1354 if (!remoteUser)
1355 {
1356 bool* passwordExpiredPtr = nullptr;
1357 auto passwordExpiredIter =
1358 userInfo.find("UserPasswordExpired");
1359 if (passwordExpiredIter != userInfo.end())
1360 {
1361 passwordExpiredPtr =
1362 std::get_if<bool>(&passwordExpiredIter->second);
1363 }
1364 if (passwordExpiredPtr != nullptr)
1365 {
1366 passwordExpired = *passwordExpiredPtr;
1367 }
1368 else
1369 {
1370 BMCWEB_LOG_ERROR
1371 << "UserPasswordExpired property is expected for"
1372 " local user but is missing or wrong type";
zhanghch058d1b46d2021-04-01 11:18:24 +08001373 asyncResp->res.result(
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001374 boost::beast::http::status::internal_server_error);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001375 return;
1376 }
1377 }
1378
Ed Tanous23a21a12020-07-25 04:45:05 +00001379 // Get the userprivileges from the role
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001380 redfish::Privileges userPrivileges =
1381 redfish::getUserPrivileges(userRole);
1382
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001383 // Set isConfigureSelfOnly based on D-Bus results. This
1384 // ignores the results from both pamAuthenticateUser and the
1385 // value from any previous use of this session.
1386 req.session->isConfigureSelfOnly = passwordExpired;
1387
Ed Tanous23a21a12020-07-25 04:45:05 +00001388 // Modifyprivileges if isConfigureSelfOnly.
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001389 if (req.session->isConfigureSelfOnly)
1390 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001391 // Remove allprivileges except ConfigureSelf
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001392 userPrivileges = userPrivileges.intersection(
1393 redfish::Privileges{"ConfigureSelf"});
1394 BMCWEB_LOG_DEBUG << "Operation limited to ConfigureSelf";
1395 }
1396
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001397 if (!rules[ruleIndex]->checkPrivileges(userPrivileges))
1398 {
zhanghch058d1b46d2021-04-01 11:18:24 +08001399 asyncResp->res.result(
1400 boost::beast::http::status::forbidden);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001401 if (req.session->isConfigureSelfOnly)
1402 {
1403 redfish::messages::passwordChangeRequired(
zhanghch058d1b46d2021-04-01 11:18:24 +08001404 asyncResp->res,
1405 "/redfish/v1/AccountService/Accounts/" +
1406 req.session->username);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001407 }
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001408 return;
1409 }
1410
1411 req.userRole = userRole;
zhanghch058d1b46d2021-04-01 11:18:24 +08001412 rules[ruleIndex]->handle(req, asyncResp, found.second);
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001413 },
1414 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1415 "xyz.openbmc_project.User.Manager", "GetUserInfo",
1416 req.session->username);
Ed Tanous7045c8d2017-04-03 10:04:37 -07001417 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001418
Ed Tanous1abe55e2018-09-05 08:30:59 -07001419 void debugPrint()
1420 {
Ed Tanous271584a2019-07-09 16:24:22 -07001421 for (size_t i = 0; i < perMethods.size(); i++)
Tanousf00032d2018-11-05 01:18:10 -03001422 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001423 BMCWEB_LOG_DEBUG << boost::beast::http::to_string(
1424 static_cast<boost::beast::http::verb>(i));
Tanousf00032d2018-11-05 01:18:10 -03001425 perMethods[i].trie.debugPrint();
1426 }
Ed Tanous3dac7492017-08-02 13:46:20 -07001427 }
Ed Tanousb4a7bfa2017-04-04 17:23:00 -07001428
Ed Tanous1abe55e2018-09-05 08:30:59 -07001429 std::vector<const std::string*> getRoutes(const std::string& parent)
1430 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001431 std::vector<const std::string*> ret;
Tanousf00032d2018-11-05 01:18:10 -03001432
1433 for (const PerMethod& pm : perMethods)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001434 {
Tanousf00032d2018-11-05 01:18:10 -03001435 std::vector<unsigned> x;
1436 pm.trie.findRouteIndexes(parent, x);
1437 for (unsigned index : x)
1438 {
1439 ret.push_back(&pm.rules[index]->rule);
1440 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001441 }
1442 return ret;
1443 }
1444
1445 private:
Tanousf00032d2018-11-05 01:18:10 -03001446 struct PerMethod
1447 {
1448 std::vector<BaseRule*> rules;
1449 Trie trie;
1450 // rule index 0, 1 has special meaning; preallocate it to avoid
1451 // duplication.
1452 PerMethod() : rules(2)
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001453 {}
Tanousf00032d2018-11-05 01:18:10 -03001454 };
Ed Tanous888880a2020-08-24 13:48:50 -07001455
1456 const static size_t maxHttpVerbCount =
Ed Tanouscc090442020-10-07 08:20:50 -07001457 static_cast<size_t>(boost::beast::http::verb::unlink);
Ed Tanous888880a2020-08-24 13:48:50 -07001458
Tanousf00032d2018-11-05 01:18:10 -03001459 std::array<PerMethod, maxHttpVerbCount> perMethods;
1460 std::vector<std::unique_ptr<BaseRule>> allRules;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001461};
Ed Tanous1abe55e2018-09-05 08:30:59 -07001462} // namespace crow