/src/pistache/src/server/router.cc
Line | Count | Source |
1 | | /* |
2 | | * SPDX-FileCopyrightText: 2016 Mathieu Stefani |
3 | | * |
4 | | * SPDX-License-Identifier: Apache-2.0 |
5 | | */ |
6 | | |
7 | | /* router.cc |
8 | | Mathieu Stefani, 05 janvier 2016 |
9 | | |
10 | | Rest routing implementation |
11 | | */ |
12 | | |
13 | | #include <algorithm> |
14 | | |
15 | | #include <pistache/description.h> |
16 | | #include <pistache/router.h> |
17 | | |
18 | | namespace Pistache::Rest |
19 | | { |
20 | | |
21 | | Request::Request(Http::Request request, std::vector<TypedParam>&& params, |
22 | | std::vector<TypedParam>&& splats) |
23 | 0 | : Http::Request(std::move(request)) |
24 | 0 | , params_(std::move(params)) |
25 | 0 | , splats_(std::move(splats)) |
26 | 0 | { } |
27 | | |
28 | | bool Request::hasParam(const std::string& name) const |
29 | 0 | { |
30 | 0 | auto it = std::find_if( |
31 | 0 | params_.begin(), params_.end(), |
32 | 0 | [&](const TypedParam& param) { return param.name() == name; }); |
33 | |
|
34 | 0 | return it != std::end(params_); |
35 | 0 | } |
36 | | |
37 | | TypedParam Request::param(const std::string& name) const |
38 | 0 | { |
39 | 0 | auto it = std::find_if( |
40 | 0 | params_.begin(), params_.end(), |
41 | 0 | [&](const TypedParam& param) { return param.name() == name; }); |
42 | |
|
43 | 0 | if (it == std::end(params_)) |
44 | 0 | { |
45 | 0 | throw std::runtime_error("Unknown parameter"); |
46 | 0 | } |
47 | | |
48 | 0 | return *it; |
49 | 0 | } |
50 | | |
51 | | TypedParam Request::splatAt(size_t index) const |
52 | 0 | { |
53 | 0 | if (index >= splats_.size()) |
54 | 0 | { |
55 | 0 | throw std::out_of_range("Request splat index out of range"); |
56 | 0 | } |
57 | 0 | return splats_[index]; |
58 | 0 | } |
59 | | |
60 | 0 | std::vector<TypedParam> Request::splat() const { return splats_; } |
61 | | |
62 | | SegmentTreeNode::SegmentTreeNode() |
63 | 2.27k | : resource_ref_() |
64 | 2.27k | , fixed_() |
65 | 2.27k | , param_() |
66 | 2.27k | , optional_() |
67 | 2.27k | , splat_(nullptr) |
68 | 2.27k | , route_(nullptr) |
69 | 2.27k | { |
70 | 2.27k | std::shared_ptr<char> ptr(new char[1], std::default_delete<char[]>()); |
71 | 2.27k | resource_ref_.swap(ptr); |
72 | 2.27k | } |
73 | | |
74 | | SegmentTreeNode::SegmentTreeNode(const std::shared_ptr<char>& resourceReference) |
75 | 286k | : resource_ref_(resourceReference) |
76 | 286k | , fixed_() |
77 | 286k | , param_() |
78 | 286k | , optional_() |
79 | 286k | , splat_(nullptr) |
80 | 286k | , route_(nullptr) |
81 | 286k | { } |
82 | | |
83 | | SegmentTreeNode::SegmentType |
84 | | SegmentTreeNode::getSegmentType(const std::string_view& fragment) |
85 | 381k | { |
86 | 381k | if (fragment.empty()) |
87 | 26.0k | return SegmentType::Fixed; |
88 | | |
89 | 355k | auto optpos = fragment.find('?'); |
90 | 355k | if (fragment[0] == ':') |
91 | 172k | { |
92 | 172k | if (optpos != std::string_view::npos) |
93 | 4.48k | { |
94 | 4.48k | if (optpos != fragment.length() - 1) |
95 | 1.11k | { |
96 | 1.11k | throw std::runtime_error("? should be at the end of the string"); |
97 | 1.11k | } |
98 | 3.36k | return SegmentType::Optional; |
99 | 4.48k | } |
100 | 168k | return SegmentType::Param; |
101 | 172k | } |
102 | 183k | else if (fragment[0] == '*') |
103 | 11.5k | { |
104 | 11.5k | if (fragment.length() > 1) |
105 | 778 | { |
106 | 778 | throw std::runtime_error("Invalid splat parameter"); |
107 | 778 | } |
108 | 10.7k | return SegmentType::Splat; |
109 | 11.5k | } |
110 | | |
111 | 171k | if (optpos != std::string_view::npos) |
112 | 5.66k | { |
113 | 5.66k | throw std::runtime_error( |
114 | 5.66k | "Only optional parameters are currently supported"); |
115 | 5.66k | } |
116 | | |
117 | 165k | return SegmentType::Fixed; |
118 | 171k | } |
119 | | |
120 | | std::string SegmentTreeNode::sanitizeResource(const std::string& path) |
121 | 109k | { |
122 | 109k | std::string dup; |
123 | 109k | dup.reserve(path.size()); |
124 | | |
125 | 109k | for (const char c : path) |
126 | 48.2M | { |
127 | 48.2M | if (c != '/' || dup.empty() || dup.back() != '/') |
128 | 48.1M | dup.push_back(c); |
129 | 48.2M | } |
130 | | |
131 | 109k | if (dup[dup.length() - 1] == '/') |
132 | 7.74k | { |
133 | 7.74k | return dup.substr(1, dup.length() - 2); |
134 | 7.74k | } |
135 | 101k | return dup.substr(1); |
136 | 109k | } |
137 | | |
138 | | void SegmentTreeNode::addRoute( |
139 | | const std::string_view& path, const Route::Handler& handler, |
140 | | const std::shared_ptr<char>& resource_reference) |
141 | 404k | { |
142 | | // recursion to correct path segment |
143 | 404k | if (!path.empty()) |
144 | 354k | { |
145 | 354k | const auto segment_delimiter = path.find('/'); |
146 | | // current segment value |
147 | 354k | auto current_segment = path.substr(0, segment_delimiter); |
148 | | // complete child path (path without this segment) |
149 | | // if no '/' was found, it means that it is a leaf resource |
150 | 354k | const auto lower_path = (segment_delimiter == std::string_view::npos) |
151 | 354k | ? std::string_view { nullptr, 0 } |
152 | 354k | : path.substr(segment_delimiter + 1); |
153 | | |
154 | 354k | std::unordered_map<std::string_view, std::shared_ptr<SegmentTreeNode>>* collection = nullptr; |
155 | 354k | const auto fragmentType = getSegmentType(current_segment); |
156 | 354k | switch (fragmentType) |
157 | 354k | { |
158 | 173k | case SegmentType::Fixed: |
159 | 173k | collection = &fixed_; |
160 | 173k | break; |
161 | 162k | case SegmentType::Param: |
162 | 162k | collection = ¶m_; |
163 | 162k | break; |
164 | 3.13k | case SegmentType::Optional: |
165 | | // remove the trailing question mark |
166 | 3.13k | current_segment = current_segment.substr(0, current_segment.length() - 1); |
167 | 3.13k | collection = &optional_; |
168 | 3.13k | break; |
169 | 7.30k | case SegmentType::Splat: |
170 | 7.30k | if (splat_ == nullptr) |
171 | 3.89k | { |
172 | 3.89k | splat_ = std::make_shared<SegmentTreeNode>(resource_reference); |
173 | 3.89k | } |
174 | 7.30k | splat_->addRoute(lower_path, handler, resource_reference); |
175 | 7.30k | return; |
176 | 354k | } |
177 | | |
178 | | // if the segment tree nodes for the lower path does not exist |
179 | 339k | if (collection->count(current_segment) == 0) |
180 | 282k | { |
181 | | // first create it |
182 | 282k | collection->insert(std::make_pair( |
183 | 282k | current_segment, |
184 | 282k | std::make_shared<SegmentTreeNode>(resource_reference))); |
185 | 282k | } |
186 | 339k | collection->at(current_segment) |
187 | 339k | ->addRoute(lower_path, handler, resource_reference); |
188 | 339k | } |
189 | 50.5k | else |
190 | 50.5k | { // current path segment requested |
191 | 50.5k | if (route_ != nullptr) |
192 | 21.1k | throw std::runtime_error("Requested route already exist."); |
193 | 29.4k | route_ = std::make_shared<Route>(handler); |
194 | 29.4k | } |
195 | 404k | } |
196 | | |
197 | | bool Pistache::Rest::SegmentTreeNode::removeRoute( |
198 | | const std::string_view& path) |
199 | 40.9k | { |
200 | | // recursion to correct path segment |
201 | 40.9k | if (!path.empty()) |
202 | 27.6k | { |
203 | 27.6k | const auto segment_delimiter = path.find('/'); |
204 | | // current segment value |
205 | 27.6k | auto current_segment = path.substr(0, segment_delimiter); |
206 | | // complete child path (path without this segment) |
207 | | // if no '/' was found, it means that it is a leaf resource |
208 | 27.6k | const auto lower_path = (segment_delimiter == std::string_view::npos) |
209 | 27.6k | ? std::string_view { nullptr, 0 } |
210 | 27.6k | : path.substr(segment_delimiter + 1); |
211 | | |
212 | 27.6k | std::unordered_map<std::string_view, std::shared_ptr<SegmentTreeNode>>* collection = nullptr; |
213 | 27.6k | auto fragmentType = getSegmentType(current_segment); |
214 | 27.6k | switch (fragmentType) |
215 | 27.6k | { |
216 | 17.9k | case SegmentType::Fixed: |
217 | 17.9k | collection = &fixed_; |
218 | 17.9k | break; |
219 | 5.42k | case SegmentType::Param: |
220 | 5.42k | collection = ¶m_; |
221 | 5.42k | break; |
222 | 233 | case SegmentType::Optional: |
223 | | // remove the trailing question mark |
224 | 233 | current_segment = current_segment.substr(0, current_segment.length() - 1); |
225 | 233 | collection = &optional_; |
226 | 233 | break; |
227 | 3.48k | case SegmentType::Splat: |
228 | 3.48k | if (!splat_) |
229 | 1.19k | throw std::runtime_error("Requested route does not exist."); |
230 | 2.29k | return splat_->removeRoute(lower_path); |
231 | 27.6k | } |
232 | | |
233 | 23.6k | try |
234 | 23.6k | { |
235 | 23.6k | const bool removable = collection->at(current_segment)->removeRoute(lower_path); |
236 | 23.6k | if (removable) |
237 | 8.15k | { |
238 | 8.15k | collection->erase(current_segment); |
239 | 8.15k | } |
240 | 23.6k | } |
241 | 23.6k | catch (const std::out_of_range&) |
242 | 23.6k | { |
243 | 7.17k | throw std::runtime_error("Requested route does not exist."); |
244 | 7.17k | } |
245 | 23.6k | } |
246 | 13.2k | else |
247 | 13.2k | { // current leaf requested |
248 | 13.2k | route_.reset(); |
249 | 13.2k | } |
250 | 23.7k | return fixed_.empty() && param_.empty() && optional_.empty() && splat_ == nullptr && route_ == nullptr; |
251 | 40.9k | } |
252 | | |
253 | | std::tuple<std::shared_ptr<Route>, std::vector<TypedParam>, |
254 | | std::vector<TypedParam>> |
255 | | Pistache::Rest::SegmentTreeNode::findRoute( |
256 | | const std::string_view& path, std::vector<TypedParam>& params, |
257 | | std::vector<TypedParam>& splats) const |
258 | 678k | { |
259 | | // recursion to correct path segment |
260 | 678k | if (!path.empty()) |
261 | 656k | { |
262 | 656k | const auto segment_delimiter = path.find('/'); |
263 | | // current segment value |
264 | 656k | auto current_segment = path.substr(0, segment_delimiter); |
265 | | // complete child path (path without this segment) |
266 | | // if no '/' was found, it means that it is a leaf resource |
267 | 656k | const auto lower_path = (segment_delimiter == std::string_view::npos) |
268 | 656k | ? std::string_view { nullptr, 0 } |
269 | 656k | : path.substr(segment_delimiter + 1); |
270 | | |
271 | | // Check if it is a fixed route |
272 | 656k | if (fixed_.count(current_segment) != 0) |
273 | 2.18k | { |
274 | 2.18k | auto result = fixed_.at(current_segment)->findRoute(lower_path, params, splats); |
275 | 2.18k | auto route = std::get<0>(result); |
276 | 2.18k | if (route != nullptr) |
277 | 917 | return result; |
278 | 2.18k | } |
279 | | |
280 | | // Check if it is a path param |
281 | 655k | for (const auto& param : param_) |
282 | 618k | { |
283 | 618k | std::string para_name { param.first.data(), param.first.length() }; |
284 | 618k | std::string para_val { current_segment.data(), current_segment.length() }; |
285 | 618k | params.emplace_back(para_name, para_val); |
286 | 618k | auto result = param.second->findRoute(lower_path, params, splats); |
287 | 618k | auto route = std::get<0>(result); |
288 | 618k | if (route != nullptr) |
289 | 2.50k | return result; |
290 | 616k | params.pop_back(); |
291 | 616k | } |
292 | | |
293 | | // Check if it is an optional path param |
294 | 653k | for (const auto& optional : optional_) |
295 | 17.5k | { |
296 | 17.5k | std::string opt_name { optional.first.data(), optional.first.length() }; |
297 | 17.5k | std::string opt_val { current_segment.data(), current_segment.length() }; |
298 | 17.5k | params.emplace_back(opt_name, opt_val); |
299 | 17.5k | auto result = optional.second->findRoute(lower_path, params, splats); |
300 | | |
301 | 17.5k | auto route = std::get<0>(result); |
302 | 17.5k | if (route != nullptr) |
303 | 850 | return result; |
304 | 16.6k | params.pop_back(); |
305 | | // try to find a route for lower path assuming that |
306 | | // this optional path param is not present |
307 | 16.6k | result = optional.second->findRoute(lower_path, params, splats); |
308 | | |
309 | 16.6k | route = std::get<0>(result); |
310 | 16.6k | if (route != nullptr) |
311 | 0 | return result; |
312 | 16.6k | } |
313 | | |
314 | | // Check if it is a splat |
315 | 652k | if (splat_ != nullptr) |
316 | 11.5k | { |
317 | 11.5k | std::string splat { current_segment.data(), current_segment.length() }; |
318 | 11.5k | splats.emplace_back(splat, splat); |
319 | 11.5k | auto result = splat_->findRoute(lower_path, params, splats); |
320 | | |
321 | 11.5k | auto route = std::get<0>(result); |
322 | 11.5k | if (route != nullptr) |
323 | 1.32k | return result; |
324 | 10.2k | splats.pop_back(); |
325 | 10.2k | } |
326 | | // Requested route does not exists |
327 | 650k | return std::make_tuple(nullptr, std::vector<TypedParam>(), |
328 | 650k | std::vector<TypedParam>()); |
329 | 652k | } |
330 | 21.5k | else |
331 | 21.5k | { // current leaf requested, or empty final optional |
332 | 21.5k | if (!optional_.empty()) |
333 | 2.33k | { |
334 | | // in case of more than one optional at this point, as it is an |
335 | | // ambiguity, it is resolved by using the first optional |
336 | 2.33k | auto optional = optional_.begin(); |
337 | | // std::string opt {optional->first.data(), optional->first.length()}; |
338 | 2.33k | return optional->second->findRoute(path, params, splats); |
339 | 2.33k | } |
340 | 19.2k | else if (route_ == nullptr) |
341 | 15.4k | { |
342 | | // if we are here but route is null, we reached this point |
343 | | // trying to parse an optional, that was missing |
344 | 15.4k | return std::make_tuple(nullptr, std::vector<TypedParam>(), |
345 | 15.4k | std::vector<TypedParam>()); |
346 | 15.4k | } |
347 | 3.82k | else |
348 | 3.82k | { |
349 | 3.82k | return std::make_tuple(route_, std::move(params), std::move(splats)); |
350 | 3.82k | } |
351 | 21.5k | } |
352 | 678k | } |
353 | | |
354 | | std::tuple<std::shared_ptr<Route>, std::vector<TypedParam>, |
355 | | std::vector<TypedParam>> |
356 | | Pistache::Rest::SegmentTreeNode::findRoute(const std::string_view& path) const |
357 | 9.01k | { |
358 | 9.01k | std::vector<TypedParam> params; |
359 | 9.01k | std::vector<TypedParam> splats; |
360 | 9.01k | return findRoute(path, params, splats); |
361 | 9.01k | } |
362 | | |
363 | | namespace Private |
364 | | { |
365 | | |
366 | | RouterHandler::RouterHandler(const Rest::Router& router) |
367 | 0 | : router(std::make_shared<Rest::Router>(router)) |
368 | 0 | { } |
369 | | |
370 | | RouterHandler::RouterHandler(std::shared_ptr<Rest::Router> router) |
371 | 0 | : router(std::move(router)) |
372 | 0 | { } |
373 | | |
374 | | void RouterHandler::onRequest(const Http::Request& req, |
375 | | Http::ResponseWriter response) |
376 | 0 | { |
377 | 0 | PS_TIMEDBG_START_THIS; |
378 | 0 | router->route(req, std::move(response)); |
379 | 0 | } |
380 | | |
381 | | void RouterHandler::onDisconnection(const std::shared_ptr<Tcp::Peer>& peer) |
382 | 0 | { |
383 | 0 | PS_TIMEDBG_START_THIS; |
384 | 0 | router->disconnectPeer(peer); |
385 | 0 | } |
386 | | |
387 | | } // namespace Private |
388 | | |
389 | | Router Router::fromDescription(const Rest::Description& desc) |
390 | 0 | { |
391 | 0 | Router router; |
392 | 0 | router.initFromDescription(desc); |
393 | 0 | return router; |
394 | 0 | } |
395 | | |
396 | | std::shared_ptr<Private::RouterHandler> Router::handler() const |
397 | 0 | { |
398 | 0 | return std::make_shared<Private::RouterHandler>(*this); |
399 | 0 | } |
400 | | |
401 | | std::shared_ptr<Private::RouterHandler> |
402 | | Router::handler(std::shared_ptr<Rest::Router> router) |
403 | 0 | { |
404 | 0 | return std::make_shared<Private::RouterHandler>(router); |
405 | 0 | } |
406 | | |
407 | | void Router::initFromDescription(const Rest::Description& desc) |
408 | 0 | { |
409 | 0 | const auto& paths = desc.rawPaths(); |
410 | 0 | for (auto it = paths.flatBegin(), end = paths.flatEnd(); it != end; ++it) |
411 | 0 | { |
412 | 0 | const auto& paths_ = *it; |
413 | 0 | for (const auto& path : paths_) |
414 | 0 | { |
415 | 0 | if (!path.isBound()) |
416 | 0 | { |
417 | 0 | std::ostringstream oss; |
418 | 0 | oss << "Path '" << path.value << "' is not bound"; |
419 | 0 | throw std::runtime_error(oss.str()); |
420 | 0 | } |
421 | | |
422 | 0 | addRoute(path.method, path.value, path.handler); |
423 | 0 | } |
424 | 0 | } |
425 | 0 | } |
426 | | |
427 | | void Router::get(const std::string& resource, Route::Handler handler) |
428 | 0 | { |
429 | 0 | PS_TIMEDBG_START_THIS; |
430 | 0 | addRoute(Http::Method::Get, resource, std::move(handler)); |
431 | 0 | } |
432 | | |
433 | | void Router::post(const std::string& resource, Route::Handler handler) |
434 | 0 | { |
435 | 0 | PS_TIMEDBG_START_THIS; |
436 | 0 | addRoute(Http::Method::Post, resource, std::move(handler)); |
437 | 0 | } |
438 | | |
439 | | void Router::put(const std::string& resource, Route::Handler handler) |
440 | 0 | { |
441 | 0 | PS_TIMEDBG_START_THIS; |
442 | 0 | addRoute(Http::Method::Put, resource, std::move(handler)); |
443 | 0 | } |
444 | | |
445 | | void Router::patch(const std::string& resource, Route::Handler handler) |
446 | 0 | { |
447 | 0 | PS_TIMEDBG_START_THIS; |
448 | 0 | addRoute(Http::Method::Patch, resource, std::move(handler)); |
449 | 0 | } |
450 | | |
451 | | void Router::del(const std::string& resource, Route::Handler handler) |
452 | 0 | { |
453 | 0 | PS_TIMEDBG_START_THIS; |
454 | 0 | addRoute(Http::Method::Delete, resource, std::move(handler)); |
455 | 0 | } |
456 | | |
457 | | void Router::options(const std::string& resource, Route::Handler handler) |
458 | 0 | { |
459 | 0 | PS_TIMEDBG_START_THIS; |
460 | 0 | addRoute(Http::Method::Options, resource, std::move(handler)); |
461 | 0 | } |
462 | | |
463 | | void Router::removeRoute(Http::Method method, const std::string& resource) |
464 | 0 | { |
465 | 0 | if (resource.empty()) |
466 | 0 | throw std::runtime_error("Invalid zero-length URL."); |
467 | 0 | auto& r = routes[method]; |
468 | 0 | const auto sanitized = SegmentTreeNode::sanitizeResource(resource); |
469 | 0 | const std::string_view path { sanitized.data(), sanitized.size() }; |
470 | 0 | r.removeRoute(path); |
471 | 0 | } |
472 | | |
473 | | void Router::head(const std::string& resource, Route::Handler handler) |
474 | 0 | { |
475 | 0 | addRoute(Http::Method::Head, resource, std::move(handler)); |
476 | 0 | } |
477 | | |
478 | | void Router::addCustomHandler(Route::Handler handler) |
479 | 0 | { |
480 | 0 | customHandlers.push_back(std::move(handler)); |
481 | 0 | } |
482 | | |
483 | | void Router::addMiddleware(Route::Middleware middleware) |
484 | 0 | { |
485 | 0 | middlewares.push_back(std::move(middleware)); |
486 | 0 | } |
487 | | |
488 | | void Router::addDisconnectHandler(Route::DisconnectHandler handler) |
489 | 0 | { |
490 | 0 | disconnectHandlers.push_back(std::move(handler)); |
491 | 0 | } |
492 | | |
493 | | void Router::addNotFoundHandler(Route::Handler handler) |
494 | 0 | { |
495 | 0 | notFoundHandler = std::move(handler); |
496 | 0 | } |
497 | | |
498 | | void Router::invokeNotFoundHandler(const Http::Request& req, |
499 | | Http::ResponseWriter resp) const |
500 | 0 | { |
501 | 0 | notFoundHandler(Rest::Request(req, std::vector<TypedParam>(), |
502 | 0 | std::vector<TypedParam>()), |
503 | 0 | std::move(resp)); |
504 | 0 | } |
505 | | |
506 | | Route::Status Router::route(const Http::Request& request, |
507 | | Http::ResponseWriter response) const |
508 | 0 | { |
509 | 0 | PS_TIMEDBG_START_THIS; |
510 | |
|
511 | 0 | const auto& resource = request.resource(); |
512 | 0 | if (resource.empty()) |
513 | 0 | throw std::runtime_error("Invalid zero-length URL."); |
514 | | |
515 | 0 | auto req = request; |
516 | 0 | auto resp = response.clone(); |
517 | |
|
518 | 0 | for (const auto& middleware : middlewares) |
519 | 0 | { |
520 | 0 | auto result = middleware(req, resp); |
521 | | |
522 | | // Handler returns true, go to the next piped handler, otherwise break and return |
523 | 0 | if (!result) |
524 | 0 | return Route::Status::Match; |
525 | 0 | } |
526 | | |
527 | 0 | std::tuple<std::shared_ptr<Route>, std::vector<TypedParam>, |
528 | 0 | std::vector<TypedParam>> |
529 | 0 | result; |
530 | |
|
531 | 0 | const auto sanitized = SegmentTreeNode::sanitizeResource(resource); |
532 | 0 | const std::string_view path { sanitized.data(), sanitized.size() }; |
533 | |
|
534 | 0 | const auto routesIt = routes.find(req.method()); |
535 | 0 | if (routesIt != routes.end()) |
536 | 0 | result = routesIt->second.findRoute(path); |
537 | |
|
538 | 0 | auto route = std::get<0>(result); |
539 | 0 | if (route != nullptr) |
540 | 0 | { |
541 | 0 | auto params = std::get<1>(result); |
542 | 0 | auto splats = std::get<2>(result); |
543 | 0 | route->invokeHandler(Request(std::move(req), std::move(params), std::move(splats)), |
544 | 0 | std::move(resp)); |
545 | 0 | return Route::Status::Match; |
546 | 0 | } |
547 | | |
548 | 0 | for (const auto& handler : customHandlers) |
549 | 0 | { |
550 | 0 | auto cloned_resp = response.clone(); |
551 | 0 | auto handler1 = handler( |
552 | 0 | Request(req, std::vector<TypedParam>(), std::vector<TypedParam>()), |
553 | 0 | std::move(cloned_resp)); |
554 | 0 | if (handler1 == Route::Result::Ok) |
555 | 0 | return Route::Status::Match; |
556 | 0 | } |
557 | | |
558 | | // No route or custom handler found. Let's walk through the |
559 | | // list of other methods and see if any of them support |
560 | | // this resource. |
561 | | // This will allow server to send a |
562 | | // HTTP 405 (method not allowed) response. |
563 | | // RFC 7231 requires HTTP 405 responses to include a list of |
564 | | // supported methods for the requested resource. |
565 | 0 | std::vector<Http::Method> supportedMethods; |
566 | 0 | for (auto& methods : routes) |
567 | 0 | { |
568 | 0 | if (methods.first == req.method()) |
569 | 0 | continue; |
570 | | |
571 | 0 | auto res = methods.second.findRoute(path); |
572 | 0 | auto rte = std::get<0>(res); |
573 | 0 | if (rte != nullptr) |
574 | 0 | { |
575 | 0 | supportedMethods.push_back(methods.first); |
576 | 0 | } |
577 | 0 | } |
578 | |
|
579 | 0 | if (!supportedMethods.empty()) |
580 | 0 | { |
581 | 0 | response.sendMethodNotAllowed(supportedMethods); |
582 | 0 | return Route::Status::NotAllowed; |
583 | 0 | } |
584 | | |
585 | 0 | if (hasNotFoundHandler()) |
586 | 0 | { |
587 | 0 | invokeNotFoundHandler(req, std::move(response)); |
588 | 0 | } |
589 | 0 | else |
590 | 0 | { |
591 | 0 | response.send(Http::Code::Not_Found, "Could not find a matching route"); |
592 | 0 | } |
593 | 0 | return Route::Status::NotFound; |
594 | 0 | } |
595 | | |
596 | | void Router::addRoute(Http::Method method, const std::string& resource, |
597 | | Route::Handler handler) |
598 | 0 | { |
599 | 0 | PS_TIMEDBG_START_THIS; |
600 | |
|
601 | 0 | if (resource.empty()) |
602 | 0 | throw std::runtime_error("Invalid zero-length URL."); |
603 | 0 | auto& r = routes[method]; |
604 | 0 | const auto sanitized = SegmentTreeNode::sanitizeResource(resource); |
605 | 0 | std::shared_ptr<char> ptr(new char[sanitized.length()], |
606 | 0 | std::default_delete<char[]>()); |
607 | 0 | std::memcpy(ptr.get(), sanitized.data(), sanitized.length()); |
608 | 0 | const std::string_view path { ptr.get(), sanitized.length() }; |
609 | 0 | r.addRoute(path, handler, ptr); |
610 | 0 | } |
611 | | |
612 | | void Router::disconnectPeer(const std::shared_ptr<Tcp::Peer>& peer) |
613 | 0 | { |
614 | 0 | PS_TIMEDBG_START_THIS; |
615 | |
|
616 | 0 | for (const auto& handler : disconnectHandlers) |
617 | 0 | { |
618 | 0 | handler(peer); |
619 | 0 | } |
620 | 0 | } |
621 | | |
622 | | namespace Routes |
623 | | { |
624 | | |
625 | | void Get(Router& router, const std::string& resource, Route::Handler handler) |
626 | 0 | { |
627 | 0 | router.get(resource, std::move(handler)); |
628 | 0 | } |
629 | | |
630 | | void Post(Router& router, const std::string& resource, Route::Handler handler) |
631 | 0 | { |
632 | 0 | router.post(resource, std::move(handler)); |
633 | 0 | } |
634 | | |
635 | | void Put(Router& router, const std::string& resource, Route::Handler handler) |
636 | 0 | { |
637 | 0 | router.put(resource, std::move(handler)); |
638 | 0 | } |
639 | | |
640 | | void Patch(Router& router, const std::string& resource, |
641 | | Route::Handler handler) |
642 | 0 | { |
643 | 0 | router.patch(resource, std::move(handler)); |
644 | 0 | } |
645 | | |
646 | | void Delete(Router& router, const std::string& resource, |
647 | | Route::Handler handler) |
648 | 0 | { |
649 | 0 | router.del(resource, std::move(handler)); |
650 | 0 | } |
651 | | |
652 | | void Options(Router& router, const std::string& resource, |
653 | | Route::Handler handler) |
654 | 0 | { |
655 | 0 | router.options(resource, std::move(handler)); |
656 | 0 | } |
657 | | |
658 | | void Remove(Router& router, Http::Method method, const std::string& resource) |
659 | 0 | { |
660 | 0 | router.removeRoute(method, resource); |
661 | 0 | } |
662 | | |
663 | | void NotFound(Router& router, Route::Handler handler) |
664 | 0 | { |
665 | 0 | router.addNotFoundHandler(std::move(handler)); |
666 | 0 | } |
667 | | |
668 | | void Head(Router& router, const std::string& resource, Route::Handler handler) |
669 | 0 | { |
670 | 0 | router.head(resource, std::move(handler)); |
671 | 0 | } |
672 | | |
673 | | } // namespace Routes |
674 | | } // namespace Pistache::Rest |