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