-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouter.hpp
636 lines (570 loc) · 21.3 KB
/
router.hpp
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
//
// Created by Rakesh on 15/03/2022.
//
#pragma once
#include "concat.hpp"
#include "error.hpp"
#include "split.hpp"
#include <functional>
#include <mutex>
#include <optional>
#if defined __has_include
#if __has_include(<log/NanoLog.hpp>)
#include <log/NanoLog.hpp>
#define HAS_LOGGER 1
#endif
#ifndef HAS_BOOST
#if __has_include(<boost/container/flat_map.hpp>)
#define HAS_BOOST 1
#endif
#endif
#endif
#ifdef HAS_BOOST
#include <ostream>
#include <boost/algorithm/string/replace.hpp>
#include <boost/container/flat_map.hpp>
#include <boost/json/array.hpp>
#include <boost/json/serialize.hpp>
#else
#include <map>
#endif
namespace spt::http::router
{
/**
* Simple path based HTTP request router. Configured paths are stored in
* a sorted vector, and request path matching is performed via binary search.
* @tparam Request User defined structure with the request context necessary for
* the handler function.
* @tparam Response The response from the handler function.
* @tparam Map The type of map to use to return the parsed path parameters.
* If boost has been found defaults to boost::container::flat_map, else std::map
*/
#ifdef HAS_BOOST
template <typename Request, typename Response, typename Map = boost::container::flat_map<std::string_view, std::string_view>>
#else
template <typename Request, typename Response, typename Map = std::map<std::string_view, std::string_view>>
#endif
requires (std::same_as<std::string, typename Map::key_type> && std::same_as<std::string, typename Map::mapped_type>) ||
(std::same_as<std::string_view, typename Map::key_type> && std::same_as<std::string_view, typename Map::mapped_type>)
class HttpRouter
{
struct Path
{
Path( std::string&& p, std::string&& m, std::size_t h, std::string&& r = {} ) :
path{ std::move( p ) }, ref{ std::move( r ) }, parts{ util::split<std::string>( path ) }
{
using std::operator""sv;
epath.reserve( path.size() );
for ( auto&& part : parts )
{
if ( ( part.starts_with( '{' ) && !part.ends_with( '}' ) ) || part.starts_with( ':' ) )
{
throw InvalidParameterError{ util::concat( "Path "sv, path, " has invalid parameter "sv, part ) };
}
if ( part.starts_with( '{' ) ) epath.append( "/{}" );
else if ( part == "~" )
{
epath.append( "/" ).append( "*" );
wildcard = true;
}
else epath.append( "/" ).append( part );
}
methods.push_back( std::move( m ) );
handlers.push_back( h );
}
~Path() = default;
Path(Path&&) noexcept = default;
Path& operator=(Path&&) noexcept = default;
Path(const Path&) = delete;
Path& operator=(const Path&) = delete;
[[nodiscard]] std::optional<std::size_t> indexOf( const std::string& method ) const
{
auto it = std::find( std::cbegin( methods ), std::cend( methods ), method );
if ( it == std::cend( methods ) ) return std::nullopt;
return std::distance( std::cbegin( methods ), it );
}
std::string path;
std::string epath;
std::string ref;
std::vector<std::string> parts;
std::vector<std::string> methods;
std::vector<std::size_t> handlers;
bool wildcard{ false };
};
public:
using MapType [[maybe_unused]] = Map;
struct Builder;
/**
* The key in the path parameters map with the sub-path that matches a wildcard route.
*/
static inline const auto WildcardKey = std::string{ "_wildcard_" };
/**
* Request handler callback function. Path parameters extracted are passed
* as either a std::map or boost::container::flat_map.
*/
using Handler = std::function<Response( Request, Map&& )>;
/**
* Add the specified path for the specified HTTP method/verb to the router.
* This is thread safe.
*
* @param method The HTTP method/verb for which the route is configured.
* @param path The path to configure. Either a static (no parameters in curly braces) or parametrised value.
* @param handler The callback function to invoke if a request path matches.
* @param ref Optional reference to associate with the path when outputting
* the YAML that could be used in developing the OpenAPI Specification for the API.
* @return A reference to the router for chaining.
* @throws DuplicateRouteError If the specified `path` has been configured
* for the specified `method` already.
* @throws InvalidParameterError If the specified `path` has parameters and
* use the `:<parameter>` form, or if the trailing `}` in the
* `{parameter}` is missing.
*/
HttpRouter& add( std::string_view method, std::string_view path,
Handler&& handler, std::string_view ref = {} )
{
auto lock = std::scoped_lock<std::mutex>{ mutex };
addParameter( method, path, ref );
handlers.push_back( std::move( handler ) );
return *this;
}
/**
* Attempt to route the request for specified path and method.
* @param method The HTTP method/verb from the client.
* @param path The request URI path.
* @param request The custom data used by the handler callback function.
* @param checkWithoutTrailingSlash If `true` and if the `path` ends with
* a trailing slash ('/'), attempt to find a match after trimming the
* trailing slash in case the original path does not match.
* @return Returns std::nullopt if no configured route matches.
*/
std::optional<Response> route( std::string_view method, std::string_view path,
Request request, bool checkWithoutTrailingSlash = false ) const
{
if ( method.empty() || path.empty() ) return std::nullopt;
try
{
auto resp = routeParameters( method, path, request );
if ( !resp && checkWithoutTrailingSlash && path.ends_with( '/' ) )
{
return routeParameters( method, path.substr( 0, path.size() - 1 ), request );
}
return resp;
}
catch ( const std::exception& e )
{
if ( errorHandler )
{
#ifdef HAS_LOGGER
LOG_WARN << "Error handling " << method << " request to " << path <<
". " << e.what();
#endif
return (*errorHandler)( request, {} );
}
throw;
}
}
/// Check if a handler has been registered for the specified resource using the specified method/verb.
/// @param method The HTTP method/verb configured for the resourse
/// @param path The path to check if a handler has been configured
/// @return A tuple of two booleans. The first value indicates the resource has a handler, while the
/// second indicates if the method has been configured for the resource.
[[nodiscard]] std::tuple<bool, bool> canRoute( std::string_view method, std::string_view path ) const
{
using std::operator""sv;
if ( method.empty() || path.empty() ) return { false, false };
auto full = std::string{ path };
auto m = std::string{ method };
auto iter = std::lower_bound( std::cbegin( paths ), std::cend( paths ), full,
[]( const Path& p, const std::string& pth )
{
return p.path < pth;
} );
if ( iter == std::cend( paths ) ) return { false, false };
if ( full == iter->path && !iter->wildcard )
{
if ( auto midx = iter->indexOf( m ); midx ) return { true, true };
return { true, false };
}
const auto parts = util::split<std::string_view>( full );
auto handler = -1;
for ( ; iter != std::cend( paths ); ++iter )
{
if ( parts.size() != iter->parts.size() )
{
if ( !iter->wildcard ) continue;
if ( parts.size() < iter->parts.size() ) continue;
for ( std::size_t i = 0; i < iter->parts.size(); ++i )
{
auto iview = std::string_view{ iter->parts[i] };
if ( parts[i] == iview ) continue;
if ( iview != "~"sv ) break;
if ( auto midx = iter->indexOf( m ); midx ) return { true, true };
return { true, false };
}
}
auto midx = iter->indexOf( m );
for ( std::size_t i = 0; i < parts.size(); ++i )
{
auto iview = std::string_view{ iter->parts[i] };
if ( parts[i] == iview )
{
if ( i == parts.size() - 1 )
{
if ( midx ) handler = static_cast<int>( iter->handlers[*midx] );
else return { true, false };
}
else continue;
}
if ( iview == "~" && iter->wildcard )
{
if ( midx )
{
handler = static_cast<int>( iter->handlers[*midx] );
}
else return { true, false };
}
if ( iview[0] != '{' ) break;
if ( i == parts.size() - 1 )
{
if ( midx ) handler = static_cast<int>( iter->handlers[*midx] );
else return { true, false };
}
}
if ( handler != -1 ) break;
}
using O = std::tuple<bool, bool>;
return handler == -1 ? O{ false, false } : O{ true, true };
}
#ifdef HAS_BOOST
/**
* Output the configured routes as a JSON structure.
* @return JSON representation with some additional metadata about the configured routes.
*/
[[nodiscard]] boost::json::value json() const
{
using std::operator""sv;
auto arr = boost::json::array{};
int s = 0;
int d = 0;
for ( auto&& p : paths )
{
auto m = boost::json::array{};
for ( const auto& method : p.methods ) m.push_back( boost::json::value{ method } );
if ( p.path.ends_with( '~' ) )
{
auto path = boost::algorithm::replace_last_copy( p.path, "~"sv, "*"sv );
arr.push_back( boost::json::object{ { "path", path }, { "methods", m } } );
}
else
{
arr.push_back( boost::json::object{ { "path", p.path }, { "methods", m } } );
}
if ( p.path.find( "{" ) != std::string::npos ) ++d;
else if ( p.path.ends_with( '~' ) ) ++d;
else ++s;
}
return boost::json::object{
{ "paths", arr },
{ "total", paths.size() },
{ "static", s },
{ "dynamic", d }
};
}
/**
* Output a string representation of the configured routes.
* @return String representation of the routes.
*/
[[nodiscard]] std::string str() const
{
return boost::json::serialize( json() );
}
#endif
[[nodiscard]] std::string yaml() const
{
using std::operator""sv;
std::string out;
out.reserve( 1024 );
out.append( "paths:\n" );
for ( auto&& path : paths )
{
if ( path.path.ends_with( '~' ) )
{
auto p = boost::algorithm::replace_last_copy( path.path, "~"sv, "*"sv );
out.append( " " ).append( p ).append( ":\n" ).
append( " $ref: " ).append( "\"" ).append( path.ref ).append( "\"\n" );
}
else
{
out.append( " " ).append( path.path ).append( ":\n" ).
append( " $ref: " ).append( "\"" ).append( path.ref ).append( "\"\n" );
}
}
return out;
}
/**
* Create a new instance of the router.
* @param error404 Optional handler function to handle path not found condition.
* @param error405 Optional handler function to handle path not configured for method condition.
* @param error500 Optional handler function to handle exception caught while despatching the request to handler.
*/
HttpRouter( std::optional<Handler>&& error404 = std::nullopt,
std::optional<Handler>&& error405 = std::nullopt,
std::optional<Handler>&& error500 = std::nullopt ) :
notFound{ std::move( error404 ) }, methodNotAllowed{ std::move( error405 ) },
errorHandler{ std::move( error500 ) }
{
handlers.reserve( 32 );
paths.reserve( 32 );
}
~HttpRouter() = default;
HttpRouter(const HttpRouter&) = delete;
HttpRouter& operator=(const HttpRouter&) = delete;
private:
void addParameter( std::string_view method, std::string_view path, std::string_view ref )
{
using std::operator""s;
using std::operator""sv;
auto full = std::string{ path };
auto m = std::string{ method };
auto iter = std::lower_bound( std::begin( paths ), std::end( paths ), full,
[]( const Path& p, const std::string& pth )
{
return p.path < pth;
} );
if ( iter != std::cend( paths ) && full == iter->path )
{
if ( iter->indexOf( m ) )
{
throw DuplicateRouteError{ util::concat( "Duplicate path "sv, path, " for method "sv, m ) };
}
iter->methods.push_back( std::move( m ) );
iter->handlers.push_back( handlers.size() );
return;
}
if ( const auto idx = full.find( '*' ); idx != std::string::npos )
{
if ( idx != full.size() - 1 ) throw InvalidWildcardError( "Wildcard character at invalid position"s );
if ( idx > 0 && full[idx-1] != '/' ) throw InvalidWildcardError( "Wildcard character not preceded by /"s );
}
if ( full.ends_with( '*' ) ) full[full.size() - 1] = '~';
auto ps = Path{ std::move( full ), std::move( m ), handlers.size(), std::string{ ref } };
for ( auto&& p : paths )
{
if ( p.epath == ps.epath )
{
if ( auto it = std::find( std::cbegin( iter->methods ), std::cend( iter->methods ), ps.methods[0] );
it != std::cend( iter->methods ) )
{
throw DuplicateRouteError{ util::concat( "Duplicate path "sv, ps.path, " clashes with "sv, p.path ) };
}
}
}
paths.push_back( std::move( ps ) );
std::sort( std::begin( paths ), std::end( paths ), []( const Path& p1, const Path& p2 )
{
return p1.path < p2.path;
} );
}
std::optional<Response> routeParameters( std::string_view method,
std::string_view path, Request request ) const
{
using std::operator""sv;
Map params{};
auto full = std::string{ path };
auto m = std::string{ method };
auto iter = std::lower_bound( std::cbegin( paths ), std::cend( paths ), full,
[]( const Path& p, const std::string& pth )
{
return p.path < pth;
} );
if ( iter == std::cend( paths ) ) return std::nullopt;
if ( full == iter->path && !iter->wildcard )
{
if ( auto midx = iter->indexOf( m ); midx )
{
return handlers[iter->handlers[*midx]]( request, std::move( params ) );
}
#ifdef HAS_LOGGER
LOG_INFO << "Method " << method << " not configured for path " << path;
#endif
if ( methodNotAllowed ) return (*methodNotAllowed)( request, std::move( params ) );
return std::nullopt;
}
const auto parts = util::split<std::string_view>( full );
auto handler = -1;
for ( ; iter != std::cend( paths ); ++iter )
{
if ( parts.size() != iter->parts.size() )
{
if ( !iter->wildcard ) continue;
if ( parts.size() < iter->parts.size() ) continue;
for ( std::size_t i = 0; i < iter->parts.size(); ++i )
{
auto iview = std::string_view{ iter->parts[i] };
if ( parts[i] == iview ) continue;
if ( iview[0] == '{' )
{
auto key = iview.substr( 1, iview.size() - 2 );
params.try_emplace( { key.data(), key.size() }, parts[i] );
}
if ( iview != "~"sv ) break;
if ( auto midx = iter->indexOf( m ); midx )
{
std::size_t idx = 0;
for ( std::size_t j = 0; j < iter->parts.size() - 1; ++j )
{
idx += ( 1 + parts[j].size() );
}
params.try_emplace( WildcardKey, path.substr( ++idx ) );
return handlers[iter->handlers[*midx]]( request, std::move( params ) );
}
#ifdef HAS_LOGGER
LOG_INFO << "Method " << method << " not configured for path " << path;
#endif
if ( methodNotAllowed ) return (*methodNotAllowed)( request, std::move( params ) );
return std::nullopt;
}
}
auto midx = iter->indexOf( m );
for ( std::size_t i = 0; i < parts.size(); ++i )
{
auto iview = std::string_view{ iter->parts[i] };
if ( parts[i] == iview )
{
if ( i == parts.size() - 1 )
{
if ( midx ) handler = static_cast<int>( iter->handlers[*midx] );
else
{
#ifdef HAS_LOGGER
LOG_INFO << "Method " << method << " not configured for path " << path;
#endif
if ( methodNotAllowed ) return (*methodNotAllowed)( request, std::move( params ) );
return std::nullopt;
}
}
else continue;
}
if ( iview == "~" && iter->wildcard )
{
if ( midx )
{
handler = static_cast<int>( iter->handlers[*midx] );
std::size_t idx = 0;
for ( std::size_t j = 0; j < iter->parts.size() - 1; ++j )
{
idx += ( 1 + parts[j].size() );
}
params.try_emplace( WildcardKey, path.substr( ++idx ) );
}
else
{
#ifdef HAS_LOGGER
LOG_INFO << "Method " << method << " not configured for path " << path;
#endif
if ( methodNotAllowed ) return (*methodNotAllowed)( request, std::move( params ) );
return std::nullopt;
}
}
if ( iview[0] != '{' ) break;
auto key = iview.substr( 1, iview.size() - 2 );
params.try_emplace( { key.data(), key.size() }, parts[i] );
if ( i == parts.size() - 1 )
{
if ( midx ) handler = static_cast<int>( iter->handlers[*midx] );
else
{
#ifdef HAS_LOGGER
LOG_INFO << "Method " << method << " not configured for path " << path;
#endif
if ( methodNotAllowed ) return (*methodNotAllowed)( request, std::move( params ) );
return std::nullopt;
}
}
}
if ( handler != -1 ) break;
params.clear();
}
if ( handler == -1 )
{
if ( notFound ) return (*notFound)( request, std::move( params ) );
return std::nullopt;
}
return handlers[handler]( request, std::move( params ) );
}
std::vector<Handler> handlers{};
std::vector<Path> paths;
std::optional<Handler> notFound{ std::nullopt };
std::optional<Handler> methodNotAllowed{ std::nullopt };
std::optional<Handler> errorHandler{ std::nullopt };
std::mutex mutex;
};
#ifdef HAS_BOOST
template <typename Request, typename Response>
std::ostream& operator<<( std::ostream& os, const HttpRouter<Request, Response>& router )
{
os << router.json();
return os;
}
#endif
/**
* A builder for the HttpRouter. Use to configure error handlers when
* instantiating the router.
* @tparam Request User defined structure with the request context necessary for
* the router handler function.
* @tparam Response The response from the handler function.
*/
template <typename Request, typename Response, typename Map>
requires (std::same_as<std::string, typename Map::key_type> && std::same_as<std::string, typename Map::mapped_type>) ||
(std::same_as<std::string_view, typename Map::key_type> && std::same_as<std::string_view, typename Map::mapped_type>)
struct HttpRouter<Request, Response, Map>::Builder
{
Builder() = default;
~Builder() = default;
Builder(const Builder&) = delete;
Builder& operator=(const Builder&) = delete;
/**
* Set the HTTP 404 error handler to use with the router.
* @param h The handler function.
* @return Reference to this builder for chaining.
*/
Builder& withNotFound( typename HttpRouter<Request, Response>::Handler&& h )
{
notFound = std::move( h );
return *this;
}
/**
* Set the HTTP 405 error handler to use with the router.
* @param h The handler function.
* @return Reference to this builder for chaining.
*/
Builder& withMethodNotAllowed( typename HttpRouter<Request, Response>::Handler&& h )
{
methodNotAllowed = std::move( h );
return *this;
}
/**
* Set the HTTP 500 error handler to use with the router.
* @param h The handler function.
* @return Reference to this builder for chaining.
*/
Builder& withErrorHandler( typename HttpRouter<Request, Response>::Handler&& h )
{
errorHandler = std::move( h );
return *this;
}
/**
* Build the router with the error handlers provided. The handlers are
* moved, so no further use of the builder is possible.
* @return The properly initialised router.
*/
[[nodiscard]] HttpRouter<Request, Response> build()
{
return { std::move( notFound ), std::move( methodNotAllowed ), std::move( errorHandler ) };
}
private:
std::optional<Handler> notFound{ std::nullopt };
std::optional<Handler> methodNotAllowed{ std::nullopt };
std::optional<Handler> errorHandler{ std::nullopt };
};
}