Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 3 | #include "common.hpp" |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 4 | #include "dbus_utility.hpp" |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 5 | #include "error_messages.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 6 | #include "http_request.hpp" |
| 7 | #include "http_response.hpp" |
| 8 | #include "logging.hpp" |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 9 | #include "privileges.hpp" |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 10 | #include "sessions.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 11 | #include "utility.hpp" |
| 12 | #include "websocket.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 13 | |
Iwona Klimaszewska | c0a1c8a | 2019-07-12 18:26:38 +0200 | [diff] [blame] | 14 | #include <async_resp.hpp> |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 15 | #include <boost/beast/ssl/ssl_stream.hpp> |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 16 | #include <boost/container/flat_map.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 17 | |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 18 | #include <cerrno> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 19 | #include <cstdint> |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 20 | #include <cstdlib> |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 21 | #include <limits> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 22 | #include <memory> |
| 23 | #include <tuple> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 24 | #include <utility> |
| 25 | #include <vector> |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 26 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 27 | namespace crow |
| 28 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 29 | |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 30 | // Note, this is an imperfect abstraction. There are a lot of verbs that we |
| 31 | // use memory for, but are basically unused by most implementations. |
| 32 | // Ideally we would have a list of verbs that we do use, and only index in |
| 33 | // to a smaller array of those, but that would require a translation from |
| 34 | // boost::beast::http::verb, to the bmcweb index. |
| 35 | static constexpr size_t maxVerbIndex = |
| 36 | static_cast<size_t>(boost::beast::http::verb::patch); |
| 37 | |
| 38 | // MaxVerb + 1 is designated as the "not found" verb. It is done this way |
| 39 | // to keep the BaseRule as a single bitfield (thus keeping the struct small) |
| 40 | // while still having a way to declare a route a "not found" route. |
| 41 | static constexpr const size_t notFoundIndex = maxVerbIndex + 1; |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 42 | static constexpr const size_t methodNotAllowedIndex = notFoundIndex + 1; |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 43 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 44 | class BaseRule |
| 45 | { |
| 46 | public: |
Ed Tanous | 4e23a44 | 2022-06-06 09:57:26 -0700 | [diff] [blame] | 47 | explicit BaseRule(const std::string& thisRule) : rule(thisRule) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 48 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 49 | |
Ed Tanous | 0c0084a | 2019-10-24 15:57:51 -0700 | [diff] [blame] | 50 | virtual ~BaseRule() = default; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 51 | |
Ed Tanous | ecd6a3a | 2022-01-07 09:18:40 -0800 | [diff] [blame] | 52 | BaseRule(const BaseRule&) = delete; |
| 53 | BaseRule(BaseRule&&) = delete; |
| 54 | BaseRule& operator=(const BaseRule&) = delete; |
| 55 | BaseRule& operator=(const BaseRule&&) = delete; |
| 56 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 57 | virtual void validate() = 0; |
| 58 | std::unique_ptr<BaseRule> upgrade() |
| 59 | { |
| 60 | if (ruleToUpgrade) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 61 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 62 | return std::move(ruleToUpgrade); |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 63 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | return {}; |
| 65 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 66 | |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 67 | virtual void handle(const Request& /*req*/, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 68 | const std::shared_ptr<bmcweb::AsyncResp>&, |
| 69 | const RoutingParams&) = 0; |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 70 | virtual void handleUpgrade(const Request& /*req*/, Response& res, |
| 71 | boost::asio::ip::tcp::socket&& /*adaptor*/) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 72 | { |
Ed Tanous | de5c9f3 | 2019-03-26 09:17:55 -0700 | [diff] [blame] | 73 | res.result(boost::beast::http::status::not_found); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 74 | res.end(); |
| 75 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 76 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 77 | virtual void handleUpgrade( |
| 78 | const Request& /*req*/, Response& res, |
| 79 | boost::beast::ssl_stream<boost::asio::ip::tcp::socket>&& /*adaptor*/) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 80 | { |
Ed Tanous | de5c9f3 | 2019-03-26 09:17:55 -0700 | [diff] [blame] | 81 | res.result(boost::beast::http::status::not_found); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 82 | res.end(); |
| 83 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 84 | #endif |
| 85 | |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 86 | size_t getMethods() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 87 | { |
| 88 | return methodsBitfield; |
| 89 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 90 | |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 91 | bool checkPrivileges(const redfish::Privileges& userPrivileges) |
| 92 | { |
| 93 | // If there are no privileges assigned, assume no privileges |
| 94 | // required |
| 95 | if (privilegesSet.empty()) |
| 96 | { |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | for (const redfish::Privileges& requiredPrivileges : privilegesSet) |
| 101 | { |
| 102 | if (userPrivileges.isSupersetOf(requiredPrivileges)) |
| 103 | { |
| 104 | return true; |
| 105 | } |
| 106 | } |
| 107 | return false; |
| 108 | } |
| 109 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 110 | size_t methodsBitfield{ |
| 111 | 1 << static_cast<size_t>(boost::beast::http::verb::get)}; |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 112 | static_assert(std::numeric_limits<decltype(methodsBitfield)>::digits > |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 113 | methodNotAllowedIndex, |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 114 | "Not enough bits to store bitfield"); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 115 | |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 116 | std::vector<redfish::Privileges> privilegesSet; |
| 117 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 118 | std::string rule; |
| 119 | std::string nameStr; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 120 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 121 | std::unique_ptr<BaseRule> ruleToUpgrade; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 122 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 123 | friend class Router; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 124 | template <typename T> |
| 125 | friend struct RuleParameterTraits; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 128 | namespace detail |
| 129 | { |
| 130 | namespace routing_handler_call_helper |
| 131 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 132 | template <typename T, int Pos> |
| 133 | struct CallPair |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 134 | { |
| 135 | using type = T; |
| 136 | static const int pos = Pos; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 139 | template <typename H1> |
| 140 | struct CallParams |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 141 | { |
| 142 | H1& handler; |
| 143 | const RoutingParams& params; |
| 144 | const Request& req; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 145 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 146 | }; |
| 147 | |
| 148 | template <typename F, int NInt, int NUint, int NDouble, int NString, |
| 149 | typename S1, typename S2> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 150 | struct Call |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 151 | {}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 152 | |
| 153 | template <typename F, int NInt, int NUint, int NDouble, int NString, |
| 154 | typename... Args1, typename... Args2> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 155 | 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] | 156 | black_magic::S<Args2...>> |
| 157 | { |
| 158 | void operator()(F cparams) |
| 159 | { |
| 160 | using pushed = typename black_magic::S<Args2...>::template push_back< |
| 161 | CallPair<int64_t, NInt>>; |
| 162 | Call<F, NInt + 1, NUint, NDouble, NString, black_magic::S<Args1...>, |
| 163 | pushed>()(cparams); |
| 164 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | template <typename F, int NInt, int NUint, int NDouble, int NString, |
| 168 | typename... Args1, typename... Args2> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 169 | struct Call<F, NInt, NUint, NDouble, NString, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 170 | black_magic::S<uint64_t, Args1...>, black_magic::S<Args2...>> |
| 171 | { |
| 172 | void operator()(F cparams) |
| 173 | { |
| 174 | using pushed = typename black_magic::S<Args2...>::template push_back< |
| 175 | CallPair<uint64_t, NUint>>; |
| 176 | Call<F, NInt, NUint + 1, NDouble, NString, black_magic::S<Args1...>, |
| 177 | pushed>()(cparams); |
| 178 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | template <typename F, int NInt, int NUint, int NDouble, int NString, |
| 182 | typename... Args1, typename... Args2> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 183 | struct Call<F, NInt, NUint, NDouble, NString, black_magic::S<double, Args1...>, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 184 | black_magic::S<Args2...>> |
| 185 | { |
| 186 | void operator()(F cparams) |
| 187 | { |
| 188 | using pushed = typename black_magic::S<Args2...>::template push_back< |
| 189 | CallPair<double, NDouble>>; |
| 190 | Call<F, NInt, NUint, NDouble + 1, NString, black_magic::S<Args1...>, |
| 191 | pushed>()(cparams); |
| 192 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | template <typename F, int NInt, int NUint, int NDouble, int NString, |
| 196 | typename... Args1, typename... Args2> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 197 | struct Call<F, NInt, NUint, NDouble, NString, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 198 | black_magic::S<std::string, Args1...>, black_magic::S<Args2...>> |
| 199 | { |
| 200 | void operator()(F cparams) |
| 201 | { |
| 202 | using pushed = typename black_magic::S<Args2...>::template push_back< |
| 203 | CallPair<std::string, NString>>; |
| 204 | Call<F, NInt, NUint, NDouble, NString + 1, black_magic::S<Args1...>, |
| 205 | pushed>()(cparams); |
| 206 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | template <typename F, int NInt, int NUint, int NDouble, int NString, |
| 210 | typename... Args1> |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 211 | struct Call<F, NInt, NUint, NDouble, NString, black_magic::S<>, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 212 | black_magic::S<Args1...>> |
| 213 | { |
| 214 | void operator()(F cparams) |
| 215 | { |
| 216 | cparams.handler( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 217 | cparams.req, cparams.asyncResp, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 218 | cparams.params.template get<typename Args1::type>(Args1::pos)...); |
| 219 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 222 | template <typename Func, typename... ArgsWrapped> |
| 223 | struct Wrapped |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 224 | { |
| 225 | template <typename... Args> |
| 226 | void set( |
| 227 | Func f, |
| 228 | typename std::enable_if< |
| 229 | !std::is_same< |
| 230 | typename std::tuple_element<0, std::tuple<Args..., void>>::type, |
| 231 | const Request&>::value, |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 232 | int>::type /*enable*/ |
| 233 | = 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 234 | { |
Ed Tanous | f94c4ec | 2022-01-06 12:44:41 -0800 | [diff] [blame] | 235 | handler = [f = std::forward<Func>(f)]( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 236 | const Request&, |
| 237 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 238 | Args... args) { asyncResp->res.result(f(args...)); }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 241 | template <typename Req, typename... Args> |
| 242 | struct ReqHandlerWrapper |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 243 | { |
Ed Tanous | 4e23a44 | 2022-06-06 09:57:26 -0700 | [diff] [blame] | 244 | explicit ReqHandlerWrapper(Func fIn) : f(std::move(fIn)) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 245 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 246 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 247 | void operator()(const Request& req, |
| 248 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 249 | Args... args) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 250 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 251 | asyncResp->res.result(f(req, args...)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 252 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 253 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 254 | Func f; |
| 255 | }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 256 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 257 | template <typename... Args> |
| 258 | void set( |
| 259 | Func f, |
| 260 | typename std::enable_if< |
| 261 | std::is_same< |
| 262 | typename std::tuple_element<0, std::tuple<Args..., void>>::type, |
| 263 | const Request&>::value && |
| 264 | !std::is_same<typename std::tuple_element< |
| 265 | 1, std::tuple<Args..., void, void>>::type, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 266 | const std::shared_ptr<bmcweb::AsyncResp>&>::value, |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 267 | int>::type /*enable*/ |
| 268 | = 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 269 | { |
| 270 | handler = ReqHandlerWrapper<Args...>(std::move(f)); |
| 271 | /*handler = ( |
| 272 | [f = std::move(f)] |
| 273 | (const Request& req, Response& res, Args... args){ |
Ed Tanous | de5c9f3 | 2019-03-26 09:17:55 -0700 | [diff] [blame] | 274 | res.result(f(req, args...)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 275 | res.end(); |
| 276 | });*/ |
| 277 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 278 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 279 | template <typename... Args> |
| 280 | void set( |
| 281 | Func f, |
| 282 | typename std::enable_if< |
| 283 | std::is_same< |
| 284 | typename std::tuple_element<0, std::tuple<Args..., void>>::type, |
| 285 | const Request&>::value && |
| 286 | std::is_same<typename std::tuple_element< |
| 287 | 1, std::tuple<Args..., void, void>>::type, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 288 | const std::shared_ptr<bmcweb::AsyncResp>&>::value, |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 289 | int>::type /*enable*/ |
| 290 | = 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 291 | { |
| 292 | handler = std::move(f); |
| 293 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 294 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 295 | template <typename... Args> |
| 296 | struct HandlerTypeHelper |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 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> |
| 306 | struct HandlerTypeHelper<const Request&, Args...> |
| 307 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 308 | using type = std::function<void( |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 309 | const crow::Request& /*req*/, |
| 310 | const std::shared_ptr<bmcweb::AsyncResp>&, Args...)>; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 311 | using args_type = |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 312 | black_magic::S<typename black_magic::PromoteT<Args>...>; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 313 | }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 314 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 315 | template <typename... Args> |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 316 | struct HandlerTypeHelper<const Request&, |
| 317 | const std::shared_ptr<bmcweb::AsyncResp>&, Args...> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 318 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 319 | using type = std::function<void( |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 320 | const crow::Request& /*req*/, |
| 321 | const std::shared_ptr<bmcweb::AsyncResp>&, Args...)>; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 322 | using args_type = |
Ed Tanous | 988403c | 2020-08-24 11:29:49 -0700 | [diff] [blame] | 323 | black_magic::S<typename black_magic::PromoteT<Args>...>; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 324 | }; |
| 325 | |
| 326 | typename HandlerTypeHelper<ArgsWrapped...>::type handler; |
| 327 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 328 | void operator()(const Request& req, |
| 329 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 330 | const RoutingParams& params) |
| 331 | { |
| 332 | detail::routing_handler_call_helper::Call< |
| 333 | detail::routing_handler_call_helper::CallParams<decltype(handler)>, |
| 334 | 0, 0, 0, 0, typename HandlerTypeHelper<ArgsWrapped...>::args_type, |
| 335 | black_magic::S<>>()( |
| 336 | detail::routing_handler_call_helper::CallParams<decltype(handler)>{ |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 337 | handler, params, req, asyncResp}); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 338 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 339 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 340 | } // namespace routing_handler_call_helper |
| 341 | } // namespace detail |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 342 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 343 | class WebSocketRule : public BaseRule |
| 344 | { |
| 345 | using self_t = WebSocketRule; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 346 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 347 | public: |
Ed Tanous | 4e23a44 | 2022-06-06 09:57:26 -0700 | [diff] [blame] | 348 | explicit WebSocketRule(const std::string& ruleIn) : BaseRule(ruleIn) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 349 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 350 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 351 | void validate() override |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 352 | {} |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 353 | |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 354 | void handle(const Request& /*req*/, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 355 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 356 | const RoutingParams& /*params*/) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 357 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 358 | asyncResp->res.result(boost::beast::http::status::not_found); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 359 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 360 | |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 361 | void handleUpgrade(const Request& req, Response& /*res*/, |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 362 | boost::asio::ip::tcp::socket&& adaptor) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 363 | { |
Nan Zhou | 93c0202 | 2022-02-24 18:21:07 -0800 | [diff] [blame] | 364 | BMCWEB_LOG_DEBUG << "Websocket handles upgrade"; |
Ratan Gupta | 02453b1 | 2019-10-22 14:43:36 +0530 | [diff] [blame] | 365 | std::shared_ptr< |
| 366 | crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>> |
| 367 | myConnection = std::make_shared< |
| 368 | crow::websocket::ConnectionImpl<boost::asio::ip::tcp::socket>>( |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 369 | req, std::move(adaptor), openHandler, messageHandler, |
Ratan Gupta | 02453b1 | 2019-10-22 14:43:36 +0530 | [diff] [blame] | 370 | closeHandler, errorHandler); |
| 371 | myConnection->start(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 372 | } |
| 373 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 104f09c | 2022-01-25 09:56:04 -0800 | [diff] [blame] | 374 | void handleUpgrade(const Request& req, Response& /*res*/, |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 375 | boost::beast::ssl_stream<boost::asio::ip::tcp::socket>&& |
| 376 | adaptor) override |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 377 | { |
Nan Zhou | 93c0202 | 2022-02-24 18:21:07 -0800 | [diff] [blame] | 378 | BMCWEB_LOG_DEBUG << "Websocket handles upgrade"; |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 379 | std::shared_ptr<crow::websocket::ConnectionImpl< |
| 380 | boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>> |
| 381 | myConnection = std::make_shared<crow::websocket::ConnectionImpl< |
| 382 | boost::beast::ssl_stream<boost::asio::ip::tcp::socket>>>( |
Jan Sowinski | ee52ae1 | 2020-01-09 16:28:32 +0000 | [diff] [blame] | 383 | req, std::move(adaptor), openHandler, messageHandler, |
Ed Tanous | ceac6f7 | 2018-12-02 11:58:47 -0800 | [diff] [blame] | 384 | closeHandler, errorHandler); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 385 | myConnection->start(); |
| 386 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 387 | #endif |
| 388 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 389 | template <typename Func> |
| 390 | self_t& onopen(Func f) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 391 | { |
| 392 | openHandler = f; |
| 393 | return *this; |
| 394 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 395 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 396 | template <typename Func> |
| 397 | self_t& onmessage(Func f) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 398 | { |
| 399 | messageHandler = f; |
| 400 | return *this; |
| 401 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 402 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 403 | template <typename Func> |
| 404 | self_t& onclose(Func f) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 405 | { |
| 406 | closeHandler = f; |
| 407 | return *this; |
| 408 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 409 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 410 | template <typename Func> |
| 411 | self_t& onerror(Func f) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 412 | { |
| 413 | errorHandler = f; |
| 414 | return *this; |
| 415 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 416 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 417 | protected: |
zhanghch05 | 7772638 | 2021-10-21 14:07:57 +0800 | [diff] [blame] | 418 | std::function<void(crow::websocket::Connection&)> openHandler; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 419 | std::function<void(crow::websocket::Connection&, const std::string&, bool)> |
| 420 | messageHandler; |
| 421 | std::function<void(crow::websocket::Connection&, const std::string&)> |
| 422 | closeHandler; |
| 423 | std::function<void(crow::websocket::Connection&)> errorHandler; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 424 | }; |
| 425 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 426 | template <typename T> |
| 427 | struct RuleParameterTraits |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 428 | { |
| 429 | using self_t = T; |
| 430 | WebSocketRule& websocket() |
| 431 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 432 | self_t* self = static_cast<self_t*>(this); |
| 433 | WebSocketRule* p = new WebSocketRule(self->rule); |
| 434 | self->ruleToUpgrade.reset(p); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 435 | return *p; |
| 436 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 437 | |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 438 | self_t& name(const std::string_view name) noexcept |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 439 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 440 | self_t* self = static_cast<self_t*>(this); |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 441 | self->nameStr = name; |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 442 | return *self; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 443 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 444 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 445 | self_t& methods(boost::beast::http::verb method) |
| 446 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 447 | self_t* self = static_cast<self_t*>(this); |
| 448 | self->methodsBitfield = 1U << static_cast<size_t>(method); |
| 449 | return *self; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 450 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 451 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 452 | template <typename... MethodArgs> |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 453 | self_t& methods(boost::beast::http::verb method, MethodArgs... argsMethod) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 454 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 455 | self_t* self = static_cast<self_t*>(this); |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 456 | methods(argsMethod...); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 457 | self->methodsBitfield |= 1U << static_cast<size_t>(method); |
| 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 | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 671 | void operator()(const 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 | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 815 | find(const 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 | |
| 1213 | size_t reqMethodIndex = static_cast<size_t>(req.method()); |
| 1214 | // Check to see if this url exists at any verb |
| 1215 | for (size_t perMethodIndex = 0; perMethodIndex <= maxVerbIndex; |
| 1216 | perMethodIndex++) |
| 1217 | { |
| 1218 | // Make sure it's safe to deference the array at that index |
| 1219 | static_assert(maxVerbIndex < |
| 1220 | std::tuple_size_v<decltype(perMethods)>); |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 1221 | FindRoute route = findRouteByIndex(req.url, perMethodIndex); |
| 1222 | if (route.rule == nullptr) |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1223 | { |
| 1224 | continue; |
| 1225 | } |
| 1226 | if (!findRoute.allowHeader.empty()) |
| 1227 | { |
| 1228 | findRoute.allowHeader += ", "; |
| 1229 | } |
| 1230 | findRoute.allowHeader += boost::beast::http::to_string( |
| 1231 | static_cast<boost::beast::http::verb>(perMethodIndex)); |
| 1232 | if (perMethodIndex == reqMethodIndex) |
| 1233 | { |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 1234 | findRoute.route = route; |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1235 | } |
| 1236 | } |
| 1237 | return findRoute; |
| 1238 | } |
| 1239 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1240 | template <typename Adaptor> |
| 1241 | void handleUpgrade(const Request& req, Response& res, Adaptor&& adaptor) |
| 1242 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1243 | if (static_cast<size_t>(req.method()) >= perMethods.size()) |
Ed Tanous | 888880a | 2020-08-24 13:48:50 -0700 | [diff] [blame] | 1244 | { |
| 1245 | res.result(boost::beast::http::status::not_found); |
| 1246 | res.end(); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1247 | return; |
Ed Tanous | 888880a | 2020-08-24 13:48:50 -0700 | [diff] [blame] | 1248 | } |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1249 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1250 | PerMethod& perMethod = perMethods[static_cast<size_t>(req.method())]; |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1251 | Trie& trie = perMethod.trie; |
| 1252 | std::vector<BaseRule*>& rules = perMethod.rules; |
| 1253 | |
| 1254 | const std::pair<unsigned, RoutingParams>& found = trie.find(req.url); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1255 | unsigned ruleIndex = found.first; |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 1256 | if (ruleIndex == 0U) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1257 | { |
| 1258 | BMCWEB_LOG_DEBUG << "Cannot match rules " << req.url; |
Ed Tanous | de5c9f3 | 2019-03-26 09:17:55 -0700 | [diff] [blame] | 1259 | res.result(boost::beast::http::status::not_found); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1260 | res.end(); |
| 1261 | return; |
| 1262 | } |
| 1263 | |
| 1264 | if (ruleIndex >= rules.size()) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 1265 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1266 | throw std::runtime_error("Trie internal structure corrupted!"); |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 1267 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1268 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1269 | if ((rules[ruleIndex]->getMethods() & |
| 1270 | (1U << static_cast<size_t>(req.method()))) == 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1271 | { |
| 1272 | BMCWEB_LOG_DEBUG << "Rule found but method mismatch: " << req.url |
| 1273 | << " with " << req.methodString() << "(" |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1274 | << static_cast<uint32_t>(req.method()) << ") / " |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1275 | << rules[ruleIndex]->getMethods(); |
Ed Tanous | de5c9f3 | 2019-03-26 09:17:55 -0700 | [diff] [blame] | 1276 | res.result(boost::beast::http::status::not_found); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1277 | res.end(); |
| 1278 | return; |
| 1279 | } |
| 1280 | |
| 1281 | BMCWEB_LOG_DEBUG << "Matched rule (upgrade) '" << rules[ruleIndex]->rule |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1282 | << "' " << static_cast<uint32_t>(req.method()) << " / " |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1283 | << rules[ruleIndex]->getMethods(); |
| 1284 | |
| 1285 | // any uncaught exceptions become 500s |
| 1286 | try |
| 1287 | { |
Ed Tanous | f94c4ec | 2022-01-06 12:44:41 -0800 | [diff] [blame] | 1288 | rules[ruleIndex]->handleUpgrade(req, res, |
| 1289 | std::forward<Adaptor>(adaptor)); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1290 | } |
Patrick Williams | c596704 | 2021-10-06 12:39:54 -0500 | [diff] [blame] | 1291 | catch (const std::exception& e) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1292 | { |
| 1293 | BMCWEB_LOG_ERROR << "An uncaught exception occurred: " << e.what(); |
Ed Tanous | de5c9f3 | 2019-03-26 09:17:55 -0700 | [diff] [blame] | 1294 | res.result(boost::beast::http::status::internal_server_error); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1295 | res.end(); |
| 1296 | return; |
| 1297 | } |
| 1298 | catch (...) |
| 1299 | { |
| 1300 | BMCWEB_LOG_ERROR |
| 1301 | << "An uncaught exception occurred. The type was unknown " |
| 1302 | "so no information was available."; |
Ed Tanous | de5c9f3 | 2019-03-26 09:17:55 -0700 | [diff] [blame] | 1303 | res.result(boost::beast::http::status::internal_server_error); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1304 | res.end(); |
| 1305 | return; |
| 1306 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1309 | void handle(Request& req, |
| 1310 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1311 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1312 | if (static_cast<size_t>(req.method()) >= perMethods.size()) |
Ed Tanous | 888880a | 2020-08-24 13:48:50 -0700 | [diff] [blame] | 1313 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1314 | asyncResp->res.result(boost::beast::http::status::not_found); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1315 | return; |
Ed Tanous | 888880a | 2020-08-24 13:48:50 -0700 | [diff] [blame] | 1316 | } |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1317 | |
| 1318 | FindRouteResponse foundRoute = findRoute(req); |
| 1319 | |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 1320 | if (foundRoute.route.rule == nullptr) |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 1321 | { |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 1322 | // Couldn't find a normal route with any verb, try looking for a 404 |
| 1323 | // route |
| 1324 | if (foundRoute.allowHeader.empty()) |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1325 | { |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 1326 | foundRoute.route = findRouteByIndex(req.url, notFoundIndex); |
| 1327 | } |
| 1328 | else |
| 1329 | { |
| 1330 | // See if we have a method not allowed (405) handler |
| 1331 | foundRoute.route = |
| 1332 | findRouteByIndex(req.url, methodNotAllowedIndex); |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1333 | } |
| 1334 | } |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 1335 | |
| 1336 | // Fill in the allow header if it's valid |
| 1337 | if (!foundRoute.allowHeader.empty()) |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1338 | { |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 1339 | |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 1340 | asyncResp->res.addHeader(boost::beast::http::field::allow, |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1341 | foundRoute.allowHeader); |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 1342 | } |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1343 | |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1344 | // If we couldn't find a real route or a 404 route, return a generic |
| 1345 | // response |
| 1346 | if (foundRoute.route.rule == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1347 | { |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1348 | if (foundRoute.allowHeader.empty()) |
| 1349 | { |
| 1350 | asyncResp->res.result(boost::beast::http::status::not_found); |
| 1351 | } |
| 1352 | else |
Ed Tanous | 2634dcd | 2019-03-26 09:28:06 -0700 | [diff] [blame] | 1353 | { |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 1354 | asyncResp->res.result( |
| 1355 | boost::beast::http::status::method_not_allowed); |
Ed Tanous | 2634dcd | 2019-03-26 09:28:06 -0700 | [diff] [blame] | 1356 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1357 | return; |
| 1358 | } |
| 1359 | |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1360 | BaseRule& rule = *foundRoute.route.rule; |
| 1361 | RoutingParams params = std::move(foundRoute.route.params); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1362 | |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1363 | BMCWEB_LOG_DEBUG << "Matched rule '" << rule.rule << "' " |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1364 | << static_cast<uint32_t>(req.method()) << " / " |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1365 | << rule.getMethods(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1366 | |
RAJESWARAN THILLAIGOVINDAN | 61dbeef | 2019-12-13 04:26:54 -0600 | [diff] [blame] | 1367 | if (req.session == nullptr) |
James Feist | 7166bf0 | 2019-12-10 16:52:14 +0000 | [diff] [blame] | 1368 | { |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1369 | rule.handle(req, asyncResp, params); |
James Feist | 7166bf0 | 2019-12-10 16:52:14 +0000 | [diff] [blame] | 1370 | return; |
| 1371 | } |
RAJESWARAN THILLAIGOVINDAN | 61dbeef | 2019-12-13 04:26:54 -0600 | [diff] [blame] | 1372 | |
| 1373 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1374 | [&req, asyncResp, &rule, |
| 1375 | params](const boost::system::error_code ec, |
| 1376 | const dbus::utility::DBusPropertiesMap& userInfoMap) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1377 | if (ec) |
| 1378 | { |
| 1379 | BMCWEB_LOG_ERROR << "GetUserInfo failed..."; |
| 1380 | asyncResp->res.result( |
| 1381 | boost::beast::http::status::internal_server_error); |
| 1382 | return; |
| 1383 | } |
| 1384 | std::string userRole{}; |
| 1385 | const bool* remoteUser = nullptr; |
| 1386 | std::optional<bool> passwordExpired; |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 1387 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1388 | for (const auto& userInfo : userInfoMap) |
| 1389 | { |
| 1390 | if (userInfo.first == "UserPrivilege") |
RAJESWARAN THILLAIGOVINDAN | 61dbeef | 2019-12-13 04:26:54 -0600 | [diff] [blame] | 1391 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1392 | const std::string* userRolePtr = |
| 1393 | std::get_if<std::string>(&userInfo.second); |
| 1394 | if (userRolePtr == nullptr) |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 1395 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1396 | continue; |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 1397 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1398 | userRole = *userRolePtr; |
| 1399 | BMCWEB_LOG_DEBUG << "userName = " << req.session->username |
| 1400 | << " userRole = " << *userRolePtr; |
RAJESWARAN THILLAIGOVINDAN | 61dbeef | 2019-12-13 04:26:54 -0600 | [diff] [blame] | 1401 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1402 | else if (userInfo.first == "RemoteUser") |
| 1403 | { |
| 1404 | remoteUser = std::get_if<bool>(&userInfo.second); |
| 1405 | } |
| 1406 | else if (userInfo.first == "UserPasswordExpired") |
| 1407 | { |
| 1408 | const bool* passwordExpiredPtr = |
| 1409 | std::get_if<bool>(&userInfo.second); |
| 1410 | if (passwordExpiredPtr == nullptr) |
| 1411 | { |
| 1412 | continue; |
| 1413 | } |
| 1414 | passwordExpired = *passwordExpiredPtr; |
| 1415 | } |
| 1416 | } |
RAJESWARAN THILLAIGOVINDAN | 61dbeef | 2019-12-13 04:26:54 -0600 | [diff] [blame] | 1417 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1418 | if (remoteUser == nullptr) |
| 1419 | { |
| 1420 | BMCWEB_LOG_ERROR << "RemoteUser property missing or wrong type"; |
| 1421 | asyncResp->res.result( |
| 1422 | boost::beast::http::status::internal_server_error); |
| 1423 | return; |
| 1424 | } |
| 1425 | |
| 1426 | if (passwordExpired == std::nullopt) |
| 1427 | { |
| 1428 | if (!*remoteUser) |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 1429 | { |
| 1430 | BMCWEB_LOG_ERROR |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1431 | << "UserPasswordExpired property is expected for" |
| 1432 | " local user but is missing or wrong type"; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1433 | asyncResp->res.result( |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 1434 | boost::beast::http::status::internal_server_error); |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 1435 | return; |
| 1436 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1437 | passwordExpired = false; |
| 1438 | } |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 1439 | |
Nan Zhou | ac6250a | 2022-08-09 20:15:44 +0000 | [diff] [blame] | 1440 | // Get the user's privileges from the role |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1441 | redfish::Privileges userPrivileges = |
| 1442 | redfish::getUserPrivileges(userRole); |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 1443 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1444 | // Set isConfigureSelfOnly based on D-Bus results. This |
| 1445 | // ignores the results from both pamAuthenticateUser and the |
| 1446 | // value from any previous use of this session. |
| 1447 | req.session->isConfigureSelfOnly = *passwordExpired; |
RAJESWARAN THILLAIGOVINDAN | 61dbeef | 2019-12-13 04:26:54 -0600 | [diff] [blame] | 1448 | |
Nan Zhou | ac6250a | 2022-08-09 20:15:44 +0000 | [diff] [blame] | 1449 | // Modify privileges if isConfigureSelfOnly. |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1450 | if (req.session->isConfigureSelfOnly) |
| 1451 | { |
Nan Zhou | ac6250a | 2022-08-09 20:15:44 +0000 | [diff] [blame] | 1452 | // Remove all privileges except ConfigureSelf |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1453 | userPrivileges = userPrivileges.intersection( |
| 1454 | redfish::Privileges{"ConfigureSelf"}); |
| 1455 | BMCWEB_LOG_DEBUG << "Operation limited to ConfigureSelf"; |
| 1456 | } |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 1457 | |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1458 | if (!rule.checkPrivileges(userPrivileges)) |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1459 | { |
| 1460 | asyncResp->res.result(boost::beast::http::status::forbidden); |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 1461 | if (req.session->isConfigureSelfOnly) |
| 1462 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1463 | redfish::messages::passwordChangeRequired( |
| 1464 | asyncResp->res, crow::utility::urlFromPieces( |
| 1465 | "redfish", "v1", "AccountService", |
| 1466 | "Accounts", req.session->username)); |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 1467 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1468 | return; |
| 1469 | } |
Joseph Reynolds | 3bf4e63 | 2020-02-06 14:44:32 -0600 | [diff] [blame] | 1470 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 1471 | req.userRole = userRole; |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 1472 | rule.handle(req, asyncResp, params); |
RAJESWARAN THILLAIGOVINDAN | 61dbeef | 2019-12-13 04:26:54 -0600 | [diff] [blame] | 1473 | }, |
| 1474 | "xyz.openbmc_project.User.Manager", "/xyz/openbmc_project/user", |
| 1475 | "xyz.openbmc_project.User.Manager", "GetUserInfo", |
| 1476 | req.session->username); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1477 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1478 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1479 | void debugPrint() |
| 1480 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1481 | for (size_t i = 0; i < perMethods.size(); i++) |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1482 | { |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 1483 | BMCWEB_LOG_DEBUG << boost::beast::http::to_string( |
| 1484 | static_cast<boost::beast::http::verb>(i)); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1485 | perMethods[i].trie.debugPrint(); |
| 1486 | } |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 1487 | } |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 1488 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1489 | std::vector<const std::string*> getRoutes(const std::string& parent) |
| 1490 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1491 | std::vector<const std::string*> ret; |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1492 | |
| 1493 | for (const PerMethod& pm : perMethods) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1494 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1495 | std::vector<unsigned> x; |
| 1496 | pm.trie.findRouteIndexes(parent, x); |
| 1497 | for (unsigned index : x) |
| 1498 | { |
| 1499 | ret.push_back(&pm.rules[index]->rule); |
| 1500 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1501 | } |
| 1502 | return ret; |
| 1503 | } |
| 1504 | |
| 1505 | private: |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1506 | struct PerMethod |
| 1507 | { |
| 1508 | std::vector<BaseRule*> rules; |
| 1509 | Trie trie; |
Ed Tanous | 313a3c2 | 2022-03-14 09:27:38 -0700 | [diff] [blame] | 1510 | // rule index 0 has special meaning; preallocate it to avoid |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1511 | // duplication. |
Ed Tanous | 313a3c2 | 2022-03-14 09:27:38 -0700 | [diff] [blame] | 1512 | PerMethod() : rules(1) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1513 | {} |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1514 | }; |
Ed Tanous | 888880a | 2020-08-24 13:48:50 -0700 | [diff] [blame] | 1515 | |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 1516 | std::array<PerMethod, methodNotAllowedIndex + 1> perMethods; |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 1517 | std::vector<std::unique_ptr<BaseRule>> allRules; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 1518 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1519 | } // namespace crow |