| 1 |
|
|
1 |
|
|
| 2 |
|
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
|
2 |
|
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
|
| 3 |
|
|
3 |
|
|
| 4 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
4 |
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
| 5 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
5 |
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
| 6 |
|
|
6 |
|
|
| 7 |
|
// Official repository: https://github.com/cppalliance/http
|
7 |
|
// Official repository: https://github.com/cppalliance/http
|
| 8 |
|
|
8 |
|
|
| 9 |
|
|
9 |
|
|
| 10 |
|
#ifndef BOOST_HTTP_SERVER_ROUTE_HANDLER_HPP
|
10 |
|
#ifndef BOOST_HTTP_SERVER_ROUTE_HANDLER_HPP
|
| 11 |
|
#define BOOST_HTTP_SERVER_ROUTE_HANDLER_HPP
|
11 |
|
#define BOOST_HTTP_SERVER_ROUTE_HANDLER_HPP
|
| 12 |
|
|
12 |
|
|
| 13 |
|
#include <boost/http/detail/config.hpp>
|
13 |
|
#include <boost/http/detail/config.hpp>
|
| 14 |
|
#include <boost/http/method.hpp>
|
14 |
|
#include <boost/http/method.hpp>
|
| 15 |
|
#include <boost/http/detail/except.hpp>
|
15 |
|
#include <boost/http/detail/except.hpp>
|
| 16 |
|
#include <boost/http/datastore.hpp>
|
16 |
|
#include <boost/http/datastore.hpp>
|
| 17 |
|
#include <boost/http/request.hpp>
|
17 |
|
#include <boost/http/request.hpp>
|
| 18 |
|
#include <boost/http/response.hpp>
|
18 |
|
#include <boost/http/response.hpp>
|
| 19 |
|
#include <boost/core/detail/string_view.hpp>
|
19 |
|
#include <boost/core/detail/string_view.hpp>
|
| 20 |
|
#include <boost/capy/buffers.hpp>
|
20 |
|
#include <boost/capy/buffers.hpp>
|
| 21 |
|
#include <boost/capy/buffers/make_buffer.hpp>
|
21 |
|
#include <boost/capy/buffers/make_buffer.hpp>
|
| 22 |
|
#include <boost/capy/io_result.hpp>
|
22 |
|
#include <boost/capy/io_result.hpp>
|
| 23 |
|
#include <boost/capy/io_task.hpp>
|
23 |
|
#include <boost/capy/io_task.hpp>
|
| 24 |
|
#include <boost/capy/task.hpp>
|
24 |
|
#include <boost/capy/task.hpp>
|
| 25 |
|
#include <boost/capy/write.hpp>
|
25 |
|
#include <boost/capy/write.hpp>
|
| 26 |
|
#include <boost/capy/io/any_buffer_source.hpp>
|
26 |
|
#include <boost/capy/io/any_buffer_source.hpp>
|
| 27 |
|
#include <boost/capy/io/any_buffer_sink.hpp>
|
27 |
|
#include <boost/capy/io/any_buffer_sink.hpp>
|
| 28 |
|
#include <boost/url/url_view.hpp>
|
28 |
|
#include <boost/url/url_view.hpp>
|
| 29 |
|
#include <boost/system/error_category.hpp>
|
29 |
|
#include <boost/system/error_category.hpp>
|
| 30 |
|
#include <boost/system/error_code.hpp>
|
30 |
|
#include <boost/system/error_code.hpp>
|
| 31 |
|
|
31 |
|
|
| 32 |
|
|
32 |
|
|
| 33 |
|
|
33 |
|
|
| 34 |
|
|
34 |
|
|
| 35 |
|
|
35 |
|
|
| 36 |
|
|
36 |
|
|
| 37 |
|
|
37 |
|
|
| 38 |
|
|
38 |
|
|
| 39 |
|
|
39 |
|
|
| 40 |
|
|
40 |
|
|
| 41 |
|
|
41 |
|
|
| 42 |
|
|
42 |
|
|
| 43 |
|
/** Directive values for route handler results.
|
43 |
|
/** Directive values for route handler results.
|
| 44 |
|
|
44 |
|
|
| 45 |
|
These values indicate how the router should proceed
|
45 |
|
These values indicate how the router should proceed
|
| 46 |
|
after a handler completes. Handlers return one of
|
46 |
|
after a handler completes. Handlers return one of
|
| 47 |
|
the predefined constants (@ref route_done, @ref route_next,
|
47 |
|
the predefined constants (@ref route_done, @ref route_next,
|
| 48 |
|
@ref route_next_route, @ref route_close) or an error code.
|
48 |
|
@ref route_next_route, @ref route_close) or an error code.
|
| 49 |
|
|
49 |
|
|
| 50 |
|
@see route_result, route_task
|
50 |
|
@see route_result, route_task
|
| 51 |
|
|
51 |
|
|
| 52 |
|
|
52 |
|
|
| 53 |
|
|
53 |
|
|
| 54 |
|
/// Handler completed successfully, response was sent
|
54 |
|
/// Handler completed successfully, response was sent
|
| 55 |
|
|
55 |
|
|
| 56 |
|
|
56 |
|
|
| 57 |
|
/// Handler declined, try next handler in the route
|
57 |
|
/// Handler declined, try next handler in the route
|
| 58 |
|
|
58 |
|
|
| 59 |
|
|
59 |
|
|
| 60 |
|
/// Handler declined, skip to next matching route
|
60 |
|
/// Handler declined, skip to next matching route
|
| 61 |
|
|
61 |
|
|
| 62 |
|
|
62 |
|
|
| 63 |
|
/// Handler requests connection closure
|
63 |
|
/// Handler requests connection closure
|
| 64 |
|
|
64 |
|
|
| 65 |
|
|
65 |
|
|
| 66 |
|
/// Handler encountered an error
|
66 |
|
/// Handler encountered an error
|
| 67 |
|
|
67 |
|
|
| 68 |
|
|
68 |
|
|
| 69 |
|
|
69 |
|
|
| 70 |
|
//------------------------------------------------
|
70 |
|
//------------------------------------------------
|
| 71 |
|
|
71 |
|
|
| 72 |
|
/** The result type returned by route handlers.
|
72 |
|
/** The result type returned by route handlers.
|
| 73 |
|
|
73 |
|
|
| 74 |
|
This class represents the outcome of a route handler.
|
74 |
|
This class represents the outcome of a route handler.
|
| 75 |
|
Handlers return this type to indicate how the router
|
75 |
|
Handlers return this type to indicate how the router
|
| 76 |
|
should proceed. Construct from a directive constant
|
76 |
|
should proceed. Construct from a directive constant
|
| 77 |
|
|
77 |
|
|
| 78 |
|
|
78 |
|
|
| 79 |
|
|
79 |
|
|
| 80 |
|
route_task my_handler(route_params& p)
|
80 |
|
route_task my_handler(route_params& p)
|
| 81 |
|
|
81 |
|
|
| 82 |
|
|
82 |
|
|
| 83 |
|
co_return route_next; // try next handler
|
83 |
|
co_return route_next; // try next handler
|
| 84 |
|
|
84 |
|
|
| 85 |
|
if(auto ec = process(p); ec)
|
85 |
|
if(auto ec = process(p); ec)
|
| 86 |
|
co_return ec; // return error
|
86 |
|
co_return ec; // return error
|
| 87 |
|
|
87 |
|
|
| 88 |
|
co_return route_done; // success
|
88 |
|
co_return route_done; // success
|
| 89 |
|
|
89 |
|
|
| 90 |
|
|
90 |
|
|
| 91 |
|
|
91 |
|
|
| 92 |
|
|
92 |
|
|
| 93 |
|
|
93 |
|
|
| 94 |
|
Use @ref what() to determine the directive, and
|
94 |
|
Use @ref what() to determine the directive, and
|
| 95 |
|
@ref error() to retrieve any error code:
|
95 |
|
@ref error() to retrieve any error code:
|
| 96 |
|
|
96 |
|
|
| 97 |
|
|
97 |
|
|
| 98 |
|
route_result rv = co_await handler(p);
|
98 |
|
route_result rv = co_await handler(p);
|
| 99 |
|
if(rv.what() == route_what::error)
|
99 |
|
if(rv.what() == route_what::error)
|
| 100 |
|
handle_error(rv.error());
|
100 |
|
handle_error(rv.error());
|
| 101 |
|
|
101 |
|
|
| 102 |
|
|
102 |
|
|
| 103 |
|
@see route_task, route_what, route_done, route_next
|
103 |
|
@see route_task, route_what, route_done, route_next
|
| 104 |
|
|
104 |
|
|
| 105 |
|
|
105 |
|
|
| 106 |
|
|
106 |
|
|
| 107 |
|
|
107 |
|
|
| 108 |
|
|
108 |
|
|
| 109 |
|
|
109 |
|
|
| 110 |
|
|
110 |
|
|
| 111 |
|
|
111 |
|
|
| 112 |
|
|
112 |
|
|
| 113 |
|
route_result(system::error_code ec);
|
113 |
|
route_result(system::error_code ec);
|
| 114 |
|
|
114 |
|
|
| 115 |
|
|
115 |
|
|
| 116 |
|
|
116 |
|
|
| 117 |
|
route_result() = default;
|
117 |
|
route_result() = default;
|
| 118 |
|
|
118 |
|
|
| 119 |
|
/** Construct from a directive constant.
|
119 |
|
/** Construct from a directive constant.
|
| 120 |
|
|
120 |
|
|
| 121 |
|
This constructor allows implicit conversion from
|
121 |
|
This constructor allows implicit conversion from
|
| 122 |
|
the predefined constants (@ref route_done, @ref route_next,
|
122 |
|
the predefined constants (@ref route_done, @ref route_next,
|
| 123 |
|
@ref route_next_route, @ref route_close).
|
123 |
|
@ref route_next_route, @ref route_close).
|
| 124 |
|
|
124 |
|
|
| 125 |
|
|
125 |
|
|
| 126 |
|
route_task handler(route_params& p)
|
126 |
|
route_task handler(route_params& p)
|
| 127 |
|
|
127 |
|
|
| 128 |
|
co_return route_done; // implicitly converts
|
128 |
|
co_return route_done; // implicitly converts
|
| 129 |
|
|
129 |
|
|
| 130 |
|
|
130 |
|
|
| 131 |
|
|
131 |
|
|
| 132 |
|
|
132 |
|
|
| 133 |
|
|
133 |
|
|
| 134 |
|
|
134 |
|
|
| 135 |
|
static_assert(W != route_what::error);
|
135 |
|
static_assert(W != route_what::error);
|
| 136 |
|
|
136 |
|
|
| 137 |
|
|
137 |
|
|
| 138 |
|
|
138 |
|
|
| 139 |
|
/** Return the directive for this result.
|
139 |
|
/** Return the directive for this result.
|
| 140 |
|
|
140 |
|
|
| 141 |
|
Call this to determine how the router should proceed:
|
141 |
|
Call this to determine how the router should proceed:
|
| 142 |
|
|
142 |
|
|
| 143 |
|
|
143 |
|
|
| 144 |
|
route_result rv = co_await handler(p);
|
144 |
|
route_result rv = co_await handler(p);
|
| 145 |
|
|
145 |
|
|
| 146 |
|
|
146 |
|
|
| 147 |
|
|
147 |
|
|
| 148 |
|
// response sent, done with request
|
148 |
|
// response sent, done with request
|
| 149 |
|
|
149 |
|
|
| 150 |
|
|
150 |
|
|
| 151 |
|
|
151 |
|
|
| 152 |
|
|
152 |
|
|
| 153 |
|
|
153 |
|
|
| 154 |
|
|
154 |
|
|
| 155 |
|
|
155 |
|
|
| 156 |
|
|
156 |
|
|
| 157 |
|
|
157 |
|
|
| 158 |
|
|
158 |
|
|
| 159 |
|
@return The directive value.
|
159 |
|
@return The directive value.
|
| 160 |
|
|
160 |
|
|
| 161 |
|
|
161 |
|
|
| 162 |
|
|
162 |
|
|
| 163 |
|
|
163 |
|
|
| 164 |
|
|
164 |
|
|
| 165 |
|
/** Return the error code, if any.
|
165 |
|
/** Return the error code, if any.
|
| 166 |
|
|
166 |
|
|
| 167 |
|
If @ref what() returns `route_what::error`, this
|
167 |
|
If @ref what() returns `route_what::error`, this
|
| 168 |
|
returns the underlying error code. Otherwise returns
|
168 |
|
returns the underlying error code. Otherwise returns
|
| 169 |
|
a default-constructed (non-failing) error code.
|
169 |
|
a default-constructed (non-failing) error code.
|
| 170 |
|
|
170 |
|
|
| 171 |
|
@return The error code, or a non-failing code.
|
171 |
|
@return The error code, or a non-failing code.
|
| 172 |
|
|
172 |
|
|
| 173 |
|
|
173 |
|
|
| 174 |
|
error() const noexcept ->
|
174 |
|
error() const noexcept ->
|
| 175 |
|
|
175 |
|
|
| 176 |
|
|
176 |
|
|
| 177 |
|
/** Return true if the result indicates an error.
|
177 |
|
/** Return true if the result indicates an error.
|
| 178 |
|
|
178 |
|
|
| 179 |
|
@return `true` if @ref what() equals `route_what::error`.
|
179 |
|
@return `true` if @ref what() equals `route_what::error`.
|
| 180 |
|
|
180 |
|
|
| 181 |
|
bool failed() const noexcept
|
181 |
|
bool failed() const noexcept
|
| 182 |
|
|
182 |
|
|
| 183 |
|
return what() == route_what::error;
|
183 |
|
return what() == route_what::error;
|
| 184 |
|
|
184 |
|
|
| 185 |
|
|
185 |
|
|
| 186 |
|
static constexpr route_result::what_t<route_what::done> route_done{};
|
186 |
|
static constexpr route_result::what_t<route_what::done> route_done{};
|
| 187 |
|
static constexpr route_result::what_t<route_what::next> route_next{};
|
187 |
|
static constexpr route_result::what_t<route_what::next> route_next{};
|
| 188 |
|
static constexpr route_result::what_t<route_what::next_route> route_next_route{};
|
188 |
|
static constexpr route_result::what_t<route_what::next_route> route_next_route{};
|
| 189 |
|
static constexpr route_result::what_t<route_what::close> route_close{};
|
189 |
|
static constexpr route_result::what_t<route_what::close> route_close{};
|
| 190 |
|
friend route_result route_error(system::error_code ec) noexcept;
|
190 |
|
friend route_result route_error(system::error_code ec) noexcept;
|
| 191 |
|
|
191 |
|
|
| 192 |
|
|
192 |
|
|
| 193 |
|
friend auto route_error(E e) noexcept ->
|
193 |
|
friend auto route_error(E e) noexcept ->
|
| 194 |
|
|
194 |
|
|
| 195 |
|
system::is_error_code_enum<E>::value,
|
195 |
|
system::is_error_code_enum<E>::value,
|
| 196 |
|
|
196 |
|
|
| 197 |
|
|
197 |
|
|
| 198 |
|
|
198 |
|
|
| 199 |
|
//------------------------------------------------
|
199 |
|
//------------------------------------------------
|
| 200 |
|
|
200 |
|
|
| 201 |
|
/** Handler completed successfully.
|
201 |
|
/** Handler completed successfully.
|
| 202 |
|
|
202 |
|
|
| 203 |
|
Return this from a handler to indicate the response
|
203 |
|
Return this from a handler to indicate the response
|
| 204 |
|
was sent and the request is complete:
|
204 |
|
was sent and the request is complete:
|
| 205 |
|
|
205 |
|
|
| 206 |
|
|
206 |
|
|
| 207 |
|
route_task handler(route_params& p)
|
207 |
|
route_task handler(route_params& p)
|
| 208 |
|
|
208 |
|
|
| 209 |
|
p.res.set(field::content_type, "text/plain");
|
209 |
|
p.res.set(field::content_type, "text/plain");
|
| 210 |
|
co_await p.send("Hello, World!");
|
210 |
|
co_await p.send("Hello, World!");
|
| 211 |
|
|
211 |
|
|
| 212 |
|
|
212 |
|
|
| 213 |
|
|
213 |
|
|
| 214 |
|
|
214 |
|
|
| 215 |
|
inline constexpr decltype(auto) route_done = route_result::route_done;
|
215 |
|
inline constexpr decltype(auto) route_done = route_result::route_done;
|
| 216 |
|
|
216 |
|
|
| 217 |
|
/** Handler declined, try next handler.
|
217 |
|
/** Handler declined, try next handler.
|
| 218 |
|
|
218 |
|
|
| 219 |
|
Return this from a handler to decline processing
|
219 |
|
Return this from a handler to decline processing
|
| 220 |
|
and allow the next handler in the route to try:
|
220 |
|
and allow the next handler in the route to try:
|
| 221 |
|
|
221 |
|
|
| 222 |
|
|
222 |
|
|
| 223 |
|
route_task auth_handler(route_params& p)
|
223 |
|
route_task auth_handler(route_params& p)
|
| 224 |
|
|
224 |
|
|
| 225 |
|
if(! p.req.exists(field::authorization))
|
225 |
|
if(! p.req.exists(field::authorization))
|
| 226 |
|
co_return route_next; // let another handler try
|
226 |
|
co_return route_next; // let another handler try
|
| 227 |
|
|
227 |
|
|
| 228 |
|
// process authenticated request...
|
228 |
|
// process authenticated request...
|
| 229 |
|
|
229 |
|
|
| 230 |
|
|
230 |
|
|
| 231 |
|
|
231 |
|
|
| 232 |
|
|
232 |
|
|
| 233 |
|
inline constexpr decltype(auto) route_next = route_result::route_next;
|
233 |
|
inline constexpr decltype(auto) route_next = route_result::route_next;
|
| 234 |
|
|
234 |
|
|
| 235 |
|
/** Handler declined, skip to next route.
|
235 |
|
/** Handler declined, skip to next route.
|
| 236 |
|
|
236 |
|
|
| 237 |
|
Return this from a handler to skip all remaining
|
237 |
|
Return this from a handler to skip all remaining
|
| 238 |
|
handlers in the current route and proceed to the
|
238 |
|
handlers in the current route and proceed to the
|
| 239 |
|
|
239 |
|
|
| 240 |
|
|
240 |
|
|
| 241 |
|
|
241 |
|
|
| 242 |
|
route_task version_check(route_params& p)
|
242 |
|
route_task version_check(route_params& p)
|
| 243 |
|
|
243 |
|
|
| 244 |
|
|
244 |
|
|
| 245 |
|
co_return route_next_route; // skip this route
|
245 |
|
co_return route_next_route; // skip this route
|
| 246 |
|
|
246 |
|
|
| 247 |
|
co_return route_next; // continue with this route
|
247 |
|
co_return route_next; // continue with this route
|
| 248 |
|
|
248 |
|
|
| 249 |
|
|
249 |
|
|
| 250 |
|
|
250 |
|
|
| 251 |
|
inline constexpr decltype(auto) route_next_route = route_result::route_next_route;
|
251 |
|
inline constexpr decltype(auto) route_next_route = route_result::route_next_route;
|
| 252 |
|
|
252 |
|
|
| 253 |
|
/** Handler requests connection closure.
|
253 |
|
/** Handler requests connection closure.
|
| 254 |
|
|
254 |
|
|
| 255 |
|
Return this from a handler to immediately close
|
255 |
|
Return this from a handler to immediately close
|
| 256 |
|
the connection without sending a response:
|
256 |
|
the connection without sending a response:
|
| 257 |
|
|
257 |
|
|
| 258 |
|
|
258 |
|
|
| 259 |
|
route_task ban_check(route_params& p)
|
259 |
|
route_task ban_check(route_params& p)
|
| 260 |
|
|
260 |
|
|
| 261 |
|
if(is_banned(p.req.remote_address()))
|
261 |
|
if(is_banned(p.req.remote_address()))
|
| 262 |
|
co_return route_close; // drop connection
|
262 |
|
co_return route_close; // drop connection
|
| 263 |
|
|
263 |
|
|
| 264 |
|
|
264 |
|
|
| 265 |
|
|
265 |
|
|
| 266 |
|
|
266 |
|
|
| 267 |
|
|
267 |
|
|
| 268 |
|
inline constexpr decltype(auto) route_close = route_result::route_close;
|
268 |
|
inline constexpr decltype(auto) route_close = route_result::route_close;
|
| 269 |
|
|
269 |
|
|
| 270 |
|
/** Construct from an error code.
|
270 |
|
/** Construct from an error code.
|
| 271 |
|
|
271 |
|
|
| 272 |
|
Use this constructor to return an error from a handler.
|
272 |
|
Use this constructor to return an error from a handler.
|
| 273 |
|
The error code must represent a failure condition.
|
273 |
|
The error code must represent a failure condition.
|
| 274 |
|
|
274 |
|
|
| 275 |
|
@param ec The error code to return.
|
275 |
|
@param ec The error code to return.
|
| 276 |
|
|
276 |
|
|
| 277 |
|
@throw std::invalid_argument if `!ec` (non-failing code).
|
277 |
|
@throw std::invalid_argument if `!ec` (non-failing code).
|
| 278 |
|
|
278 |
|
|
| 279 |
|
inline route_result route_error(system::error_code ec) noexcept
|
279 |
|
inline route_result route_error(system::error_code ec) noexcept
|
| 280 |
|
|
280 |
|
|
| 281 |
|
|
281 |
|
|
| 282 |
|
|
282 |
|
|
| 283 |
|
|
283 |
|
|
| 284 |
|
/** Construct from an error enum.
|
284 |
|
/** Construct from an error enum.
|
| 285 |
|
|
285 |
|
|
| 286 |
|
Use this overload to return an error from a handler
|
286 |
|
Use this overload to return an error from a handler
|
| 287 |
|
using any type satisfying `is_error_code_enum`.
|
287 |
|
using any type satisfying `is_error_code_enum`.
|
| 288 |
|
|
288 |
|
|
| 289 |
|
@param e The error enum value to return.
|
289 |
|
@param e The error enum value to return.
|
| 290 |
|
|
290 |
|
|
| 291 |
|
|
291 |
|
|
| 292 |
|
auto route_error(E e) noexcept ->
|
292 |
|
auto route_error(E e) noexcept ->
|
| 293 |
|
|
293 |
|
|
| 294 |
|
system::is_error_code_enum<E>::value,
|
294 |
|
system::is_error_code_enum<E>::value,
|
| 295 |
|
|
295 |
|
|
| 296 |
|
|
296 |
|
|
| 297 |
|
return route_result(make_error_code(e));
|
297 |
|
return route_result(make_error_code(e));
|
| 298 |
|
|
298 |
|
|
| 299 |
|
|
299 |
|
|
| 300 |
|
//------------------------------------------------
|
300 |
|
//------------------------------------------------
|
| 301 |
|
|
301 |
|
|
| 302 |
|
/** Convenience alias for route handler return type.
|
302 |
|
/** Convenience alias for route handler return type.
|
| 303 |
|
|
303 |
|
|
| 304 |
|
Route handlers are coroutines that return a @ref route_result
|
304 |
|
Route handlers are coroutines that return a @ref route_result
|
| 305 |
|
indicating how the router should proceed. This alias simplifies
|
305 |
|
indicating how the router should proceed. This alias simplifies
|
| 306 |
|
|
306 |
|
|
| 307 |
|
|
307 |
|
|
| 308 |
|
|
308 |
|
|
| 309 |
|
route_task my_handler(route_params& p)
|
309 |
|
route_task my_handler(route_params& p)
|
| 310 |
|
|
310 |
|
|
| 311 |
|
|
311 |
|
|
| 312 |
|
|
312 |
|
|
| 313 |
|
|
313 |
|
|
| 314 |
|
|
314 |
|
|
| 315 |
|
route_task auth_middleware(route_params& p)
|
315 |
|
route_task auth_middleware(route_params& p)
|
| 316 |
|
|
316 |
|
|
| 317 |
|
|
317 |
|
|
| 318 |
|
|
318 |
|
|
| 319 |
|
p.res.set_status(status::unauthorized);
|
319 |
|
p.res.set_status(status::unauthorized);
|
| 320 |
|
|
320 |
|
|
| 321 |
|
|
321 |
|
|
| 322 |
|
|
322 |
|
|
| 323 |
|
co_return route_next; // continue to next handler
|
323 |
|
co_return route_next; // continue to next handler
|
| 324 |
|
|
324 |
|
|
| 325 |
|
|
325 |
|
|
| 326 |
|
|
326 |
|
|
| 327 |
|
@see route_result, route_params
|
327 |
|
@see route_result, route_params
|
| 328 |
|
|
328 |
|
|
| 329 |
|
using route_task = capy::task<route_result>;
|
329 |
|
using route_task = capy::task<route_result>;
|
| 330 |
|
|
330 |
|
|
| 331 |
|
//------------------------------------------------
|
331 |
|
//------------------------------------------------
|
| 332 |
|
|
332 |
|
|
| 333 |
|
template<class, class> class router;
|
333 |
|
template<class, class> class router;
|
| 334 |
|
|
334 |
|
|
| 335 |
|
|
335 |
|
|
| 336 |
|
|
336 |
|
|
| 337 |
|
struct route_params_access;
|
337 |
|
struct route_params_access;
|
| 338 |
|
|
338 |
|
|
| 339 |
|
|
339 |
|
|
| 340 |
|
struct route_params_base_privates
|
340 |
|
struct route_params_base_privates
|
| 341 |
|
|
341 |
|
|
| 342 |
|
|
342 |
|
|
| 343 |
|
std::string decoded_path_;
|
343 |
|
std::string decoded_path_;
|
| 344 |
|
|
344 |
|
|
| 345 |
|
|
345 |
|
|
| 346 |
|
|
346 |
|
|
| 347 |
|
|
347 |
|
|
| 348 |
|
|
348 |
|
|
| 349 |
|
|
349 |
|
|
| 350 |
|
bool addedSlash_ = false;
|
350 |
|
bool addedSlash_ = false;
|
| 351 |
|
bool case_sensitive = false;
|
351 |
|
bool case_sensitive = false;
|
| 352 |
|
|
352 |
|
|
| 353 |
|
|
353 |
|
|
| 354 |
|
|
354 |
|
|
| 355 |
|
|
355 |
|
|
| 356 |
|
|
356 |
|
|
| 357 |
|
|
357 |
|
|
| 358 |
|
//------------------------------------------------
|
358 |
|
//------------------------------------------------
|
| 359 |
|
|
359 |
|
|
| 360 |
|
/** Parameters object for HTTP route handlers.
|
360 |
|
/** Parameters object for HTTP route handlers.
|
| 361 |
|
|
361 |
|
|
| 362 |
|
This structure holds all the context needed for a route
|
362 |
|
This structure holds all the context needed for a route
|
| 363 |
|
handler to process an HTTP request and generate a response.
|
363 |
|
handler to process an HTTP request and generate a response.
|
| 364 |
|
|
364 |
|
|
| 365 |
|
|
365 |
|
|
| 366 |
|
|
366 |
|
|
| 367 |
|
route_task my_handler(route_params& p)
|
367 |
|
route_task my_handler(route_params& p)
|
| 368 |
|
|
368 |
|
|
| 369 |
|
p.res.set(field::content_type, "text/plain");
|
369 |
|
p.res.set(field::content_type, "text/plain");
|
| 370 |
|
co_await p.send("Hello, World!");
|
370 |
|
co_await p.send("Hello, World!");
|
| 371 |
|
|
371 |
|
|
| 372 |
|
|
372 |
|
|
| 373 |
|
|
373 |
|
|
| 374 |
|
|
374 |
|
|
| 375 |
|
@see route_task, route_result
|
375 |
|
@see route_task, route_result
|
| 376 |
|
|
376 |
|
|
| 377 |
|
class BOOST_HTTP_SYMBOL_VISIBLE
|
377 |
|
class BOOST_HTTP_SYMBOL_VISIBLE
|
| 378 |
|
|
378 |
|
|
| 379 |
|
|
379 |
|
|
| 380 |
|
detail::route_params_base_privates priv_;
|
380 |
|
detail::route_params_base_privates priv_;
|
| 381 |
|
|
381 |
|
|
| 382 |
|
|
382 |
|
|
| 383 |
|
|
383 |
|
|
| 384 |
|
|
384 |
|
|
| 385 |
|
/** Return true if the request method matches `m`
|
385 |
|
/** Return true if the request method matches `m`
|
| 386 |
|
|
386 |
|
|
| 387 |
|
|
387 |
|
|
| 388 |
|
http::method m) const noexcept
|
388 |
|
http::method m) const noexcept
|
| 389 |
|
|
389 |
|
|
| 390 |
|
|
390 |
|
|
| 391 |
|
|
391 |
|
|
| 392 |
|
|
392 |
|
|
| 393 |
|
/** Return true if the request method matches `s`
|
393 |
|
/** Return true if the request method matches `s`
|
| 394 |
|
|
394 |
|
|
| 395 |
|
|
395 |
|
|
| 396 |
|
|
396 |
|
|
| 397 |
|
core::string_view s) const noexcept;
|
397 |
|
core::string_view s) const noexcept;
|
| 398 |
|
|
398 |
|
|
| 399 |
|
/** The mount path of the current router
|
399 |
|
/** The mount path of the current router
|
| 400 |
|
|
400 |
|
|
| 401 |
|
This is the portion of the request path
|
401 |
|
This is the portion of the request path
|
| 402 |
|
which was matched to select the handler.
|
402 |
|
which was matched to select the handler.
|
| 403 |
|
The remaining portion is available in
|
403 |
|
The remaining portion is available in
|
| 404 |
|
|
404 |
|
|
| 405 |
|
|
405 |
|
|
| 406 |
|
core::string_view base_path;
|
406 |
|
core::string_view base_path;
|
| 407 |
|
|
407 |
|
|
| 408 |
|
/** The current pathname, relative to the base path
|
408 |
|
/** The current pathname, relative to the base path
|
| 409 |
|
|
409 |
|
|
| 410 |
|
|
410 |
|
|
| 411 |
|
|
411 |
|
|
| 412 |
|
/** Captured route parameters
|
412 |
|
/** Captured route parameters
|
| 413 |
|
|
413 |
|
|
| 414 |
|
Contains name-value pairs extracted from the path
|
414 |
|
Contains name-value pairs extracted from the path
|
| 415 |
|
by matching :param and *wildcard tokens.
|
415 |
|
by matching :param and *wildcard tokens.
|
| 416 |
|
|
416 |
|
|
| 417 |
|
std::vector<std::pair<std::string, std::string>> params;
|
417 |
|
std::vector<std::pair<std::string, std::string>> params;
|
| 418 |
|
|
418 |
|
|
| 419 |
|
/// The complete request target
|
419 |
|
/// The complete request target
|
| 420 |
|
|
420 |
|
|
| 421 |
|
|
421 |
|
|
| 422 |
|
|
422 |
|
|
| 423 |
|
|
423 |
|
|
| 424 |
|
|
424 |
|
|
| 425 |
|
|
425 |
|
|
| 426 |
|
|
426 |
|
|
| 427 |
|
|
427 |
|
|
| 428 |
|
/// Provides access to the request body
|
428 |
|
/// Provides access to the request body
|
| 429 |
|
capy::any_buffer_source req_body;
|
429 |
|
capy::any_buffer_source req_body;
|
| 430 |
|
|
430 |
|
|
| 431 |
|
/// Provides access to the response body
|
431 |
|
/// Provides access to the response body
|
| 432 |
|
capy::any_buffer_sink res_body;
|
432 |
|
capy::any_buffer_sink res_body;
|
| 433 |
|
|
433 |
|
|
| 434 |
|
/// Arbitrary per-route data
|
434 |
|
/// Arbitrary per-route data
|
| 435 |
|
http::datastore route_data;
|
435 |
|
http::datastore route_data;
|
| 436 |
|
|
436 |
|
|
| 437 |
|
/// Arbitrary per-session data
|
437 |
|
/// Arbitrary per-session data
|
| 438 |
|
http::datastore session_data;
|
438 |
|
http::datastore session_data;
|
| 439 |
|
|
439 |
|
|
| 440 |
|
BOOST_HTTP_DECL ~route_params();
|
440 |
|
BOOST_HTTP_DECL ~route_params();
|
| 441 |
|
BOOST_HTTP_DECL void reset();
|
441 |
|
BOOST_HTTP_DECL void reset();
|
| 442 |
|
BOOST_HTTP_DECL route_params& status(http::status code);
|
442 |
|
BOOST_HTTP_DECL route_params& status(http::status code);
|
| 443 |
|
|
443 |
|
|
| 444 |
|
/** Send the response with an optional body.
|
444 |
|
/** Send the response with an optional body.
|
| 445 |
|
|
445 |
|
|
| 446 |
|
BOOST_HTTP_DECL capy::io_task<> send(std::string_view body = {});
|
446 |
|
BOOST_HTTP_DECL capy::io_task<> send(std::string_view body = {});
|
| 447 |
|
|
447 |
|
|
| 448 |
|
|
448 |
|
|
| 449 |
|
|
449 |
|
|
| 450 |
|
|
450 |
|
|
| 451 |
|
friend class detail::router_base;
|
451 |
|
friend class detail::router_base;
|
| 452 |
|
friend struct detail::route_params_access;
|
452 |
|
friend struct detail::route_params_access;
|
| 453 |
|
|
453 |
|
|
| 454 |
|
|
454 |
|
|
| 455 |
|
route_params const&) = delete;
|
455 |
|
route_params const&) = delete;
|
| 456 |
|
|
456 |
|
|
| 457 |
|
|
457 |
|
|
| 458 |
|
|
458 |
|
|
| 459 |
|
|
459 |
|
|
| 460 |
|
|
460 |
|
|
| 461 |
|
std::vector<std::pair<std::string, std::string>> params_;
|
461 |
|
std::vector<std::pair<std::string, std::string>> params_;
|
| 462 |
|
|
462 |
|
|
| 463 |
|
|
463 |
|
|
| 464 |
|
|
464 |
|
|
| 465 |
|
|
465 |
|
|
| 466 |
|
|
466 |
|
|
| 467 |
|
|
467 |
|
|
| 468 |
|
|
468 |
|
|
| 469 |
|
|
469 |
|
|
| 470 |
|
|
470 |
|
|
| 471 |
|
|
471 |
|
|
| 472 |
|
p.base_path.size() + n_ };
|
472 |
|
p.base_path.size() + n_ };
|
| 473 |
|
|
473 |
|
|
| 474 |
|
|
474 |
|
|
| 475 |
|
p.path.remove_prefix(n_);
|
475 |
|
p.path.remove_prefix(n_);
|
| 476 |
|
|
476 |
|
|
| 477 |
|
|
477 |
|
|
| 478 |
|
|
478 |
|
|
| 479 |
|
|
479 |
|
|
| 480 |
|
p.path = { p.priv_.decoded_path_.data() +
|
480 |
|
p.path = { p.priv_.decoded_path_.data() +
|
| 481 |
|
p.priv_.decoded_path_.size() - 1, 1};
|
481 |
|
p.priv_.decoded_path_.size() - 1, 1};
|
| 482 |
|
BOOST_ASSERT(p.path == "/");
|
482 |
|
BOOST_ASSERT(p.path == "/");
|
| 483 |
|
|
483 |
|
|
| 484 |
|
|
484 |
|
|
| 485 |
|
|
485 |
|
|
| 486 |
|
|
486 |
|
|
| 487 |
|
|
487 |
|
|
| 488 |
|
|
488 |
|
|
| 489 |
|
|
489 |
|
|
| 490 |
|
|
490 |
|
|
| 491 |
|
|
491 |
|
|
| 492 |
|
p.priv_.decoded_path_.data() +
|
492 |
|
p.priv_.decoded_path_.data() +
|
| 493 |
|
p.priv_.decoded_path_.size() - 1)
|
493 |
|
p.priv_.decoded_path_.size() - 1)
|
| 494 |
|
|
494 |
|
|
| 495 |
|
|
495 |
|
|
| 496 |
|
|
496 |
|
|
| 497 |
|
|
497 |
|
|
| 498 |
|
|
498 |
|
|
| 499 |
|
|
499 |
|
|
| 500 |
|
p.base_path.remove_suffix(n_);
|
500 |
|
p.base_path.remove_suffix(n_);
|
| 501 |
|
|
501 |
|
|
| 502 |
|
|
502 |
|
|
| 503 |
|
|
503 |
|
|
| 504 |
|
|
504 |
|
|
| 505 |
|
|
505 |
|
|
| 506 |
|
|
506 |
|
|
| 507 |
|
std::size_t n_ = 0; // chars moved from path to base_path
|
507 |
|
std::size_t n_ = 0; // chars moved from path to base_path
|
| 508 |
|
|
508 |
|
|
| 509 |
|
|
509 |
|
|
| 510 |
|
//------------------------------------------------
|
510 |
|
//------------------------------------------------
|
| 511 |
|
|
511 |
|
|
| 512 |
|
|
512 |
|
|
| 513 |
|
|
513 |
|
|
| 514 |
|
template<class H, class... Args>
|
514 |
|
template<class H, class... Args>
|
| 515 |
|
concept returns_route_task = std::same_as<
|
515 |
|
concept returns_route_task = std::same_as<
|
| 516 |
|
std::invoke_result_t<H, Args...>, route_task>;
|
516 |
|
std::invoke_result_t<H, Args...>, route_task>;
|
| 517 |
|
|
517 |
|
|
| 518 |
|
struct route_params_access
|
518 |
|
struct route_params_access
|
| 519 |
|
|
519 |
|
|
| 520 |
|
|
520 |
|
|
| 521 |
|
|
521 |
|
|
| 522 |
|
route_params_base_privates& operator*() const noexcept
|
522 |
|
route_params_base_privates& operator*() const noexcept
|
| 523 |
|
|
523 |
|
|
| 524 |
|
|
524 |
|
|
| 525 |
|
|
525 |
|
|
| 526 |
|
|
526 |
|
|
| 527 |
|
route_params_base_privates* operator->() const noexcept
|
527 |
|
route_params_base_privates* operator->() const noexcept
|
| 528 |
|
|
528 |
|
|
| 529 |
|
|
529 |
|
|
| 530 |
|
|
530 |
|
|
| 531 |
|
|
531 |
|
|
| 532 |
|
|
532 |
|
|
| 533 |
|
|
533 |
|
|
| 534 |
|
|
534 |
|
|
| 535 |
|
|
535 |
|
|
| 536 |
|
|
536 |
|
|
| 537 |
|
|
537 |
|
|
| 538 |
|
|
538 |
|
|