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