blob: 9c6a15b31b6d8a2000c662b0b3f1aeefcba11ce5 [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:
Tanousf00032d2018-11-05 01:18:10 -030033 BaseRule(std::string thisRule) : rule(std::move(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
Ed Tanous1abe55e2018-09-05 08:30:59 -070048 virtual void handle(const Request&, Response&, const RoutingParams&) = 0;
Ed Tanousceac6f72018-12-02 11:58:47 -080049 virtual void handleUpgrade(const Request&, Response& res,
50 boost::asio::ip::tcp::socket&&)
Ed Tanous1abe55e2018-09-05 08:30:59 -070051 {
Ed Tanousde5c9f32019-03-26 09:17:55 -070052 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -070053 res.end();
54 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -070055#ifdef BMCWEB_ENABLE_SSL
Ed Tanousceac6f72018-12-02 11:58:47 -080056 virtual void
57 handleUpgrade(const Request&, Response& res,
58 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>&&)
Ed Tanous1abe55e2018-09-05 08:30:59 -070059 {
Ed Tanousde5c9f32019-03-26 09:17:55 -070060 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -070061 res.end();
62 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070063#endif
64
Ed Tanous271584a2019-07-09 16:24:22 -070065 size_t getMethods()
Ed Tanous1abe55e2018-09-05 08:30:59 -070066 {
67 return methodsBitfield;
68 }
Ed Tanous7045c8d2017-04-03 10:04:37 -070069
Tanousf00032d2018-11-05 01:18:10 -030070 bool checkPrivileges(const redfish::Privileges& userPrivileges)
71 {
72 // If there are no privileges assigned, assume no privileges
73 // required
74 if (privilegesSet.empty())
75 {
76 return true;
77 }
78
79 for (const redfish::Privileges& requiredPrivileges : privilegesSet)
80 {
81 if (userPrivileges.isSupersetOf(requiredPrivileges))
82 {
83 return true;
84 }
85 }
86 return false;
87 }
88
Ed Tanous271584a2019-07-09 16:24:22 -070089 size_t methodsBitfield{
90 1 << static_cast<size_t>(boost::beast::http::verb::get)};
Ed Tanous7045c8d2017-04-03 10:04:37 -070091
Tanousf00032d2018-11-05 01:18:10 -030092 std::vector<redfish::Privileges> privilegesSet;
93
Ed Tanous1abe55e2018-09-05 08:30:59 -070094 std::string rule;
95 std::string nameStr;
Ed Tanous7045c8d2017-04-03 10:04:37 -070096
Ed Tanous1abe55e2018-09-05 08:30:59 -070097 std::unique_ptr<BaseRule> ruleToUpgrade;
Ed Tanous7045c8d2017-04-03 10:04:37 -070098
Ed Tanous1abe55e2018-09-05 08:30:59 -070099 friend class Router;
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500100 template <typename T>
101 friend struct RuleParameterTraits;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700102};
103
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104namespace detail
105{
106namespace routing_handler_call_helper
107{
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500108template <typename T, int Pos>
109struct CallPair
Ed Tanous1abe55e2018-09-05 08:30:59 -0700110{
111 using type = T;
112 static const int pos = Pos;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700113};
114
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500115template <typename H1>
116struct CallParams
Ed Tanous1abe55e2018-09-05 08:30:59 -0700117{
118 H1& handler;
119 const RoutingParams& params;
120 const Request& req;
121 Response& res;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700122};
123
124template <typename F, int NInt, int NUint, int NDouble, int NString,
125 typename S1, typename S2>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700126struct Call
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500127{};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700128
129template <typename F, int NInt, int NUint, int NDouble, int NString,
130 typename... Args1, typename... Args2>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700131struct Call<F, NInt, NUint, NDouble, NString, black_magic::S<int64_t, Args1...>,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700132 black_magic::S<Args2...>>
133{
134 void operator()(F cparams)
135 {
136 using pushed = typename black_magic::S<Args2...>::template push_back<
137 CallPair<int64_t, NInt>>;
138 Call<F, NInt + 1, NUint, NDouble, NString, black_magic::S<Args1...>,
139 pushed>()(cparams);
140 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700141};
142
143template <typename F, int NInt, int NUint, int NDouble, int NString,
144 typename... Args1, typename... Args2>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700145struct Call<F, NInt, NUint, NDouble, NString,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700146 black_magic::S<uint64_t, Args1...>, black_magic::S<Args2...>>
147{
148 void operator()(F cparams)
149 {
150 using pushed = typename black_magic::S<Args2...>::template push_back<
151 CallPair<uint64_t, NUint>>;
152 Call<F, NInt, NUint + 1, NDouble, NString, black_magic::S<Args1...>,
153 pushed>()(cparams);
154 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700155};
156
157template <typename F, int NInt, int NUint, int NDouble, int NString,
158 typename... Args1, typename... Args2>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700159struct Call<F, NInt, NUint, NDouble, NString, black_magic::S<double, Args1...>,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700160 black_magic::S<Args2...>>
161{
162 void operator()(F cparams)
163 {
164 using pushed = typename black_magic::S<Args2...>::template push_back<
165 CallPair<double, NDouble>>;
166 Call<F, NInt, NUint, NDouble + 1, NString, black_magic::S<Args1...>,
167 pushed>()(cparams);
168 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700169};
170
171template <typename F, int NInt, int NUint, int NDouble, int NString,
172 typename... Args1, typename... Args2>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700173struct Call<F, NInt, NUint, NDouble, NString,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700174 black_magic::S<std::string, Args1...>, black_magic::S<Args2...>>
175{
176 void operator()(F cparams)
177 {
178 using pushed = typename black_magic::S<Args2...>::template push_back<
179 CallPair<std::string, NString>>;
180 Call<F, NInt, NUint, NDouble, NString + 1, black_magic::S<Args1...>,
181 pushed>()(cparams);
182 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700183};
184
185template <typename F, int NInt, int NUint, int NDouble, int NString,
186 typename... Args1>
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700187struct Call<F, NInt, NUint, NDouble, NString, black_magic::S<>,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700188 black_magic::S<Args1...>>
189{
190 void operator()(F cparams)
191 {
192 cparams.handler(
193 cparams.req, cparams.res,
194 cparams.params.template get<typename Args1::type>(Args1::pos)...);
195 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700196};
197
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500198template <typename Func, typename... ArgsWrapped>
199struct Wrapped
Ed Tanous1abe55e2018-09-05 08:30:59 -0700200{
201 template <typename... Args>
202 void set(
203 Func f,
204 typename std::enable_if<
205 !std::is_same<
206 typename std::tuple_element<0, std::tuple<Args..., void>>::type,
207 const Request&>::value,
208 int>::type = 0)
209 {
Tanousf00032d2018-11-05 01:18:10 -0300210 handler = [f = std::move(f)](const Request&, Response& res,
211 Args... args) {
Ed Tanousde5c9f32019-03-26 09:17:55 -0700212 res.result(f(args...));
Tanousf00032d2018-11-05 01:18:10 -0300213 res.end();
214 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700215 }
216
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500217 template <typename Req, typename... Args>
218 struct ReqHandlerWrapper
Ed Tanous1abe55e2018-09-05 08:30:59 -0700219 {
Ed Tanous23a21a12020-07-25 04:45:05 +0000220 ReqHandlerWrapper(Func fIn) : f(std::move(fIn))
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500221 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700222
Ed Tanous1abe55e2018-09-05 08:30:59 -0700223 void operator()(const Request& req, Response& res, Args... args)
224 {
Ed Tanousde5c9f32019-03-26 09:17:55 -0700225 res.result(f(req, args...));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700226 res.end();
227 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700228
Ed Tanous1abe55e2018-09-05 08:30:59 -0700229 Func f;
230 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700231
Ed Tanous1abe55e2018-09-05 08:30:59 -0700232 template <typename... Args>
233 void set(
234 Func f,
235 typename std::enable_if<
236 std::is_same<
237 typename std::tuple_element<0, std::tuple<Args..., void>>::type,
238 const Request&>::value &&
239 !std::is_same<typename std::tuple_element<
240 1, std::tuple<Args..., void, void>>::type,
241 Response&>::value,
242 int>::type = 0)
243 {
244 handler = ReqHandlerWrapper<Args...>(std::move(f));
245 /*handler = (
246 [f = std::move(f)]
247 (const Request& req, Response& res, Args... args){
Ed Tanousde5c9f32019-03-26 09:17:55 -0700248 res.result(f(req, args...));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700249 res.end();
250 });*/
251 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700252
Ed Tanous1abe55e2018-09-05 08:30:59 -0700253 template <typename... Args>
254 void set(
255 Func f,
256 typename std::enable_if<
257 std::is_same<
258 typename std::tuple_element<0, std::tuple<Args..., void>>::type,
259 const Request&>::value &&
260 std::is_same<typename std::tuple_element<
261 1, std::tuple<Args..., void, void>>::type,
262 Response&>::value,
263 int>::type = 0)
264 {
265 handler = std::move(f);
266 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700267
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500268 template <typename... Args>
269 struct HandlerTypeHelper
Ed Tanous1abe55e2018-09-05 08:30:59 -0700270 {
271 using type =
272 std::function<void(const crow::Request&, crow::Response&, Args...)>;
273 using args_type =
Ed Tanous988403c2020-08-24 11:29:49 -0700274 black_magic::S<typename black_magic::PromoteT<Args>...>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700275 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700276
Ed Tanous1abe55e2018-09-05 08:30:59 -0700277 template <typename... Args>
278 struct HandlerTypeHelper<const Request&, Args...>
279 {
280 using type =
281 std::function<void(const crow::Request&, crow::Response&, Args...)>;
282 using args_type =
Ed Tanous988403c2020-08-24 11:29:49 -0700283 black_magic::S<typename black_magic::PromoteT<Args>...>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700284 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700285
Ed Tanous1abe55e2018-09-05 08:30:59 -0700286 template <typename... Args>
287 struct HandlerTypeHelper<const Request&, Response&, Args...>
288 {
289 using type =
290 std::function<void(const crow::Request&, crow::Response&, Args...)>;
291 using args_type =
Ed Tanous988403c2020-08-24 11:29:49 -0700292 black_magic::S<typename black_magic::PromoteT<Args>...>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700293 };
294
295 typename HandlerTypeHelper<ArgsWrapped...>::type handler;
296
297 void operator()(const Request& req, Response& res,
298 const RoutingParams& params)
299 {
300 detail::routing_handler_call_helper::Call<
301 detail::routing_handler_call_helper::CallParams<decltype(handler)>,
302 0, 0, 0, 0, typename HandlerTypeHelper<ArgsWrapped...>::args_type,
303 black_magic::S<>>()(
304 detail::routing_handler_call_helper::CallParams<decltype(handler)>{
305 handler, params, req, res});
306 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700307};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700308} // namespace routing_handler_call_helper
309} // namespace detail
Ed Tanous7045c8d2017-04-03 10:04:37 -0700310
Ed Tanous1abe55e2018-09-05 08:30:59 -0700311class WebSocketRule : public BaseRule
312{
313 using self_t = WebSocketRule;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700314
Ed Tanous1abe55e2018-09-05 08:30:59 -0700315 public:
Ed Tanous23a21a12020-07-25 04:45:05 +0000316 WebSocketRule(std::string ruleIn) : BaseRule(std::move(ruleIn))
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500317 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700318
Ed Tanous1abe55e2018-09-05 08:30:59 -0700319 void validate() override
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500320 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700321
Ed Tanous1abe55e2018-09-05 08:30:59 -0700322 void handle(const Request&, Response& res, const RoutingParams&) override
323 {
Ed Tanousde5c9f32019-03-26 09:17:55 -0700324 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700325 res.end();
326 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700327
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000328 void handleUpgrade(const Request& req, Response&,
Ed Tanousceac6f72018-12-02 11:58:47 -0800329 boost::asio::ip::tcp::socket&& adaptor) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700330 {
Ratan Gupta02453b12019-10-22 14:43:36 +0530331 std::shared_ptr<
332 crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>>
333 myConnection = std::make_shared<
334 crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>>(
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000335 req, std::move(adaptor), openHandler, messageHandler,
Ratan Gupta02453b12019-10-22 14:43:36 +0530336 closeHandler, errorHandler);
337 myConnection->start();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700338 }
339#ifdef BMCWEB_ENABLE_SSL
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000340 void handleUpgrade(const Request& req, Response&,
Ed Tanousceac6f72018-12-02 11:58:47 -0800341 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>&&
342 adaptor) override
Ed Tanous1abe55e2018-09-05 08:30:59 -0700343 {
Ed Tanousceac6f72018-12-02 11:58:47 -0800344 std::shared_ptr<crow::websocket::ConnectionImpl<
345 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>>
346 myConnection = std::make_shared<crow::websocket::ConnectionImpl<
347 boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>>(
Jan Sowinskiee52ae12020-01-09 16:28:32 +0000348 req, std::move(adaptor), openHandler, messageHandler,
Ed Tanousceac6f72018-12-02 11:58:47 -0800349 closeHandler, errorHandler);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700350 myConnection->start();
351 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700352#endif
353
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500354 template <typename Func>
355 self_t& onopen(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700356 {
357 openHandler = f;
358 return *this;
359 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700360
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500361 template <typename Func>
362 self_t& onmessage(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700363 {
364 messageHandler = f;
365 return *this;
366 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700367
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500368 template <typename Func>
369 self_t& onclose(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700370 {
371 closeHandler = f;
372 return *this;
373 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700374
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500375 template <typename Func>
376 self_t& onerror(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700377 {
378 errorHandler = f;
379 return *this;
380 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700381
Ed Tanous1abe55e2018-09-05 08:30:59 -0700382 protected:
Iwona Klimaszewskac0a1c8a2019-07-12 18:26:38 +0200383 std::function<void(crow::websocket::Connection&,
384 std::shared_ptr<bmcweb::AsyncResp>)>
385 openHandler;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700386 std::function<void(crow::websocket::Connection&, const std::string&, bool)>
387 messageHandler;
388 std::function<void(crow::websocket::Connection&, const std::string&)>
389 closeHandler;
390 std::function<void(crow::websocket::Connection&)> errorHandler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700391};
392
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500393template <typename T>
394struct RuleParameterTraits
Ed Tanous1abe55e2018-09-05 08:30:59 -0700395{
396 using self_t = T;
397 WebSocketRule& websocket()
398 {
Ed Tanous271584a2019-07-09 16:24:22 -0700399 self_t* self = static_cast<self_t*>(this);
400 WebSocketRule* p = new WebSocketRule(self->rule);
401 self->ruleToUpgrade.reset(p);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700402 return *p;
403 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700404
Ed Tanousb5a76932020-09-29 16:16:58 -0700405 self_t& name(const std::string& name) noexcept
Ed Tanous1abe55e2018-09-05 08:30:59 -0700406 {
Ed Tanous271584a2019-07-09 16:24:22 -0700407 self_t* self = static_cast<self_t*>(this);
408 self->nameStr = std::move(name);
409 return *self;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700410 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700411
Ed Tanous1abe55e2018-09-05 08:30:59 -0700412 self_t& methods(boost::beast::http::verb method)
413 {
Ed Tanous271584a2019-07-09 16:24:22 -0700414 self_t* self = static_cast<self_t*>(this);
415 self->methodsBitfield = 1U << static_cast<size_t>(method);
416 return *self;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700417 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700418
Ed Tanous1abe55e2018-09-05 08:30:59 -0700419 template <typename... MethodArgs>
420 self_t& methods(boost::beast::http::verb method, MethodArgs... args_method)
421 {
Ed Tanous271584a2019-07-09 16:24:22 -0700422 self_t* self = static_cast<self_t*>(this);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700423 methods(args_method...);
Ed Tanous271584a2019-07-09 16:24:22 -0700424 self->methodsBitfield |= 1U << static_cast<size_t>(method);
425 return *self;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700426 }
Tanousf00032d2018-11-05 01:18:10 -0300427
428 template <typename... MethodArgs>
Ed Tanous23a21a12020-07-25 04:45:05 +0000429 self_t& privileges(std::initializer_list<const char*> l)
Tanousf00032d2018-11-05 01:18:10 -0300430 {
Ed Tanous271584a2019-07-09 16:24:22 -0700431 self_t* self = static_cast<self_t*>(this);
432 self->privilegesSet.emplace_back(l);
433 return *self;
Tanousf00032d2018-11-05 01:18:10 -0300434 }
435
436 template <typename... MethodArgs>
Ed Tanous23a21a12020-07-25 04:45:05 +0000437 self_t& privileges(const std::vector<redfish::Privileges>& p)
Tanousf00032d2018-11-05 01:18:10 -0300438 {
Ed Tanous271584a2019-07-09 16:24:22 -0700439 self_t* self = static_cast<self_t*>(this);
Tanousf00032d2018-11-05 01:18:10 -0300440 for (const redfish::Privileges& privilege : p)
441 {
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 Tanous23a21a12020-07-25 04:45:05 +0000451 DynamicRule(std::string ruleIn) : BaseRule(std::move(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
Ed Tanous1abe55e2018-09-05 08:30:59 -0700463 void handle(const Request& req, Response& res,
464 const RoutingParams& params) override
465 {
466 erasedHandler(req, res, params);
467 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700468
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500469 template <typename Func>
470 void operator()(Func f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700471 {
472 using function_t = utility::function_traits<Func>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700473 erasedHandler =
Ed Tanous988403c2020-08-24 11:29:49 -0700474 wrap(std::move(f),
475 std::make_integer_sequence<unsigned, function_t::arity>{});
Ed Tanous1abe55e2018-09-05 08:30:59 -0700476 }
477
478 // enable_if Arg1 == request && Arg2 == Response
Gunnar Mills6be0e402020-07-08 13:21:51 -0500479 // enable_if Arg1 == request && Arg2 != response
Ed Tanous1abe55e2018-09-05 08:30:59 -0700480 // enable_if Arg1 != request
481
482 template <typename Func, unsigned... Indices>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700483 std::function<void(const Request&, Response&, const RoutingParams&)>
Ed Tanous988403c2020-08-24 11:29:49 -0700484 wrap(Func f, std::integer_sequence<unsigned, Indices...>)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700485 {
Ed Tanous988403c2020-08-24 11:29:49 -0700486 using function_t = crow::utility::function_traits<Func>;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700487
488 if (!black_magic::isParameterTagCompatible(
Ed Tanous988403c2020-08-24 11:29:49 -0700489 black_magic::getParameterTag(rule.c_str()),
490 black_magic::computeParameterTagFromArgsList<
Ed Tanous1abe55e2018-09-05 08:30:59 -0700491 typename function_t::template arg<Indices>...>::value))
492 {
493 throw std::runtime_error("routeDynamic: Handler type is mismatched "
494 "with URL parameters: " +
495 rule);
496 }
497 auto ret = detail::routing_handler_call_helper::Wrapped<
498 Func, typename function_t::template arg<Indices>...>();
499 ret.template set<typename function_t::template arg<Indices>...>(
500 std::move(f));
501 return ret;
502 }
503
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500504 template <typename Func>
505 void operator()(std::string name, Func&& f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700506 {
507 nameStr = std::move(name);
508 (*this).template operator()<Func>(std::forward(f));
509 }
510
511 private:
512 std::function<void(const Request&, Response&, const RoutingParams&)>
513 erasedHandler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700514};
515
516template <typename... Args>
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500517class TaggedRule :
518 public BaseRule,
519 public RuleParameterTraits<TaggedRule<Args...>>
Ed Tanous1abe55e2018-09-05 08:30:59 -0700520{
521 public:
522 using self_t = TaggedRule<Args...>;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700523
Ed Tanousb5a76932020-09-29 16:16:58 -0700524 TaggedRule(const std::string& ruleIn) : BaseRule(std::move(ruleIn))
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500525 {}
Ed Tanous7045c8d2017-04-03 10:04:37 -0700526
Ed Tanous1abe55e2018-09-05 08:30:59 -0700527 void validate() override
528 {
529 if (!handler)
530 {
531 throw std::runtime_error(nameStr + (!nameStr.empty() ? ": " : "") +
532 "no handler for url " + rule);
533 }
534 }
535
536 template <typename Func>
537 typename std::enable_if<
538 black_magic::CallHelper<Func, black_magic::S<Args...>>::value,
539 void>::type
540 operator()(Func&& f)
541 {
542 static_assert(
543 black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
544 black_magic::CallHelper<
545 Func, black_magic::S<crow::Request, Args...>>::value,
546 "Handler type is mismatched with URL parameters");
547 static_assert(
548 !std::is_same<void, decltype(f(std::declval<Args>()...))>::value,
549 "Handler function cannot have void return type; valid return "
550 "types: "
Gunnar Mills6be0e402020-07-08 13:21:51 -0500551 "string, int, crow::response, nlohmann::json");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700552
553 handler = [f = std::move(f)](const Request&, Response& res,
554 Args... args) {
Ed Tanousde5c9f32019-03-26 09:17:55 -0700555 res.result(f(args...));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700556 res.end();
557 };
558 }
559
560 template <typename Func>
561 typename std::enable_if<
562 !black_magic::CallHelper<Func, black_magic::S<Args...>>::value &&
Ed Tanous7045c8d2017-04-03 10:04:37 -0700563 black_magic::CallHelper<
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700564 Func, black_magic::S<crow::Request, Args...>>::value,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700565 void>::type
566 operator()(Func&& f)
567 {
568 static_assert(
569 black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
570 black_magic::CallHelper<
571 Func, black_magic::S<crow::Request, Args...>>::value,
572 "Handler type is mismatched with URL parameters");
573 static_assert(
574 !std::is_same<void, decltype(f(std::declval<crow::Request>(),
575 std::declval<Args>()...))>::value,
576 "Handler function cannot have void return type; valid return "
577 "types: "
Gunnar Mills6be0e402020-07-08 13:21:51 -0500578 "string, int, crow::response,nlohmann::json");
Ed Tanous7045c8d2017-04-03 10:04:37 -0700579
Ed Tanous1abe55e2018-09-05 08:30:59 -0700580 handler = [f = std::move(f)](const crow::Request& req,
581 crow::Response& res, Args... args) {
Ed Tanousde5c9f32019-03-26 09:17:55 -0700582 res.result(f(req, args...));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700583 res.end();
584 };
585 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700586
Ed Tanous1abe55e2018-09-05 08:30:59 -0700587 template <typename Func>
588 typename std::enable_if<
589 !black_magic::CallHelper<Func, black_magic::S<Args...>>::value &&
590 !black_magic::CallHelper<
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700591 Func, black_magic::S<crow::Request, Args...>>::value,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700592 void>::type
593 operator()(Func&& f)
594 {
595 static_assert(
596 black_magic::CallHelper<Func, black_magic::S<Args...>>::value ||
597 black_magic::CallHelper<
598 Func, black_magic::S<crow::Request, Args...>>::value ||
599 black_magic::CallHelper<
600 Func, black_magic::S<crow::Request, crow::Response&,
601 Args...>>::value,
602 "Handler type is mismatched with URL parameters");
603 static_assert(
604 std::is_same<void, decltype(f(std::declval<crow::Request>(),
605 std::declval<crow::Response&>(),
606 std::declval<Args>()...))>::value,
Tanousf00032d2018-11-05 01:18:10 -0300607 "Handler function with response argument should have void "
608 "return "
Ed Tanous1abe55e2018-09-05 08:30:59 -0700609 "type");
Ed Tanous7045c8d2017-04-03 10:04:37 -0700610
Ed Tanous1abe55e2018-09-05 08:30:59 -0700611 handler = std::move(f);
612 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700613
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500614 template <typename Func>
Ed Tanousb5a76932020-09-29 16:16:58 -0700615 void operator()(const std::string& name, Func&& f)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700616 {
617 nameStr = std::move(name);
618 (*this).template operator()<Func>(std::forward(f));
619 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700620
Ed Tanous1abe55e2018-09-05 08:30:59 -0700621 void handle(const Request& req, Response& res,
622 const RoutingParams& params) override
623 {
624 detail::routing_handler_call_helper::Call<
625 detail::routing_handler_call_helper::CallParams<decltype(handler)>,
626 0, 0, 0, 0, black_magic::S<Args...>, black_magic::S<>>()(
627 detail::routing_handler_call_helper::CallParams<decltype(handler)>{
628 handler, params, req, res});
629 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700630
Ed Tanous1abe55e2018-09-05 08:30:59 -0700631 private:
632 std::function<void(const crow::Request&, crow::Response&, Args...)> handler;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700633};
634
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700635const int ruleSpecialRedirectSlash = 1;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700636
Ed Tanous1abe55e2018-09-05 08:30:59 -0700637class Trie
638{
639 public:
640 struct Node
641 {
642 unsigned ruleIndex{};
Ed Tanous271584a2019-07-09 16:24:22 -0700643 std::array<size_t, static_cast<size_t>(ParamType::MAX)>
644 paramChildrens{};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700645 boost::container::flat_map<std::string, unsigned> children;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700646
Ed Tanous1abe55e2018-09-05 08:30:59 -0700647 bool isSimpleNode() const
648 {
649 return !ruleIndex && std::all_of(std::begin(paramChildrens),
650 std::end(paramChildrens),
Ed Tanous271584a2019-07-09 16:24:22 -0700651 [](size_t x) { return !x; });
Ed Tanous7045c8d2017-04-03 10:04:37 -0700652 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -0700653 };
Ed Tanous7045c8d2017-04-03 10:04:37 -0700654
Ed Tanous1abe55e2018-09-05 08:30:59 -0700655 Trie() : nodes(1)
Gunnar Mills1214b7e2020-06-04 10:11:30 -0500656 {}
Ed Tanous1abe55e2018-09-05 08:30:59 -0700657
658 private:
659 void optimizeNode(Node* node)
660 {
Ed Tanous271584a2019-07-09 16:24:22 -0700661 for (size_t x : node->paramChildrens)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700662 {
663 if (!x)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700664 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700665 continue;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700666 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700667 Node* child = &nodes[x];
668 optimizeNode(child);
Ed Tanous7045c8d2017-04-03 10:04:37 -0700669 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700670 if (node->children.empty())
Ed Tanous3174e4d2020-10-07 11:41:22 -0700671 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700672 return;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700673 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700674 bool mergeWithChild = true;
Tanousf00032d2018-11-05 01:18:10 -0300675 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700676 {
677 Node* child = &nodes[kv.second];
678 if (!child->isSimpleNode())
679 {
680 mergeWithChild = false;
681 break;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700682 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700683 }
684 if (mergeWithChild)
685 {
686 decltype(node->children) merged;
Tanousf00032d2018-11-05 01:18:10 -0300687 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700688 {
689 Node* child = &nodes[kv.second];
Tanousf00032d2018-11-05 01:18:10 -0300690 for (const std::pair<std::string, unsigned>& childKv :
691 child->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700692 {
693 merged[kv.first + childKv.first] = childKv.second;
694 }
695 }
696 node->children = std::move(merged);
697 optimizeNode(node);
698 }
699 else
700 {
Tanousf00032d2018-11-05 01:18:10 -0300701 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700702 {
703 Node* child = &nodes[kv.second];
704 optimizeNode(child);
705 }
706 }
707 }
708
709 void optimize()
710 {
711 optimizeNode(head());
712 }
713
714 public:
715 void validate()
716 {
717 if (!head()->isSimpleNode())
Ed Tanous3174e4d2020-10-07 11:41:22 -0700718 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700719 throw std::runtime_error(
720 "Internal error: Trie header should be simple!");
Ed Tanous3174e4d2020-10-07 11:41:22 -0700721 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700722 optimize();
723 }
724
725 void findRouteIndexes(const std::string& req_url,
726 std::vector<unsigned>& route_indexes,
Tanousf00032d2018-11-05 01:18:10 -0300727 const Node* node = nullptr, unsigned pos = 0) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700728 {
729 if (node == nullptr)
730 {
731 node = head();
732 }
Tanousf00032d2018-11-05 01:18:10 -0300733 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700734 {
735 const std::string& fragment = kv.first;
736 const Node* child = &nodes[kv.second];
737 if (pos >= req_url.size())
738 {
739 if (child->ruleIndex != 0 && fragment != "/")
740 {
741 route_indexes.push_back(child->ruleIndex);
742 }
743 findRouteIndexes(req_url, route_indexes, child,
Ed Tanous271584a2019-07-09 16:24:22 -0700744 static_cast<unsigned>(pos + fragment.size()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700745 }
746 else
747 {
748 if (req_url.compare(pos, fragment.size(), fragment) == 0)
749 {
Ed Tanous271584a2019-07-09 16:24:22 -0700750 findRouteIndexes(
751 req_url, route_indexes, child,
752 static_cast<unsigned>(pos + fragment.size()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700753 }
754 }
755 }
756 }
757
758 std::pair<unsigned, RoutingParams>
Ed Tanous39e77502019-03-04 17:35:53 -0800759 find(const std::string_view req_url, const Node* node = nullptr,
Ed Tanous271584a2019-07-09 16:24:22 -0700760 size_t pos = 0, RoutingParams* params = nullptr) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700761 {
762 RoutingParams empty;
763 if (params == nullptr)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700764 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700765 params = &empty;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700766 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700767
768 unsigned found{};
769 RoutingParams matchParams;
770
771 if (node == nullptr)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700772 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700773 node = head();
Ed Tanous3174e4d2020-10-07 11:41:22 -0700774 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700775 if (pos == req_url.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -0700776 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700777 return {node->ruleIndex, *params};
Ed Tanous3174e4d2020-10-07 11:41:22 -0700778 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700779
780 auto updateFound =
781 [&found, &matchParams](std::pair<unsigned, RoutingParams>& ret) {
782 if (ret.first && (!found || found > ret.first))
783 {
784 found = ret.first;
785 matchParams = std::move(ret.second);
786 }
787 };
788
Ed Tanous271584a2019-07-09 16:24:22 -0700789 if (node->paramChildrens[static_cast<size_t>(ParamType::INT)])
Ed Tanous1abe55e2018-09-05 08:30:59 -0700790 {
791 char c = req_url[pos];
792 if ((c >= '0' && c <= '9') || c == '+' || c == '-')
793 {
794 char* eptr;
795 errno = 0;
796 long long int value =
797 std::strtoll(req_url.data() + pos, &eptr, 10);
798 if (errno != ERANGE && eptr != req_url.data() + pos)
799 {
800 params->intParams.push_back(value);
Ed Tanous271584a2019-07-09 16:24:22 -0700801 std::pair<unsigned, RoutingParams> ret = find(
802 req_url,
803 &nodes[node->paramChildrens[static_cast<size_t>(
804 ParamType::INT)]],
805 static_cast<size_t>(eptr - req_url.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700806 updateFound(ret);
807 params->intParams.pop_back();
808 }
809 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700810 }
811
Ed Tanous271584a2019-07-09 16:24:22 -0700812 if (node->paramChildrens[static_cast<size_t>(ParamType::UINT)])
Ed Tanous1abe55e2018-09-05 08:30:59 -0700813 {
814 char c = req_url[pos];
815 if ((c >= '0' && c <= '9') || c == '+')
816 {
817 char* eptr;
818 errno = 0;
819 unsigned long long int value =
820 std::strtoull(req_url.data() + pos, &eptr, 10);
821 if (errno != ERANGE && eptr != req_url.data() + pos)
822 {
823 params->uintParams.push_back(value);
Ed Tanous271584a2019-07-09 16:24:22 -0700824 std::pair<unsigned, RoutingParams> ret = find(
825 req_url,
826 &nodes[node->paramChildrens[static_cast<size_t>(
827 ParamType::UINT)]],
828 static_cast<size_t>(eptr - req_url.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700829 updateFound(ret);
830 params->uintParams.pop_back();
831 }
832 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700833 }
834
Ed Tanous271584a2019-07-09 16:24:22 -0700835 if (node->paramChildrens[static_cast<size_t>(ParamType::DOUBLE)])
Ed Tanous1abe55e2018-09-05 08:30:59 -0700836 {
837 char c = req_url[pos];
838 if ((c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.')
839 {
840 char* eptr;
841 errno = 0;
842 double value = std::strtod(req_url.data() + pos, &eptr);
843 if (errno != ERANGE && eptr != req_url.data() + pos)
844 {
845 params->doubleParams.push_back(value);
Tanousf00032d2018-11-05 01:18:10 -0300846 std::pair<unsigned, RoutingParams> ret = find(
Ed Tanous1abe55e2018-09-05 08:30:59 -0700847 req_url,
Ed Tanous271584a2019-07-09 16:24:22 -0700848 &nodes[node->paramChildrens[static_cast<size_t>(
849 ParamType::DOUBLE)]],
850 static_cast<size_t>(eptr - req_url.data()), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700851 updateFound(ret);
852 params->doubleParams.pop_back();
853 }
854 }
855 }
856
Ed Tanous271584a2019-07-09 16:24:22 -0700857 if (node->paramChildrens[static_cast<size_t>(ParamType::STRING)])
Ed Tanous1abe55e2018-09-05 08:30:59 -0700858 {
Ed Tanousb01bf292019-03-25 19:25:26 +0000859 size_t epos = pos;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700860 for (; epos < req_url.size(); epos++)
861 {
862 if (req_url[epos] == '/')
Ed Tanous3174e4d2020-10-07 11:41:22 -0700863 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700864 break;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700865 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700866 }
867
868 if (epos != pos)
869 {
870 params->stringParams.emplace_back(
871 req_url.substr(pos, epos - pos));
Tanousf00032d2018-11-05 01:18:10 -0300872 std::pair<unsigned, RoutingParams> ret =
Ed Tanousb01bf292019-03-25 19:25:26 +0000873 find(req_url,
Ed Tanous271584a2019-07-09 16:24:22 -0700874 &nodes[node->paramChildrens[static_cast<size_t>(
875 ParamType::STRING)]],
Ed Tanousb01bf292019-03-25 19:25:26 +0000876 epos, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700877 updateFound(ret);
878 params->stringParams.pop_back();
879 }
880 }
881
Ed Tanous271584a2019-07-09 16:24:22 -0700882 if (node->paramChildrens[static_cast<size_t>(ParamType::PATH)])
Ed Tanous1abe55e2018-09-05 08:30:59 -0700883 {
Ed Tanousb01bf292019-03-25 19:25:26 +0000884 size_t epos = req_url.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700885
886 if (epos != pos)
887 {
888 params->stringParams.emplace_back(
889 req_url.substr(pos, epos - pos));
Ed Tanous271584a2019-07-09 16:24:22 -0700890 std::pair<unsigned, RoutingParams> ret =
891 find(req_url,
892 &nodes[node->paramChildrens[static_cast<size_t>(
893 ParamType::PATH)]],
894 epos, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700895 updateFound(ret);
896 params->stringParams.pop_back();
897 }
898 }
899
Tanousf00032d2018-11-05 01:18:10 -0300900 for (const std::pair<std::string, unsigned>& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700901 {
902 const std::string& fragment = kv.first;
903 const Node* child = &nodes[kv.second];
904
905 if (req_url.compare(pos, fragment.size(), fragment) == 0)
906 {
Tanousf00032d2018-11-05 01:18:10 -0300907 std::pair<unsigned, RoutingParams> ret =
908 find(req_url, child, pos + fragment.size(), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700909 updateFound(ret);
910 }
911 }
912
913 return {found, matchParams};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700914 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700915
916 void add(const std::string& url, unsigned ruleIndex)
917 {
Ed Tanous271584a2019-07-09 16:24:22 -0700918 size_t idx = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700919
920 for (unsigned i = 0; i < url.size(); i++)
921 {
922 char c = url[i];
923 if (c == '<')
924 {
Tanousf00032d2018-11-05 01:18:10 -0300925 const static std::array<std::pair<ParamType, std::string>, 7>
926 paramTraits = {{
927 {ParamType::INT, "<int>"},
928 {ParamType::UINT, "<uint>"},
929 {ParamType::DOUBLE, "<float>"},
930 {ParamType::DOUBLE, "<double>"},
931 {ParamType::STRING, "<str>"},
932 {ParamType::STRING, "<string>"},
933 {ParamType::PATH, "<path>"},
934 }};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700935
Tanousf00032d2018-11-05 01:18:10 -0300936 for (const std::pair<ParamType, std::string>& x : paramTraits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700937 {
Tanousf00032d2018-11-05 01:18:10 -0300938 if (url.compare(i, x.second.size(), x.second) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700939 {
Ed Tanous271584a2019-07-09 16:24:22 -0700940 size_t index = static_cast<size_t>(x.first);
941 if (!nodes[idx].paramChildrens[index])
Ed Tanous1abe55e2018-09-05 08:30:59 -0700942 {
Tanousf00032d2018-11-05 01:18:10 -0300943 unsigned newNodeIdx = newNode();
Ed Tanous271584a2019-07-09 16:24:22 -0700944 nodes[idx].paramChildrens[index] = newNodeIdx;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700945 }
Ed Tanous271584a2019-07-09 16:24:22 -0700946 idx = nodes[idx].paramChildrens[index];
947 i += static_cast<unsigned>(x.second.size());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700948 break;
949 }
950 }
951
952 i--;
953 }
954 else
955 {
956 std::string piece(&c, 1);
957 if (!nodes[idx].children.count(piece))
958 {
Tanousf00032d2018-11-05 01:18:10 -0300959 unsigned newNodeIdx = newNode();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700960 nodes[idx].children.emplace(piece, newNodeIdx);
961 }
962 idx = nodes[idx].children[piece];
963 }
964 }
965 if (nodes[idx].ruleIndex)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700966 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700967 throw std::runtime_error("handler already exists for " + url);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700968 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700969 nodes[idx].ruleIndex = ruleIndex;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700970 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700971
Ed Tanous1abe55e2018-09-05 08:30:59 -0700972 private:
Ed Tanous271584a2019-07-09 16:24:22 -0700973 void debugNodePrint(Node* n, size_t level)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700974 {
Ed Tanous271584a2019-07-09 16:24:22 -0700975 for (size_t i = 0; i < static_cast<size_t>(ParamType::MAX); i++)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700976 {
977 if (n->paramChildrens[i])
978 {
979 BMCWEB_LOG_DEBUG << std::string(
Ed Tanous271584a2019-07-09 16:24:22 -0700980 2U * level, ' ') /*<< "("<<n->paramChildrens[i]<<") "*/;
981 switch (static_cast<ParamType>(i))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700982 {
983 case ParamType::INT:
984 BMCWEB_LOG_DEBUG << "<int>";
985 break;
986 case ParamType::UINT:
987 BMCWEB_LOG_DEBUG << "<uint>";
988 break;
989 case ParamType::DOUBLE:
990 BMCWEB_LOG_DEBUG << "<float>";
991 break;
992 case ParamType::STRING:
993 BMCWEB_LOG_DEBUG << "<str>";
994 break;
995 case ParamType::PATH:
996 BMCWEB_LOG_DEBUG << "<path>";
997 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000998 case ParamType::MAX:
Ed Tanous1abe55e2018-09-05 08:30:59 -0700999 BMCWEB_LOG_DEBUG << "<ERROR>";
1000 break;
1001 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001002
Ed Tanous1abe55e2018-09-05 08:30:59 -07001003 debugNodePrint(&nodes[n->paramChildrens[i]], level + 1);
1004 }
1005 }
Tanousf00032d2018-11-05 01:18:10 -03001006 for (const std::pair<std::string, unsigned>& kv : n->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001007 {
1008 BMCWEB_LOG_DEBUG
Ed Tanous271584a2019-07-09 16:24:22 -07001009 << std::string(2U * level, ' ') /*<< "(" << kv.second << ") "*/
Ed Tanous1abe55e2018-09-05 08:30:59 -07001010 << kv.first;
1011 debugNodePrint(&nodes[kv.second], level + 1);
1012 }
1013 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001014
Ed Tanous1abe55e2018-09-05 08:30:59 -07001015 public:
1016 void debugPrint()
1017 {
Ed Tanous271584a2019-07-09 16:24:22 -07001018 debugNodePrint(head(), 0U);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001019 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001020
Ed Tanous1abe55e2018-09-05 08:30:59 -07001021 private:
1022 const Node* head() const
1023 {
1024 return &nodes.front();
1025 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001026
Ed Tanous1abe55e2018-09-05 08:30:59 -07001027 Node* head()
1028 {
1029 return &nodes.front();
1030 }
1031
1032 unsigned newNode()
1033 {
1034 nodes.resize(nodes.size() + 1);
Ed Tanous271584a2019-07-09 16:24:22 -07001035 return static_cast<unsigned>(nodes.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001036 }
1037
1038 std::vector<Node> nodes;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001039};
1040
Ed Tanous1abe55e2018-09-05 08:30:59 -07001041class Router
1042{
1043 public:
Ed Tanous0c0084a2019-10-24 15:57:51 -07001044 Router() = default;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001045
Ed Tanous1abe55e2018-09-05 08:30:59 -07001046 DynamicRule& newRuleDynamic(const std::string& rule)
1047 {
1048 std::unique_ptr<DynamicRule> ruleObject =
1049 std::make_unique<DynamicRule>(rule);
1050 DynamicRule* ptr = ruleObject.get();
Tanousf00032d2018-11-05 01:18:10 -03001051 allRules.emplace_back(std::move(ruleObject));
Ed Tanous7045c8d2017-04-03 10:04:37 -07001052
Ed Tanous1abe55e2018-09-05 08:30:59 -07001053 return *ptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001054 }
1055
Ed Tanous1abe55e2018-09-05 08:30:59 -07001056 template <uint64_t N>
1057 typename black_magic::Arguments<N>::type::template rebind<TaggedRule>&
1058 newRuleTagged(const std::string& rule)
1059 {
1060 using RuleT = typename black_magic::Arguments<N>::type::template rebind<
1061 TaggedRule>;
1062 std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule);
1063 RuleT* ptr = ruleObject.get();
Tanousf00032d2018-11-05 01:18:10 -03001064 allRules.emplace_back(std::move(ruleObject));
Ed Tanous1abe55e2018-09-05 08:30:59 -07001065
1066 return *ptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001067 }
1068
Tanousf00032d2018-11-05 01:18:10 -03001069 void internalAddRuleObject(const std::string& rule, BaseRule* ruleObject)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001070 {
Tanousf00032d2018-11-05 01:18:10 -03001071 if (ruleObject == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001072 {
Tanousf00032d2018-11-05 01:18:10 -03001073 return;
1074 }
Ed Tanous888880a2020-08-24 13:48:50 -07001075 for (size_t method = 0, methodBit = 1; method < maxHttpVerbCount;
Ed Tanous2c70f802020-09-28 14:29:23 -07001076 method++, methodBit <<= 1)
Tanousf00032d2018-11-05 01:18:10 -03001077 {
Ed Tanous2c70f802020-09-28 14:29:23 -07001078 if (ruleObject->methodsBitfield & methodBit)
Tanousf00032d2018-11-05 01:18:10 -03001079 {
1080 perMethods[method].rules.emplace_back(ruleObject);
1081 perMethods[method].trie.add(
Ed Tanous271584a2019-07-09 16:24:22 -07001082 rule, static_cast<unsigned>(
1083 perMethods[method].rules.size() - 1U));
Tanousf00032d2018-11-05 01:18:10 -03001084 // directory case:
1085 // request to `/about' url matches `/about/' rule
1086 if (rule.size() > 2 && rule.back() == '/')
1087 {
1088 perMethods[method].trie.add(
1089 rule.substr(0, rule.size() - 1),
Ed Tanous271584a2019-07-09 16:24:22 -07001090 static_cast<unsigned>(perMethods[method].rules.size() -
1091 1));
Tanousf00032d2018-11-05 01:18:10 -03001092 }
1093 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001094 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001095 }
1096
Ed Tanous1abe55e2018-09-05 08:30:59 -07001097 void validate()
1098 {
Tanousf00032d2018-11-05 01:18:10 -03001099 for (std::unique_ptr<BaseRule>& rule : allRules)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001100 {
1101 if (rule)
1102 {
Tanousf00032d2018-11-05 01:18:10 -03001103 std::unique_ptr<BaseRule> upgraded = rule->upgrade();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001104 if (upgraded)
Ed Tanous3174e4d2020-10-07 11:41:22 -07001105 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001106 rule = std::move(upgraded);
Ed Tanous3174e4d2020-10-07 11:41:22 -07001107 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001108 rule->validate();
Tanousf00032d2018-11-05 01:18:10 -03001109 internalAddRuleObject(rule->rule, rule.get());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001110 }
1111 }
Tanousf00032d2018-11-05 01:18:10 -03001112 for (PerMethod& perMethod : perMethods)
1113 {
1114 perMethod.trie.validate();
1115 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001116 }
1117
Ed Tanous1abe55e2018-09-05 08:30:59 -07001118 template <typename Adaptor>
1119 void handleUpgrade(const Request& req, Response& res, Adaptor&& adaptor)
1120 {
Ed Tanous271584a2019-07-09 16:24:22 -07001121 if (static_cast<size_t>(req.method()) >= perMethods.size())
Ed Tanous888880a2020-08-24 13:48:50 -07001122 {
1123 res.result(boost::beast::http::status::not_found);
1124 res.end();
Tanousf00032d2018-11-05 01:18:10 -03001125 return;
Ed Tanous888880a2020-08-24 13:48:50 -07001126 }
Tanousf00032d2018-11-05 01:18:10 -03001127
Ed Tanous271584a2019-07-09 16:24:22 -07001128 PerMethod& perMethod = perMethods[static_cast<size_t>(req.method())];
Tanousf00032d2018-11-05 01:18:10 -03001129 Trie& trie = perMethod.trie;
1130 std::vector<BaseRule*>& rules = perMethod.rules;
1131
1132 const std::pair<unsigned, RoutingParams>& found = trie.find(req.url);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001133 unsigned ruleIndex = found.first;
1134 if (!ruleIndex)
1135 {
1136 BMCWEB_LOG_DEBUG << "Cannot match rules " << req.url;
Ed Tanousde5c9f32019-03-26 09:17:55 -07001137 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001138 res.end();
1139 return;
1140 }
1141
1142 if (ruleIndex >= rules.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -07001143 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001144 throw std::runtime_error("Trie internal structure corrupted!");
Ed Tanous3174e4d2020-10-07 11:41:22 -07001145 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001146
1147 if (ruleIndex == ruleSpecialRedirectSlash)
1148 {
1149 BMCWEB_LOG_INFO << "Redirecting to a url with trailing slash: "
1150 << req.url;
Ed Tanousde5c9f32019-03-26 09:17:55 -07001151 res.result(boost::beast::http::status::moved_permanently);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001152
1153 // TODO absolute url building
1154 if (req.getHeaderValue("Host").empty())
1155 {
1156 res.addHeader("Location", std::string(req.url) + "/");
1157 }
1158 else
1159 {
1160 res.addHeader(
1161 "Location",
1162 req.isSecure
1163 ? "https://"
1164 : "http://" + std::string(req.getHeaderValue("Host")) +
1165 std::string(req.url) + "/");
1166 }
1167 res.end();
1168 return;
1169 }
1170
Ed Tanous271584a2019-07-09 16:24:22 -07001171 if ((rules[ruleIndex]->getMethods() &
1172 (1U << static_cast<size_t>(req.method()))) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001173 {
1174 BMCWEB_LOG_DEBUG << "Rule found but method mismatch: " << req.url
1175 << " with " << req.methodString() << "("
Ed Tanous271584a2019-07-09 16:24:22 -07001176 << static_cast<uint32_t>(req.method()) << ") / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001177 << rules[ruleIndex]->getMethods();
Ed Tanousde5c9f32019-03-26 09:17:55 -07001178 res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001179 res.end();
1180 return;
1181 }
1182
1183 BMCWEB_LOG_DEBUG << "Matched rule (upgrade) '" << rules[ruleIndex]->rule
Ed Tanous271584a2019-07-09 16:24:22 -07001184 << "' " << static_cast<uint32_t>(req.method()) << " / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001185 << rules[ruleIndex]->getMethods();
1186
1187 // any uncaught exceptions become 500s
1188 try
1189 {
1190 rules[ruleIndex]->handleUpgrade(req, res, std::move(adaptor));
1191 }
1192 catch (std::exception& e)
1193 {
1194 BMCWEB_LOG_ERROR << "An uncaught exception occurred: " << e.what();
Ed Tanousde5c9f32019-03-26 09:17:55 -07001195 res.result(boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001196 res.end();
1197 return;
1198 }
1199 catch (...)
1200 {
1201 BMCWEB_LOG_ERROR
1202 << "An uncaught exception occurred. The type was unknown "
1203 "so no information was available.";
Ed Tanousde5c9f32019-03-26 09:17:55 -07001204 res.result(boost::beast::http::status::internal_server_error);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001205 res.end();
1206 return;
1207 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001208 }
1209
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001210 void handle(Request& req, Response& res)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001211 {
Ed Tanous271584a2019-07-09 16:24:22 -07001212 if (static_cast<size_t>(req.method()) >= perMethods.size())
Ed Tanous888880a2020-08-24 13:48:50 -07001213 {
1214 res.result(boost::beast::http::status::not_found);
1215 res.end();
Tanousf00032d2018-11-05 01:18:10 -03001216 return;
Ed Tanous888880a2020-08-24 13:48:50 -07001217 }
Ed Tanous271584a2019-07-09 16:24:22 -07001218 PerMethod& perMethod = perMethods[static_cast<size_t>(req.method())];
Tanousf00032d2018-11-05 01:18:10 -03001219 Trie& trie = perMethod.trie;
1220 std::vector<BaseRule*>& rules = perMethod.rules;
1221
1222 const std::pair<unsigned, RoutingParams>& found = trie.find(req.url);
Ed Tanous7045c8d2017-04-03 10:04:37 -07001223
Ed Tanous1abe55e2018-09-05 08:30:59 -07001224 unsigned ruleIndex = found.first;
1225
1226 if (!ruleIndex)
1227 {
Ed Tanous2634dcd2019-03-26 09:28:06 -07001228 // Check to see if this url exists at any verb
1229 for (const PerMethod& p : perMethods)
1230 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001231 const std::pair<unsigned, RoutingParams>& found2 =
Ed Tanous2634dcd2019-03-26 09:28:06 -07001232 p.trie.find(req.url);
Ed Tanous23a21a12020-07-25 04:45:05 +00001233 if (found2.first > 0)
Ed Tanous2634dcd2019-03-26 09:28:06 -07001234 {
1235 res.result(boost::beast::http::status::method_not_allowed);
1236 res.end();
1237 return;
1238 }
1239 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001240 BMCWEB_LOG_DEBUG << "Cannot match rules " << req.url;
1241 res.result(boost::beast::http::status::not_found);
1242 res.end();
1243 return;
1244 }
1245
1246 if (ruleIndex >= rules.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -07001247 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001248 throw std::runtime_error("Trie internal structure corrupted!");
Ed Tanous3174e4d2020-10-07 11:41:22 -07001249 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001250
1251 if (ruleIndex == ruleSpecialRedirectSlash)
1252 {
1253 BMCWEB_LOG_INFO << "Redirecting to a url with trailing slash: "
1254 << req.url;
Ed Tanousde5c9f32019-03-26 09:17:55 -07001255 res.result(boost::beast::http::status::moved_permanently);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001256
1257 // TODO absolute url building
1258 if (req.getHeaderValue("Host").empty())
1259 {
1260 res.addHeader("Location", std::string(req.url) + "/");
1261 }
1262 else
1263 {
1264 res.addHeader("Location",
1265 (req.isSecure ? "https://" : "http://") +
1266 std::string(req.getHeaderValue("Host")) +
1267 std::string(req.url) + "/");
1268 }
1269 res.end();
1270 return;
1271 }
1272
Ed Tanous271584a2019-07-09 16:24:22 -07001273 if ((rules[ruleIndex]->getMethods() &
1274 (1U << static_cast<uint32_t>(req.method()))) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001275 {
1276 BMCWEB_LOG_DEBUG << "Rule found but method mismatch: " << req.url
1277 << " with " << req.methodString() << "("
Ed Tanous271584a2019-07-09 16:24:22 -07001278 << static_cast<uint32_t>(req.method()) << ") / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001279 << rules[ruleIndex]->getMethods();
Ed Tanousde5c9f32019-03-26 09:17:55 -07001280 res.result(boost::beast::http::status::method_not_allowed);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001281 res.end();
1282 return;
1283 }
1284
1285 BMCWEB_LOG_DEBUG << "Matched rule '" << rules[ruleIndex]->rule << "' "
Ed Tanous271584a2019-07-09 16:24:22 -07001286 << static_cast<uint32_t>(req.method()) << " / "
Ed Tanous1abe55e2018-09-05 08:30:59 -07001287 << rules[ruleIndex]->getMethods();
1288
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001289 if (req.session == nullptr)
James Feist7166bf02019-12-10 16:52:14 +00001290 {
1291 rules[ruleIndex]->handle(req, res, found.second);
James Feist7166bf02019-12-10 16:52:14 +00001292 return;
1293 }
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001294
1295 crow::connections::systemBus->async_method_call(
1296 [&req, &res, &rules, ruleIndex, found](
1297 const boost::system::error_code ec,
1298 std::map<std::string, std::variant<bool, std::string,
1299 std::vector<std::string>>>
1300 userInfo) {
1301 if (ec)
1302 {
1303 BMCWEB_LOG_ERROR << "GetUserInfo failed...";
1304 res.result(
1305 boost::beast::http::status::internal_server_error);
1306 res.end();
1307 return;
1308 }
1309
1310 const std::string* userRolePtr = nullptr;
1311 auto userInfoIter = userInfo.find("UserPrivilege");
1312 if (userInfoIter != userInfo.end())
1313 {
1314 userRolePtr =
1315 std::get_if<std::string>(&userInfoIter->second);
1316 }
1317
1318 std::string userRole{};
1319 if (userRolePtr != nullptr)
1320 {
1321 userRole = *userRolePtr;
1322 BMCWEB_LOG_DEBUG << "userName = " << req.session->username
1323 << " userRole = " << *userRolePtr;
1324 }
1325
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001326 bool* remoteUserPtr = nullptr;
1327 auto remoteUserIter = userInfo.find("RemoteUser");
1328 if (remoteUserIter != userInfo.end())
1329 {
1330 remoteUserPtr = std::get_if<bool>(&remoteUserIter->second);
1331 }
1332 if (remoteUserPtr == nullptr)
1333 {
1334 BMCWEB_LOG_ERROR
1335 << "RemoteUser property missing or wrong type";
1336 res.result(
1337 boost::beast::http::status::internal_server_error);
1338 res.end();
1339 return;
1340 }
1341 bool remoteUser = *remoteUserPtr;
1342
1343 bool passwordExpired = false; // default for remote user
1344 if (!remoteUser)
1345 {
1346 bool* passwordExpiredPtr = nullptr;
1347 auto passwordExpiredIter =
1348 userInfo.find("UserPasswordExpired");
1349 if (passwordExpiredIter != userInfo.end())
1350 {
1351 passwordExpiredPtr =
1352 std::get_if<bool>(&passwordExpiredIter->second);
1353 }
1354 if (passwordExpiredPtr != nullptr)
1355 {
1356 passwordExpired = *passwordExpiredPtr;
1357 }
1358 else
1359 {
1360 BMCWEB_LOG_ERROR
1361 << "UserPasswordExpired property is expected for"
1362 " local user but is missing or wrong type";
1363 res.result(
1364 boost::beast::http::status::internal_server_error);
1365 res.end();
1366 return;
1367 }
1368 }
1369
Ed Tanous23a21a12020-07-25 04:45:05 +00001370 // Get the userprivileges from the role
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001371 redfish::Privileges userPrivileges =
1372 redfish::getUserPrivileges(userRole);
1373
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001374 // Set isConfigureSelfOnly based on D-Bus results. This
1375 // ignores the results from both pamAuthenticateUser and the
1376 // value from any previous use of this session.
1377 req.session->isConfigureSelfOnly = passwordExpired;
1378
Ed Tanous23a21a12020-07-25 04:45:05 +00001379 // Modifyprivileges if isConfigureSelfOnly.
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001380 if (req.session->isConfigureSelfOnly)
1381 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001382 // Remove allprivileges except ConfigureSelf
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001383 userPrivileges = userPrivileges.intersection(
1384 redfish::Privileges{"ConfigureSelf"});
1385 BMCWEB_LOG_DEBUG << "Operation limited to ConfigureSelf";
1386 }
1387
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001388 if (!rules[ruleIndex]->checkPrivileges(userPrivileges))
1389 {
1390 res.result(boost::beast::http::status::forbidden);
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06001391 if (req.session->isConfigureSelfOnly)
1392 {
1393 redfish::messages::passwordChangeRequired(
1394 res, "/redfish/v1/AccountService/Accounts/" +
1395 req.session->username);
1396 }
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -06001397 res.end();
1398 return;
1399 }
1400
1401 req.userRole = userRole;
1402
1403 rules[ruleIndex]->handle(req, res, found.second);
1404 },
1405 "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user",
1406 "xyz.openbmc_project.User.Manager", "GetUserInfo",
1407 req.session->username);
Ed Tanous7045c8d2017-04-03 10:04:37 -07001408 }
Ed Tanous7045c8d2017-04-03 10:04:37 -07001409
Ed Tanous1abe55e2018-09-05 08:30:59 -07001410 void debugPrint()
1411 {
Ed Tanous271584a2019-07-09 16:24:22 -07001412 for (size_t i = 0; i < perMethods.size(); i++)
Tanousf00032d2018-11-05 01:18:10 -03001413 {
Ed Tanous23a21a12020-07-25 04:45:05 +00001414 BMCWEB_LOG_DEBUG << boost::beast::http::to_string(
1415 static_cast<boost::beast::http::verb>(i));
Tanousf00032d2018-11-05 01:18:10 -03001416 perMethods[i].trie.debugPrint();
1417 }
Ed Tanous3dac7492017-08-02 13:46:20 -07001418 }
Ed Tanousb4a7bfa2017-04-04 17:23:00 -07001419
Ed Tanous1abe55e2018-09-05 08:30:59 -07001420 std::vector<const std::string*> getRoutes(const std::string& parent)
1421 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001422 std::vector<const std::string*> ret;
Tanousf00032d2018-11-05 01:18:10 -03001423
1424 for (const PerMethod& pm : perMethods)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001425 {
Tanousf00032d2018-11-05 01:18:10 -03001426 std::vector<unsigned> x;
1427 pm.trie.findRouteIndexes(parent, x);
1428 for (unsigned index : x)
1429 {
1430 ret.push_back(&pm.rules[index]->rule);
1431 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001432 }
1433 return ret;
1434 }
1435
1436 private:
Tanousf00032d2018-11-05 01:18:10 -03001437 struct PerMethod
1438 {
1439 std::vector<BaseRule*> rules;
1440 Trie trie;
1441 // rule index 0, 1 has special meaning; preallocate it to avoid
1442 // duplication.
1443 PerMethod() : rules(2)
Gunnar Mills1214b7e2020-06-04 10:11:30 -05001444 {}
Tanousf00032d2018-11-05 01:18:10 -03001445 };
Ed Tanous888880a2020-08-24 13:48:50 -07001446
1447 const static size_t maxHttpVerbCount =
Ed Tanouscc090442020-10-07 08:20:50 -07001448 static_cast<size_t>(boost::beast::http::verb::unlink);
Ed Tanous888880a2020-08-24 13:48:50 -07001449
Tanousf00032d2018-11-05 01:18:10 -03001450 std::array<PerMethod, maxHttpVerbCount> perMethods;
1451 std::vector<std::unique_ptr<BaseRule>> allRules;
Ed Tanous7045c8d2017-04-03 10:04:37 -07001452};
Ed Tanous1abe55e2018-09-05 08:30:59 -07001453} // namespace crow