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