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