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 | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 4 | #include "dbus_privileges.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" |
Ed Tanous | 08bbe11 | 2023-04-06 13:10:02 -0700 | [diff] [blame] | 11 | #include "routing/baserule.hpp" |
| 12 | #include "routing/dynamicrule.hpp" |
| 13 | #include "routing/sserule.hpp" |
| 14 | #include "routing/taggedrule.hpp" |
| 15 | #include "routing/websocketrule.hpp" |
Ratan Gupta | 6f35956 | 2019-04-03 10:39:08 +0530 | [diff] [blame] | 16 | #include "sessions.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 17 | #include "utility.hpp" |
Ed Tanous | 3d18320 | 2023-03-10 09:21:58 -0800 | [diff] [blame] | 18 | #include "utils/dbus_utils.hpp" |
Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 19 | #include "verb.hpp" |
Ed Tanous | 04e438c | 2020-10-03 08:06:26 -0700 | [diff] [blame] | 20 | #include "websocket.hpp" |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 21 | |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 22 | #include <boost/container/flat_map.hpp> |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 23 | #include <boost/container/small_vector.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 24 | |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 25 | #include <algorithm> |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 26 | #include <cerrno> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 27 | #include <cstdint> |
Ed Tanous | e0d918b | 2018-03-27 17:41:04 -0700 | [diff] [blame] | 28 | #include <cstdlib> |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 29 | #include <limits> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 30 | #include <memory> |
Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 31 | #include <optional> |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 32 | #include <string_view> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 33 | #include <tuple> |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 34 | #include <utility> |
| 35 | #include <vector> |
Ed Tanous | 9140a67 | 2017-04-24 17:01:32 -0700 | [diff] [blame] | 36 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 37 | namespace crow |
| 38 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 39 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 40 | class Trie |
| 41 | { |
| 42 | public: |
| 43 | struct Node |
| 44 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 45 | unsigned ruleIndex = 0U; |
| 46 | |
| 47 | size_t stringParamChild = 0U; |
| 48 | size_t pathParamChild = 0U; |
| 49 | |
Ed Tanous | a94ac61 | 2022-02-22 11:13:24 -0800 | [diff] [blame] | 50 | using ChildMap = boost::container::flat_map< |
| 51 | std::string, unsigned, std::less<>, |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 52 | boost::container::small_vector<std::pair<std::string, unsigned>, |
| 53 | 1>>; |
Ed Tanous | a94ac61 | 2022-02-22 11:13:24 -0800 | [diff] [blame] | 54 | ChildMap children; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 55 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 56 | bool isSimpleNode() const |
| 57 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 58 | return ruleIndex == 0 && stringParamChild == 0 && |
| 59 | pathParamChild == 0; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 60 | } |
Ed Tanous | 55c7b7a | 2018-05-22 15:27:24 -0700 | [diff] [blame] | 61 | }; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 62 | |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 63 | Trie() : nodes(1) {} |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 64 | |
| 65 | private: |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 66 | void optimizeNode(Node& node) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 67 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 68 | if (node.stringParamChild != 0U) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 69 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 70 | optimizeNode(nodes[node.stringParamChild]); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 71 | } |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 72 | if (node.pathParamChild != 0U) |
| 73 | { |
| 74 | optimizeNode(nodes[node.pathParamChild]); |
| 75 | } |
| 76 | |
| 77 | if (node.children.empty()) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 78 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 79 | return; |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 80 | } |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 81 | while (true) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 82 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 83 | bool didMerge = false; |
| 84 | Node::ChildMap merged; |
| 85 | for (const Node::ChildMap::value_type& kv : node.children) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 86 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 87 | Node& child = nodes[kv.second]; |
| 88 | if (child.isSimpleNode()) |
| 89 | { |
| 90 | for (const Node::ChildMap::value_type& childKv : |
| 91 | child.children) |
| 92 | { |
| 93 | merged[kv.first + childKv.first] = childKv.second; |
| 94 | didMerge = true; |
| 95 | } |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | merged[kv.first] = kv.second; |
| 100 | } |
| 101 | } |
| 102 | node.children = std::move(merged); |
| 103 | if (!didMerge) |
| 104 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 105 | break; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 106 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 107 | } |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 108 | |
| 109 | for (const Node::ChildMap::value_type& kv : node.children) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 110 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 111 | optimizeNode(nodes[kv.second]); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 112 | } |
| 113 | } |
| 114 | |
| 115 | void optimize() |
| 116 | { |
| 117 | optimizeNode(head()); |
| 118 | } |
| 119 | |
| 120 | public: |
| 121 | void validate() |
| 122 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 123 | optimize(); |
| 124 | } |
| 125 | |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 126 | void findRouteIndexesHelper(std::string_view reqUrl, |
| 127 | std::vector<unsigned>& routeIndexes, |
| 128 | const Node& node) const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 129 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 130 | for (const Node::ChildMap::value_type& kv : node.children) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 131 | { |
| 132 | const std::string& fragment = kv.first; |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 133 | const Node& child = nodes[kv.second]; |
| 134 | if (reqUrl.empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 135 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 136 | if (child.ruleIndex != 0 && fragment != "/") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 137 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 138 | routeIndexes.push_back(child.ruleIndex); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 139 | } |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 140 | findRouteIndexesHelper(reqUrl, routeIndexes, child); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 141 | } |
| 142 | else |
| 143 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 144 | if (reqUrl.starts_with(fragment)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 145 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 146 | findRouteIndexesHelper(reqUrl.substr(fragment.size()), |
| 147 | routeIndexes, child); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 153 | void findRouteIndexes(const std::string& reqUrl, |
| 154 | std::vector<unsigned>& routeIndexes) const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 155 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 156 | findRouteIndexesHelper(reqUrl, routeIndexes, head()); |
| 157 | } |
| 158 | |
| 159 | struct FindResult |
| 160 | { |
| 161 | unsigned ruleIndex; |
| 162 | std::vector<std::string> params; |
| 163 | }; |
| 164 | |
| 165 | private: |
| 166 | FindResult findHelper(const std::string_view reqUrl, const Node& node, |
| 167 | std::vector<std::string>& params) const |
| 168 | { |
| 169 | if (reqUrl.empty()) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 170 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 171 | return {node.ruleIndex, params}; |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 172 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 173 | |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 174 | if (node.stringParamChild != 0U) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 175 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 176 | size_t epos = 0; |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 177 | for (; epos < reqUrl.size(); epos++) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 178 | { |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 179 | if (reqUrl[epos] == '/') |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 180 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 181 | break; |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 182 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 185 | if (epos != 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 186 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 187 | params.emplace_back(reqUrl.substr(0, epos)); |
| 188 | FindResult ret = findHelper( |
| 189 | reqUrl.substr(epos), nodes[node.stringParamChild], params); |
| 190 | if (ret.ruleIndex != 0U) |
| 191 | { |
| 192 | return {ret.ruleIndex, std::move(ret.params)}; |
| 193 | } |
| 194 | params.pop_back(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 198 | if (node.pathParamChild != 0U) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 199 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 200 | params.emplace_back(reqUrl); |
| 201 | FindResult ret = findHelper("", nodes[node.pathParamChild], params); |
| 202 | if (ret.ruleIndex != 0U) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 203 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 204 | return {ret.ruleIndex, std::move(ret.params)}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 205 | } |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 206 | params.pop_back(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 209 | for (const Node::ChildMap::value_type& kv : node.children) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 210 | { |
| 211 | const std::string& fragment = kv.first; |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 212 | const Node& child = nodes[kv.second]; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 213 | |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 214 | if (reqUrl.starts_with(fragment)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 215 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 216 | FindResult ret = findHelper(reqUrl.substr(fragment.size()), |
| 217 | child, params); |
| 218 | if (ret.ruleIndex != 0U) |
| 219 | { |
| 220 | return {ret.ruleIndex, std::move(ret.params)}; |
| 221 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 225 | return {0U, std::vector<std::string>()}; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 226 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 227 | |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 228 | public: |
| 229 | FindResult find(const std::string_view reqUrl) const |
| 230 | { |
| 231 | std::vector<std::string> start; |
| 232 | return findHelper(reqUrl, head(), start); |
| 233 | } |
| 234 | |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 235 | void add(std::string_view urlIn, unsigned ruleIndex) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 236 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 237 | size_t idx = 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 238 | |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 239 | std::string_view url = urlIn; |
| 240 | |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 241 | while (!url.empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 242 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 243 | char c = url[0]; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 244 | if (c == '<') |
| 245 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 246 | bool found = false; |
| 247 | for (const std::string_view str1 : |
| 248 | {"<str>", "<string>", "<path>"}) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 249 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 250 | if (!url.starts_with(str1)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 251 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 252 | continue; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 253 | } |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 254 | found = true; |
| 255 | Node& node = nodes[idx]; |
| 256 | size_t* param = &node.stringParamChild; |
| 257 | if (str1 == "<path>") |
| 258 | { |
| 259 | param = &node.pathParamChild; |
| 260 | } |
| 261 | if (*param == 0U) |
| 262 | { |
| 263 | *param = newNode(); |
| 264 | } |
| 265 | idx = *param; |
| 266 | |
| 267 | url.remove_prefix(str1.size()); |
| 268 | break; |
| 269 | } |
| 270 | if (found) |
| 271 | { |
| 272 | continue; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Manojkiran Eda | efff2b5 | 2024-06-18 18:01:46 +0530 | [diff] [blame] | 275 | BMCWEB_LOG_CRITICAL("Can't find tag for {}", urlIn); |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 276 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 277 | } |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 278 | std::string piece(&c, 1); |
| 279 | if (!nodes[idx].children.contains(piece)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 280 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 281 | unsigned newNodeIdx = newNode(); |
| 282 | nodes[idx].children.emplace(piece, newNodeIdx); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 283 | } |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 284 | idx = nodes[idx].children[piece]; |
| 285 | url.remove_prefix(1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 286 | } |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 287 | Node& node = nodes[idx]; |
| 288 | if (node.ruleIndex != 0U) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 289 | { |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 290 | BMCWEB_LOG_CRITICAL("handler already exists for \"{}\"", urlIn); |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 291 | throw std::runtime_error( |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 292 | std::format("handler already exists for \"{}\"", urlIn)); |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 293 | } |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 294 | node.ruleIndex = ruleIndex; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 295 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 296 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 297 | private: |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 298 | void debugNodePrint(Node& n, size_t level) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 299 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 300 | std::string spaces(level, ' '); |
| 301 | if (n.stringParamChild != 0U) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 302 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 303 | BMCWEB_LOG_DEBUG("{}<str>", spaces); |
| 304 | debugNodePrint(nodes[n.stringParamChild], level + 5); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 305 | } |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 306 | if (n.pathParamChild != 0U) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 307 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 308 | BMCWEB_LOG_DEBUG("{} <path>", spaces); |
| 309 | debugNodePrint(nodes[n.pathParamChild], level + 6); |
| 310 | } |
| 311 | for (const Node::ChildMap::value_type& kv : n.children) |
| 312 | { |
| 313 | BMCWEB_LOG_DEBUG("{}{}", spaces, kv.first); |
| 314 | debugNodePrint(nodes[kv.second], level + kv.first.size()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 315 | } |
| 316 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 317 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 318 | public: |
| 319 | void debugPrint() |
| 320 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 321 | debugNodePrint(head(), 0U); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 322 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 323 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 324 | private: |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 325 | const Node& head() const |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 326 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 327 | return nodes.front(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 328 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 329 | |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 330 | Node& head() |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 331 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 332 | return nodes.front(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | unsigned newNode() |
| 336 | { |
| 337 | nodes.resize(nodes.size() + 1); |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 338 | return static_cast<unsigned>(nodes.size() - 1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | std::vector<Node> nodes; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 342 | }; |
| 343 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 344 | class Router |
| 345 | { |
| 346 | public: |
Ed Tanous | 0c0084a | 2019-10-24 15:57:51 -0700 | [diff] [blame] | 347 | Router() = default; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 348 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 349 | DynamicRule& newRuleDynamic(const std::string& rule) |
| 350 | { |
| 351 | std::unique_ptr<DynamicRule> ruleObject = |
| 352 | std::make_unique<DynamicRule>(rule); |
| 353 | DynamicRule* ptr = ruleObject.get(); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 354 | allRules.emplace_back(std::move(ruleObject)); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 355 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 356 | return *ptr; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 359 | template <uint64_t NumArgs> |
Ed Tanous | cfe3bc0 | 2023-06-26 12:47:24 -0700 | [diff] [blame] | 360 | auto& newRuleTagged(const std::string& rule) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 361 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 362 | if constexpr (NumArgs == 0) |
Ed Tanous | cfe3bc0 | 2023-06-26 12:47:24 -0700 | [diff] [blame] | 363 | { |
| 364 | using RuleT = TaggedRule<>; |
| 365 | std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule); |
| 366 | RuleT* ptr = ruleObject.get(); |
| 367 | allRules.emplace_back(std::move(ruleObject)); |
| 368 | return *ptr; |
| 369 | } |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 370 | else if constexpr (NumArgs == 1) |
Ed Tanous | cfe3bc0 | 2023-06-26 12:47:24 -0700 | [diff] [blame] | 371 | { |
| 372 | using RuleT = TaggedRule<std::string>; |
| 373 | std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule); |
| 374 | RuleT* ptr = ruleObject.get(); |
| 375 | allRules.emplace_back(std::move(ruleObject)); |
| 376 | return *ptr; |
| 377 | } |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 378 | else if constexpr (NumArgs == 2) |
Ed Tanous | cfe3bc0 | 2023-06-26 12:47:24 -0700 | [diff] [blame] | 379 | { |
| 380 | using RuleT = TaggedRule<std::string, std::string>; |
| 381 | std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule); |
| 382 | RuleT* ptr = ruleObject.get(); |
| 383 | allRules.emplace_back(std::move(ruleObject)); |
| 384 | return *ptr; |
| 385 | } |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 386 | else if constexpr (NumArgs == 3) |
Ed Tanous | cfe3bc0 | 2023-06-26 12:47:24 -0700 | [diff] [blame] | 387 | { |
| 388 | using RuleT = TaggedRule<std::string, std::string, std::string>; |
| 389 | std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule); |
| 390 | RuleT* ptr = ruleObject.get(); |
| 391 | allRules.emplace_back(std::move(ruleObject)); |
| 392 | return *ptr; |
| 393 | } |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 394 | else if constexpr (NumArgs == 4) |
Ed Tanous | cfe3bc0 | 2023-06-26 12:47:24 -0700 | [diff] [blame] | 395 | { |
| 396 | using RuleT = |
| 397 | TaggedRule<std::string, std::string, std::string, std::string>; |
| 398 | std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule); |
| 399 | RuleT* ptr = ruleObject.get(); |
| 400 | allRules.emplace_back(std::move(ruleObject)); |
| 401 | return *ptr; |
| 402 | } |
| 403 | else |
| 404 | { |
| 405 | using RuleT = TaggedRule<std::string, std::string, std::string, |
| 406 | std::string, std::string>; |
| 407 | std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule); |
| 408 | RuleT* ptr = ruleObject.get(); |
| 409 | allRules.emplace_back(std::move(ruleObject)); |
| 410 | return *ptr; |
| 411 | } |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 412 | static_assert(NumArgs <= 5, "Max number of args supported is 5"); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 415 | struct PerMethod |
| 416 | { |
| 417 | std::vector<BaseRule*> rules; |
| 418 | Trie trie; |
| 419 | // rule index 0 has special meaning; preallocate it to avoid |
| 420 | // duplication. |
| 421 | PerMethod() : rules(1) {} |
| 422 | |
| 423 | void internalAdd(std::string_view rule, BaseRule* ruleObject) |
| 424 | { |
| 425 | rules.emplace_back(ruleObject); |
| 426 | trie.add(rule, static_cast<unsigned>(rules.size() - 1U)); |
| 427 | // directory case: |
| 428 | // request to `/about' url matches `/about/' rule |
| 429 | if (rule.size() > 2 && rule.back() == '/') |
| 430 | { |
| 431 | trie.add(rule.substr(0, rule.size() - 1), |
| 432 | static_cast<unsigned>(rules.size() - 1)); |
| 433 | } |
| 434 | } |
| 435 | }; |
| 436 | |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 437 | void internalAddRuleObject(const std::string& rule, BaseRule* ruleObject) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 438 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 439 | if (ruleObject == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 440 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 441 | return; |
| 442 | } |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 443 | for (size_t method = 0; method <= maxVerbIndex; method++) |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 444 | { |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 445 | size_t methodBit = 1 << method; |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 446 | if ((ruleObject->methodsBitfield & methodBit) > 0U) |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 447 | { |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 448 | perMethods[method].internalAdd(rule, ruleObject); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 449 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 450 | } |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 451 | |
| 452 | if (ruleObject->isNotFound) |
| 453 | { |
| 454 | notFoundRoutes.internalAdd(rule, ruleObject); |
| 455 | } |
| 456 | |
| 457 | if (ruleObject->isMethodNotAllowed) |
| 458 | { |
| 459 | methodNotAllowedRoutes.internalAdd(rule, ruleObject); |
| 460 | } |
| 461 | |
| 462 | if (ruleObject->isUpgrade) |
| 463 | { |
| 464 | upgradeRoutes.internalAdd(rule, ruleObject); |
| 465 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 466 | } |
| 467 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 468 | void validate() |
| 469 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 470 | for (std::unique_ptr<BaseRule>& rule : allRules) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 471 | { |
| 472 | if (rule) |
| 473 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 474 | std::unique_ptr<BaseRule> upgraded = rule->upgrade(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 475 | if (upgraded) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 476 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 477 | rule = std::move(upgraded); |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 478 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 479 | rule->validate(); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 480 | internalAddRuleObject(rule->rule, rule.get()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 481 | } |
| 482 | } |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 483 | for (PerMethod& perMethod : perMethods) |
| 484 | { |
| 485 | perMethod.trie.validate(); |
| 486 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 489 | struct FindRoute |
| 490 | { |
| 491 | BaseRule* rule = nullptr; |
Ed Tanous | 15a42df | 2023-02-09 18:08:23 -0800 | [diff] [blame] | 492 | std::vector<std::string> params; |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 493 | }; |
| 494 | |
| 495 | struct FindRouteResponse |
| 496 | { |
| 497 | std::string allowHeader; |
| 498 | FindRoute route; |
| 499 | }; |
| 500 | |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 501 | static FindRoute findRouteByPerMethod(std::string_view url, |
| 502 | const PerMethod& perMethod) |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 503 | { |
| 504 | FindRoute route; |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 505 | |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 506 | Trie::FindResult found = perMethod.trie.find(url); |
| 507 | if (found.ruleIndex >= perMethod.rules.size()) |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 508 | { |
| 509 | throw std::runtime_error("Trie internal structure corrupted!"); |
| 510 | } |
| 511 | // Found a 404 route, switch that in |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 512 | if (found.ruleIndex != 0U) |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 513 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 514 | route.rule = perMethod.rules[found.ruleIndex]; |
| 515 | route.params = std::move(found.params); |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 516 | } |
| 517 | return route; |
| 518 | } |
| 519 | |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 520 | FindRouteResponse findRoute(const Request& req) const |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 521 | { |
| 522 | FindRouteResponse findRoute; |
| 523 | |
Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 524 | std::optional<HttpVerb> verb = httpVerbFromBoost(req.method()); |
| 525 | if (!verb) |
| 526 | { |
| 527 | return findRoute; |
| 528 | } |
| 529 | size_t reqMethodIndex = static_cast<size_t>(*verb); |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 530 | // Check to see if this url exists at any verb |
| 531 | for (size_t perMethodIndex = 0; perMethodIndex <= maxVerbIndex; |
| 532 | perMethodIndex++) |
| 533 | { |
| 534 | // Make sure it's safe to deference the array at that index |
| 535 | static_assert(maxVerbIndex < |
| 536 | std::tuple_size_v<decltype(perMethods)>); |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 537 | FindRoute route = findRouteByPerMethod(req.url().encoded_path(), |
| 538 | perMethods[perMethodIndex]); |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 539 | if (route.rule == nullptr) |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 540 | { |
| 541 | continue; |
| 542 | } |
| 543 | if (!findRoute.allowHeader.empty()) |
| 544 | { |
| 545 | findRoute.allowHeader += ", "; |
| 546 | } |
Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 547 | HttpVerb thisVerb = static_cast<HttpVerb>(perMethodIndex); |
| 548 | findRoute.allowHeader += httpVerbToString(thisVerb); |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 549 | if (perMethodIndex == reqMethodIndex) |
| 550 | { |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 551 | findRoute.route = route; |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | return findRoute; |
| 555 | } |
| 556 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 557 | template <typename Adaptor> |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 558 | void handleUpgrade(const std::shared_ptr<Request>& req, |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame] | 559 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 560 | Adaptor&& adaptor) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 561 | { |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 562 | PerMethod& perMethod = upgradeRoutes; |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 563 | Trie& trie = perMethod.trie; |
| 564 | std::vector<BaseRule*>& rules = perMethod.rules; |
| 565 | |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 566 | Trie::FindResult found = trie.find(req->url().encoded_path()); |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 567 | unsigned ruleIndex = found.ruleIndex; |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 568 | if (ruleIndex == 0U) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 569 | { |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 570 | BMCWEB_LOG_DEBUG("Cannot match rules {}", |
| 571 | req->url().encoded_path()); |
P Dheeraj Srujan Kumar | a9f076e | 2021-10-18 22:45:37 +0530 | [diff] [blame] | 572 | asyncResp->res.result(boost::beast::http::status::not_found); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 573 | return; |
| 574 | } |
| 575 | |
| 576 | if (ruleIndex >= rules.size()) |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 577 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 578 | throw std::runtime_error("Trie internal structure corrupted!"); |
Ed Tanous | 3174e4d | 2020-10-07 11:41:22 -0700 | [diff] [blame] | 579 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 580 | |
P Dheeraj Srujan Kumar | 7e9093e | 2021-09-18 01:19:04 +0530 | [diff] [blame] | 581 | BaseRule& rule = *rules[ruleIndex]; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 582 | |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 583 | BMCWEB_LOG_DEBUG("Matched rule (upgrade) '{}'", rule.rule); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 584 | |
P Dheeraj Srujan Kumar | 7e9093e | 2021-09-18 01:19:04 +0530 | [diff] [blame] | 585 | // TODO(ed) This should be able to use std::bind_front, but it doesn't |
| 586 | // appear to work with the std::move on adaptor. |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 587 | validatePrivilege(req, asyncResp, rule, |
| 588 | [req, &rule, asyncResp, |
| 589 | adaptor = std::forward<Adaptor>(adaptor)]() mutable { |
| 590 | rule.handleUpgrade(*req, asyncResp, std::move(adaptor)); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 591 | }); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 592 | } |
| 593 | |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 594 | void handle(const std::shared_ptr<Request>& req, |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 595 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 596 | { |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 597 | std::optional<HttpVerb> verb = httpVerbFromBoost(req->method()); |
Ed Tanous | 2c9efc3 | 2022-07-31 22:08:26 -0700 | [diff] [blame] | 598 | if (!verb || static_cast<size_t>(*verb) >= perMethods.size()) |
Ed Tanous | 888880a | 2020-08-24 13:48:50 -0700 | [diff] [blame] | 599 | { |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 600 | asyncResp->res.result(boost::beast::http::status::not_found); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 601 | return; |
Ed Tanous | 888880a | 2020-08-24 13:48:50 -0700 | [diff] [blame] | 602 | } |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 603 | |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 604 | FindRouteResponse foundRoute = findRoute(*req); |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 605 | |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 606 | if (foundRoute.route.rule == nullptr) |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 607 | { |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 608 | // Couldn't find a normal route with any verb, try looking for a 404 |
| 609 | // route |
| 610 | if (foundRoute.allowHeader.empty()) |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 611 | { |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 612 | foundRoute.route = findRouteByPerMethod( |
| 613 | req->url().encoded_path(), notFoundRoutes); |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 614 | } |
| 615 | else |
| 616 | { |
| 617 | // See if we have a method not allowed (405) handler |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 618 | foundRoute.route = findRouteByPerMethod( |
| 619 | req->url().encoded_path(), methodNotAllowedRoutes); |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 620 | } |
| 621 | } |
Ed Tanous | 759cf10 | 2022-07-31 16:36:52 -0700 | [diff] [blame] | 622 | |
| 623 | // Fill in the allow header if it's valid |
| 624 | if (!foundRoute.allowHeader.empty()) |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 625 | { |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 626 | asyncResp->res.addHeader(boost::beast::http::field::allow, |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 627 | foundRoute.allowHeader); |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 628 | } |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 629 | |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 630 | // If we couldn't find a real route or a 404 route, return a generic |
| 631 | // response |
| 632 | if (foundRoute.route.rule == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 633 | { |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 634 | if (foundRoute.allowHeader.empty()) |
| 635 | { |
| 636 | asyncResp->res.result(boost::beast::http::status::not_found); |
| 637 | } |
| 638 | else |
Ed Tanous | 2634dcd | 2019-03-26 09:28:06 -0700 | [diff] [blame] | 639 | { |
Ed Tanous | 88a03c5 | 2022-03-14 10:16:07 -0700 | [diff] [blame] | 640 | asyncResp->res.result( |
| 641 | boost::beast::http::status::method_not_allowed); |
Ed Tanous | 2634dcd | 2019-03-26 09:28:06 -0700 | [diff] [blame] | 642 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 643 | return; |
| 644 | } |
| 645 | |
Ed Tanous | 44e4518 | 2022-07-26 16:47:23 -0700 | [diff] [blame] | 646 | BaseRule& rule = *foundRoute.route.rule; |
Ed Tanous | 15a42df | 2023-02-09 18:08:23 -0800 | [diff] [blame] | 647 | std::vector<std::string> params = std::move(foundRoute.route.params); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 648 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 649 | BMCWEB_LOG_DEBUG("Matched rule '{}' {} / {}", rule.rule, |
| 650 | static_cast<uint32_t>(*verb), rule.getMethods()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 651 | |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 652 | if (req->session == nullptr) |
James Feist | 7166bf0 | 2019-12-10 16:52:14 +0000 | [diff] [blame] | 653 | { |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 654 | rule.handle(*req, asyncResp, params); |
James Feist | 7166bf0 | 2019-12-10 16:52:14 +0000 | [diff] [blame] | 655 | return; |
| 656 | } |
Jonathan Doman | 102a4cd | 2024-04-15 16:56:23 -0700 | [diff] [blame] | 657 | validatePrivilege( |
| 658 | req, asyncResp, rule, |
| 659 | [req, asyncResp, &rule, params = std::move(params)]() { |
| 660 | rule.handle(*req, asyncResp, params); |
Jonathan Doman | d3c8ce6 | 2023-03-21 18:17:06 -0700 | [diff] [blame] | 661 | }); |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 662 | } |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 663 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 664 | void debugPrint() |
| 665 | { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 666 | for (size_t i = 0; i < perMethods.size(); i++) |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 667 | { |
Ed Tanous | d9e89df | 2024-03-27 14:08:59 -0700 | [diff] [blame] | 668 | BMCWEB_LOG_DEBUG("{}", httpVerbToString(static_cast<HttpVerb>(i))); |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 669 | perMethods[i].trie.debugPrint(); |
| 670 | } |
Ed Tanous | 3dac749 | 2017-08-02 13:46:20 -0700 | [diff] [blame] | 671 | } |
Ed Tanous | b4a7bfa | 2017-04-04 17:23:00 -0700 | [diff] [blame] | 672 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 673 | std::vector<const std::string*> getRoutes(const std::string& parent) |
| 674 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 675 | std::vector<const std::string*> ret; |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 676 | |
| 677 | for (const PerMethod& pm : perMethods) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 678 | { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 679 | std::vector<unsigned> x; |
| 680 | pm.trie.findRouteIndexes(parent, x); |
| 681 | for (unsigned index : x) |
| 682 | { |
| 683 | ret.push_back(&pm.rules[index]->rule); |
| 684 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 685 | } |
| 686 | return ret; |
| 687 | } |
| 688 | |
| 689 | private: |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 690 | std::array<PerMethod, static_cast<size_t>(HttpVerb::Max)> perMethods; |
Ed Tanous | 888880a | 2020-08-24 13:48:50 -0700 | [diff] [blame] | 691 | |
Ed Tanous | a3b9eb9 | 2024-06-03 08:39:37 -0700 | [diff] [blame] | 692 | PerMethod notFoundRoutes; |
| 693 | PerMethod upgradeRoutes; |
| 694 | PerMethod methodNotAllowedRoutes; |
| 695 | |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 696 | std::vector<std::unique_ptr<BaseRule>> allRules; |
Ed Tanous | 7045c8d | 2017-04-03 10:04:37 -0700 | [diff] [blame] | 697 | }; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 698 | } // namespace crow |