cwist
Modern C web framework with libttak runtime
Loading...
Searching...
No Matches
mux.c File Reference

Fixed-bucket HTTP route dispatch using a lightweight path signature. More...

#include <cwist/net/http/mux.h>
#include <cwist/net/http/http.h>
#include <cwist/core/mem/alloc.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdint.h>
#include <math.h>
Include dependency graph for mux.c:

Go to the source code of this file.

Macros

#define _POSIX_C_SOURCE   200809L
 
#define CWIST_MUX_DEFAULT_BUCKETS   4099
 

Functions

cwist_mux_router * cwist_mux_router_create (void)
 Create an empty mux router with the default bucket fan-out.
 
void cwist_mux_router_destroy (cwist_mux_router *router)
 Destroy a mux router and all registered route records.
 
void cwist_mux_handle (cwist_mux_router *router, cwist_http_method_t method, const char *path, cwist_http_handler_func handler)
 Register a concrete method/path pair inside the mux buckets.
 
cwist_mux_route * cwist_mux_find_route (cwist_mux_router *router, cwist_http_method_t method, const char *path)
 Look up a registered route for a method/path pair without dispatching to it.
 
cwist_mux_group * cwist_mux_group_create (cwist_mux_router *router, const char *prefix)
 Create a route group that registers all its handlers under a shared path prefix.
 
void cwist_mux_group_destroy (cwist_mux_group *group)
 Destroy a route group and its stored prefix string.
 
void cwist_mux_group_handle (cwist_mux_group *group, cwist_http_method_t method, const char *path, cwist_http_handler_func handler)
 Register a method/path handler on the router, prefixed by the group's prefix.
 
void cwist_mux_route_use (cwist_mux_route *route, cwist_middleware_func mw)
 Attach a middleware callback to a route, applied only when that route is served.
 
bool cwist_mux_serve (cwist_mux_router *router, cwist_http_request *req, cwist_http_response *res)
 Attempt to dispatch an incoming request through the mux table.
 

Detailed Description

Fixed-bucket HTTP route dispatch using a lightweight path signature.

Definition in file mux.c.

Macro Definition Documentation

◆ _POSIX_C_SOURCE

#define _POSIX_C_SOURCE   200809L

Definition at line 1 of file mux.c.

◆ CWIST_MUX_DEFAULT_BUCKETS

#define CWIST_MUX_DEFAULT_BUCKETS   4099

Definition at line 16 of file mux.c.

Function Documentation

◆ cwist_mux_find_route()

cwist_mux_route * cwist_mux_find_route ( cwist_mux_router *  router,
cwist_http_method_t  method,
const char *  path 
)

Look up a registered route for a method/path pair without dispatching to it.

Find a matching route.

Parameters
routerRouter whose exact and parametric routes are searched.
methodHTTP method to match.
pathRequest path to resolve; NULL is treated as the root.
Returns
Matching route record, or NULL when no exact or parametric route matches.
Note
Wildcard routes are not considered by this lookup.

Definition at line 325 of file mux.c.

References cwist_mux_route::bucket_next, cwist_mux_router::buckets, cap, cwist_query_map_destroy(), and cwist_mux_router::param_routes.

◆ cwist_mux_group_create()

cwist_mux_group * cwist_mux_group_create ( cwist_mux_router *  router,
const char *  prefix 
)

Create a route group that registers all its handlers under a shared path prefix.

Create a route group with a path prefix.

Parameters
routerRouter that will own the routes registered through the group.
prefixPath prefix prepended to every route path registered on the group.
Returns
Newly allocated group, or NULL when an argument is NULL or allocation fails.

Definition at line 361 of file mux.c.

References cap, cwist_alloc(), cwist_free(), and cwist_mux_group::router.

◆ cwist_mux_group_destroy()

void cwist_mux_group_destroy ( cwist_mux_group *  group)

Destroy a route group and its stored prefix string.

Destroy a route group.

Parameters
groupGroup to destroy. NULL is ignored.
Note
Only the group record is freed; routes registered through the group remain owned by the router and are released by cwist_mux_router_destroy().

Definition at line 381 of file mux.c.

