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