C Collections

chttpserver · Section 3

chttpsvr_subrouter

chttpsvr_subrouter - create a sub-router with a path prefix

C Collections (libccollections, -lccollections)

#include <ccollections/chttpserver.h>
 
chttpsvr_router *chttpsvr_subrouter(chttpsvr srv, const char *prefix);

Creates a new router under the given path prefix and attaches it to
the server.  The prefix must start with '/'.  A route registered on
the sub-router is matched against the part of the path after the
prefix, and the sub-router owns every path under the prefix; see
NOTES.
 
The prefix must not contain two consecutive slashes, as in "//api"
or "/a//b".  Such a prefix can never match a valid HTTP path and
would quietly create a router that nothing reaches, so
chttpsvr_subrouter refuses it and returns NULL.
 
A trailing slash on the prefix is removed, so "/api/v1/" and
"/api/v1" are the same prefix and route in the same way.
 
The returned pointer belongs to the server, and the caller must not
free it.  Every chttpsvr_router_register_handler,
chttpsvr_router_register_streaming_handler and chttpsvr_router_use
call on that router first checks that the server
has not been destroyed, also against a destroy on another thread at
the same time, so you can keep the pointer across a restart with
chttpsvr_stop and chttpsvr_start.  A registration call that races a
chttpsvr_destroy of srv returns ccol_invalid_args and touches no
freed memory.  To keep that promise for a pointer that outlives its
server, each sub-router keeps a small part of its memory, about 100
bytes, until the process exits, while the destroy of srv frees its
routes, its middleware and its prefix.  A program that creates and
destroys many servers with sub-routers keeps that much for every
sub-router it ever created.  A server that is destroyed while the
process exits, after the exit-time cleanup of the library has run,
frees that memory as well.
 
The function is thread-safe and may be called both before and after
the server starts.

A pointer to the new chttpsvr_router on success.  NULL when srv is
CHTTPSVR_NULL, when srv is a stale handle that has already been
destroyed, when prefix is NULL, when prefix does not start with '/',
when prefix contains two consecutive slashes, or when the library
runs out of memory.

A mounted prefix owns every path under it.  A path is under a prefix
when its decoded form (the one that chttpsvr_req_path reports) equals
the prefix or continues it with a '/': "/admin", "/admin/x/y",
"/%61dmin/x" and "/admin%2Fx" are all under "/admin", but
"/administrator" is not.  The most specific mount that owns the path
handles the request alone, so for "/api/v1/x" a mount of
"/api/v1" wins over a mount of "/api", and no route of a less
specific mount or of the root level can answer it.  When none of the
owner's routes matches, the owner answers 404, or 405 when one of its
routes matches the path but not the method, and the owner's
middleware runs for that rejection just as it runs for its routes.
A path under no mount goes to the root router, which holds the routes
that chttpsvr_register_handler and chttpsvr_register_streaming_handler
register directly on the server.
 
So a guard in the middleware of a sub-router covers every path under
its prefix, whatever any other router registers.  A root-level
pattern such as "/{a}/{b}/{c}" overlaps every three-segment path, but
it never answers "/admin/a/b" while "/admin" is mounted.  A mount
nested under another one, such as "/admin/deep" under "/admin", owns
the paths under itself, and the middleware of "/admin" doesn't run
for them, so give the nested mount a guard of its own when it needs
one.  Two sub-routers on the identical prefix share it: The routes of
both can answer, the earlier registration first, and the earlier one
answers a 404 or a 405.  Which router wins depends only on the
prefixes and patterns you register and never on the order of
registration.  Inside one router the server tries the routes in
registration order; see chttpsvr_register_handler(3).
 
The server decodes each raw segment separately before matching it.
"/admin%2Fx" is under "/admin", but no route of that mount can match
it, because a route matches whole segments, and the mount answers it
with a 404.
 
The "/" prefix is a special case.  The loop that strips a trailing
slash only runs for a prefix longer than one character, so "/" stays
as it is, with prefix_len = 1.  A "/" sub-router owns only the exact
request path "/".  No other path is under it, and the server routes
every other path, including one that starts with a second slash such
as "//foo", as if the "/" sub-router did not exist.  Every route on a
"/" sub-router can therefore be reached only from the exact path "/".
 
That makes this prefix a trap for a sub-router with its own
middleware: An authenticator or a rate limiter on a "/" sub-router
does not refuse everything outside "/".  A root-level route or
another sub-router serves any other path normally, and the
middleware of the "/" sub-router doesn't run for it.
 
Use the prefix "/" only when you want to match the exact root path
"/" and nothing else.  To run a middleware for every request whatever
its path, register it globally with chttpsvr_use, and to catch every
request whatever its path, register routes directly on the server
with chttpsvr_register_handler or chttpsvr_register_streaming_handler.

chttpsvr_router_register_handler(3), chttpsvr_router_use(3)