| Ed Tanous | 40e9b92 | 2024-09-10 13:50:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 | 
|  | 2 | // SPDX-FileCopyrightText: Copyright OpenBMC Authors | 
| Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 3 | #pragma once | 
|  | 4 |  | 
| Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 5 | #include "async_resp.hpp" | 
| Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 6 | #include "dbus_privileges.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" | 
| Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 10 | #include "routing/baserule.hpp" | 
|  | 11 | #include "routing/dynamicrule.hpp" | 
| Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 12 | #include "routing/taggedrule.hpp" | 
| Ed Tanous | 5b607ea | 2025-03-26 12:13:39 -0700 | [diff] [blame] | 13 | #include "routing/trie.hpp" | 
| Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 14 | #include "verb.hpp" | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 15 |  | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 16 | #include <boost/beast/http/field.hpp> | 
|  | 17 | #include <boost/beast/http/status.hpp> | 
| Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 18 |  | 
| Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 19 | #include <algorithm> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 20 | #include <array> | 
| Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 21 | #include <cstdint> | 
| Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 22 | #include <cstdlib> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 23 | #include <format> | 
|  | 24 | #include <functional> | 
| Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 25 | #include <memory> | 
| Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 26 | #include <optional> | 
| Ed Tanous | d785720 | 2025-01-28 15:32:26 -0800 | [diff] [blame] | 27 | #include <stdexcept> | 
|  | 28 | #include <string> | 
| Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 29 | #include <string_view> | 
| Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 30 | #include <tuple> | 
| Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 31 | #include <utility> | 
|  | 32 | #include <vector> | 
| Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 33 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 34 | namespace crow | 
|  | 35 | { | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 36 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 37 | class Router | 
|  | 38 | { | 
|  | 39 | public: | 
| Ed Tanous | 0c0084a | 2019-10-24 15:57:51 -0700 | [diff] [blame] | 40 | Router() = default; | 
| Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 41 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 42 | DynamicRule& newRuleDynamic(const std::string& rule) | 
|  | 43 | { | 
|  | 44 | std::unique_ptr<DynamicRule> ruleObject = | 
|  | 45 | std::make_unique<DynamicRule>(rule); | 
|  | 46 | DynamicRule* ptr = ruleObject.get(); | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 47 | allRules.emplace_back(std::move(ruleObject)); | 
| Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 48 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 49 | return *ptr; | 
| Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 50 | } | 
|  | 51 |  | 
| Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 52 | template <uint64_t NumArgs> | 
| Ed Tanous | cfe3bc0 | 2023-06-26 12:47:24 -0700 | [diff] [blame] | 53 | auto& newRuleTagged(const std::string& rule) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 54 | { | 
| Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 55 | if constexpr (NumArgs == 0) | 
| Ed Tanous | cfe3bc0 | 2023-06-26 12:47:24 -0700 | [diff] [blame] | 56 | { | 
|  | 57 | using RuleT = TaggedRule<>; | 
|  | 58 | std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule); | 
|  | 59 | RuleT* ptr = ruleObject.get(); | 
|  | 60 | allRules.emplace_back(std::move(ruleObject)); | 
|  | 61 | return *ptr; | 
|  | 62 | } | 
| Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 63 | else if constexpr (NumArgs == 1) | 
| Ed Tanous | cfe3bc0 | 2023-06-26 12:47:24 -0700 | [diff] [blame] | 64 | { | 
|  | 65 | using RuleT = TaggedRule<std::string>; | 
|  | 66 | std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule); | 
|  | 67 | RuleT* ptr = ruleObject.get(); | 
|  | 68 | allRules.emplace_back(std::move(ruleObject)); | 
|  | 69 | return *ptr; | 
|  | 70 | } | 
| Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 71 | else if constexpr (NumArgs == 2) | 
| Ed Tanous | cfe3bc0 | 2023-06-26 12:47:24 -0700 | [diff] [blame] | 72 | { | 
|  | 73 | using RuleT = TaggedRule<std::string, std::string>; | 
|  | 74 | std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule); | 
|  | 75 | RuleT* ptr = ruleObject.get(); | 
|  | 76 | allRules.emplace_back(std::move(ruleObject)); | 
|  | 77 | return *ptr; | 
|  | 78 | } | 
| Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 79 | else if constexpr (NumArgs == 3) | 
| Ed Tanous | cfe3bc0 | 2023-06-26 12:47:24 -0700 | [diff] [blame] | 80 | { | 
|  | 81 | using RuleT = TaggedRule<std::string, std::string, std::string>; | 
|  | 82 | std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule); | 
|  | 83 | RuleT* ptr = ruleObject.get(); | 
|  | 84 | allRules.emplace_back(std::move(ruleObject)); | 
|  | 85 | return *ptr; | 
|  | 86 | } | 
| Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 87 | else if constexpr (NumArgs == 4) | 
| Ed Tanous | cfe3bc0 | 2023-06-26 12:47:24 -0700 | [diff] [blame] | 88 | { | 
|  | 89 | using RuleT = | 
|  | 90 | TaggedRule<std::string, std::string, std::string, std::string>; | 
|  | 91 | std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule); | 
|  | 92 | RuleT* ptr = ruleObject.get(); | 
|  | 93 | allRules.emplace_back(std::move(ruleObject)); | 
|  | 94 | return *ptr; | 
|  | 95 | } | 
|  | 96 | else | 
|  | 97 | { | 
|  | 98 | using RuleT = TaggedRule<std::string, std::string, std::string, | 
|  | 99 | std::string, std::string>; | 
|  | 100 | std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule); | 
|  | 101 | RuleT* ptr = ruleObject.get(); | 
|  | 102 | allRules.emplace_back(std::move(ruleObject)); | 
|  | 103 | return *ptr; | 
|  | 104 | } | 
| Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 105 | static_assert(NumArgs <= 5, "Max number of args supported is 5"); | 
| Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 106 | } | 
|  | 107 |  | 
| Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 108 | struct PerMethod | 
|  | 109 | { | 
|  | 110 | std::vector<BaseRule*> rules; | 
| Rohit PAI | 81f915b | 2025-04-02 12:29:29 +0530 | [diff] [blame] | 111 | Trie<crow::Node> trie; | 
| Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 112 | // rule index 0 has special meaning; preallocate it to avoid | 
|  | 113 | // duplication. | 
|  | 114 | PerMethod() : rules(1) {} | 
|  | 115 |  | 
|  | 116 | void internalAdd(std::string_view rule, BaseRule* ruleObject) | 
|  | 117 | { | 
|  | 118 | rules.emplace_back(ruleObject); | 
|  | 119 | trie.add(rule, static_cast<unsigned>(rules.size() - 1U)); | 
|  | 120 | // directory case: | 
|  | 121 | //   request to `/about' url matches `/about/' rule | 
|  | 122 | if (rule.size() > 2 && rule.back() == '/') | 
|  | 123 | { | 
|  | 124 | trie.add(rule.substr(0, rule.size() - 1), | 
|  | 125 | static_cast<unsigned>(rules.size() - 1)); | 
|  | 126 | } | 
|  | 127 | } | 
|  | 128 | }; | 
|  | 129 |  | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 130 | void internalAddRuleObject(const std::string& rule, BaseRule* ruleObject) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 131 | { | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 132 | if (ruleObject == nullptr) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 133 | { | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 134 | return; | 
|  | 135 | } | 
| Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 136 | for (size_t method = 0; method <= maxVerbIndex; method++) | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 137 | { | 
| Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 138 | size_t methodBit = 1 << method; | 
| Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 139 | if ((ruleObject->methodsBitfield & methodBit) > 0U) | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 140 | { | 
| Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 141 | perMethods[method].internalAdd(rule, ruleObject); | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 142 | } | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 143 | } | 
| Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 144 |  | 
|  | 145 | if (ruleObject->isNotFound) | 
|  | 146 | { | 
|  | 147 | notFoundRoutes.internalAdd(rule, ruleObject); | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | if (ruleObject->isMethodNotAllowed) | 
|  | 151 | { | 
|  | 152 | methodNotAllowedRoutes.internalAdd(rule, ruleObject); | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | if (ruleObject->isUpgrade) | 
|  | 156 | { | 
|  | 157 | upgradeRoutes.internalAdd(rule, ruleObject); | 
|  | 158 | } | 
| Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 159 | } | 
|  | 160 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 161 | void validate() | 
|  | 162 | { | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 163 | for (std::unique_ptr<BaseRule>& rule : allRules) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 164 | { | 
|  | 165 | if (rule) | 
|  | 166 | { | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 167 | std::unique_ptr<BaseRule> upgraded = rule->upgrade(); | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 168 | if (upgraded) | 
| Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 169 | { | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 170 | rule = std::move(upgraded); | 
| Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 171 | } | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 172 | rule->validate(); | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 173 | internalAddRuleObject(rule->rule, rule.get()); | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 174 | } | 
|  | 175 | } | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 176 | for (PerMethod& perMethod : perMethods) | 
|  | 177 | { | 
|  | 178 | perMethod.trie.validate(); | 
|  | 179 | } | 
| Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 180 | } | 
|  | 181 |  | 
| Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 182 | struct FindRoute | 
|  | 183 | { | 
|  | 184 | BaseRule* rule = nullptr; | 
| Ed Tanous | 15a42df | 2023-02-09 18:08:23 -0800 | [diff] [blame] | 185 | std::vector<std::string> params; | 
| Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 186 | }; | 
|  | 187 |  | 
|  | 188 | struct FindRouteResponse | 
|  | 189 | { | 
|  | 190 | std::string allowHeader; | 
|  | 191 | FindRoute route; | 
|  | 192 | }; | 
|  | 193 |  | 
| Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 194 | static FindRoute findRouteByPerMethod(std::string_view url, | 
|  | 195 | const PerMethod& perMethod) | 
| Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 196 | { | 
|  | 197 | FindRoute route; | 
| Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 198 |  | 
| Rohit PAI | 81f915b | 2025-04-02 12:29:29 +0530 | [diff] [blame] | 199 | Trie<crow::Node>::FindResult found = perMethod.trie.find(url); | 
| Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 200 | if (found.ruleIndex >= perMethod.rules.size()) | 
| Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 201 | { | 
|  | 202 | throw std::runtime_error("Trie internal structure corrupted!"); | 
|  | 203 | } | 
|  | 204 | // Found a 404 route, switch that in | 
| Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 205 | if (found.ruleIndex != 0U) | 
| Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 206 | { | 
| Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 207 | route.rule = perMethod.rules[found.ruleIndex]; | 
|  | 208 | route.params = std::move(found.params); | 
| Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 209 | } | 
|  | 210 | return route; | 
|  | 211 | } | 
|  | 212 |  | 
| Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 213 | FindRouteResponse findRoute(const Request& req) const | 
| Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 214 | { | 
|  | 215 | FindRouteResponse findRoute; | 
|  | 216 |  | 
| Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 217 | // Check to see if this url exists at any verb | 
|  | 218 | for (size_t perMethodIndex = 0; perMethodIndex <= maxVerbIndex; | 
|  | 219 | perMethodIndex++) | 
|  | 220 | { | 
|  | 221 | // Make sure it's safe to deference the array at that index | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 222 | static_assert( | 
|  | 223 | maxVerbIndex < std::tuple_size_v<decltype(perMethods)>); | 
| Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 224 | FindRoute route = findRouteByPerMethod(req.url().encoded_path(), | 
|  | 225 | perMethods[perMethodIndex]); | 
| Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 226 | if (route.rule == nullptr) | 
| Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 227 | { | 
|  | 228 | continue; | 
|  | 229 | } | 
|  | 230 | if (!findRoute.allowHeader.empty()) | 
|  | 231 | { | 
|  | 232 | findRoute.allowHeader += ", "; | 
|  | 233 | } | 
| Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 234 | HttpVerb thisVerb = static_cast<HttpVerb>(perMethodIndex); | 
|  | 235 | findRoute.allowHeader += httpVerbToString(thisVerb); | 
| Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 236 | } | 
| Ed Tanous | 50bfc91 | 2024-07-29 14:20:50 -0700 | [diff] [blame] | 237 |  | 
|  | 238 | std::optional<HttpVerb> verb = httpVerbFromBoost(req.method()); | 
|  | 239 | if (!verb) | 
|  | 240 | { | 
|  | 241 | return findRoute; | 
|  | 242 | } | 
|  | 243 | size_t reqMethodIndex = static_cast<size_t>(*verb); | 
|  | 244 | if (reqMethodIndex >= perMethods.size()) | 
|  | 245 | { | 
|  | 246 | return findRoute; | 
|  | 247 | } | 
|  | 248 |  | 
|  | 249 | FindRoute route = findRouteByPerMethod(req.url().encoded_path(), | 
|  | 250 | perMethods[reqMethodIndex]); | 
|  | 251 | if (route.rule != nullptr) | 
|  | 252 | { | 
|  | 253 | findRoute.route = route; | 
|  | 254 | } | 
|  | 255 |  | 
| Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 256 | return findRoute; | 
|  | 257 | } | 
|  | 258 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 259 | template <typename Adaptor> | 
| Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 260 | void handleUpgrade(const std::shared_ptr<Request>& req, | 
| P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame] | 261 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, | 
|  | 262 | Adaptor&& adaptor) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 263 | { | 
| Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 264 | PerMethod& perMethod = upgradeRoutes; | 
| Rohit PAI | 81f915b | 2025-04-02 12:29:29 +0530 | [diff] [blame] | 265 | Trie<crow::Node>& trie = perMethod.trie; | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 266 | std::vector<BaseRule*>& rules = perMethod.rules; | 
|  | 267 |  | 
| Rohit PAI | 81f915b | 2025-04-02 12:29:29 +0530 | [diff] [blame] | 268 | Trie<crow::Node>::FindResult found = | 
|  | 269 | trie.find(req->url().encoded_path()); | 
| Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 270 | unsigned ruleIndex = found.ruleIndex; | 
| Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 271 | if (ruleIndex == 0U) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 272 | { | 
| Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 273 | BMCWEB_LOG_DEBUG("Cannot match rules {}", | 
|  | 274 | req->url().encoded_path()); | 
| P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame] | 275 | asyncResp->res.result(boost::beast::http::status::not_found); | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 276 | return; | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | if (ruleIndex >= rules.size()) | 
| Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 280 | { | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 281 | throw std::runtime_error("Trie internal structure corrupted!"); | 
| Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 282 | } | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 283 |  | 
| P Dheeraj Srujan Kumar | 7e9093e | 2021-09-18 01:19:04 +0530 | [diff] [blame] | 284 | BaseRule& rule = *rules[ruleIndex]; | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 285 |  | 
| Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 286 | BMCWEB_LOG_DEBUG("Matched rule (upgrade) '{}'", rule.rule); | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 287 |  | 
| P Dheeraj Srujan Kumar | 7e9093e | 2021-09-18 01:19:04 +0530 | [diff] [blame] | 288 | // TODO(ed) This should be able to use std::bind_front, but it doesn't | 
|  | 289 | // appear to work with the std::move on adaptor. | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 290 | validatePrivilege( | 
|  | 291 | req, asyncResp, rule, | 
|  | 292 | [req, &rule, asyncResp, | 
|  | 293 | adaptor = std::forward<Adaptor>(adaptor)]() mutable { | 
|  | 294 | rule.handleUpgrade(*req, asyncResp, std::move(adaptor)); | 
|  | 295 | }); | 
| Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 296 | } | 
|  | 297 |  | 
| Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 298 | void handle(const std::shared_ptr<Request>& req, | 
| zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 299 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 300 | { | 
| Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 301 | FindRouteResponse foundRoute = findRoute(*req); | 
| Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 302 |  | 
| Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 303 | if (foundRoute.route.rule == nullptr) | 
| Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 304 | { | 
| Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 305 | // Couldn't find a normal route with any verb, try looking for a 404 | 
|  | 306 | // route | 
|  | 307 | if (foundRoute.allowHeader.empty()) | 
| Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 308 | { | 
| Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 309 | foundRoute.route = findRouteByPerMethod( | 
|  | 310 | req->url().encoded_path(), notFoundRoutes); | 
| Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 311 | } | 
|  | 312 | else | 
|  | 313 | { | 
|  | 314 | // See if we have a method not allowed (405) handler | 
| Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 315 | foundRoute.route = findRouteByPerMethod( | 
|  | 316 | req->url().encoded_path(), methodNotAllowedRoutes); | 
| Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 317 | } | 
|  | 318 | } | 
| Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 319 |  | 
|  | 320 | // Fill in the allow header if it's valid | 
|  | 321 | if (!foundRoute.allowHeader.empty()) | 
| Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 322 | { | 
| Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 323 | asyncResp->res.addHeader(boost::beast::http::field::allow, | 
| Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 324 | foundRoute.allowHeader); | 
| Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 325 | } | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 326 |  | 
| Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 327 | // If we couldn't find a real route or a 404 route, return a generic | 
|  | 328 | // response | 
|  | 329 | if (foundRoute.route.rule == nullptr) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 330 | { | 
| Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 331 | if (foundRoute.allowHeader.empty()) | 
|  | 332 | { | 
|  | 333 | asyncResp->res.result(boost::beast::http::status::not_found); | 
|  | 334 | } | 
|  | 335 | else | 
| Ed Tanous | 2634dcd | 2019-03-26 09:28:06 -0700 | [diff] [blame] | 336 | { | 
| Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 337 | asyncResp->res.result( | 
|  | 338 | boost::beast::http::status::method_not_allowed); | 
| Ed Tanous | 2634dcd | 2019-03-26 09:28:06 -0700 | [diff] [blame] | 339 | } | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 340 | return; | 
|  | 341 | } | 
|  | 342 |  | 
| Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 343 | BaseRule& rule = *foundRoute.route.rule; | 
| Ed Tanous | 15a42df | 2023-02-09 18:08:23 -0800 | [diff] [blame] | 344 | std::vector<std::string> params = std::move(foundRoute.route.params); | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 345 |  | 
| Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 346 | BMCWEB_LOG_DEBUG("Matched rule '{}' {} / {}", rule.rule, | 
| Ed Tanous | 50bfc91 | 2024-07-29 14:20:50 -0700 | [diff] [blame] | 347 | req->methodString(), rule.getMethods()); | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 348 |  | 
| Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 349 | if (req->session == nullptr) | 
| James Feist | 7166bf0 | 2019-12-10 16:52:14 +0000 | [diff] [blame] | 350 | { | 
| Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 351 | rule.handle(*req, asyncResp, params); | 
| James Feist | 7166bf0 | 2019-12-10 16:52:14 +0000 | [diff] [blame] | 352 | return; | 
|  | 353 | } | 
| Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 354 | validatePrivilege( | 
|  | 355 | req, asyncResp, rule, | 
|  | 356 | [req, asyncResp, &rule, params = std::move(params)]() { | 
| Patrick Williams | bd79bce | 2024-08-16 15:22:20 -0400 | [diff] [blame] | 357 | rule.handle(*req, asyncResp, params); | 
|  | 358 | }); | 
| Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 359 | } | 
| Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 360 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 361 | void debugPrint() | 
|  | 362 | { | 
| Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 363 | for (size_t i = 0; i < perMethods.size(); i++) | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 364 | { | 
| Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 365 | BMCWEB_LOG_DEBUG("{}", httpVerbToString(static_cast<HttpVerb>(i))); | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 366 | perMethods[i].trie.debugPrint(); | 
|  | 367 | } | 
| Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 368 | } | 
| Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 369 |  | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 370 | std::vector<const std::string*> getRoutes(const std::string& parent) | 
|  | 371 | { | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 372 | std::vector<const std::string*> ret; | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 373 |  | 
|  | 374 | for (const PerMethod& pm : perMethods) | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 375 | { | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 376 | std::vector<unsigned> x; | 
|  | 377 | pm.trie.findRouteIndexes(parent, x); | 
|  | 378 | for (unsigned index : x) | 
|  | 379 | { | 
|  | 380 | ret.push_back(&pm.rules[index]->rule); | 
|  | 381 | } | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 382 | } | 
|  | 383 | return ret; | 
|  | 384 | } | 
|  | 385 |  | 
|  | 386 | private: | 
| Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 387 | std::array<PerMethod, static_cast<size_t>(HttpVerb::Max)> perMethods; | 
| Ed Tanous | 888880a | 2020-08-24 13:48:50 -0700 | [diff] [blame] | 388 |  | 
| Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 389 | PerMethod notFoundRoutes; | 
|  | 390 | PerMethod upgradeRoutes; | 
|  | 391 | PerMethod methodNotAllowedRoutes; | 
|  | 392 |  | 
| Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 393 | std::vector<std::unique_ptr<BaseRule>> allRules; | 
| Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 394 | }; | 
| Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 395 | } // namespace crow |