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