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