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