Repository navigation
Expand file tree
/
Copy pathroutes.hpp
More file actions
662 lines (607 loc) · 27.2 KB
/
Copy pathroutes.hpp
File metadata and controls
662 lines (607 loc) · 27.2 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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
/*
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 file
LICENSE in the distribution; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#ifndef SRC_HTTPSERVER_SERVER_ROUTES_HPP_
#define SRC_HTTPSERVER_SERVER_ROUTES_HPP_
#include <cstddef>
#include <cstdint>
#include <span>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
#include <httpserver/concurrency/executor.hpp>
#include <httpserver/concurrency/task.hpp>
#include <httpserver/http/fields.hpp>
#include <httpserver/http/method.hpp>
#include <httpserver/http/outcome.hpp>
#include <httpserver/http/request_head.hpp>
#include <httpserver/http/status.hpp>
#include <httpserver/server/budgets.hpp>
namespace httpserver {
// The per-connection exchange type is defined by the request-handling
// area of the v3 API. Route registration only stores and moves
// handlers, so an incomplete type suffices here by design; the
// definition must live exactly in this namespace.
class exchange;
namespace server {
// TASK-118: one captured path parameter delivered through the exchange
// (plan D2). `name` views the registry's stored pattern text: the view
// stays valid while the registry is not mutated (the native server
// enforces this -- registration is closed once listen() runs); `value`
// is owned.
struct route_captures {
std::string_view name;
std::string value;
};
namespace detail {
constexpr bool is_name_byte(char c) noexcept {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
|| (c >= '0' && c <= '9') || c == '_';
}
// Parameter name: [A-Za-z0-9_]+.
constexpr bool valid_parameter_name(std::string_view name) noexcept {
if (name.empty()) return false;
for (const char c : name) {
if (!is_name_byte(c)) return false;
}
return true;
}
// Classifies one slash-delimited pattern segment: a literal token or
// exactly one "{name}". Appends parameter names for duplicate
// detection. Returns a typed failure instead of mutating counters when
// the segment is malformed.
inline http::outcome parse_route_segment(std::string_view segment,
std::vector<std::string_view>& names,
std::size_t& parameters) {
if (segment.empty()) {
return http::outcome(
http::outcome_code::invalid_argument,
"route_pattern: empty segment (repeated slash)");
}
if (segment.front() != '{') {
// Literal segment: the RFC 9110 token grammar, which excludes
// braces, spaces, and separators.
if (!http::detail::is_token(segment)) {
return http::outcome(
http::outcome_code::invalid_argument,
"route_pattern: segment '" + std::string(segment)
+ "' is not a valid literal token");
}
return http::outcome::okay();
}
if (segment.size() < 3 || segment.back() != '}') {
return http::outcome(
http::outcome_code::invalid_argument,
"route_pattern: a parameter segment must be exactly one"
" '{name}'");
}
const std::string_view name = segment.substr(1, segment.size() - 2);
if (!valid_parameter_name(name)) {
return http::outcome(
http::outcome_code::invalid_argument,
"route_pattern: parameter name '" + std::string(name)
+ "' must match [A-Za-z0-9_]+");
}
for (const std::string_view seen : names) {
if (seen == name) {
return http::outcome(
http::outcome_code::invalid_argument,
"route_pattern: duplicate parameter name '" + std::string(name)
+ "'");
}
}
names.push_back(name);
++parameters;
return http::outcome::okay();
}
// Splits a path into its slash-delimited segments. The root and the
// empty path have no segments (both denote the same resource).
inline std::vector<std::string_view> split_path_segments(
std::string_view path) {
std::vector<std::string_view> segments;
if (path.size() < 2) return segments; // "" and "/"
std::size_t pos = 1; // skip the leading slash
while (pos < path.size()) {
const std::size_t slash = path.find('/', pos);
const std::size_t width =
slash == std::string_view::npos ? std::string_view::npos
: slash - pos;
segments.push_back(path.substr(pos, width));
if (slash == std::string_view::npos) break;
pos = slash + 1;
}
return segments;
}
// One admission-time pattern segment: byte offsets into the stored
// pattern text rather than views, so the stored form stays valid
// across entry moves (vector reallocation) for the registry's whole
// lifetime.
struct pattern_segment {
std::uint32_t offset;
std::uint32_t length;
};
// Splits a canonical pattern into offset/length segments ONCE, at
// admission (registration time). Patterns are immutable once
// registered and registration closes at listen(), so resolve/match
// consume this stored form and never re-split a pattern per request.
inline std::vector<pattern_segment> index_path_segments(
std::string_view path) {
std::vector<pattern_segment> segments;
if (path.size() < 2) return segments; // "" and "/"
std::size_t pos = 1; // skip the leading slash
while (pos < path.size()) {
const std::size_t slash = path.find('/', pos);
const std::size_t length =
slash == std::string_view::npos ? path.size() - pos
: slash - pos;
segments.push_back(pattern_segment{
static_cast<std::uint32_t>(pos),
static_cast<std::uint32_t>(length)});
if (slash == std::string_view::npos) break;
pos = slash + 1;
}
return segments;
}
// Matches the stored (admission-time) pattern segments against the
// request's already-split path: a literal segment compares equal, a
// single {name} segment captures any non-empty segment (the capture
// name views the pattern text). The prefix family matches a leading
// run of the path segments (equal length included; "/" therefore
// matches every path); the full family requires equal counts.
// Allocation-free beyond the captures the accepted run fills.
inline bool match_stored_segments(
std::string_view pattern_text,
const std::vector<pattern_segment>& pattern_segments, bool prefix,
const std::vector<std::string_view>& path_segments,
std::vector<route_captures>& captures) {
const std::size_t count = pattern_segments.size();
if (prefix ? count > path_segments.size()
: count != path_segments.size()) {
return false;
}
captures.clear();
for (std::size_t i = 0; i < count; ++i) {
const pattern_segment& stored = pattern_segments[i];
const std::string_view pattern =
pattern_text.substr(stored.offset, stored.length);
const std::string_view segment = path_segments[i];
if (pattern.front() == '{') {
if (segment.empty()) return false;
captures.push_back(route_captures{
pattern.substr(1, pattern.size() - 2), std::string(segment)});
continue;
}
if (pattern != segment) return false;
}
return true;
}
} // namespace detail
// A validated route pattern: "/" or a slash-separated sequence of
// literal-token segments, each of which may instead be exactly one
// "{name}" parameter whose name matches [A-Za-z0-9_] and is unique
// within the pattern. The stored text is canonical: one leading slash,
// no trailing slash, no empty segments. parse() reports invalid_argument
// and leaves `out` untouched on failure.
class route_pattern {
public:
route_pattern() noexcept = default;
static http::outcome parse(std::string_view text, route_pattern& out) {
if (text.empty()) {
return http::outcome(http::outcome_code::invalid_argument,
"route_pattern: pattern is empty");
}
if (text.front() != '/') {
return http::outcome(
http::outcome_code::invalid_argument,
"route_pattern: pattern must begin with '/'");
}
route_pattern parsed;
if (text != "/") {
if (text.back() == '/') {
return http::outcome(
http::outcome_code::invalid_argument,
"route_pattern: pattern must not end with '/'");
}
std::vector<std::string_view> names;
std::size_t pos = 1;
while (pos <= text.size()) {
const std::size_t slash = text.find('/', pos);
const std::size_t width =
slash == std::string_view::npos ? std::string_view::npos
: slash - pos;
if (const http::outcome result = detail::parse_route_segment(
text.substr(pos, width), names, parsed.parameters_);
!result.ok()) {
return result;
}
++parsed.segments_;
if (slash == std::string_view::npos) break;
pos = slash + 1;
}
}
parsed.text_ = std::string(text);
out = std::move(parsed);
return http::outcome::okay();
}
// Canonical pattern text; empty for a default-constructed pattern.
const std::string& text() const noexcept { return text_; }
std::size_t segment_count() const noexcept { return segments_; }
std::size_t parameter_count() const noexcept { return parameters_; }
private:
std::string text_;
std::size_t segments_ = 0;
std::size_t parameters_ = 0;
};
// The canonical coroutine handler of the v3 API: a lazily started task
// over the per-connection exchange (architecture §3.1, DR-V3-003).
using route_handler = concurrency::unique_function<task<void>(exchange&)>;
// The value one synchronous route returns: terminal status, response
// fields, and the complete body bytes. A plain aggregate; an invalid
// status makes the route's runner synthesize the 500.
struct sync_response {
http::status status; // invalid -> the runner's synthesized 500
http::fields fields; // handler-pinned framing passes through
std::vector<std::byte> body;
};
// The value-returning handler (DR-V3-003): receives the complete
// request head and the bounded buffered body (empty for a bodyless
// request) and returns the response value. No coroutine code.
using sync_route_handler = concurrency::unique_function<sync_response(const http::request_head&, std::span<const std::byte>)>;
// Budget-bounded registration table: the seed of the immutable
// published route generation. A registration is visible to every
// enabled HTTP version by construction (PRD-V3N-REQ-009): neither the
// API nor a stored entry carries a protocol dimension. Each admitted
// route holds one reservation against the routes resource of its
// budget node and every ancestor, so nested scopes share one limit.
class route_registry {
public:
route_registry() noexcept = default;
// Attaches a registry to a budget scope. invalid_argument when the
// budget carries no routes capacity (an empty budget or a zero
// capacity); `out` is untouched on failure.
static http::outcome create(const resource_budget& budget,
route_registry& out) {
if (budget.capacity(resource::routes) == 0) {
return http::outcome(
http::outcome_code::invalid_argument,
"route_registry: the budget carries no routes capacity");
}
out = route_registry(budget);
return http::outcome::okay();
}
// Registers `handler` for (method, pattern) -- the single-method
// form; an extension method registers exactly itself. Typed
// failures: invalid_argument for the unknown_ method, a malformed
// pattern, or an empty handler; invalid_state for a duplicate
// (overlapping methods, same pattern, same family) or a registry
// with no budget; limit_exceeded when the routes capacity is
// exhausted on this node or an ancestor. On failure the registry is
// unchanged.
http::outcome route(const http::method& m, std::string_view pattern,
route_handler handler) {
if (!m.valid()) {
return http::outcome(
http::outcome_code::invalid_argument,
"route_registry: the unknown_ method is not registrable");
}
if (!handler) {
return http::outcome(http::outcome_code::invalid_argument,
"route_registry: handler is empty");
}
http::method_set bits;
http::method single;
if (m.is_extension()) {
single = m;
} else {
bits.set(m.id());
}
return admit(pattern, std::move(handler), false, bits, single);
}
// Registers `handler` for a set of known methods on one pattern.
// An empty set is invalid_argument; the duplicate rule is
// overlapping-methods on the same pattern (disjoint sets coexist
// and the 405 Allow merges them).
http::outcome route(const http::method_set& set,
std::string_view pattern, route_handler handler) {
if (!set.any()) {
return http::outcome(
http::outcome_code::invalid_argument,
"route_registry: a method-set route needs at least one"
" method");
}
if (!handler) {
return http::outcome(http::outcome_code::invalid_argument,
"route_registry: handler is empty");
}
return admit(pattern, std::move(handler), false, set,
http::method{});
}
// Registers `handler` as a prefix-family route: the pattern
// segments match a leading run of the request path segments (equal
// length included; "/" is the catch-all). Admission rules match
// route(); full-match routes outrank prefix routes at resolve time.
http::outcome route_prefix(const http::method_set& set,
std::string_view pattern,
route_handler handler) {
if (!set.any()) {
return http::outcome(
http::outcome_code::invalid_argument,
"route_registry: a prefix route needs at least one"
" method");
}
if (!handler) {
return http::outcome(http::outcome_code::invalid_argument,
"route_registry: handler is empty");
}
return admit(pattern, std::move(handler), true, set,
http::method{});
}
// Exact-segment visibility probe over the stored patterns (no
// capture evaluation); the request matcher arrives with the
// request-handling tasks.
bool registered(const http::method& m, const route_pattern& p) const {
for (const entry& known : entries_) {
if (known.pattern_.text() == p.text() && accepts(known, m)) {
return true;
}
}
return false;
}
// One lookup outcome: the registered handler (null on a miss) and
// the parameter captures in pattern order.
struct match_result {
const route_handler* handler = nullptr;
std::vector<std::string> parameters;
};
// Resolves a request head to its route. Segment-wise: literal
// segments compare equal, a {name} segment captures any non-empty
// segment, first match in registration order wins, a miss yields a
// null handler. Const and lock-free. No protocol dimension (REQ-009):
// one registration serves every enabled version.
match_result match(const http::method& m,
std::string_view route_path) const {
match_result found;
const std::vector<std::string_view> path_segments =
detail::split_path_segments(route_path);
for (const entry& known : entries_) {
if (known.prefix_ || !accepts(known, m)) continue;
std::vector<route_captures> captures;
if (!detail::match_stored_segments(known.pattern_.text(),
known.segments_, false,
path_segments, captures)) {
continue;
}
found.handler = &known.handler_;
found.parameters.reserve(captures.size());
for (route_captures& capture : captures) {
found.parameters.push_back(std::move(capture.value));
}
break;
}
return found;
}
// TASK-118: the request-facing lookup with the v2 family
// precedence (plan D1). Full matches (exact and parameterized,
// first registration order) are decided before prefix matches
// (most segments wins, ties by registration order); the tier
// decision is method-blind, exactly like the v2 lookup tiers, so a
// full-match pattern that mismatches the method owns the 405. A
// hit reports the handler, the named captures in pattern order,
// and the matched pattern; a method miss reports the tier's merged
// methods (the Allow inputs: the known-slot set plus any extension
// singles' tokens); a plain miss reports neither. The v2 regex
// family and {name|regex} per-segment constraints are not ported
// (migration note in the v2-parity inventory).
//
// Observability rule of the full tier: it stops at its first hit
// (the base match() semantics) unless @p methods_observable is
// true -- pass true exactly when the merged methods can be
// observed (route_resolved/before_handler hooks consult
// route_descriptor.methods); then the tier keeps merging every
// matching entry's methods past the hit. A method miss carries the
// full merge either way (the Allow set is always observable).
struct resolve_result {
enum class resolve_kind : std::uint8_t { miss, method_miss, hit };
resolve_kind kind = resolve_kind::miss;
const route_handler* handler = nullptr;
std::vector<route_captures> captures;
std::string pattern_text;
http::method_set methods;
std::vector<std::string> extension_names;
bool is_prefix = false;
};
resolve_result resolve(const http::method& m,
std::string_view route_path,
bool methods_observable = false) const {
const std::vector<std::string_view> path_segments =
detail::split_path_segments(route_path);
resolve_result full = resolve_tier(m, path_segments, false,
methods_observable);
if (full.kind != resolve_result::resolve_kind::miss) return full;
return resolve_tier(m, path_segments, true, methods_observable);
}
std::size_t size() const noexcept { return entries_.size(); }
const resource_budget& budget() const noexcept { return budget_; }
private:
explicit route_registry(resource_budget budget) noexcept
: budget_(std::move(budget)) { }
struct entry {
http::method_set methods_; // known-slot methods
http::method single_; // extension registration
route_pattern pattern_;
// The admission-time split of pattern_ (offsets into its
// text): resolve/match consume this stored form, so no request
// re-splits an immutable pattern.
std::vector<detail::pattern_segment> segments_;
route_handler handler_;
reservation seat_;
bool prefix_ = false;
};
// True iff the entry would serve @p m: a known method tests the
// slot set; an extension single compares by identity.
static bool accepts(const entry& known, const http::method& m) noexcept {
if (known.single_.valid()) return known.single_ == m;
return known.methods_.contains(m);
}
// The shared admission tail of every registration form: parse,
// budget gate, same-family duplicate check (overlapping methods on
// one canonical pattern), then the reserved push.
http::outcome admit(std::string_view pattern, route_handler handler,
bool prefix, const http::method_set& set,
const http::method& single) {
route_pattern parsed;
if (const http::outcome result =
route_pattern::parse(pattern, parsed);
!result.ok()) {
return result;
}
if (!budget_.valid()) {
return http::outcome(
http::outcome_code::invalid_state,
"route_registry: registry has no budget");
}
const std::string& canonical = parsed.text();
for (const entry& known : entries_) {
if (known.prefix_ == prefix && known.pattern_.text() == canonical
&& overlaps(known, set, single)) {
const std::string label = single.valid()
? std::string(single.name()) : http::to_string(set);
return http::outcome(
http::outcome_code::invalid_state,
"route_registry: duplicate route for " + label + " "
+ canonical);
}
}
reservation seat;
if (const http::outcome result =
budget_.reserve(resource::routes, 1, seat);
!result.ok()) {
return result;
}
// Computed before the push: a braced-init-list evaluates
// left-to-right, so the split must not read the pattern after
// the move into the entry.
std::vector<detail::pattern_segment> segments =
detail::index_path_segments(canonical);
entries_.push_back(entry{set, single, std::move(parsed),
std::move(segments), std::move(handler),
std::move(seat), prefix});
return http::outcome::okay();
}
// Overlap test for the duplicate rule: two known-slot sets
// intersect, or two extension singles name the same token. An
// extension single and a known-slot set are always disjoint.
static bool overlaps(const entry& known, const http::method_set& set,
const http::method& single) noexcept {
if (single.valid()) {
return known.single_.valid() && known.single_ == single;
}
for (std::size_t id = 0;
id < static_cast<std::size_t>(http::method_id::extension);
++id) {
const http::method slot =
http::method::known(static_cast<http::method_id>(id));
if (set.contains(slot) && known.methods_.contains(slot)) {
return true;
}
}
return false;
}
// One tier of the resolve decision. Merges every matching entry's
// methods (the Allow inputs); the first accepting full match wins,
// the deepest accepting prefix match wins. The full tier stops at
// its first hit unless the merged methods are observable
// (@p methods_observable: route_resolved/before_handler hooks
// consult route_descriptor.methods -- the dispatcher passes that
// predicate); the prefix tier always scans fully, because a deeper
// pattern may still outrank the current best.
resolve_result resolve_tier(
const http::method& m,
const std::vector<std::string_view>& path_segments, bool prefix,
bool methods_observable) const {
resolve_result out;
const entry* best = nullptr;
std::vector<route_captures> best_captures;
for (const entry& known : entries_) {
if (known.prefix_ != prefix) continue;
std::vector<route_captures> captures;
if (!match_tier_entry(known, path_segments, prefix, captures)) {
continue;
}
merge_entry_methods(out, known);
if (!accepts(known, m) || !outranks(best, known, prefix)) {
continue;
}
best = &known;
best_captures = std::move(captures);
// The full tier's winner is always its FIRST hit: once the
// merged methods cannot be observed, the scan stops (the
// pre-task match() semantics; the merge feeds nothing).
if (!prefix && !methods_observable) break;
}
if (best == nullptr) return miss_or_method_miss(out);
out.kind = resolve_result::resolve_kind::hit;
out.handler = &best->handler_;
out.captures = std::move(best_captures);
out.pattern_text = best->pattern_.text();
out.is_prefix = prefix;
return out;
}
// Path-shape match of one entry within its tier, over the stored
// admission-time segments.
static bool match_tier_entry(
const entry& known,
const std::vector<std::string_view>& path_segments, bool prefix,
std::vector<route_captures>& captures) {
return detail::match_stored_segments(known.pattern_.text(),
known.segments_, prefix,
path_segments, captures);
}
// Folds one matching entry's methods into the tier's Allow inputs.
static void merge_entry_methods(resolve_result& out,
const entry& known) noexcept {
if (known.single_.valid()) {
out.extension_names.emplace_back(known.single_.name());
} else {
out.methods.merge(known.methods_);
}
}
// The candidate-selection rule: no winner yet always takes; the
// full tier keeps its first winner; the prefix tier takes only a
// strictly deeper pattern.
static bool outranks(const entry* best, const entry& candidate,
bool prefix) noexcept {
if (best == nullptr) return true;
if (!prefix) return false;
return candidate.pattern_.segment_count()
> best->pattern_.segment_count();
}
// No accepting entry: a miss unless the tier matched some pattern
// shape (then the merged methods feed the 405 Allow).
static resolve_result miss_or_method_miss(resolve_result out) noexcept {
if (out.methods.any() || !out.extension_names.empty()) {
out.kind = resolve_result::resolve_kind::method_miss;
}
return out;
}
std::vector<entry> entries_;
resource_budget budget_;
};
} // namespace server
} // namespace httpserver
#endif // SRC_HTTPSERVER_SERVER_ROUTES_HPP_