-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathrouter_base.hpp
More file actions
147 lines (121 loc) · 3.13 KB
/
router_base.hpp
File metadata and controls
147 lines (121 loc) · 3.13 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
//
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/cppalliance/http
//
#ifndef BOOST_HTTP_SRC_SERVER_DETAIL_ROUTER_BASE_HPP
#define BOOST_HTTP_SRC_SERVER_DETAIL_ROUTER_BASE_HPP
#include <boost/http/server/detail/router_base.hpp>
#include <boost/http/detail/except.hpp>
#include "src/server/detail/route_match.hpp"
#include <mutex>
namespace boost {
namespace http {
namespace detail {
struct router_base::entry
{
// ~32 bytes (SSO string)
std::string verb_str;
// 8 bytes each
handler_ptr h;
std::size_t matcher_idx = 0;
// 4 bytes
http::method verb = http::method::unknown;
// 1 byte (+ 3 bytes padding)
bool all;
// all methods
explicit entry(
handler_ptr h_) noexcept
: h(std::move(h_))
, all(true)
{
}
// known verb match
entry(
http::method verb_,
handler_ptr h_) noexcept
: h(std::move(h_))
, verb(verb_)
, all(false)
{
BOOST_ASSERT(verb !=
http::method::unknown);
}
// string verb match
entry(
std::string_view verb_str_,
handler_ptr h_) noexcept
: h(std::move(h_))
, verb(http::string_to_method(verb_str_))
, all(false)
{
if(verb != http::method::unknown)
return;
verb_str = verb_str_;
}
bool match_method(
route_params& rp) const noexcept
{
route_params_access RP{rp};
if(all)
return true;
if(verb != http::method::unknown)
return RP->verb_ == verb;
if(RP->verb_ != http::method::unknown)
return false;
return RP->verb_str_ == verb_str;
}
};
struct router_base::impl
{
std::vector<entry> entries;
std::vector<matcher> matchers;
std::size_t pending_route_ = SIZE_MAX;
mutable std::once_flag finalized_;
options_handler_ptr options_handler_;
std::uint64_t global_methods_ = 0;
std::vector<std::string> global_custom_verbs_;
std::string global_allow_header_;
opt_flags opt_;
std::size_t depth_ = 0;
explicit impl(
opt_flags opt) noexcept
: opt_(opt)
{
}
void finalize_pending();
// Thread-safe lazy finalization for dispatch
void ensure_finalized() const
{
std::call_once(finalized_, [this]() {
const_cast<impl*>(this)->finalize_pending();
});
}
void update_allow_for_entry(
matcher& m,
entry const& e);
void rebuild_global_allow_header();
route_task
dispatch_loop(
route_params& p,
bool is_options) const;
static std::string
build_allow_header(
std::uint64_t methods,
std::vector<std::string> const& custom);
static opt_flags
compute_effective_opts(
opt_flags parent,
opt_flags child);
static void
restore_path(
route_params& p,
std::size_t base_len);
};
} // detail
} // http
} // boost
#endif