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