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