blob: 97daef1fcf5d88efb4db14be8c20c85119e2f8db [file] [log] [blame]
Ed Tanous7045c8d2017-04-03 10:04:37 -07001#pragma once
2
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08003#include "async_resp.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -07004#include "common.hpp"
Ed Tanous08bbe112023-04-06 13:10:02 -07005#include "dbus_privileges.hpp"
Ed Tanous168e20c2021-12-13 14:39:53 -08006#include "dbus_utility.hpp"
Joseph Reynolds3bf4e632020-02-06 14:44:32 -06007#include "error_messages.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -07008#include "http_request.hpp"
9#include "http_response.hpp"
10#include "logging.hpp"
Tanousf00032d2018-11-05 01:18:10 -030011#include "privileges.hpp"
Ed Tanous08bbe112023-04-06 13:10:02 -070012#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 Gupta6f359562019-04-03 10:39:08 +053017#include "sessions.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -070018#include "utility.hpp"
Ed Tanous3d183202023-03-10 09:21:58 -080019#include "utils/dbus_utils.hpp"
Ed Tanous2c9efc32022-07-31 22:08:26 -070020#include "verb.hpp"
Ed Tanous04e438c2020-10-03 08:06:26 -070021#include "websocket.hpp"
Ed Tanous1abe55e2018-09-05 08:30:59 -070022
Ed Tanous88a03c52022-03-14 10:16:07 -070023#include <boost/beast/ssl/ssl_stream.hpp>
Tanousf00032d2018-11-05 01:18:10 -030024#include <boost/container/flat_map.hpp>
Ed Tanousef4c65b2023-04-24 15:28:50 -070025#include <boost/url/format.hpp>
Ed Tanous3d183202023-03-10 09:21:58 -080026#include <sdbusplus/unpack_properties.hpp>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050027
Ed Tanouse0d918b2018-03-27 17:41:04 -070028#include <cerrno>
Ed Tanous7045c8d2017-04-03 10:04:37 -070029#include <cstdint>
Ed Tanouse0d918b2018-03-27 17:41:04 -070030#include <cstdlib>
Ed Tanous3dac7492017-08-02 13:46:20 -070031#include <limits>
Ed Tanous7045c8d2017-04-03 10:04:37 -070032#include <memory>
Ed Tanous2c9efc32022-07-31 22:08:26 -070033#include <optional>
Ed Tanous7045c8d2017-04-03 10:04:37 -070034#include <tuple>
Ed Tanous7045c8d2017-04-03 10:04:37 -070035#include <utility>
36#include <vector>
Ed Tanous9140a672017-04-24 17:01:32 -070037
Ed Tanous1abe55e2018-09-05 08:30:59 -070038namespace crow
39{
Tanousf00032d2018-11-05 01:18:10 -030040
Ed Tanous1abe55e2018-09-05 08:30:59 -070041class Trie
42{
43 public:
44 struct Node
45 {
46 unsigned ruleIndex{};
Ed Tanous271584a2019-07-09 16:24:22 -070047 std::array<size_t, static_cast<size_t>(ParamType::MAX)>
48 paramChildrens{};
Ed Tanousa94ac612022-02-22 11:13:24 -080049 using ChildMap = boost::container::flat_map<
50 std::string, unsigned, std::less<>,
51 std::vector<std::pair<std::string, unsigned>>>;
52 ChildMap children;
Ed Tanous7045c8d2017-04-03 10:04:37 -070053
Ed Tanous1abe55e2018-09-05 08:30:59 -070054 bool isSimpleNode() const
55 {
Ed Tanouse662eae2022-01-25 10:39:19 -080056 return ruleIndex == 0 &&
57 std::all_of(std::begin(paramChildrens),
58 std::end(paramChildrens),
59 [](size_t x) { return x == 0U; });
Ed Tanous7045c8d2017-04-03 10:04:37 -070060 }
Ed Tanous55c7b7a2018-05-22 15:27:24 -070061 };
Ed Tanous7045c8d2017-04-03 10:04:37 -070062
Patrick Williams89492a12023-05-10 07:51:34 -050063 Trie() : nodes(1) {}
Ed Tanous1abe55e2018-09-05 08:30:59 -070064
65 private:
66 void optimizeNode(Node* node)
67 {
Ed Tanous271584a2019-07-09 16:24:22 -070068 for (size_t x : node->paramChildrens)
Ed Tanous1abe55e2018-09-05 08:30:59 -070069 {
Ed Tanousdbb59d42022-01-25 11:09:55 -080070 if (x == 0U)
Ed Tanous3174e4d2020-10-07 11:41:22 -070071 {
Ed Tanous1abe55e2018-09-05 08:30:59 -070072 continue;
Ed Tanous3174e4d2020-10-07 11:41:22 -070073 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070074 Node* child = &nodes[x];
75 optimizeNode(child);
Ed Tanous7045c8d2017-04-03 10:04:37 -070076 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070077 if (node->children.empty())
Ed Tanous3174e4d2020-10-07 11:41:22 -070078 {
Ed Tanous1abe55e2018-09-05 08:30:59 -070079 return;
Ed Tanous3174e4d2020-10-07 11:41:22 -070080 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070081 bool mergeWithChild = true;
Ed Tanousa94ac612022-02-22 11:13:24 -080082 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -070083 {
84 Node* child = &nodes[kv.second];
85 if (!child->isSimpleNode())
86 {
87 mergeWithChild = false;
88 break;
Ed Tanous7045c8d2017-04-03 10:04:37 -070089 }
Ed Tanous1abe55e2018-09-05 08:30:59 -070090 }
91 if (mergeWithChild)
92 {
Ed Tanousa94ac612022-02-22 11:13:24 -080093 Node::ChildMap merged;
94 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -070095 {
96 Node* child = &nodes[kv.second];
Ed Tanousa94ac612022-02-22 11:13:24 -080097 for (const Node::ChildMap::value_type& childKv :
Tanousf00032d2018-11-05 01:18:10 -030098 child->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -070099 {
100 merged[kv.first + childKv.first] = childKv.second;
101 }
102 }
103 node->children = std::move(merged);
104 optimizeNode(node);
105 }
106 else
107 {
Ed Tanousa94ac612022-02-22 11:13:24 -0800108 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700109 {
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 Tanous1abe55e2018-09-05 08:30:59 -0700124 optimize();
125 }
126
Ed Tanous81ce6092020-12-17 16:54:55 +0000127 void findRouteIndexes(const std::string& reqUrl,
128 std::vector<unsigned>& routeIndexes,
Tanousf00032d2018-11-05 01:18:10 -0300129 const Node* node = nullptr, unsigned pos = 0) const
Ed Tanous1abe55e2018-09-05 08:30:59 -0700130 {
131 if (node == nullptr)
132 {
133 node = head();
134 }
Ed Tanousa94ac612022-02-22 11:13:24 -0800135 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700136 {
137 const std::string& fragment = kv.first;
138 const Node* child = &nodes[kv.second];
Ed Tanous81ce6092020-12-17 16:54:55 +0000139 if (pos >= reqUrl.size())
Ed Tanous1abe55e2018-09-05 08:30:59 -0700140 {
141 if (child->ruleIndex != 0 && fragment != "/")
142 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000143 routeIndexes.push_back(child->ruleIndex);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700144 }
Ed Tanous81ce6092020-12-17 16:54:55 +0000145 findRouteIndexes(reqUrl, routeIndexes, child,
Ed Tanous271584a2019-07-09 16:24:22 -0700146 static_cast<unsigned>(pos + fragment.size()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700147 }
148 else
149 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000150 if (reqUrl.compare(pos, fragment.size(), fragment) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700151 {
Ed Tanous271584a2019-07-09 16:24:22 -0700152 findRouteIndexes(
Ed Tanous81ce6092020-12-17 16:54:55 +0000153 reqUrl, routeIndexes, child,
Ed Tanous271584a2019-07-09 16:24:22 -0700154 static_cast<unsigned>(pos + fragment.size()));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700155 }
156 }
157 }
158 }
159
Ed Tanous15a42df2023-02-09 18:08:23 -0800160 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 Tanous1abe55e2018-09-05 08:30:59 -0700163 {
Ed Tanous15a42df2023-02-09 18:08:23 -0800164 std::vector<std::string> empty;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700165 if (params == nullptr)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700166 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700167 params = &empty;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700168 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700169
170 unsigned found{};
Ed Tanous15a42df2023-02-09 18:08:23 -0800171 std::vector<std::string> matchParams;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700172
173 if (node == nullptr)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700174 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700175 node = head();
Ed Tanous3174e4d2020-10-07 11:41:22 -0700176 }
Ed Tanous81ce6092020-12-17 16:54:55 +0000177 if (pos == reqUrl.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -0700178 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700179 return {node->ruleIndex, *params};
Ed Tanous3174e4d2020-10-07 11:41:22 -0700180 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700181
182 auto updateFound =
Ed Tanous15a42df2023-02-09 18:08:23 -0800183 [&found,
184 &matchParams](std::pair<unsigned, std::vector<std::string>>& ret) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700185 if (ret.first != 0U && (found == 0U || found > ret.first))
186 {
187 found = ret.first;
188 matchParams = std::move(ret.second);
189 }
190 };
Ed Tanous1abe55e2018-09-05 08:30:59 -0700191
Ed Tanouse662eae2022-01-25 10:39:19 -0800192 if (node->paramChildrens[static_cast<size_t>(ParamType::STRING)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700193 {
Ed Tanousb01bf292019-03-25 19:25:26 +0000194 size_t epos = pos;
Ed Tanous81ce6092020-12-17 16:54:55 +0000195 for (; epos < reqUrl.size(); epos++)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700196 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000197 if (reqUrl[epos] == '/')
Ed Tanous3174e4d2020-10-07 11:41:22 -0700198 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700199 break;
Ed Tanous3174e4d2020-10-07 11:41:22 -0700200 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700201 }
202
203 if (epos != pos)
204 {
Ed Tanous15a42df2023-02-09 18:08:23 -0800205 params->emplace_back(reqUrl.substr(pos, epos - pos));
206 std::pair<unsigned, std::vector<std::string>> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000207 find(reqUrl,
Ed Tanous271584a2019-07-09 16:24:22 -0700208 &nodes[node->paramChildrens[static_cast<size_t>(
209 ParamType::STRING)]],
Ed Tanousb01bf292019-03-25 19:25:26 +0000210 epos, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700211 updateFound(ret);
Ed Tanous15a42df2023-02-09 18:08:23 -0800212 params->pop_back();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700213 }
214 }
215
Ed Tanouse662eae2022-01-25 10:39:19 -0800216 if (node->paramChildrens[static_cast<size_t>(ParamType::PATH)] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700217 {
Ed Tanous81ce6092020-12-17 16:54:55 +0000218 size_t epos = reqUrl.size();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700219
220 if (epos != pos)
221 {
Ed Tanous15a42df2023-02-09 18:08:23 -0800222 params->emplace_back(reqUrl.substr(pos, epos - pos));
223 std::pair<unsigned, std::vector<std::string>> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000224 find(reqUrl,
Ed Tanous271584a2019-07-09 16:24:22 -0700225 &nodes[node->paramChildrens[static_cast<size_t>(
226 ParamType::PATH)]],
227 epos, params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700228 updateFound(ret);
Ed Tanous15a42df2023-02-09 18:08:23 -0800229 params->pop_back();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700230 }
231 }
232
Ed Tanousa94ac612022-02-22 11:13:24 -0800233 for (const Node::ChildMap::value_type& kv : node->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700234 {
235 const std::string& fragment = kv.first;
236 const Node* child = &nodes[kv.second];
237
Ed Tanous81ce6092020-12-17 16:54:55 +0000238 if (reqUrl.compare(pos, fragment.size(), fragment) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700239 {
Ed Tanous15a42df2023-02-09 18:08:23 -0800240 std::pair<unsigned, std::vector<std::string>> ret =
Ed Tanous81ce6092020-12-17 16:54:55 +0000241 find(reqUrl, child, pos + fragment.size(), params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700242 updateFound(ret);
243 }
244 }
245
246 return {found, matchParams};
Ed Tanous7045c8d2017-04-03 10:04:37 -0700247 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700248
249 void add(const std::string& url, unsigned ruleIndex)
250 {
Ed Tanous271584a2019-07-09 16:24:22 -0700251 size_t idx = 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700252
253 for (unsigned i = 0; i < url.size(); i++)
254 {
255 char c = url[i];
256 if (c == '<')
257 {
Ed Tanous15a42df2023-02-09 18:08:23 -0800258 constexpr static std::array<
259 std::pair<ParamType, std::string_view>, 3>
Tanousf00032d2018-11-05 01:18:10 -0300260 paramTraits = {{
Tanousf00032d2018-11-05 01:18:10 -0300261 {ParamType::STRING, "<str>"},
262 {ParamType::STRING, "<string>"},
263 {ParamType::PATH, "<path>"},
264 }};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700265
Ed Tanous15a42df2023-02-09 18:08:23 -0800266 for (const std::pair<ParamType, std::string_view>& x :
267 paramTraits)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700268 {
Tanousf00032d2018-11-05 01:18:10 -0300269 if (url.compare(i, x.second.size(), x.second) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700270 {
Ed Tanous271584a2019-07-09 16:24:22 -0700271 size_t index = static_cast<size_t>(x.first);
Ed Tanouse662eae2022-01-25 10:39:19 -0800272 if (nodes[idx].paramChildrens[index] == 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700273 {
Tanousf00032d2018-11-05 01:18:10 -0300274 unsigned newNodeIdx = newNode();
Ed Tanous271584a2019-07-09 16:24:22 -0700275 nodes[idx].paramChildrens[index] = newNodeIdx;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700276 }
Ed Tanous271584a2019-07-09 16:24:22 -0700277 idx = nodes[idx].paramChildrens[index];
278 i += static_cast<unsigned>(x.second.size());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700279 break;
280 }
281 }
282
283 i--;
284 }
285 else
286 {
287 std::string piece(&c, 1);
Ed Tanouse662eae2022-01-25 10:39:19 -0800288 if (nodes[idx].children.count(piece) == 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700289 {
Tanousf00032d2018-11-05 01:18:10 -0300290 unsigned newNodeIdx = newNode();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700291 nodes[idx].children.emplace(piece, newNodeIdx);
292 }
293 idx = nodes[idx].children[piece];
294 }
295 }
Ed Tanouse662eae2022-01-25 10:39:19 -0800296 if (nodes[idx].ruleIndex != 0U)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700297 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700298 throw std::runtime_error("handler already exists for " + url);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700299 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700300 nodes[idx].ruleIndex = ruleIndex;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700301 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700302
Ed Tanous1abe55e2018-09-05 08:30:59 -0700303 private:
Ed Tanous271584a2019-07-09 16:24:22 -0700304 void debugNodePrint(Node* n, size_t level)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700305 {
Ed Tanous271584a2019-07-09 16:24:22 -0700306 for (size_t i = 0; i < static_cast<size_t>(ParamType::MAX); i++)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700307 {
Ed Tanouse662eae2022-01-25 10:39:19 -0800308 if (n->paramChildrens[i] != 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700309 {
310 BMCWEB_LOG_DEBUG << std::string(
Ed Tanous271584a2019-07-09 16:24:22 -0700311 2U * level, ' ') /*<< "("<<n->paramChildrens[i]<<") "*/;
312 switch (static_cast<ParamType>(i))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700313 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700314 case ParamType::STRING:
315 BMCWEB_LOG_DEBUG << "<str>";
316 break;
317 case ParamType::PATH:
318 BMCWEB_LOG_DEBUG << "<path>";
319 break;
Ed Tanous23a21a12020-07-25 04:45:05 +0000320 case ParamType::MAX:
Ed Tanous1abe55e2018-09-05 08:30:59 -0700321 BMCWEB_LOG_DEBUG << "<ERROR>";
322 break;
323 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700324
Ed Tanous1abe55e2018-09-05 08:30:59 -0700325 debugNodePrint(&nodes[n->paramChildrens[i]], level + 1);
326 }
327 }
Ed Tanousa94ac612022-02-22 11:13:24 -0800328 for (const Node::ChildMap::value_type& kv : n->children)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700329 {
330 BMCWEB_LOG_DEBUG
Ed Tanous271584a2019-07-09 16:24:22 -0700331 << std::string(2U * level, ' ') /*<< "(" << kv.second << ") "*/
Ed Tanous1abe55e2018-09-05 08:30:59 -0700332 << kv.first;
333 debugNodePrint(&nodes[kv.second], level + 1);
334 }
335 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700336
Ed Tanous1abe55e2018-09-05 08:30:59 -0700337 public:
338 void debugPrint()
339 {
Ed Tanous271584a2019-07-09 16:24:22 -0700340 debugNodePrint(head(), 0U);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700341 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700342
Ed Tanous1abe55e2018-09-05 08:30:59 -0700343 private:
344 const Node* head() const
345 {
346 return &nodes.front();
347 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700348
Ed Tanous1abe55e2018-09-05 08:30:59 -0700349 Node* head()
350 {
351 return &nodes.front();
352 }
353
354 unsigned newNode()
355 {
356 nodes.resize(nodes.size() + 1);
Ed Tanous271584a2019-07-09 16:24:22 -0700357 return static_cast<unsigned>(nodes.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700358 }
359
360 std::vector<Node> nodes;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700361};
362
Ed Tanous1abe55e2018-09-05 08:30:59 -0700363class Router
364{
365 public:
Ed Tanous0c0084a2019-10-24 15:57:51 -0700366 Router() = default;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700367
Ed Tanous1abe55e2018-09-05 08:30:59 -0700368 DynamicRule& newRuleDynamic(const std::string& rule)
369 {
370 std::unique_ptr<DynamicRule> ruleObject =
371 std::make_unique<DynamicRule>(rule);
372 DynamicRule* ptr = ruleObject.get();
Tanousf00032d2018-11-05 01:18:10 -0300373 allRules.emplace_back(std::move(ruleObject));
Ed Tanous7045c8d2017-04-03 10:04:37 -0700374
Ed Tanous1abe55e2018-09-05 08:30:59 -0700375 return *ptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700376 }
377
Ed Tanous1abe55e2018-09-05 08:30:59 -0700378 template <uint64_t N>
379 typename black_magic::Arguments<N>::type::template rebind<TaggedRule>&
380 newRuleTagged(const std::string& rule)
381 {
382 using RuleT = typename black_magic::Arguments<N>::type::template rebind<
383 TaggedRule>;
384 std::unique_ptr<RuleT> ruleObject = std::make_unique<RuleT>(rule);
385 RuleT* ptr = ruleObject.get();
Tanousf00032d2018-11-05 01:18:10 -0300386 allRules.emplace_back(std::move(ruleObject));
Ed Tanous1abe55e2018-09-05 08:30:59 -0700387
388 return *ptr;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700389 }
390
Tanousf00032d2018-11-05 01:18:10 -0300391 void internalAddRuleObject(const std::string& rule, BaseRule* ruleObject)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700392 {
Tanousf00032d2018-11-05 01:18:10 -0300393 if (ruleObject == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700394 {
Tanousf00032d2018-11-05 01:18:10 -0300395 return;
396 }
Ed Tanous759cf102022-07-31 16:36:52 -0700397 for (size_t method = 0, methodBit = 1; method <= methodNotAllowedIndex;
Ed Tanous2c70f802020-09-28 14:29:23 -0700398 method++, methodBit <<= 1)
Tanousf00032d2018-11-05 01:18:10 -0300399 {
Ed Tanouse662eae2022-01-25 10:39:19 -0800400 if ((ruleObject->methodsBitfield & methodBit) > 0U)
Tanousf00032d2018-11-05 01:18:10 -0300401 {
402 perMethods[method].rules.emplace_back(ruleObject);
403 perMethods[method].trie.add(
Ed Tanous271584a2019-07-09 16:24:22 -0700404 rule, static_cast<unsigned>(
405 perMethods[method].rules.size() - 1U));
Tanousf00032d2018-11-05 01:18:10 -0300406 // directory case:
407 // request to `/about' url matches `/about/' rule
408 if (rule.size() > 2 && rule.back() == '/')
409 {
410 perMethods[method].trie.add(
411 rule.substr(0, rule.size() - 1),
Ed Tanous271584a2019-07-09 16:24:22 -0700412 static_cast<unsigned>(perMethods[method].rules.size() -
413 1));
Tanousf00032d2018-11-05 01:18:10 -0300414 }
415 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700416 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700417 }
418
Ed Tanous1abe55e2018-09-05 08:30:59 -0700419 void validate()
420 {
Tanousf00032d2018-11-05 01:18:10 -0300421 for (std::unique_ptr<BaseRule>& rule : allRules)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700422 {
423 if (rule)
424 {
Tanousf00032d2018-11-05 01:18:10 -0300425 std::unique_ptr<BaseRule> upgraded = rule->upgrade();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700426 if (upgraded)
Ed Tanous3174e4d2020-10-07 11:41:22 -0700427 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700428 rule = std::move(upgraded);
Ed Tanous3174e4d2020-10-07 11:41:22 -0700429 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700430 rule->validate();
Tanousf00032d2018-11-05 01:18:10 -0300431 internalAddRuleObject(rule->rule, rule.get());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700432 }
433 }
Tanousf00032d2018-11-05 01:18:10 -0300434 for (PerMethod& perMethod : perMethods)
435 {
436 perMethod.trie.validate();
437 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700438 }
439
Ed Tanous44e45182022-07-26 16:47:23 -0700440 struct FindRoute
441 {
442 BaseRule* rule = nullptr;
Ed Tanous15a42df2023-02-09 18:08:23 -0800443 std::vector<std::string> params;
Ed Tanous44e45182022-07-26 16:47:23 -0700444 };
445
446 struct FindRouteResponse
447 {
448 std::string allowHeader;
449 FindRoute route;
450 };
451
Ed Tanous759cf102022-07-31 16:36:52 -0700452 FindRoute findRouteByIndex(std::string_view url, size_t index) const
453 {
454 FindRoute route;
455 if (index >= perMethods.size())
456 {
457 BMCWEB_LOG_CRITICAL << "Bad index???";
458 return route;
459 }
460 const PerMethod& perMethod = perMethods[index];
Ed Tanous15a42df2023-02-09 18:08:23 -0800461 std::pair<unsigned, std::vector<std::string>> found =
462 perMethod.trie.find(url);
Ed Tanous759cf102022-07-31 16:36:52 -0700463 if (found.first >= perMethod.rules.size())
464 {
465 throw std::runtime_error("Trie internal structure corrupted!");
466 }
467 // Found a 404 route, switch that in
468 if (found.first != 0U)
469 {
470 route.rule = perMethod.rules[found.first];
471 route.params = std::move(found.second);
472 }
473 return route;
474 }
475
476 FindRouteResponse findRoute(Request& req) const
Ed Tanous44e45182022-07-26 16:47:23 -0700477 {
478 FindRouteResponse findRoute;
479
Ed Tanous2c9efc32022-07-31 22:08:26 -0700480 std::optional<HttpVerb> verb = httpVerbFromBoost(req.method());
481 if (!verb)
482 {
483 return findRoute;
484 }
485 size_t reqMethodIndex = static_cast<size_t>(*verb);
Ed Tanous44e45182022-07-26 16:47:23 -0700486 // Check to see if this url exists at any verb
487 for (size_t perMethodIndex = 0; perMethodIndex <= maxVerbIndex;
488 perMethodIndex++)
489 {
490 // Make sure it's safe to deference the array at that index
491 static_assert(maxVerbIndex <
492 std::tuple_size_v<decltype(perMethods)>);
Patrick Williams89492a12023-05-10 07:51:34 -0500493 FindRoute route = findRouteByIndex(req.url().encoded_path(),
494 perMethodIndex);
Ed Tanous759cf102022-07-31 16:36:52 -0700495 if (route.rule == nullptr)
Ed Tanous44e45182022-07-26 16:47:23 -0700496 {
497 continue;
498 }
499 if (!findRoute.allowHeader.empty())
500 {
501 findRoute.allowHeader += ", ";
502 }
Ed Tanous2c9efc32022-07-31 22:08:26 -0700503 HttpVerb thisVerb = static_cast<HttpVerb>(perMethodIndex);
504 findRoute.allowHeader += httpVerbToString(thisVerb);
Ed Tanous44e45182022-07-26 16:47:23 -0700505 if (perMethodIndex == reqMethodIndex)
506 {
Ed Tanous759cf102022-07-31 16:36:52 -0700507 findRoute.route = route;
Ed Tanous44e45182022-07-26 16:47:23 -0700508 }
509 }
510 return findRoute;
511 }
512
Ed Tanous1abe55e2018-09-05 08:30:59 -0700513 template <typename Adaptor>
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +0530514 void handleUpgrade(Request& req,
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +0530515 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
516 Adaptor&& adaptor)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700517 {
Ed Tanous2c9efc32022-07-31 22:08:26 -0700518 std::optional<HttpVerb> verb = httpVerbFromBoost(req.method());
519 if (!verb || static_cast<size_t>(*verb) >= perMethods.size())
Ed Tanous888880a2020-08-24 13:48:50 -0700520 {
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +0530521 asyncResp->res.result(boost::beast::http::status::not_found);
Tanousf00032d2018-11-05 01:18:10 -0300522 return;
Ed Tanous888880a2020-08-24 13:48:50 -0700523 }
Ed Tanous2c9efc32022-07-31 22:08:26 -0700524 PerMethod& perMethod = perMethods[static_cast<size_t>(*verb)];
Tanousf00032d2018-11-05 01:18:10 -0300525 Trie& trie = perMethod.trie;
526 std::vector<BaseRule*>& rules = perMethod.rules;
527
Ed Tanous15a42df2023-02-09 18:08:23 -0800528 const std::pair<unsigned, std::vector<std::string>>& found =
Ed Tanous39662a32023-02-06 15:09:46 -0800529 trie.find(req.url().encoded_path());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700530 unsigned ruleIndex = found.first;
Ed Tanouse662eae2022-01-25 10:39:19 -0800531 if (ruleIndex == 0U)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700532 {
Ed Tanous39662a32023-02-06 15:09:46 -0800533 BMCWEB_LOG_DEBUG << "Cannot match rules "
534 << req.url().encoded_path();
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +0530535 asyncResp->res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700536 return;
537 }
538
539 if (ruleIndex >= rules.size())
Ed Tanous3174e4d2020-10-07 11:41:22 -0700540 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700541 throw std::runtime_error("Trie internal structure corrupted!");
Ed Tanous3174e4d2020-10-07 11:41:22 -0700542 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700543
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +0530544 BaseRule& rule = *rules[ruleIndex];
545 size_t methods = rule.getMethods();
546 if ((methods & (1U << static_cast<size_t>(*verb))) == 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700547 {
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +0530548 BMCWEB_LOG_DEBUG
549 << "Rule found but method mismatch: "
550 << req.url().encoded_path() << " with " << req.methodString()
551 << "(" << static_cast<uint32_t>(*verb) << ") / " << methods;
P Dheeraj Srujan Kumara9f076e2021-10-18 22:45:37 +0530552 asyncResp->res.result(boost::beast::http::status::not_found);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700553 return;
554 }
555
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +0530556 BMCWEB_LOG_DEBUG << "Matched rule (upgrade) '" << rule.rule << "' "
557 << static_cast<uint32_t>(*verb) << " / " << methods;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700558
P Dheeraj Srujan Kumar7e9093e2021-09-18 01:19:04 +0530559 // TODO(ed) This should be able to use std::bind_front, but it doesn't
560 // appear to work with the std::move on adaptor.
Ed Tanous915d2d42023-03-15 13:09:34 -0700561 validatePrivilege(
Jonathan Domand3c8ce62023-03-21 18:17:06 -0700562 req, asyncResp, rule,
Ed Tanous915d2d42023-03-15 13:09:34 -0700563 [&rule, asyncResp, adaptor(std::forward<Adaptor>(adaptor))](
Jonathan Domand3c8ce62023-03-21 18:17:06 -0700564 Request& thisReq) mutable {
Ed Tanous915d2d42023-03-15 13:09:34 -0700565 rule.handleUpgrade(thisReq, asyncResp, std::move(adaptor));
566 });
Ed Tanous7045c8d2017-04-03 10:04:37 -0700567 }
568
zhanghch058d1b46d2021-04-01 11:18:24 +0800569 void handle(Request& req,
570 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700571 {
Ed Tanous2c9efc32022-07-31 22:08:26 -0700572 std::optional<HttpVerb> verb = httpVerbFromBoost(req.method());
573 if (!verb || static_cast<size_t>(*verb) >= perMethods.size())
Ed Tanous888880a2020-08-24 13:48:50 -0700574 {
zhanghch058d1b46d2021-04-01 11:18:24 +0800575 asyncResp->res.result(boost::beast::http::status::not_found);
Tanousf00032d2018-11-05 01:18:10 -0300576 return;
Ed Tanous888880a2020-08-24 13:48:50 -0700577 }
Ed Tanous44e45182022-07-26 16:47:23 -0700578
579 FindRouteResponse foundRoute = findRoute(req);
580
Ed Tanous759cf102022-07-31 16:36:52 -0700581 if (foundRoute.route.rule == nullptr)
Ed Tanous88a03c52022-03-14 10:16:07 -0700582 {
Ed Tanous759cf102022-07-31 16:36:52 -0700583 // Couldn't find a normal route with any verb, try looking for a 404
584 // route
585 if (foundRoute.allowHeader.empty())
Ed Tanous44e45182022-07-26 16:47:23 -0700586 {
Patrick Williams89492a12023-05-10 07:51:34 -0500587 foundRoute.route = findRouteByIndex(req.url().encoded_path(),
588 notFoundIndex);
Ed Tanous759cf102022-07-31 16:36:52 -0700589 }
590 else
591 {
592 // See if we have a method not allowed (405) handler
Ed Tanous39662a32023-02-06 15:09:46 -0800593 foundRoute.route = findRouteByIndex(req.url().encoded_path(),
594 methodNotAllowedIndex);
Ed Tanous44e45182022-07-26 16:47:23 -0700595 }
596 }
Ed Tanous759cf102022-07-31 16:36:52 -0700597
598 // Fill in the allow header if it's valid
599 if (!foundRoute.allowHeader.empty())
Ed Tanous44e45182022-07-26 16:47:23 -0700600 {
Ed Tanous88a03c52022-03-14 10:16:07 -0700601 asyncResp->res.addHeader(boost::beast::http::field::allow,
Ed Tanous44e45182022-07-26 16:47:23 -0700602 foundRoute.allowHeader);
Ed Tanous88a03c52022-03-14 10:16:07 -0700603 }
Tanousf00032d2018-11-05 01:18:10 -0300604
Ed Tanous44e45182022-07-26 16:47:23 -0700605 // If we couldn't find a real route or a 404 route, return a generic
606 // response
607 if (foundRoute.route.rule == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700608 {
Ed Tanous44e45182022-07-26 16:47:23 -0700609 if (foundRoute.allowHeader.empty())
610 {
611 asyncResp->res.result(boost::beast::http::status::not_found);
612 }
613 else
Ed Tanous2634dcd2019-03-26 09:28:06 -0700614 {
Ed Tanous88a03c52022-03-14 10:16:07 -0700615 asyncResp->res.result(
616 boost::beast::http::status::method_not_allowed);
Ed Tanous2634dcd2019-03-26 09:28:06 -0700617 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700618 return;
619 }
620
Ed Tanous44e45182022-07-26 16:47:23 -0700621 BaseRule& rule = *foundRoute.route.rule;
Ed Tanous15a42df2023-02-09 18:08:23 -0800622 std::vector<std::string> params = std::move(foundRoute.route.params);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700623
Ed Tanous44e45182022-07-26 16:47:23 -0700624 BMCWEB_LOG_DEBUG << "Matched rule '" << rule.rule << "' "
Snehalatha Venkatesh1c99da02022-12-27 06:45:35 +0000625 << static_cast<uint32_t>(*verb) << " / "
Ed Tanous44e45182022-07-26 16:47:23 -0700626 << rule.getMethods();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700627
RAJESWARAN THILLAIGOVINDAN61dbeef2019-12-13 04:26:54 -0600628 if (req.session == nullptr)
James Feist7166bf02019-12-10 16:52:14 +0000629 {
Ed Tanous44e45182022-07-26 16:47:23 -0700630 rule.handle(req, asyncResp, params);
James Feist7166bf02019-12-10 16:52:14 +0000631 return;
632 }
Jonathan Domand3c8ce62023-03-21 18:17:06 -0700633 validatePrivilege(req, asyncResp, rule,
634 [&rule, asyncResp, params](Request& thisReq) mutable {
Ed Tanous915d2d42023-03-15 13:09:34 -0700635 rule.handle(thisReq, asyncResp, params);
Jonathan Domand3c8ce62023-03-21 18:17:06 -0700636 });
Ed Tanous7045c8d2017-04-03 10:04:37 -0700637 }
Ed Tanous7045c8d2017-04-03 10:04:37 -0700638
Ed Tanous1abe55e2018-09-05 08:30:59 -0700639 void debugPrint()
640 {
Ed Tanous271584a2019-07-09 16:24:22 -0700641 for (size_t i = 0; i < perMethods.size(); i++)
Tanousf00032d2018-11-05 01:18:10 -0300642 {
Ed Tanous23a21a12020-07-25 04:45:05 +0000643 BMCWEB_LOG_DEBUG << boost::beast::http::to_string(
644 static_cast<boost::beast::http::verb>(i));
Tanousf00032d2018-11-05 01:18:10 -0300645 perMethods[i].trie.debugPrint();
646 }
Ed Tanous3dac7492017-08-02 13:46:20 -0700647 }
Ed Tanousb4a7bfa2017-04-04 17:23:00 -0700648
Ed Tanous1abe55e2018-09-05 08:30:59 -0700649 std::vector<const std::string*> getRoutes(const std::string& parent)
650 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700651 std::vector<const std::string*> ret;
Tanousf00032d2018-11-05 01:18:10 -0300652
653 for (const PerMethod& pm : perMethods)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700654 {
Tanousf00032d2018-11-05 01:18:10 -0300655 std::vector<unsigned> x;
656 pm.trie.findRouteIndexes(parent, x);
657 for (unsigned index : x)
658 {
659 ret.push_back(&pm.rules[index]->rule);
660 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700661 }
662 return ret;
663 }
664
665 private:
Tanousf00032d2018-11-05 01:18:10 -0300666 struct PerMethod
667 {
668 std::vector<BaseRule*> rules;
669 Trie trie;
Ed Tanous313a3c22022-03-14 09:27:38 -0700670 // rule index 0 has special meaning; preallocate it to avoid
Tanousf00032d2018-11-05 01:18:10 -0300671 // duplication.
Patrick Williams89492a12023-05-10 07:51:34 -0500672 PerMethod() : rules(1) {}
Tanousf00032d2018-11-05 01:18:10 -0300673 };
Ed Tanous888880a2020-08-24 13:48:50 -0700674
Ed Tanous759cf102022-07-31 16:36:52 -0700675 std::array<PerMethod, methodNotAllowedIndex + 1> perMethods;
Tanousf00032d2018-11-05 01:18:10 -0300676 std::vector<std::unique_ptr<BaseRule>> allRules;
Ed Tanous7045c8d2017-04-03 10:04:37 -0700677};
Ed Tanous1abe55e2018-09-05 08:30:59 -0700678} // namespace crow