C Collections

chttpserver · Section 3

chttpsvr_router_use

chttpsvr_router_use - append middleware to a sub-router

C Collections (libccollections, -lccollections)

#include <ccollections/chttpserver.h>
 
ccol_retval chttpsvr_router_use(chttpsvr_router *router,
                                   chttpsvr_middleware_fn fn, void *ctx);

Appends a middleware to the chain of the given sub-router.  It runs
after the global middleware and before the route handler, and only
for requests that the server routes through this sub-router.
 
It also runs for a request that was routed to this sub-router and
then rejected with 413 before its handler could run, and for the 404
or 405 of a path that this sub-router owns but that none of its
routes matches.  chttpsvr_subrouter(3) says which paths a sub-router
owns, and chttpsvr_use(3) gives the full rule and the rejections that
run no middleware at all.
 
The function is thread-safe and may be called at any time, even while
another thread runs chttpsvr_destroy on the owning server.  A call
that races such a destroy returns ccol_invalid_args and touches no
freed memory.

ccol_success on success.
ccol_invalid_args when router or fn is NULL, or when the server that
                  owns router has been destroyed.
ccol_not_enough_memory when an allocation fails.
ccol_not_permitted when the middleware chain of this router is already
at the cap of 32 entries.

The NOTES section of chttpsvr_use covers three more points: The cap
that each router applies when you register a middleware, the limit
of 32 middleware steps per request in total when a request is
dispatched, and the rule that of two duplicate routes the first one
wins.

chttpsvr_use(3), chttpsvr_subrouter(3), chttpsvr_req_set_context(3)