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