References cap, and cwist_free().

◆ cwist_mux_group_handle()

void cwist_mux_group_handle ( cwist_mux_group *  group,
cwist_http_method_t  method,
const char *  path,
cwist_http_handler_func  handler 
)

Register a method/path handler on the router, prefixed by the group's prefix.

Register a handler inside a group.

Parameters
groupGroup supplying the path prefix and target router.
methodHTTP method to match.
pathRoute path relative to the group prefix; the two are concatenated verbatim.
handlerCallback to invoke for a matching request.
Note
Invalid arguments (NULL group, router, path, or handler) are silently ignored, as is an allocation failure while building the full path.

Definition at line 396 of file mux.c.

References cap, cwist_alloc(), CWIST_DEFER_FREE, and cwist_mux_handle().

◆ cwist_mux_handle()

void cwist_mux_handle ( cwist_mux_router *  router,
cwist_http_method_t  method,
const char *  path,
cwist_http_handler_func  handler 
)

Register a concrete method/path pair inside the mux buckets.

Register a route handler.

Parameters
routerRouter that will own the route metadata.
methodHTTP method to match.
pathExact request path to dispatch.
handlerCallback to invoke for a matching request.

Definition at line 160 of file mux.c.

References cwist_mux_router::buckets, cap, cwist_alloc(), cwist_sstring_assign(), cwist_sstring_create(), cwist_mux_route::method, cwist_mux_route::param_next, cwist_mux_router::param_routes, cwist_mux_router::routes, and cwist_mux_router::wildcard_routes.

Referenced by cwist_mux_group_handle().

◆ cwist_mux_route_use()

void cwist_mux_route_use ( cwist_mux_route *  route,
cwist_middleware_func  mw 
)

Attach a middleware callback to a route, applied only when that route is served.

Attach middleware to a specific route.

Parameters
routeRoute to attach the middleware to. NULL is ignored.
mwMiddleware function to prepend to the route's chain. NULL is ignored.
Note
Middleware runs in most-recently-added-first order because each node is prepended to the head of the list. An allocation failure is silently ignored.

Definition at line 418 of file mux.c.

References cap, cwist_alloc(), cwist_mux_middleware_node::func, and cwist_mux_middleware_node::next.

◆ cwist_mux_router_create()

cwist_mux_router * cwist_mux_router_create ( void  )

Create an empty mux router with the default bucket fan-out.

Create a new router.

Returns
Newly allocated router, or NULL when allocation fails.

Definition at line 113 of file mux.c.

References cwist_mux_router::bucket_count, cwist_mux_router::buckets, cap, cwist_alloc(), cwist_alloc_array(), cwist_free(), CWIST_MUX_DEFAULT_BUCKETS, cwist_mux_router::param_routes, cwist_mux_router::routes, and cwist_mux_router::wildcard_routes.

◆ cwist_mux_router_destroy()

void cwist_mux_router_destroy ( cwist_mux_router *  router)

Destroy a mux router and all registered route records.

Destroy a router and all its routes.

Destroy a router.

Parameters
routerRouter to destroy. NULL is ignored.

Definition at line 134 of file mux.c.

References cwist_mux_router::buckets, cap, cwist_free(), cwist_sstring_destroy(), cwist_mux_route::next, cwist_mux_middleware_node::next, and cwist_mux_router::routes.

◆ cwist_mux_serve()

bool cwist_mux_serve ( cwist_mux_router *  router,
cwist_http_request *  req,
cwist_http_response *  res 
)

Attempt to dispatch an incoming request through the mux table.

Dispatches an HTTP request through the router.

Dispatch a request.

Parameters
routerRouter containing registered exact-match handlers.
reqParsed HTTP request with method and path information.
resHTTP response object passed through to the matched handler.
Returns
true when a route was found and its handler was executed, otherwise false.

Definition at line 435 of file mux.c.

References cwist_mux_router::buckets, cap, cwist_query_map_destroy(), cwist_sstring::data, cwist_http_request::method, cwist_mux_router::param_routes, cwist_http_request::path, cwist_http_request::path_params, and cwist_mux_router::wildcard_routes.