1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright OpenBMC Authors
#pragma once
#include "logging.hpp"
#include "routing/trie.hpp"
#include <cstdlib>
#include <format>
#include <stdexcept>
#include <string>
#include <string_view>
#include <vector>
namespace crow
{
struct SubRouteNode : public crow::Node
{
using ChildMap = crow::Node::ChildMap;
ChildMap fragmentChildren;
bool isSimpleNode() const
{
return crow::Node::isSimpleNode() && fragmentChildren.empty();
}
};
template <typename ContainedType>
class SubRouteTrie : public crow::Trie<ContainedType>
{
public:
struct FindResult
{
std::vector<std::string> params;
std::vector<unsigned> fragmentRuleIndexes;
};
private:
FindResult findHelper(const std::string_view reqUrl,
const ContainedType& node,
std::vector<std::string>& params) const
{
if (reqUrl.empty())
{
FindResult result = {params, {}};
for (const auto& [fragment, fragmentRuleIndex] :
node.fragmentChildren)
{
result.fragmentRuleIndexes.push_back(fragmentRuleIndex);
}
return result;
}
if (node.stringParamChild != 0U)
{
size_t epos = reqUrl.find('/');
if (epos == std::string_view::npos)
{
params.emplace_back(reqUrl);
FindResult ret =
findHelper("", this->nodes[node.stringParamChild], params);
if (!ret.fragmentRuleIndexes.empty())
{
return ret;
}
params.pop_back();
}
else
{
params.emplace_back(reqUrl.substr(0, epos));
FindResult ret =
findHelper(reqUrl.substr(epos),
this->nodes[node.stringParamChild], params);
if (!ret.fragmentRuleIndexes.empty())
{
return ret;
}
params.pop_back();
}
}
if (node.pathParamChild != 0U)
{
params.emplace_back(reqUrl);
FindResult ret =
findHelper("", this->nodes[node.pathParamChild], params);
if (!ret.fragmentRuleIndexes.empty())
{
return ret;
}
params.pop_back();
}
for (const typename ContainedType::ChildMap::value_type& kv :
node.children)
{
const std::string& fragment = kv.first;
const ContainedType& child = this->nodes[kv.second];
if (reqUrl.starts_with(fragment))
{
FindResult ret =
findHelper(reqUrl.substr(fragment.size()), child, params);
if (!ret.fragmentRuleIndexes.empty())
{
return ret;
}
}
}
return {std::vector<std::string>(), {}};
}
public:
FindResult find(const std::string_view reqUrl) const
{
std::vector<std::string> start;
return findHelper(reqUrl, this->head(), start);
}
void add(std::string_view urlIn, unsigned ruleIndex)
{
size_t idx = 0;
std::string_view url = urlIn;
std::string_view fragment;
// Check if the URL contains a fragment (#)
size_t fragmentPos = urlIn.find('#');
size_t queryPos = urlIn.find('?');
if (fragmentPos != std::string::npos && queryPos == std::string::npos &&
fragmentPos != urlIn.length() - 1)
{
fragment = urlIn.substr(fragmentPos + 1);
url = urlIn.substr(0, fragmentPos);
}
if (fragment.empty())
{
BMCWEB_LOG_CRITICAL("empty fragment on rule \"{}\"", urlIn);
throw std::runtime_error(
std::format("empty fragment on rule \"{}\"", urlIn));
}
while (!url.empty())
{
char c = url[0];
if (c == '<')
{
bool found = false;
for (const std::string_view str1 :
{"<str>", "<string>", "<path>"})
{
if (!url.starts_with(str1))
{
continue;
}
found = true;
ContainedType& node = this->nodes[idx];
size_t* param = &node.stringParamChild;
if (str1 == "<path>")
{
param = &node.pathParamChild;
}
if (*param == 0U)
{
*param = this->newNode();
}
idx = *param;
url.remove_prefix(str1.size());
break;
}
if (found)
{
continue;
}
BMCWEB_LOG_CRITICAL("Can't find tag for {}", urlIn);
return;
}
std::string piece(&c, 1);
if (!this->nodes[idx].children.contains(piece))
{
unsigned newNodeIdx = this->newNode();
this->nodes[idx].children.emplace(piece, newNodeIdx);
}
idx = this->nodes[idx].children[piece];
url.remove_prefix(1);
}
ContainedType& node = this->nodes[idx];
if (node.fragmentChildren.find(fragment) != node.fragmentChildren.end())
{
BMCWEB_LOG_CRITICAL(
R"(fragment handler already exists for "{}" fragment "{}")",
urlIn, fragment);
throw std::runtime_error(std::format(
R"(handler already exists for url "{}" fragment "{}")", urlIn,
fragment));
}
node.fragmentChildren.emplace(fragment, ruleIndex);
}
private:
void debugNodePrint(ContainedType& n, size_t level)
{
std::string spaces(level, ' ');
if (n.stringParamChild != 0U)
{
BMCWEB_LOG_DEBUG("{}<str>", spaces);
debugNodePrint(this->nodes[n.stringParamChild], level + 5);
}
if (n.pathParamChild != 0U)
{
BMCWEB_LOG_DEBUG("{} <path>", spaces);
debugNodePrint(this->nodes[n.pathParamChild], level + 6);
}
for (const typename ContainedType::ChildMap::value_type& kv :
n.fragmentChildren)
{
BMCWEB_LOG_DEBUG("{}#{}", spaces, kv.first);
}
for (const typename ContainedType::ChildMap::value_type& kv :
n.children)
{
BMCWEB_LOG_DEBUG("{}{}", spaces, kv.first);
debugNodePrint(this->nodes[kv.second], level + kv.first.size());
}
}
public:
void debugPrint()
{
debugNodePrint(this->head(), 0U);
}
};
} // namespace crow
|