-
-
Notifications
You must be signed in to change notification settings - Fork 191
Expand file tree
/
Copy pathsegment_trie.hpp
More file actions
415 lines (381 loc) · 18.5 KB
/
Copy pathsegment_trie.hpp
File metadata and controls
415 lines (381 loc) · 18.5 KB
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
This file is part of libhttpserver
Copyright (C) 2011-2026 Sebastiano Merlino
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
USA
*/
// Segment-trie for the parameterized + prefix route tier.
//
// A segment trie avoids dragging in a vendored library (which would
// conflict with the project's tightly curated source tree and
// LGPL-2.1 distribution). The architecture commits only to the outer
// three-tier + cache shape; the implementation choice is
// intentionally left open.
//
// Internal header — only reachable when compiling libhttpserver.
#if !defined(HTTPSERVER_COMPILATION)
#error "segment_trie.hpp is internal; only reachable when compiling libhttpserver."
#endif
#ifndef SRC_HTTPSERVER_DETAIL_SEGMENT_TRIE_HPP_
#define SRC_HTTPSERVER_DETAIL_SEGMENT_TRIE_HPP_
#include <cstddef>
#include <functional>
#include <map>
#include <memory>
#include <optional>
#include <regex> // NOLINT [build/c++11] -- regex is not banned project-wide.
#include <stdexcept>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include "httpserver/http_utils.hpp"
namespace httpserver {
namespace detail {
// segment_trie_match: result type of segment_trie<T>::find.
// `entry` is a non-owning pointer into the tree; valid until the next mutation.
template <typename T>
struct segment_trie_match {
const T* entry = nullptr;
std::vector<std::pair<std::string, std::string>> captures;
bool is_prefix_match = false;
};
// Single trie node. `wildcard_child_` is a single optional pointer (not
// another map entry) because there can be at most one unnamed wildcard per
// path level by design — two {name} siblings at the same depth are
// ambiguous and rejected at registration time.
template <typename T>
struct segment_trie_node {
// Per-segment children are kept in std::map rather than
// std::unordered_map for hash-flooding immunity (CWE-407). URL path
// segments are attacker-controlled and neither libc++ nor libstdc++
// seed std::hash<std::string> by default, so std::unordered_map is
// vulnerable to algorithmic-complexity DoS via crafted sibling keys.
// std::map (red-black tree) gives O(log n) worst-case per probe with
// no hashing in the loop. Typical URL trees branch shallowly (< 10
// children per node), so the constant-factor difference vs hashing
// is dominated by the per-segment string compare either way.
//
// std::less<> is the transparent comparator: it lets find() take a
// std::string_view directly and compare against the std::string keys
// without constructing a temporary std::string per probe.
std::map<std::string, std::unique_ptr<segment_trie_node>, std::less<>> children_;
std::unique_ptr<segment_trie_node> wildcard_child_;
// wildcard_name_ is the bare parameter name (e.g. "id" for the
// pattern `{id|([0-9]+)}`), matching v1 semantics: callers reach the
// captured value via `req.get_arg("id")`, not via the full source
// token. wildcard_constraint_, when populated, carries the per-segment
// regex from the `|regex` suffix and is enforced during find(); a
// segment that fails the constraint is treated as a wildcard miss
// (the descent breaks, falling back to a prefix candidate or 404).
std::string wildcard_name_;
std::optional<std::regex> wildcard_constraint_;
std::optional<T> exact_terminus_;
std::optional<T> prefix_terminus_;
};
// segment_trie<T>: an uncompressed per-segment trie (one node per '/'-
// delimited path segment; no edge compression, hence "segment trie" and
// not "radix tree"). Inserts route paths split on '/', supports `{name}`
// wildcard segments, and carries a `is_prefix` flag per insertion so the
// same tree backs both parameterized exact and prefix registrations.
//
// Concurrency: this type is NOT internally synchronized. The owning
// detail::route_table protects all three tier structures (exact_routes_,
// param_and_prefix_routes_, regex_routes_) with a single std::shared_mutex.
template <typename T>
class segment_trie {
public:
segment_trie() : root_(std::make_unique<segment_trie_node<T>>()) {}
// Insert `path` with the given entry. is_prefix selects whether the
// entry terminates in `prefix_terminus_` (and matches any deeper
// request path) or `exact_terminus_` (and matches only this path).
// The segment_trie itself does not look inside `entry` — the caller
// (webserver_impl) is responsible for keeping the is_prefix argument
// consistent with route_entry::is_prefix, which is the source of
// truth. Replaces an existing terminus of the same kind.
void insert(const std::string& path, T entry, bool is_prefix = false) {
segment_trie_node<T>* node = root_.get();
const auto segments = tokenize(path);
for (const std::string& seg : segments) {
node = descend_or_create(node, seg);
}
if (is_prefix) {
node->prefix_terminus_ = std::move(entry);
} else {
node->exact_terminus_ = std::move(entry);
}
}
// Match the root node's termini (exact first, then prefix). Pulled
// out of find() so the empty-segments branch stays a one-liner.
bool match_root_terminus(segment_trie_match<T>& out) const {
const segment_trie_node<T>* node = root_.get();
if (node->exact_terminus_.has_value()) {
out.entry = &node->exact_terminus_.value();
out.is_prefix_match = false;
return true;
}
if (node->prefix_terminus_.has_value()) {
out.entry = &node->prefix_terminus_.value();
out.is_prefix_match = true;
return true;
}
return false;
}
// Find the most specific match for `path`. Returns true on hit and
// populates `out`. Lookup preference (most specific first):
// 1. exact_terminus_ on the matched node, if every request segment
// consumed by exact-or-wildcard descent.
// 2. prefix_terminus_ on the deepest ancestor that has one.
//
// Hot-path implementation: iterates `path` directly without calling
// tokenize(), avoiding the std::vector<std::string> allocation and
// per-segment string copies. Each segment is extracted as a
// std::string_view and compared against children_ keys (std::string)
// via the transparent comparator (std::less<>) on std::map — no
// temporary std::string is constructed per probe.
// The wildcard path copies the segment into captures<string,string>
// only when a wildcard node is taken.
bool find(std::string_view path, segment_trie_match<T>& out) const {
out = {};
const segment_trie_node<T>* node = root_.get();
// Strip a single leading slash; a bare "/" normalises to empty.
std::string_view rest = path;
if (!rest.empty() && rest.front() == '/') rest.remove_prefix(1);
if (rest.empty()) return match_root_terminus(out);
// Track best prefix candidate seen during descent (deepest wins).
// Root prefix terminus: a `register_prefix("/")` matches every
// request, so seed best_prefix with it before walking deeper.
const T* best_prefix = node->prefix_terminus_.has_value()
? &node->prefix_terminus_.value() : nullptr;
std::vector<std::pair<std::string, std::string>> best_prefix_caps;
std::vector<std::pair<std::string, std::string>> caps;
while (!rest.empty()) {
std::string_view seg = pop_next_segment_(rest);
const segment_trie_node<T>* next = step_to_child_(node, seg, caps);
if (next == nullptr) break;
node = next;
if (node->prefix_terminus_.has_value()) {
best_prefix = &node->prefix_terminus_.value();
best_prefix_caps = caps;
}
// If we just consumed the last request segment, an exact
// terminus here beats any prefix candidate.
if (try_consume_exact_terminus_(rest, node, caps, out)) {
return true;
}
}
if (best_prefix == nullptr) return false;
out.entry = best_prefix;
out.captures = std::move(best_prefix_caps);
out.is_prefix_match = true;
return true;
}
private:
// Pop the next '/'-delimited segment off `rest`, advancing rest past it
// (and past the separator). Extracted from find() so the per-segment
// logic stays a single statement.
static std::string_view pop_next_segment_(std::string_view& rest) {
std::string_view::size_type slash = rest.find('/');
if (slash == std::string_view::npos) {
std::string_view seg = rest;
rest = {};
return seg;
}
std::string_view seg = rest.substr(0, slash);
rest = rest.substr(slash + 1);
return seg;
}
// If `rest` is fully consumed and `node` carries an exact terminus,
// populate `out` and return true. Returns false otherwise (caller
// continues the descent or falls back to the best prefix candidate).
static bool try_consume_exact_terminus_(std::string_view rest,
const segment_trie_node<T>* node,
std::vector<std::pair<std::string, std::string>>& caps,
segment_trie_match<T>& out) {
if (!rest.empty() || !node->exact_terminus_.has_value()) return false;
out.entry = &node->exact_terminus_.value();
out.captures = std::move(caps);
out.is_prefix_match = false;
return true;
}
// Take one descent step from `node` along `seg`: prefer an exact child,
// fall back to a wildcard child (capturing into `caps` and enforcing
// any per-segment regex constraint). Returns the new node or nullptr
// when no descent is possible (caller falls back to a prefix candidate
// or returns 404).
static const segment_trie_node<T>* step_to_child_(const segment_trie_node<T>* node,
std::string_view seg,
std::vector<std::pair<std::string, std::string>>& caps) {
// Prefer exact child over wildcard. std::map's transparent
// comparator (std::less<>) accepts std::string_view directly --
// no temporary std::string is constructed on the hot path.
auto it = node->children_.find(seg);
if (it != node->children_.end()) return it->second.get();
if (!node->wildcard_child_) return nullptr;
// Per-segment regex constraint enforcement: if the wildcard
// carries a `|regex` suffix, the actual segment must satisfy
// it; otherwise the branch is not taken and we fall back to a
// prefix candidate or 404, matching v1 semantics.
const segment_trie_node<T>* candidate = node->wildcard_child_.get();
if (candidate->wildcard_constraint_.has_value()
&& !std::regex_match(seg.begin(), seg.end(),
*candidate->wildcard_constraint_)) {
return nullptr;
}
caps.emplace_back(candidate->wildcard_name_, std::string(seg));
return candidate;
}
public:
// Probe for a terminus of the specified kind at the EXACT
// node reached by tokenizing `path` (pattern-equality, not request-
// path matching). Unlike find(), this does NOT fall back to a
// prefix ancestor; it DOES descend the wildcard child when the
// pattern segment is wildcard-shaped (same shape rule as remove()),
// because the caller passes the registered pattern, not a concrete
// request segment. Returns true iff such a terminus exists.
//
// Designed for the registration-time collision guard
// (route_table::reject_terminus_collision): when
// inserting a NEW exact terminus at /admin we need to refuse if a
// prefix terminus is already registered at /admin (and vice versa)
// — silent shadowing would corrupt the (method, path) cache key.
bool has_terminus_at(const std::string& path, bool is_prefix) const {
const segment_trie_node<T>* node = walk_registered_pattern_(root_.get(),
tokenize(path));
if (node == nullptr) return false;
return is_prefix ? node->prefix_terminus_.has_value()
: node->exact_terminus_.has_value();
}
// Remove the entry at `path`. is_prefix selects which terminus to
// clear. Returns true iff a terminus was actually cleared.
// NOTE: unlike find(), where descent uses the concrete request-path
// segment value (e.g. "42"), remove() receives the registered pattern
// (e.g. "{id}") and matches wildcard nodes by the placeholder shape.
bool remove(const std::string& path, bool is_prefix) {
segment_trie_node<T>* node = walk_registered_pattern_(root_.get(),
tokenize(path));
if (node == nullptr) return false;
if (is_prefix) {
if (!node->prefix_terminus_.has_value()) return false;
node->prefix_terminus_.reset();
} else {
if (!node->exact_terminus_.has_value()) return false;
node->exact_terminus_.reset();
}
return true;
// Note: we do not collapse empty branches. This is intentional —
// dead nodes are cheap (a few pointers) and avoiding rebalancing
// keeps the data structure trivially safe under the writer lock.
}
bool empty() const noexcept {
return is_node_empty(root_.get());
}
private:
static std::vector<std::string> tokenize(const std::string& path) {
// tokenize_url takes a const std::string&; passing the already-
// owned string binds directly, with no temporary constructed.
return ::httpserver::http::http_utils::tokenize_url(path);
}
// Descend from `start` along `segments`, matching exact children
// first and falling back to a wildcard child when the segment has
// the {name} placeholder shape. Returns the terminal node or
// nullptr if any segment failed to match.
//
// Templated on Node (segment_trie_node<T> or const segment_trie_node<T>) so the
// const-correct mutable / const callers (has_terminus_at, remove)
// share one descent body.
template <class Node>
static Node* walk_registered_pattern_(Node* start,
const std::vector<std::string>& segments) {
Node* node = start;
for (const std::string& seg : segments) {
auto it = node->children_.find(seg);
if (it != node->children_.end()) {
node = it->second.get();
continue;
}
if (node->wildcard_child_ && is_wildcard_segment(seg)) {
node = node->wildcard_child_.get();
continue;
}
return nullptr;
}
return node;
}
static bool is_wildcard_segment(const std::string& seg) noexcept {
return seg.size() >= 2 && seg.front() == '{' && seg.back() == '}';
}
// Split `{name|regex}` into (bare name, regex-pattern). When no `|`
// is present, returns (name, ""). The regex pattern (if any) is the
// substring between the first `|` and the closing `}`. Mirrors
// http_endpoint::append_parameter_url_part's split rule so the same
// user-facing `{name|regex}` syntax has identical semantics in both
// the radix tier and the regex-fallback tier.
static std::pair<std::string, std::string>
parse_wildcard(const std::string& seg) {
// Caller has already verified is_wildcard_segment(seg).
const std::string::size_type bar = seg.find_first_of('|');
if (bar == std::string::npos) {
return {seg.substr(1, seg.size() - 2), {}};
}
return {seg.substr(1, bar - 1),
seg.substr(bar + 1, seg.size() - bar - 2)};
}
static segment_trie_node<T>* descend_or_create(segment_trie_node<T>* node,
const std::string& seg) {
if (is_wildcard_segment(seg)) {
auto [name, constraint_src] = parse_wildcard(seg);
if (!node->wildcard_child_) {
node->wildcard_child_ = std::make_unique<segment_trie_node<T>>();
node->wildcard_child_->wildcard_name_ = std::move(name);
if (!constraint_src.empty()) {
try {
node->wildcard_child_->wildcard_constraint_.emplace(
constraint_src,
std::regex::extended | std::regex::icase
| std::regex::nosubs);
} catch (const std::regex_error&) {
throw std::invalid_argument(
"Not a valid regex in URL: " + constraint_src);
}
}
}
// If a wildcard child already exists, the first registered
// name and constraint win. Re-registering a different name
// or constraint on the same path is a user error and is
// caught by the upstream conflict check before insert.
return node->wildcard_child_.get();
}
auto it = node->children_.find(seg);
if (it == node->children_.end()) {
it = node->children_.emplace(seg,
std::make_unique<segment_trie_node<T>>()).first;
}
return it->second.get();
}
static bool is_node_empty(const segment_trie_node<T>* n) noexcept {
if (n == nullptr) return true;
if (n->exact_terminus_.has_value()
|| n->prefix_terminus_.has_value()) return false;
for (const auto& kv : n->children_) {
if (!is_node_empty(kv.second.get())) return false;
}
if (n->wildcard_child_
&& !is_node_empty(n->wildcard_child_.get())) return false;
return true;
}
std::unique_ptr<segment_trie_node<T>> root_;
};
} // namespace detail
} // namespace httpserver
#endif // SRC_HTTPSERVER_DETAIL_SEGMENT_TRIE_HPP_