|
cwist
Modern C web framework with libttak runtime
|
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>
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. | |
Fixed-bucket HTTP route dispatch using a lightweight path signature.
Definition in file mux.c.
| 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.
| router | Router whose exact and parametric routes are searched. |
| method | HTTP method to match. |
| path | Request path to resolve; NULL is treated as the root. |
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 * 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.
| router | Router that will own the routes registered through the group. |
| prefix | Path prefix prepended to every route path registered on the group. |
Definition at line 361 of file mux.c.
References cap, cwist_alloc(), cwist_free(), and cwist_mux_group::router.
| void cwist_mux_group_destroy | ( | cwist_mux_group * | group | ) |
Destroy a route group and its stored prefix string.
Destroy a route group.
| group | Group to destroy. NULL is ignored. |
Definition at line 381 of file mux.c.
References cap, and cwist_free().
| 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.
| group | Group supplying the path prefix and target router. |
| method | HTTP method to match. |
| path | Route path relative to the group prefix; the two are concatenated verbatim. |
| handler | Callback to invoke for a matching request. |
Definition at line 396 of file mux.c.
References cap, cwist_alloc(), CWIST_DEFER_FREE, and 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.
| router | Router that will own the route metadata. |
| method | HTTP method to match. |
| path | Exact request path to dispatch. |
| handler | Callback 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().
| 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.
| route | Route to attach the middleware to. NULL is ignored. |
| mw | Middleware function to prepend to the route's chain. NULL is 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 * cwist_mux_router_create | ( | void | ) |
Create an empty mux router with the default bucket fan-out.
Create a new router.
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.
| 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.
| router | Router 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.
| 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.
| router | Router containing registered exact-match handlers. |
| req | Parsed HTTP request with method and path information. |
| res | HTTP response object passed through to the matched handler. |
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.