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