Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 3 | #include "async_resp.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 4 | #include "common.hpp" |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 5 | #include "dbus_utility.hpp" |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 6 | #include "error_messages.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 7 | #include "http_request.hpp" |
| 8 | #include "http_response.hpp" |
| 9 | #include "logging.hpp" |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 10 | #include "privileges.hpp" |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 11 | #include "sessions.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 12 | #include "utility.hpp" |
Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 13 | #include "verb.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 14 | #include "websocket.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 15 | |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 16 | #include <boost/beast/ssl/ssl_stream.hpp> |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 17 | #include <boost/container/flat_map.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 18 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 19 | #include <cerrno> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 20 | #include <cstdint> |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 21 | #include <cstdlib> |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 22 | #include <limits> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 23 | #include <memory> |
Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 24 | #include <optional> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 25 | #include <tuple> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 26 | #include <utility> |
| 27 | #include <vector> |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 28 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 29 | namespace crow |
| 30 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 31 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 32 | class BaseRule |
| 33 | { |
| 34 | public: |
Ed Tanous | 4e23a44 | 2022-06-06 09:57:26 -0700 | [diff] [blame] | 35 | explicit BaseRule(const std::string& thisRule) : rule(thisRule) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 36 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 37 | |
Ed Tanous | 0c0084a | 2019-10-24 15:57:51 -0700 | [diff] [blame] | 38 | virtual ~BaseRule() = default; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 39 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 40 | BaseRule(const BaseRule&) = delete; |
| 41 | BaseRule(BaseRule&&) = delete; |
| 42 | BaseRule& operator=(const BaseRule&) = delete; |
| 43 | BaseRule& operator=(const BaseRule&&) = delete; |
| 44 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 45 | virtual void validate() = 0; |
| 46 | std::unique_ptr<BaseRule> upgrade() |
| 47 | { |
| 48 | if (ruleToUpgrade) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 49 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 50 | return std::move(ruleToUpgrade); |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 51 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 52 | return {}; |
| 53 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 54 | |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 55 | virtual void handle(const Request& /*req*/, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 56 | const std::shared_ptr<bmcweb::AsyncResp>&, |
| 57 | const RoutingParams&) = 0; |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame^] | 58 | virtual void |
| 59 | handleUpgrade(const Request& /*req*/, |
| 60 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 61 | boost::asio::ip::tcp::socket&& /*adaptor*/) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 62 | { |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame^] | 63 | asyncResp->res.result(boost::beast::http::status::not_found); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 65 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 66 | virtual void handleUpgrade( |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame^] | 67 | const Request& /*req*/, |
| 68 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 69 | boost::beast::ssl_stream<boost::asio::ip::tcp::socket>&& /*adaptor*/) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 70 | { |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame^] | 71 | asyncResp->res.result(boost::beast::http::status::not_found); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 72 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 73 | #endif |
| 74 | |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 75 | size_t getMethods() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 76 | { |
| 77 | return methodsBitfield; |
| 78 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 79 | |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 80 | bool checkPrivileges(const redfish::Privileges& userPrivileges) |
| 81 | { |
| 82 | // If there are no privileges assigned, assume no privileges |
| 83 | // required |
| 84 | if (privilegesSet.empty()) |
| 85 | { |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | for (const redfish::Privileges& requiredPrivileges : privilegesSet) |
| 90 | { |
| 91 | if (userPrivileges.isSupersetOf(requiredPrivileges)) |
| 92 | { |
| 93 | return true; |
| 94 | } |
| 95 | } |
| 96 | return false; |
| 97 | } |
| 98 | |
Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 99 | size_t methodsBitfield{1 << static_cast<size_t>(HttpVerb::Get)}; |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 100 | static_assert(std::numeric_limits<decltype(methodsBitfield)>::digits > |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 101 | methodNotAllowedIndex, |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 102 | "Not enough bits to store bitfield"); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 103 | |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 104 | std::vector<redfish::Privileges> privilegesSet; |
| 105 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 106 | std::string rule; |
| 107 | std::string nameStr; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 108 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 109 | std::unique_ptr<BaseRule> ruleToUpgrade; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 110 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 111 | friend class Router; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 112 | template <typename T> |
| 113 | friend struct RuleParameterTraits; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 114 | }; |
| 115 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 116 | namespace detail |
| 117 | { |
| 118 | namespace routing_handler_call_helper |
| 119 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 120 | template <typename T, int Pos> |
| 121 | struct CallPair |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 122 | { |
| 123 | using type = T; |
| 124 | static const int pos = Pos; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 127 | template <typename H1> |
| 128 | struct CallParams |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 129 | { |
| 130 | H1& handler; |
| 131 | const RoutingParams& params; |
| 132 | const Request& req; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 133 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | template <typename F, int NInt, int NUint, int NDouble, int NString, |
| 137 | typename S1, typename S2> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 138 | struct Call |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 139 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 140 | |
| 141 | template <typename F, int NInt, int NUint, int NDouble, int NString, |
| 142 | typename... Args1, typename... Args2> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 143 | struct Call<F, NInt, NUint, NDouble, NString, black_magic::S<int64_t, Args1...>, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 144 | black_magic::S<Args2...>> |
| 145 | { |
| 146 | void operator()(F cparams) |
| 147 | { |
| 148 | using pushed = typename black_magic::S<Args2...>::template push_back< |
| 149 | CallPair<int64_t, NInt>>; |
| 150 | Call<F, NInt + 1, NUint, NDouble, NString, black_magic::S<Args1...>, |
| 151 | pushed>()(cparams); |
| 152 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | template <typename F, int NInt, int NUint, int NDouble, int NString, |
| 156 | typename... Args1, typename... Args2> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 157 | struct Call<F, NInt, NUint, NDouble, NString, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 158 | black_magic::S<uint64_t, Args1...>, black_magic::S<Args2...>> |
| 159 | { |
| 160 | void operator()(F cparams) |
| 161 | { |
| 162 | using pushed = typename black_magic::S<Args2...>::template push_back< |
| 163 | CallPair<uint64_t, NUint>>; |
| 164 | Call<F, NInt, NUint + 1, NDouble, NString, black_magic::S<Args1...>, |
| 165 | pushed>()(cparams); |
| 166 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
| 169 | template <typename F, int NInt, int NUint, int NDouble, int NString, |
| 170 | typename... Args1, typename... Args2> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 171 | struct Call<F, NInt, NUint, NDouble, NString, black_magic::S<double, Args1...>, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 172 | black_magic::S<Args2...>> |
| 173 | { |
| 174 | void operator()(F cparams) |
| 175 | { |
| 176 | using pushed = typename black_magic::S<Args2...>::template push_back< |
| 177 | CallPair<double, NDouble>>; |
| 178 | Call<F, NInt, NUint, NDouble + 1, NString, black_magic::S<Args1...>, |
| 179 | pushed>()(cparams); |
| 180 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 181 | }; |
| 182 | |
| 183 | template <typename F, int NInt, int NUint, int NDouble, int NString, |
| 184 | typename... Args1, typename... Args2> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 185 | struct Call<F, NInt, NUint, NDouble, NString, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 186 | black_magic::S<std::string, Args1...>, black_magic::S<Args2...>> |
| 187 | { |
| 188 | void operator()(F cparams) |
| 189 | { |
| 190 | using pushed = typename black_magic::S<Args2...>::template push_back< |
| 191 | CallPair<std::string, NString>>; |
| 192 | Call<F, NInt, NUint, NDouble, NString + 1, black_magic::S<Args1...>, |
| 193 | pushed>()(cparams); |
| 194 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 195 | }; |
| 196 | |
| 197 | template <typename F, int NInt, int NUint, int NDouble, int NString, |
| 198 | typename... Args1> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 199 | struct Call<F, NInt, NUint, NDouble, NString, black_magic::S<>, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 200 | black_magic::S<Args1...>> |
| 201 | { |
| 202 | void operator()(F cparams) |
| 203 | { |
| 204 | cparams.handler( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 205 | cparams.req, cparams.asyncResp, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 206 | cparams.params.template get<typename Args1::type>(Args1::pos)...); |
| 207 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 208 | }; |
| 209 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 210 | template <typename Func, typename... ArgsWrapped> |
| 211 | struct Wrapped |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 212 | { |
| 213 | template <typename... Args> |
| 214 | void set( |
| 215 | Func f, |
| 216 | typename std::enable_if< |
| 217 | !std::is_same< |
| 218 | typename std::tuple_element<0, std::tuple<Args..., void>>::type, |
| 219 | const Request&>::value, |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 220 | int>::type /*enable*/ |
| 221 | = 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 222 | { |
Ed Tanous | f94c4ec | 2022-01-06 12:44:41 -0800 | [diff] [blame] | 223 | handler = [f = std::forward<Func>(f)]( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 224 | const Request&, |
| 225 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 226 | Args... args) { asyncResp->res.result(f(args...)); }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 229 | template <typename Req, typename... Args> |
| 230 | struct ReqHandlerWrapper |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 231 | { |
Ed Tanous | 4e23a44 | 2022-06-06 09:57:26 -0700 | [diff] [blame] | 232 | explicit ReqHandlerWrapper(Func fIn) : f(std::move(fIn)) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 233 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 234 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 235 | void operator()(const Request& req, |
| 236 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 237 | Args... args) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 238 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 239 | asyncResp->res.result(f(req, args...)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 240 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 241 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 242 | Func f; |
| 243 | }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 244 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 245 | template <typename... Args> |
| 246 | void set( |
| 247 | Func f, |
| 248 | typename std::enable_if< |
| 249 | std::is_same< |
| 250 | typename std::tuple_element<0, std::tuple<Args..., void>>::type, |
| 251 | const Request&>::value && |
| 252 | !std::is_same<typename std::tuple_element< |
| 253 | 1, std::tuple<Args..., void, void>>::type, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 254 | const std::shared_ptr<bmcweb::AsyncResp>&>::value, |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 255 | int>::type /*enable*/ |
| 256 | = 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 257 | { |
| 258 | handler = ReqHandlerWrapper<Args...>(std::move(f)); |
| 259 | /*handler = ( |
| 260 | [f = std::move(f)] |
| 261 | (const Request& req, Response& res, Args... args){ |
Ed Tanous | de5c9f3 | 2019-03-26 09:17:55 -0700 | [diff] [blame] | 262 | res.result(f(req, args...)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 263 | res.end(); |
| 264 | });*/ |
| 265 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 266 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 267 | template <typename... Args> |
| 268 | void set( |
| 269 | Func f, |
| 270 | typename std::enable_if< |
| 271 | std::is_same< |
| 272 | typename std::tuple_element<0, std::tuple<Args..., void>>::type, |
| 273 | const Request&>::value && |
| 274 | std::is_same<typename std::tuple_element< |
| 275 | 1, std::tuple<Args..., void, void>>::type, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 276 | const std::shared_ptr<bmcweb::AsyncResp>&>::value, |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 277 | int>::type /*enable*/ |
| 278 | = 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 279 | { |
| 280 | handler = std::move(f); |
| 281 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 282 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 283 | template <typename... Args> |
| 284 | struct HandlerTypeHelper |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 285 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 286 | using type = std::function<void( |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 287 | const crow::Request& /*req*/, |
| 288 | const std::shared_ptr<bmcweb::AsyncResp>&, Args...)>; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 289 | using args_type = |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 290 | black_magic::S<typename black_magic::PromoteT<Args>...>; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 291 | }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 292 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 293 | template <typename... Args> |
| 294 | struct HandlerTypeHelper<const Request&, Args...> |
| 295 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 296 | using type = std::function<void( |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 297 | const crow::Request& /*req*/, |
| 298 | const std::shared_ptr<bmcweb::AsyncResp>&, Args...)>; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 299 | using args_type = |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 300 | black_magic::S<typename black_magic::PromoteT<Args>...>; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 301 | }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 302 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 303 | template <typename... Args> |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 304 | struct HandlerTypeHelper<const Request&, |
| 305 | const std::shared_ptr<bmcweb::AsyncResp>&, Args...> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 306 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 307 | using type = std::function<void( |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 308 | const crow::Request& /*req*/, |
| 309 | const std::shared_ptr<bmcweb::AsyncResp>&, Args...)>; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 310 | using args_type = |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 311 | black_magic::S<typename black_magic::PromoteT<Args>...>; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | typename HandlerTypeHelper<ArgsWrapped...>::type handler; |
| 315 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 316 | void operator()(const Request& req, |
| 317 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 318 | const RoutingParams& params) |
| 319 | { |
| 320 | detail::routing_handler_call_helper::Call< |
| 321 | detail::routing_handler_call_helper::CallParams<decltype(handler)>, |
| 322 | 0, 0, 0, 0, typename HandlerTypeHelper<ArgsWrapped...>::args_type, |
| 323 | black_magic::S<>>()( |
| 324 | detail::routing_handler_call_helper::CallParams<decltype(handler)>{ |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 325 | handler, params, req, asyncResp}); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 326 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 327 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 328 | } // namespace routing_handler_call_helper |
| 329 | } // namespace detail |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 330 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 331 | class WebSocketRule : public BaseRule |
| 332 | { |
| 333 | using self_t = WebSocketRule; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 334 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 335 | public: |
Ed Tanous | 4e23a44 | 2022-06-06 09:57:26 -0700 | [diff] [blame] | 336 | explicit WebSocketRule(const std::string& ruleIn) : BaseRule(ruleIn) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 337 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 338 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 339 | void validate() override |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 340 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 341 | |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 342 | void handle(const Request& /*req*/, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 343 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 344 | const RoutingParams& /*params*/) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 345 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 346 | asyncResp->res.result(boost::beast::http::status::not_found); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 347 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 348 | |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame^] | 349 | void handleUpgrade(const Request& req, |
| 350 | const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/, |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 351 | boost::asio::ip::tcp::socket&& adaptor) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 352 | { |
Nan Zhou | 93c0202 | 2022-02-24 18:21:07 -0800 | [diff] [blame] | 353 | BMCWEB_LOG_DEBUG << "Websocket handles upgrade"; |
Ratan Gupta | 02453b1 | 2019-10-22 14:43:36 +0530 | [diff] [blame] | 354 | std::shared_ptr< |
| 355 | crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>> |
| 356 | myConnection = std::make_shared< |
| 357 | crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>>( |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 358 | req, std::move(adaptor), openHandler, messageHandler, |
Ratan Gupta | 02453b1 | 2019-10-22 14:43:36 +0530 | [diff] [blame] | 359 | closeHandler, errorHandler); |
| 360 | myConnection->start(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 361 | } |
| 362 | #ifdef BMCWEB_ENABLE_SSL |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame^] | 363 | void handleUpgrade(const Request& req, |
| 364 | const std::shared_ptr<bmcweb::AsyncResp>& /*asyncResp*/, |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 365 | boost::beast::ssl_stream<boost::asio::ip::tcp::socket>&& |
| 366 | adaptor) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 367 | { |
Nan Zhou | 93c0202 | 2022-02-24 18:21:07 -0800 | [diff] [blame] | 368 | BMCWEB_LOG_DEBUG << "Websocket handles upgrade"; |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 369 | std::shared_ptr<crow::websocket::ConnectionImpl< |
| 370 | boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>> |
| 371 | myConnection = std::make_shared<crow::websocket::ConnectionImpl< |
| 372 | boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>>( |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 373 | req, std::move(adaptor), openHandler, messageHandler, |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 374 | closeHandler, errorHandler); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 375 | myConnection->start(); |
| 376 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 377 | #endif |
| 378 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 379 | template <typename Func> |
| 380 | self_t& onopen(Func f) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 381 | { |
| 382 | openHandler = f; |
| 383 | return *this; |
| 384 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 385 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 386 | template <typename Func> |
| 387 | self_t& onmessage(Func f) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 388 | { |
| 389 | messageHandler = f; |
| 390 | return *this; |
| 391 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 392 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 393 | template <typename Func> |
| 394 | self_t& onclose(Func f) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 395 | { |
| 396 | closeHandler = f; |
| 397 | return *this; |
| 398 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 399 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 400 | template <typename Func> |
| 401 | self_t& onerror(Func f) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 402 | { |
| 403 | errorHandler = f; |
| 404 | return *this; |
| 405 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 406 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 407 | protected: |
zhanghch05 | 7772638 | 2021-10-21 14:07:57 +0800 | [diff] [blame] | 408 | std::function<void(crow::websocket::Connection&)> openHandler; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 409 | std::function<void(crow::websocket::Connection&, const std::string&, bool)> |
| 410 | messageHandler; |
| 411 | std::function<void(crow::websocket::Connection&, const std::string&)> |
| 412 | closeHandler; |
| 413 | std::function<void(crow::websocket::Connection&)> errorHandler; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 414 | }; |
| 415 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 416 | template <typename T> |
| 417 | struct RuleParameterTraits |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 418 | { |
| 419 | using self_t = T; |
| 420 | WebSocketRule& websocket() |
| 421 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 422 | self_t* self = static_cast<self_t*>(this); |
| 423 | WebSocketRule* p = new WebSocketRule(self->rule); |
| 424 | self->ruleToUpgrade.reset(p); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 425 | return *p; |
| 426 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 427 | |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 428 | self_t& name(std::string_view name) noexcept |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 429 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 430 | self_t* self = static_cast<self_t*>(this); |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 431 | self->nameStr = name; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 432 | return *self; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 433 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 434 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 435 | self_t& methods(boost::beast::http::verb method) |
| 436 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 437 | self_t* self = static_cast<self_t*>(this); |
Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 438 | std::optional<HttpVerb> verb = httpVerbFromBoost(method); |
| 439 | if (verb) |
| 440 | { |
| 441 | self->methodsBitfield = 1U << static_cast<size_t>(*verb); |
| 442 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 443 | return *self; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 444 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 445 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 446 | template <typename... MethodArgs> |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 447 | self_t& methods(boost::beast::http::verb method, MethodArgs... argsMethod) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 448 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 449 | self_t* self = static_cast<self_t*>(this); |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 450 | methods(argsMethod...); |
Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 451 | std::optional<HttpVerb> verb = httpVerbFromBoost(method); |
| 452 | if (verb) |
| 453 | { |
| 454 | self->methodsBitfield |= 1U << static_cast<size_t>(*verb); |
| 455 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 456 | return *self; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 457 | } |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 458 | |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 459 | self_t& notFound() |
| 460 | { |
| 461 | self_t* self = static_cast<self_t*>(this); |
| 462 | self->methodsBitfield = 1U << notFoundIndex; |
| 463 | return *self; |
| 464 | } |
| 465 | |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 466 | self_t& methodNotAllowed() |
| 467 | { |
| 468 | self_t* self = static_cast<self_t*>(this); |
| 469 | self->methodsBitfield = 1U << methodNotAllowedIndex; |
| 470 | return *self; |
| 471 | } |
| 472 | |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 473 | self_t& privileges( |
| 474 | const std::initializer_list<std::initializer_list<const char*>>& p) |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 475 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 476 | self_t* self = static_cast<self_t*>(this); |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 477 | for (const std::initializer_list<const char*>& privilege : p) |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 478 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 479 | self->privilegesSet.emplace_back(privilege); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 480 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 481 | return *self; |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 482 | } |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 483 | |
| 484 | template <size_t N, typename... MethodArgs> |
| 485 | self_t& privileges(const std::array<redfish::Privileges, N>& p) |
| 486 | { |
| 487 | self_t* self = static_cast<self_t*>(this); |
| 488 | for (const redfish::Privileges& privilege : p) |
| 489 | { |
| 490 | self->privilegesSet.emplace_back(privilege); |
| 491 | } |
| 492 | return *self; |
| 493 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 494 | }; |
| 495 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 496 | class DynamicRule : public BaseRule, public RuleParameterTraits<DynamicRule> |
| 497 | { |
| 498 | public: |
Ed Tanous | 4e23a44 | 2022-06-06 09:57:26 -0700 | [diff] [blame] | 499 | explicit DynamicRule(const std::string& ruleIn) : BaseRule(ruleIn) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 500 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 501 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 502 | void validate() override |
| 503 | { |
| 504 | if (!erasedHandler) |
| 505 | { |
| 506 | throw std::runtime_error(nameStr + (!nameStr.empty() ? ": " : "") + |
| 507 | "no handler for url " + rule); |
| 508 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 509 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 510 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 511 | void handle(const Request& req, |
| 512 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 513 | const RoutingParams& params) override |
| 514 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 515 | erasedHandler(req, asyncResp, params); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 516 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 517 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 518 | template <typename Func> |
| 519 | void operator()(Func f) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 520 | { |
Ed Tanous | c867a83 | 2022-03-10 14:17:00 -0800 | [diff] [blame] | 521 | using boost::callable_traits::args_t; |
| 522 | constexpr size_t arity = std::tuple_size<args_t<Func>>::value; |
| 523 | constexpr auto is = std::make_integer_sequence<unsigned, arity>{}; |
| 524 | erasedHandler = wrap(std::move(f), is); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | // enable_if Arg1 == request && Arg2 == Response |
Gunnar Mills | 6be0e40 | 2020-07-08 13:21:51 -0500 | [diff] [blame] | 528 | // enable_if Arg1 == request && Arg2 != response |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 529 | // enable_if Arg1 != request |
| 530 | |
| 531 | template <typename Func, unsigned... Indices> |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 532 | std::function<void(const Request&, |
| 533 | const std::shared_ptr<bmcweb::AsyncResp>&, |
| 534 | const RoutingParams&)> |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 535 | wrap(Func f, std::integer_sequence<unsigned, Indices...> /*is*/) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 536 | { |
Ed Tanous | c867a83 | 2022-03-10 14:17:00 -0800 | [diff] [blame] | 537 | using function_t = crow::utility::FunctionTraits<Func>; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 538 | |
| 539 | if (!black_magic::isParameterTagCompatible( |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 540 | black_magic::getParameterTag(rule.c_str()), |
| 541 | black_magic::computeParameterTagFromArgsList< |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 542 | typename function_t::template arg<Indices>...>::value)) |
| 543 | { |
| 544 | throw std::runtime_error("routeDynamic: Handler type is mismatched " |
| 545 | "with URL parameters: " + |
| 546 | rule); |
| 547 | } |
| 548 | auto ret = detail::routing_handler_call_helper::Wrapped< |
| 549 | Func, typename function_t::template arg<Indices>...>(); |
| 550 | ret.template set<typename function_t::template arg<Indices>...>( |
| 551 | std::move(f)); |
| 552 | return ret; |
| 553 | } |
| 554 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 555 | template <typename Func> |
| 556 | void operator()(std::string name, Func&& f) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 557 | { |
| 558 | nameStr = std::move(name); |
| 559 | (*this).template operator()<Func>(std::forward(f)); |
| 560 | } |
| 561 | |
| 562 | private: |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 563 | std::function<void(const Request&, |
| 564 | const std::shared_ptr<bmcweb::AsyncResp>&, |
| 565 | const RoutingParams&)> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 566 | erasedHandler; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 567 | }; |
| 568 | |
| 569 | template <typename... Args> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 570 | class TaggedRule : |
| 571 | public BaseRule, |
| 572 | public RuleParameterTraits<TaggedRule<Args...>> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 573 | { |
| 574 | public: |
| 575 | using self_t = TaggedRule<Args...>; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 576 | |
Ed Tanous | 4e23a44 | 2022-06-06 09:57:26 -0700 | [diff] [blame] | 577 | explicit TaggedRule(const std::string& ruleIn) : BaseRule(ruleIn) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 578 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 579 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 580 | void validate() override |
| 581 | { |
| 582 | if (!handler) |
| 583 | { |
| 584 | throw std::runtime_error(nameStr + (!nameStr.empty() ? ": " : "") + |
| 585 | "no handler for url " + rule); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | template <typename Func> |
| 590 | typename std::enable_if< |
| 591 | black_magic::CallHelper<Func, black_magic::S<Args...>>::value, |
| 592 | 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 | "Handler type is mismatched with URL parameters"); |
| 600 | static_assert( |
| 601 | !std::is_same<void, decltype(f(std::declval<Args>()...))>::value, |
| 602 | "Handler function cannot have void return type; valid return " |
| 603 | "types: " |
Gunnar Mills | 6be0e40 | 2020-07-08 13:21:51 -0500 | [diff] [blame] | 604 | "string, int, crow::response, nlohmann::json"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 605 | |
Ed Tanous | f94c4ec | 2022-01-06 12:44:41 -0800 | [diff] [blame] | 606 | handler = [f = std::forward<Func>(f)]( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 607 | const Request&, |
| 608 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 609 | Args... args) { asyncResp->res.result(f(args...)); }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | template <typename Func> |
| 613 | typename std::enable_if< |
| 614 | !black_magic::CallHelper<Func, black_magic::S<Args...>>::value && |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 615 | black_magic::CallHelper< |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 616 | Func, black_magic::S<crow::Request, Args...>>::value, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 617 | void>::type |
| 618 | operator()(Func&& f) |
| 619 | { |
| 620 | static_assert( |
| 621 | black_magic::CallHelper<Func, black_magic::S<Args...>>::value || |
| 622 | black_magic::CallHelper< |
| 623 | Func, black_magic::S<crow::Request, Args...>>::value, |
| 624 | "Handler type is mismatched with URL parameters"); |
| 625 | static_assert( |
| 626 | !std::is_same<void, decltype(f(std::declval<crow::Request>(), |
| 627 | std::declval<Args>()...))>::value, |
| 628 | "Handler function cannot have void return type; valid return " |
| 629 | "types: " |
Gunnar Mills | 6be0e40 | 2020-07-08 13:21:51 -0500 | [diff] [blame] | 630 | "string, int, crow::response,nlohmann::json"); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 631 | |
Ed Tanous | f94c4ec | 2022-01-06 12:44:41 -0800 | [diff] [blame] | 632 | handler = [f = std::forward<Func>(f)]( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 633 | const crow::Request& req, |
| 634 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 635 | Args... args) { asyncResp->res.result(f(req, args...)); }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 636 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 637 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 638 | template <typename Func> |
| 639 | typename std::enable_if< |
| 640 | !black_magic::CallHelper<Func, black_magic::S<Args...>>::value && |
| 641 | !black_magic::CallHelper< |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 642 | Func, black_magic::S<crow::Request, Args...>>::value, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 643 | void>::type |
| 644 | operator()(Func&& f) |
| 645 | { |
| 646 | static_assert( |
| 647 | black_magic::CallHelper<Func, black_magic::S<Args...>>::value || |
| 648 | black_magic::CallHelper< |
| 649 | Func, black_magic::S<crow::Request, Args...>>::value || |
| 650 | black_magic::CallHelper< |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 651 | Func, black_magic::S<crow::Request, |
| 652 | std::shared_ptr<bmcweb::AsyncResp>&, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 653 | Args...>>::value, |
| 654 | "Handler type is mismatched with URL parameters"); |
| 655 | static_assert( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 656 | std::is_same< |
| 657 | void, |
| 658 | decltype(f(std::declval<crow::Request>(), |
| 659 | std::declval<std::shared_ptr<bmcweb::AsyncResp>&>(), |
| 660 | std::declval<Args>()...))>::value, |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 661 | "Handler function with response argument should have void " |
| 662 | "return " |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 663 | "type"); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 664 | |
Ed Tanous | f94c4ec | 2022-01-06 12:44:41 -0800 | [diff] [blame] | 665 | handler = std::forward<Func>(f); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 666 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 667 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 668 | template <typename Func> |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 669 | void operator()(std::string_view name, Func&& f) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 670 | { |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 671 | nameStr = name; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 672 | (*this).template operator()<Func>(std::forward(f)); |
| 673 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 674 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 675 | void handle(const Request& req, |
| 676 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 677 | const RoutingParams& params) override |
| 678 | { |
| 679 | detail::routing_handler_call_helper::Call< |
| 680 | detail::routing_handler_call_helper::CallParams<decltype(handler)>, |
| 681 | 0, 0, 0, 0, black_magic::S<Args...>, black_magic::S<>>()( |
| 682 | detail::routing_handler_call_helper::CallParams<decltype(handler)>{ |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 683 | handler, params, req, asyncResp}); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 684 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 685 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 686 | private: |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 687 | std::function<void(const crow::Request&, |
| 688 | const std::shared_ptr<bmcweb::AsyncResp>&, Args...)> |
| 689 | handler; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 690 | }; |
| 691 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 692 | class Trie |
| 693 | { |
| 694 | public: |
| 695 | struct Node |
| 696 | { |
| 697 | unsigned ruleIndex{}; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 698 | std::array<size_t, static_cast<size_t>(ParamType::MAX)> |
| 699 | paramChildrens{}; |
Ed Tanous | a94ac61 | 2022-02-22 11:13:24 -0800 | [diff] [blame] | 700 | using ChildMap = boost::container::flat_map< |
| 701 | std::string, unsigned, std::less<>, |
| 702 | std::vector<std::pair<std::string, unsigned>>>; |
| 703 | ChildMap children; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 704 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 705 | bool isSimpleNode() const |
| 706 | { |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 707 | return ruleIndex == 0 && |
| 708 | std::all_of(std::begin(paramChildrens), |
| 709 | std::end(paramChildrens), |
| 710 | [](size_t x) { return x == 0U; }); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 711 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 712 | }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 713 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 714 | Trie() : nodes(1) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 715 | {} |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 716 | |
| 717 | private: |
| 718 | void optimizeNode(Node* node) |
| 719 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 720 | for (size_t x : node->paramChildrens) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 721 | { |
Ed Tanous | dbb59d4 | 2022-01-25 11:09:55 -0800 | [diff] [blame] | 722 | if (x == 0U) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 723 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 724 | continue; |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 725 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 726 | Node* child = &nodes[x]; |
| 727 | optimizeNode(child); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 728 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 729 | if (node->children.empty()) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 730 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 731 | return; |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 732 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 733 | bool mergeWithChild = true; |
Ed Tanous | a94ac61 | 2022-02-22 11:13:24 -0800 | [diff] [blame] | 734 | for (const Node::ChildMap::value_type& kv : node->children) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 735 | { |
| 736 | Node* child = &nodes[kv.second]; |
| 737 | if (!child->isSimpleNode()) |
| 738 | { |
| 739 | mergeWithChild = false; |
| 740 | break; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 741 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 742 | } |
| 743 | if (mergeWithChild) |
| 744 | { |
Ed Tanous | a94ac61 | 2022-02-22 11:13:24 -0800 | [diff] [blame] | 745 | Node::ChildMap merged; |
| 746 | for (const Node::ChildMap::value_type& kv : node->children) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 747 | { |
| 748 | Node* child = &nodes[kv.second]; |
Ed Tanous | a94ac61 | 2022-02-22 11:13:24 -0800 | [diff] [blame] | 749 | for (const Node::ChildMap::value_type& childKv : |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 750 | child->children) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 751 | { |
| 752 | merged[kv.first + childKv.first] = childKv.second; |
| 753 | } |
| 754 | } |
| 755 | node->children = std::move(merged); |
| 756 | optimizeNode(node); |
| 757 | } |
| 758 | else |
| 759 | { |
Ed Tanous | a94ac61 | 2022-02-22 11:13:24 -0800 | [diff] [blame] | 760 | for (const Node::ChildMap::value_type& kv : node->children) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 761 | { |
| 762 | Node* child = &nodes[kv.second]; |
| 763 | optimizeNode(child); |
| 764 | } |
| 765 | } |
| 766 | } |
| 767 | |
| 768 | void optimize() |
| 769 | { |
| 770 | optimizeNode(head()); |
| 771 | } |
| 772 | |
| 773 | public: |
| 774 | void validate() |
| 775 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 776 | optimize(); |
| 777 | } |
| 778 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 779 | void findRouteIndexes(const std::string& reqUrl, |
| 780 | std::vector<unsigned>& routeIndexes, |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 781 | const Node* node = nullptr, unsigned pos = 0) const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 782 | { |
| 783 | if (node == nullptr) |
| 784 | { |
| 785 | node = head(); |
| 786 | } |
Ed Tanous | a94ac61 | 2022-02-22 11:13:24 -0800 | [diff] [blame] | 787 | for (const Node::ChildMap::value_type& kv : node->children) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 788 | { |
| 789 | const std::string& fragment = kv.first; |
| 790 | const Node* child = &nodes[kv.second]; |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 791 | if (pos >= reqUrl.size()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 792 | { |
| 793 | if (child->ruleIndex != 0 && fragment != "/") |
| 794 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 795 | routeIndexes.push_back(child->ruleIndex); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 796 | } |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 797 | findRouteIndexes(reqUrl, routeIndexes, child, |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 798 | static_cast<unsigned>(pos + fragment.size())); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 799 | } |
| 800 | else |
| 801 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 802 | if (reqUrl.compare(pos, fragment.size(), fragment) == 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 803 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 804 | findRouteIndexes( |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 805 | reqUrl, routeIndexes, child, |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 806 | static_cast<unsigned>(pos + fragment.size())); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 807 | } |
| 808 | } |
| 809 | } |
| 810 | } |
| 811 | |
| 812 | std::pair<unsigned, RoutingParams> |
Ed Tanous | 26ccae3 | 2023-02-16 10:28:44 -0800 | [diff] [blame] | 813 | find(std::string_view reqUrl, const Node* node = nullptr, |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 814 | size_t pos = 0, RoutingParams* params = nullptr) const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 815 | { |
| 816 | RoutingParams empty; |
| 817 | if (params == nullptr) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 818 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 819 | params = ∅ |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 820 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 821 | |
| 822 | unsigned found{}; |
| 823 | RoutingParams matchParams; |
| 824 | |
| 825 | if (node == nullptr) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 826 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 827 | node = head(); |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 828 | } |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 829 | if (pos == reqUrl.size()) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 830 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 831 | return {node->ruleIndex, *params}; |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 832 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 833 | |
| 834 | auto updateFound = |
| 835 | [&found, &matchParams](std::pair<unsigned, RoutingParams>& ret) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 836 | if (ret.first != 0U && (found == 0U || found > ret.first)) |
| 837 | { |
| 838 | found = ret.first; |
| 839 | matchParams = std::move(ret.second); |
| 840 | } |
| 841 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 842 | |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 843 | if (node->paramChildrens[static_cast<size_t>(ParamType::INT)] != 0U) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 844 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 845 | char c = reqUrl[pos]; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 846 | if ((c >= '0' && c <= '9') || c == '+' || c == '-') |
| 847 | { |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 848 | char* eptr = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 849 | errno = 0; |
| 850 | long long int value = |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 851 | std::strtoll(reqUrl.data() + pos, &eptr, 10); |
| 852 | if (errno != ERANGE && eptr != reqUrl.data() + pos) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 853 | { |
| 854 | params->intParams.push_back(value); |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 855 | std::pair<unsigned, RoutingParams> ret = |
| 856 | find(reqUrl, |
| 857 | &nodes[node->paramChildrens[static_cast<size_t>( |
| 858 | ParamType::INT)]], |
| 859 | static_cast<size_t>(eptr - reqUrl.data()), params); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 860 | updateFound(ret); |
| 861 | params->intParams.pop_back(); |
| 862 | } |
| 863 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 866 | if (node->paramChildrens[static_cast<size_t>(ParamType::UINT)] != 0U) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 867 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 868 | char c = reqUrl[pos]; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 869 | if ((c >= '0' && c <= '9') || c == '+') |
| 870 | { |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 871 | char* eptr = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 872 | errno = 0; |
| 873 | unsigned long long int value = |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 874 | std::strtoull(reqUrl.data() + pos, &eptr, 10); |
| 875 | if (errno != ERANGE && eptr != reqUrl.data() + pos) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 876 | { |
| 877 | params->uintParams.push_back(value); |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 878 | std::pair<unsigned, RoutingParams> ret = |
| 879 | find(reqUrl, |
| 880 | &nodes[node->paramChildrens[static_cast<size_t>( |
| 881 | ParamType::UINT)]], |
| 882 | static_cast<size_t>(eptr - reqUrl.data()), params); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 883 | updateFound(ret); |
| 884 | params->uintParams.pop_back(); |
| 885 | } |
| 886 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 887 | } |
| 888 | |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 889 | if (node->paramChildrens[static_cast<size_t>(ParamType::DOUBLE)] != 0U) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 890 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 891 | char c = reqUrl[pos]; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 892 | if ((c >= '0' && c <= '9') || c == '+' || c == '-' || c == '.') |
| 893 | { |
Ed Tanous | 543f440 | 2022-01-06 13:12:53 -0800 | [diff] [blame] | 894 | char* eptr = nullptr; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 895 | errno = 0; |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 896 | double value = std::strtod(reqUrl.data() + pos, &eptr); |
| 897 | if (errno != ERANGE && eptr != reqUrl.data() + pos) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 898 | { |
| 899 | params->doubleParams.push_back(value); |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 900 | std::pair<unsigned, RoutingParams> ret = |
| 901 | find(reqUrl, |
| 902 | &nodes[node->paramChildrens[static_cast<size_t>( |
| 903 | ParamType::DOUBLE)]], |
| 904 | static_cast<size_t>(eptr - reqUrl.data()), params); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 905 | updateFound(ret); |
| 906 | params->doubleParams.pop_back(); |
| 907 | } |
| 908 | } |
| 909 | } |
| 910 | |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 911 | if (node->paramChildrens[static_cast<size_t>(ParamType::STRING)] != 0U) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 912 | { |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 913 | size_t epos = pos; |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 914 | for (; epos < reqUrl.size(); epos++) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 915 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 916 | if (reqUrl[epos] == '/') |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 917 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 918 | break; |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 919 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | if (epos != pos) |
| 923 | { |
| 924 | params->stringParams.emplace_back( |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 925 | reqUrl.substr(pos, epos - pos)); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 926 | std::pair<unsigned, RoutingParams> ret = |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 927 | find(reqUrl, |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 928 | &nodes[node->paramChildrens[static_cast<size_t>( |
| 929 | ParamType::STRING)]], |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 930 | epos, params); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 931 | updateFound(ret); |
| 932 | params->stringParams.pop_back(); |
| 933 | } |
| 934 | } |
| 935 | |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 936 | if (node->paramChildrens[static_cast<size_t>(ParamType::PATH)] != 0U) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 937 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 938 | size_t epos = reqUrl.size(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 939 | |
| 940 | if (epos != pos) |
| 941 | { |
| 942 | params->stringParams.emplace_back( |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 943 | reqUrl.substr(pos, epos - pos)); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 944 | std::pair<unsigned, RoutingParams> ret = |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 945 | find(reqUrl, |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 946 | &nodes[node->paramChildrens[static_cast<size_t>( |
| 947 | ParamType::PATH)]], |
| 948 | epos, params); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 949 | updateFound(ret); |
| 950 | params->stringParams.pop_back(); |
| 951 | } |
| 952 | } |
| 953 | |
Ed Tanous | a94ac61 | 2022-02-22 11:13:24 -0800 | [diff] [blame] | 954 | for (const Node::ChildMap::value_type& kv : node->children) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 955 | { |
| 956 | const std::string& fragment = kv.first; |
| 957 | const Node* child = &nodes[kv.second]; |
| 958 | |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 959 | if (reqUrl.compare(pos, fragment.size(), fragment) == 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 960 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 961 | std::pair<unsigned, RoutingParams> ret = |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 962 | find(reqUrl, child, pos + fragment.size(), params); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 963 | updateFound(ret); |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | return {found, matchParams}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 968 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 969 | |
| 970 | void add(const std::string& url, unsigned ruleIndex) |
| 971 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 972 | size_t idx = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 973 | |
| 974 | for (unsigned i = 0; i < url.size(); i++) |
| 975 | { |
| 976 | char c = url[i]; |
| 977 | if (c == '<') |
| 978 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 979 | const static std::array<std::pair<ParamType, std::string>, 7> |
| 980 | paramTraits = {{ |
| 981 | {ParamType::INT, "<int>"}, |
| 982 | {ParamType::UINT, "<uint>"}, |
| 983 | {ParamType::DOUBLE, "<float>"}, |
| 984 | {ParamType::DOUBLE, "<double>"}, |
| 985 | {ParamType::STRING, "<str>"}, |
| 986 | {ParamType::STRING, "<string>"}, |
| 987 | {ParamType::PATH, "<path>"}, |
| 988 | }}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 989 | |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 990 | for (const std::pair<ParamType, std::string>& x : paramTraits) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 991 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 992 | if (url.compare(i, x.second.size(), x.second) == 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 993 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 994 | size_t index = static_cast<size_t>(x.first); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 995 | if (nodes[idx].paramChildrens[index] == 0U) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 996 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 997 | unsigned newNodeIdx = newNode(); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 998 | nodes[idx].paramChildrens[index] = newNodeIdx; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 999 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1000 | idx = nodes[idx].paramChildrens[index]; |
| 1001 | i += static_cast<unsigned>(x.second.size()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1002 | break; |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | i--; |
| 1007 | } |
| 1008 | else |
| 1009 | { |
| 1010 | std::string piece(&c, 1); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 1011 | if (nodes[idx].children.count(piece) == 0U) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1012 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1013 | unsigned newNodeIdx = newNode(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1014 | nodes[idx].children.emplace(piece, newNodeIdx); |
| 1015 | } |
| 1016 | idx = nodes[idx].children[piece]; |
| 1017 | } |
| 1018 | } |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 1019 | if (nodes[idx].ruleIndex != 0U) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 1020 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1021 | throw std::runtime_error("handler already exists for " + url); |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 1022 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1023 | nodes[idx].ruleIndex = ruleIndex; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1024 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1025 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1026 | private: |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1027 | void debugNodePrint(Node* n, size_t level) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1028 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1029 | for (size_t i = 0; i < static_cast<size_t>(ParamType::MAX); i++) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1030 | { |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 1031 | if (n->paramChildrens[i] != 0U) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1032 | { |
| 1033 | BMCWEB_LOG_DEBUG << std::string( |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1034 | 2U * level, ' ') /*<< "("<<n->paramChildrens[i]<<") "*/; |
| 1035 | switch (static_cast<ParamType>(i)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1036 | { |
| 1037 | case ParamType::INT: |
| 1038 | BMCWEB_LOG_DEBUG << "<int>"; |
| 1039 | break; |
| 1040 | case ParamType::UINT: |
| 1041 | BMCWEB_LOG_DEBUG << "<uint>"; |
| 1042 | break; |
| 1043 | case ParamType::DOUBLE: |
| 1044 | BMCWEB_LOG_DEBUG << "<float>"; |
| 1045 | break; |
| 1046 | case ParamType::STRING: |
| 1047 | BMCWEB_LOG_DEBUG << "<str>"; |
| 1048 | break; |
| 1049 | case ParamType::PATH: |
| 1050 | BMCWEB_LOG_DEBUG << "<path>"; |
| 1051 | break; |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1052 | case ParamType::MAX: |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1053 | BMCWEB_LOG_DEBUG << "<ERROR>"; |
| 1054 | break; |
| 1055 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1056 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1057 | debugNodePrint(&nodes[n->paramChildrens[i]], level + 1); |
| 1058 | } |
| 1059 | } |
Ed Tanous | a94ac61 | 2022-02-22 11:13:24 -0800 | [diff] [blame] | 1060 | for (const Node::ChildMap::value_type& kv : n->children) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1061 | { |
| 1062 | BMCWEB_LOG_DEBUG |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1063 | << std::string(2U * level, ' ') /*<< "(" << kv.second << ") "*/ |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1064 | << kv.first; |
| 1065 | debugNodePrint(&nodes[kv.second], level + 1); |
| 1066 | } |
| 1067 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1068 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1069 | public: |
| 1070 | void debugPrint() |
| 1071 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1072 | debugNodePrint(head(), 0U); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1073 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1074 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1075 | private: |
| 1076 | const Node* head() const |
| 1077 | { |
| 1078 | return &nodes.front(); |
| 1079 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1080 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1081 | Node* head() |
| 1082 | { |
| 1083 | return &nodes.front(); |
| 1084 | } |
| 1085 | |
| 1086 | unsigned newNode() |
| 1087 | { |
| 1088 | nodes.resize(nodes.size() + 1); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1089 | return static_cast<unsigned>(nodes.size() - 1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1090 | } |
| 1091 | |
| 1092 | std::vector<Node> nodes; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1093 | }; |
| 1094 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1095 | class Router |
| 1096 | { |
| 1097 | public: |
Ed Tanous | 0c0084a | 2019-10-24 15:57:51 -0700 | [diff] [blame] | 1098 | Router() = default; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1099 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1100 | DynamicRule& newRuleDynamic(const std::string& rule) |
| 1101 | { |
| 1102 | std::unique_ptr<DynamicRule> ruleObject = |
| 1103 | std::make_unique<DynamicRule>(rule); |
| 1104 | DynamicRule* ptr = ruleObject.get(); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1105 | allRules.emplace_back(std::move(ruleObject)); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1106 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1107 | return *ptr; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1108 | } |
| 1109 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1110 | template <uint64_t N> |
| 1111 | typename black_magic::Arguments<N>::type::template rebind<TaggedRule>& |
| 1112 | newRuleTagged(const std::string& rule) |
| 1113 | { |
| 1114 | using RuleT = typename black_magic::Arguments<N>::type::template rebind< |
| 1115 | TaggedRule>; |
| 1116 | std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule); |
| 1117 | RuleT* ptr = ruleObject.get(); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1118 | allRules.emplace_back(std::move(ruleObject)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1119 | |
| 1120 | return *ptr; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1123 | void internalAddRuleObject(const std::string& rule, BaseRule* ruleObject) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1124 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1125 | if (ruleObject == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1126 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1127 | return; |
| 1128 | } |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 1129 | for (size_t method = 0, methodBit = 1; method <= methodNotAllowedIndex; |
Ed Tanous | 2c70f80 | 2020-09-28 14:29:23 -0700 | [diff] [blame] | 1130 | method++, methodBit <<= 1) |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1131 | { |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 1132 | if ((ruleObject->methodsBitfield & methodBit) > 0U) |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1133 | { |
| 1134 | perMethods[method].rules.emplace_back(ruleObject); |
| 1135 | perMethods[method].trie.add( |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1136 | rule, static_cast<unsigned>( |
| 1137 | perMethods[method].rules.size() - 1U)); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1138 | // directory case: |
| 1139 | // request to `/about' url matches `/about/' rule |
| 1140 | if (rule.size() > 2 && rule.back() == '/') |
| 1141 | { |
| 1142 | perMethods[method].trie.add( |
| 1143 | rule.substr(0, rule.size() - 1), |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1144 | static_cast<unsigned>(perMethods[method].rules.size() - |
| 1145 | 1)); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1146 | } |
| 1147 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1148 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1149 | } |
| 1150 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1151 | void validate() |
| 1152 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1153 | for (std::unique_ptr<BaseRule>& rule : allRules) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1154 | { |
| 1155 | if (rule) |
| 1156 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1157 | std::unique_ptr<BaseRule> upgraded = rule->upgrade(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1158 | if (upgraded) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 1159 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1160 | rule = std::move(upgraded); |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 1161 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1162 | rule->validate(); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1163 | internalAddRuleObject(rule->rule, rule.get()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1164 | } |
| 1165 | } |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1166 | for (PerMethod& perMethod : perMethods) |
| 1167 | { |
| 1168 | perMethod.trie.validate(); |
| 1169 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1170 | } |
| 1171 | |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1172 | struct FindRoute |
| 1173 | { |
| 1174 | BaseRule* rule = nullptr; |
| 1175 | RoutingParams params; |
| 1176 | }; |
| 1177 | |
| 1178 | struct FindRouteResponse |
| 1179 | { |
| 1180 | std::string allowHeader; |
| 1181 | FindRoute route; |
| 1182 | }; |
| 1183 | |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 1184 | FindRoute findRouteByIndex(std::string_view url, size_t index) const |
| 1185 | { |
| 1186 | FindRoute route; |
| 1187 | if (index >= perMethods.size()) |
| 1188 | { |
| 1189 | BMCWEB_LOG_CRITICAL << "Bad index???"; |
| 1190 | return route; |
| 1191 | } |
| 1192 | const PerMethod& perMethod = perMethods[index]; |
| 1193 | std::pair<unsigned, RoutingParams> found = perMethod.trie.find(url); |
| 1194 | if (found.first >= perMethod.rules.size()) |
| 1195 | { |
| 1196 | throw std::runtime_error("Trie internal structure corrupted!"); |
| 1197 | } |
| 1198 | // Found a 404 route, switch that in |
| 1199 | if (found.first != 0U) |
| 1200 | { |
| 1201 | route.rule = perMethod.rules[found.first]; |
| 1202 | route.params = std::move(found.second); |
| 1203 | } |
| 1204 | return route; |
| 1205 | } |
| 1206 | |
| 1207 | FindRouteResponse findRoute(Request& req) const |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1208 | { |
| 1209 | FindRouteResponse findRoute; |
| 1210 | |
Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 1211 | std::optional<HttpVerb> verb = httpVerbFromBoost(req.method()); |
| 1212 | if (!verb) |
| 1213 | { |
| 1214 | return findRoute; |
| 1215 | } |
| 1216 | size_t reqMethodIndex = static_cast<size_t>(*verb); |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1217 | // Check to see if this url exists at any verb |
| 1218 | for (size_t perMethodIndex = 0; perMethodIndex <= maxVerbIndex; |
| 1219 | perMethodIndex++) |
| 1220 | { |
| 1221 | // Make sure it's safe to deference the array at that index |
| 1222 | static_assert(maxVerbIndex < |
| 1223 | std::tuple_size_v<decltype(perMethods)>); |
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 1224 | FindRoute route = |
| 1225 | findRouteByIndex(req.url().encoded_path(), perMethodIndex); |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 1226 | if (route.rule == nullptr) |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1227 | { |
| 1228 | continue; |
| 1229 | } |
| 1230 | if (!findRoute.allowHeader.empty()) |
| 1231 | { |
| 1232 | findRoute.allowHeader += ", "; |
| 1233 | } |
Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 1234 | HttpVerb thisVerb = static_cast<HttpVerb>(perMethodIndex); |
| 1235 | findRoute.allowHeader += httpVerbToString(thisVerb); |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1236 | if (perMethodIndex == reqMethodIndex) |
| 1237 | { |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 1238 | findRoute.route = route; |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1239 | } |
| 1240 | } |
| 1241 | return findRoute; |
| 1242 | } |
| 1243 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1244 | template <typename Adaptor> |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame^] | 1245 | void handleUpgrade(const Request& req, |
| 1246 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1247 | Adaptor&& adaptor) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1248 | { |
Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 1249 | std::optional<HttpVerb> verb = httpVerbFromBoost(req.method()); |
| 1250 | if (!verb || static_cast<size_t>(*verb) >= perMethods.size()) |
Ed Tanous | 888880a | 2020-08-24 13:48:50 -0700 | [diff] [blame] | 1251 | { |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame^] | 1252 | asyncResp->res.result(boost::beast::http::status::not_found); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1253 | return; |
Ed Tanous | 888880a | 2020-08-24 13:48:50 -0700 | [diff] [blame] | 1254 | } |
Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 1255 | PerMethod& perMethod = perMethods[static_cast<size_t>(*verb)]; |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1256 | Trie& trie = perMethod.trie; |
| 1257 | std::vector<BaseRule*>& rules = perMethod.rules; |
| 1258 | |
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 1259 | const std::pair<unsigned, RoutingParams>& found = |
| 1260 | trie.find(req.url().encoded_path()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1261 | unsigned ruleIndex = found.first; |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 1262 | if (ruleIndex == 0U) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1263 | { |
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 1264 | BMCWEB_LOG_DEBUG << "Cannot match rules " |
| 1265 | << req.url().encoded_path(); |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame^] | 1266 | asyncResp->res.result(boost::beast::http::status::not_found); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1267 | return; |
| 1268 | } |
| 1269 | |
| 1270 | if (ruleIndex >= rules.size()) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 1271 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1272 | throw std::runtime_error("Trie internal structure corrupted!"); |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 1273 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1274 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1275 | if ((rules[ruleIndex]->getMethods() & |
Snehalatha Venkatesh | 1c99da0 | 2022-12-27 06:45:35 +0000 | [diff] [blame] | 1276 | (1U << static_cast<size_t>(*verb))) == 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1277 | { |
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 1278 | BMCWEB_LOG_DEBUG << "Rule found but method mismatch: " |
| 1279 | << req.url().encoded_path() << " with " |
| 1280 | << req.methodString() << "(" |
Snehalatha Venkatesh | 1c99da0 | 2022-12-27 06:45:35 +0000 | [diff] [blame] | 1281 | << static_cast<uint32_t>(*verb) << ") / " |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1282 | << rules[ruleIndex]->getMethods(); |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame^] | 1283 | asyncResp->res.result(boost::beast::http::status::not_found); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1284 | return; |
| 1285 | } |
| 1286 | |
| 1287 | BMCWEB_LOG_DEBUG << "Matched rule (upgrade) '" << rules[ruleIndex]->rule |
Snehalatha Venkatesh | 1c99da0 | 2022-12-27 06:45:35 +0000 | [diff] [blame] | 1288 | << "' " << static_cast<uint32_t>(*verb) << " / " |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1289 | << rules[ruleIndex]->getMethods(); |
| 1290 | |
| 1291 | // any uncaught exceptions become 500s |
| 1292 | try |
| 1293 | { |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame^] | 1294 | rules[ruleIndex]->handleUpgrade(req, asyncResp, |
Ed Tanous | f94c4ec | 2022-01-06 12:44:41 -0800 | [diff] [blame] | 1295 | std::forward<Adaptor>(adaptor)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1296 | } |
Patrick Williams | c596704 | 2021-10-06 12:39:54 -0500 | [diff] [blame] | 1297 | catch (const std::exception& e) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1298 | { |
| 1299 | BMCWEB_LOG_ERROR << "An uncaught exception occurred: " << e.what(); |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame^] | 1300 | asyncResp->res.result( |
| 1301 | boost::beast::http::status::internal_server_error); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1302 | return; |
| 1303 | } |
| 1304 | catch (...) |
| 1305 | { |
| 1306 | BMCWEB_LOG_ERROR |
| 1307 | << "An uncaught exception occurred. The type was unknown " |
| 1308 | "so no information was available."; |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame^] | 1309 | asyncResp->res.result( |
| 1310 | boost::beast::http::status::internal_server_error); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1311 | return; |
| 1312 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1313 | } |
| 1314 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1315 | void handle(Request& req, |
| 1316 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1317 | { |
Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 1318 | std::optional<HttpVerb> verb = httpVerbFromBoost(req.method()); |
| 1319 | if (!verb || static_cast<size_t>(*verb) >= perMethods.size()) |
Ed Tanous | 888880a | 2020-08-24 13:48:50 -0700 | [diff] [blame] | 1320 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1321 | asyncResp->res.result(boost::beast::http::status::not_found); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1322 | return; |
Ed Tanous | 888880a | 2020-08-24 13:48:50 -0700 | [diff] [blame] | 1323 | } |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1324 | |
| 1325 | FindRouteResponse foundRoute = findRoute(req); |
| 1326 | |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 1327 | if (foundRoute.route.rule == nullptr) |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 1328 | { |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 1329 | // Couldn't find a normal route with any verb, try looking for a 404 |
| 1330 | // route |
| 1331 | if (foundRoute.allowHeader.empty()) |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1332 | { |
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 1333 | foundRoute.route = |
| 1334 | findRouteByIndex(req.url().encoded_path(), notFoundIndex); |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 1335 | } |
| 1336 | else |
| 1337 | { |
| 1338 | // See if we have a method not allowed (405) handler |
Ed Tanous | 39662a3 | 2023-02-06 15:09:46 -0800 | [diff] [blame] | 1339 | foundRoute.route = findRouteByIndex(req.url().encoded_path(), |
| 1340 | methodNotAllowedIndex); |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1341 | } |
| 1342 | } |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 1343 | |
| 1344 | // Fill in the allow header if it's valid |
| 1345 | if (!foundRoute.allowHeader.empty()) |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1346 | { |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 1347 | |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 1348 | asyncResp->res.addHeader(boost::beast::http::field::allow, |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1349 | foundRoute.allowHeader); |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 1350 | } |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1351 | |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1352 | // If we couldn't find a real route or a 404 route, return a generic |
| 1353 | // response |
| 1354 | if (foundRoute.route.rule == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1355 | { |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1356 | if (foundRoute.allowHeader.empty()) |
| 1357 | { |
| 1358 | asyncResp->res.result(boost::beast::http::status::not_found); |
| 1359 | } |
| 1360 | else |
Ed Tanous | 2634dcd | 2019-03-26 09:28:06 -0700 | [diff] [blame] | 1361 | { |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 1362 | asyncResp->res.result( |
| 1363 | boost::beast::http::status::method_not_allowed); |
Ed Tanous | 2634dcd | 2019-03-26 09:28:06 -0700 | [diff] [blame] | 1364 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1365 | return; |
| 1366 | } |
| 1367 | |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1368 | BaseRule& rule = *foundRoute.route.rule; |
| 1369 | RoutingParams params = std::move(foundRoute.route.params); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1370 | |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1371 | BMCWEB_LOG_DEBUG << "Matched rule '" << rule.rule << "' " |
Snehalatha Venkatesh | 1c99da0 | 2022-12-27 06:45:35 +0000 | [diff] [blame] | 1372 | << static_cast<uint32_t>(*verb) << " / " |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1373 | << rule.getMethods(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1374 | |
RAJESWARAN THILLAIGOVINDAN | 61dbeef | 2019-12-13 04:26:54 -0600 | [diff] [blame] | 1375 | if (req.session == nullptr) |
James Feist | 7166bf0 | 2019-12-10 16:52:14 +0000 | [diff] [blame] | 1376 | { |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1377 | rule.handle(req, asyncResp, params); |
James Feist | 7166bf0 | 2019-12-10 16:52:14 +0000 | [diff] [blame] | 1378 | return; |
| 1379 | } |
Ed Tanous | 2d6cb56 | 2022-07-07 20:44:54 -0700 | [diff] [blame] | 1380 | std::string username = req.session->username; |
RAJESWARAN THILLAIGOVINDAN | 61dbeef | 2019-12-13 04:26:54 -0600 | [diff] [blame] | 1381 | |
| 1382 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 2d6cb56 | 2022-07-07 20:44:54 -0700 | [diff] [blame] | 1383 | [req{std::move(req)}, asyncResp, &rule, params]( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 1384 | const boost::system::error_code& ec, |
Ed Tanous | 2d6cb56 | 2022-07-07 20:44:54 -0700 | [diff] [blame] | 1385 | const dbus::utility::DBusPropertiesMap& userInfoMap) mutable { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1386 | if (ec) |
| 1387 | { |
| 1388 | BMCWEB_LOG_ERROR << "GetUserInfo failed..."; |
| 1389 | asyncResp->res.result( |
| 1390 | boost::beast::http::status::internal_server_error); |
| 1391 | return; |
| 1392 | } |
| 1393 | std::string userRole{}; |
| 1394 | const bool* remoteUser = nullptr; |
| 1395 | std::optional<bool> passwordExpired; |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 1396 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1397 | for (const auto& userInfo : userInfoMap) |
| 1398 | { |
| 1399 | if (userInfo.first == "UserPrivilege") |
RAJESWARAN THILLAIGOVINDAN | 61dbeef | 2019-12-13 04:26:54 -0600 | [diff] [blame] | 1400 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1401 | const std::string* userRolePtr = |
| 1402 | std::get_if<std::string>(&userInfo.second); |
| 1403 | if (userRolePtr == nullptr) |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 1404 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1405 | continue; |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 1406 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1407 | userRole = *userRolePtr; |
| 1408 | BMCWEB_LOG_DEBUG << "userName = " << req.session->username |
| 1409 | << " userRole = " << *userRolePtr; |
RAJESWARAN THILLAIGOVINDAN | 61dbeef | 2019-12-13 04:26:54 -0600 | [diff] [blame] | 1410 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1411 | else if (userInfo.first == "RemoteUser") |
| 1412 | { |
| 1413 | remoteUser = std::get_if<bool>(&userInfo.second); |
| 1414 | } |
| 1415 | else if (userInfo.first == "UserPasswordExpired") |
| 1416 | { |
| 1417 | const bool* passwordExpiredPtr = |
| 1418 | std::get_if<bool>(&userInfo.second); |
| 1419 | if (passwordExpiredPtr == nullptr) |
| 1420 | { |
| 1421 | continue; |
| 1422 | } |
| 1423 | passwordExpired = *passwordExpiredPtr; |
| 1424 | } |
| 1425 | } |
RAJESWARAN THILLAIGOVINDAN | 61dbeef | 2019-12-13 04:26:54 -0600 | [diff] [blame] | 1426 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1427 | if (remoteUser == nullptr) |
| 1428 | { |
| 1429 | BMCWEB_LOG_ERROR << "RemoteUser property missing or wrong type"; |
| 1430 | asyncResp->res.result( |
| 1431 | boost::beast::http::status::internal_server_error); |
| 1432 | return; |
| 1433 | } |
| 1434 | |
| 1435 | if (passwordExpired == std::nullopt) |
| 1436 | { |
| 1437 | if (!*remoteUser) |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 1438 | { |
| 1439 | BMCWEB_LOG_ERROR |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1440 | << "UserPasswordExpired property is expected for" |
| 1441 | " local user but is missing or wrong type"; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1442 | asyncResp->res.result( |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 1443 | boost::beast::http::status::internal_server_error); |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 1444 | return; |
| 1445 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1446 | passwordExpired = false; |
| 1447 | } |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 1448 | |
Nan Zhou | ac6250a | 2022-08-09 20:15:44 +0000 | [diff] [blame] | 1449 | // Get the user's privileges from the role |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1450 | redfish::Privileges userPrivileges = |
| 1451 | redfish::getUserPrivileges(userRole); |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 1452 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1453 | // Set isConfigureSelfOnly based on D-Bus results. This |
| 1454 | // ignores the results from both pamAuthenticateUser and the |
| 1455 | // value from any previous use of this session. |
| 1456 | req.session->isConfigureSelfOnly = *passwordExpired; |
RAJESWARAN THILLAIGOVINDAN | 61dbeef | 2019-12-13 04:26:54 -0600 | [diff] [blame] | 1457 | |
Nan Zhou | ac6250a | 2022-08-09 20:15:44 +0000 | [diff] [blame] | 1458 | // Modify privileges if isConfigureSelfOnly. |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1459 | if (req.session->isConfigureSelfOnly) |
| 1460 | { |
Nan Zhou | ac6250a | 2022-08-09 20:15:44 +0000 | [diff] [blame] | 1461 | // Remove all privileges except ConfigureSelf |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1462 | userPrivileges = userPrivileges.intersection( |
| 1463 | redfish::Privileges{"ConfigureSelf"}); |
| 1464 | BMCWEB_LOG_DEBUG << "Operation limited to ConfigureSelf"; |
| 1465 | } |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 1466 | |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1467 | if (!rule.checkPrivileges(userPrivileges)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1468 | { |
| 1469 | asyncResp->res.result(boost::beast::http::status::forbidden); |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 1470 | if (req.session->isConfigureSelfOnly) |
| 1471 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1472 | redfish::messages::passwordChangeRequired( |
| 1473 | asyncResp->res, crow::utility::urlFromPieces( |
| 1474 | "redfish", "v1", "AccountService", |
| 1475 | "Accounts", req.session->username)); |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 1476 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1477 | return; |
| 1478 | } |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 1479 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1480 | req.userRole = userRole; |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1481 | rule.handle(req, asyncResp, params); |
RAJESWARAN THILLAIGOVINDAN | 61dbeef | 2019-12-13 04:26:54 -0600 | [diff] [blame] | 1482 | }, |
| 1483 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
Ed Tanous | 2d6cb56 | 2022-07-07 20:44:54 -0700 | [diff] [blame] | 1484 | "xyz.openbmc_project.User.Manager", "GetUserInfo", username); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1485 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1486 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1487 | void debugPrint() |
| 1488 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1489 | for (size_t i = 0; i < perMethods.size(); i++) |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1490 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1491 | BMCWEB_LOG_DEBUG << boost::beast::http::to_string( |
| 1492 | static_cast<boost::beast::http::verb>(i)); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1493 | perMethods[i].trie.debugPrint(); |
| 1494 | } |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 1495 | } |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 1496 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1497 | std::vector<const std::string*> getRoutes(const std::string& parent) |
| 1498 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1499 | std::vector<const std::string*> ret; |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1500 | |
| 1501 | for (const PerMethod& pm : perMethods) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1502 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1503 | std::vector<unsigned> x; |
| 1504 | pm.trie.findRouteIndexes(parent, x); |
| 1505 | for (unsigned index : x) |
| 1506 | { |
| 1507 | ret.push_back(&pm.rules[index]->rule); |
| 1508 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1509 | } |
| 1510 | return ret; |
| 1511 | } |
| 1512 | |
| 1513 | private: |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1514 | struct PerMethod |
| 1515 | { |
| 1516 | std::vector<BaseRule*> rules; |
| 1517 | Trie trie; |
Ed Tanous | 313a3c2 | 2022-03-14 09:27:38 -0700 | [diff] [blame] | 1518 | // rule index 0 has special meaning; preallocate it to avoid |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1519 | // duplication. |
Ed Tanous | 313a3c2 | 2022-03-14 09:27:38 -0700 | [diff] [blame] | 1520 | PerMethod() : rules(1) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1521 | {} |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1522 | }; |
Ed Tanous | 888880a | 2020-08-24 13:48:50 -0700 | [diff] [blame] | 1523 | |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 1524 | std::array<PerMethod, methodNotAllowedIndex + 1> perMethods; |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1525 | std::vector<std::unique_ptr<BaseRule>> allRules; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1526 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1527 | } // namespace crow |