HTTP Router
Simple general purpose HTTP path based request router. Requires a compiler with C++20 support. No assumption is made on the type of framework being used. We have used it mainly with nghttp2
Supports static and parametrised URI paths.
Parameters (slugs) are represented using curly brace enclosed name
{param}
.Curly brace form was chosen in favour of
:param
for sorting purpose.Sorting with
{
implies use of non-ascii characters in path will have inconsistent behaviour.
Wildcard path pattern is supported.
Only a single wildcard (
*
) as the last component of the URI path is supported.Parameters (slugs) are supported in the same URI path (
/device/sensor/id/{id}/*
).Internally the wildcard character
*
is replaced by~
for sorting purpose.As a consequence of sorting, it is possible to configure more specific URI wildcard paths in combination with wildcard paths at the base level. Example:
/device/sensor/*
and/device/sensor/id/*
.
Templated on the Response type and an input Request. Optionally specify the type of Map container to use to hold the parsed path parameters. Defaults to
boost::container::flat_map
if boost is found, or tostd::map
. The type specified must be interface compatible withstd::map
. Thekey
andvalue
must be eitherstd::string_view
orstd::string
.Function based routing. Successful matches are routed to the specified handler callback function.
Parameters are returned as a map. The type of map is determined via the optional third template parameter.
Callback function has signature
Response( Request, MapType<String, String>&& )
whereMapType
is eitherboost::container::flat_map
orstd::map
(if using defaults, or the container you specify) andString
isstd::string_view
(if using defaults) orstd::string
if you specify. See string.cpp test for sample of specifying your preferred container andstd::string
as the type in the container.The
MapType
will hold the parsed parameter->value pairs.The path part matching the wildcard (for wildcard paths) is added to the
MapType
as_wildcard_
key. Keep this in mind when naming path parameters for wildcard paths.
Install
No install is necessary. Copy the router.hpp, split.hpp, and concat.hpp files into your project and use.
The headers may be installed into a standard location using cmake
.