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