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