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

mux.h interface. More...

#include <cwist/http.h>
#include <cwist/sstring.h>
Include dependency graph for mux.h:

Go to the source code of this file.

Classes

struct  cwist_mux_route
 
struct  cwist_mux_router
 

Typedefs

typedef void(* cwist_http_handler_func) (cwist_http_request *req, cwist_http_response *res)
 
typedef struct cwist_mux_route cwist_mux_route
 
typedef struct cwist_mux_router cwist_mux_router
 

Functions

Lifecycle
cwist_mux_router * cwist_mux_router_create (void)
 Create a new router.
 
void cwist_mux_router_destroy (cwist_mux_router *router)
 Destroy a router.
 
Route Management
void cwist_mux_handle (cwist_mux_router *router, cwist_http_method_t method, const char *path, cwist_http_handler_func handler)
 Register a route handler.
 
Dispatch
bool cwist_mux_serve (cwist_mux_router *router, cwist_http_request *req, cwist_http_response *res)
 Dispatch a request.
 

Detailed Description

mux.h interface.

Definition in file mux.h.

Typedef Documentation

◆ cwist_http_handler_func

typedef void(* cwist_http_handler_func) (cwist_http_request *req, cwist_http_response *res)

— Mux Router —

Definition at line 12 of file mux.h.

◆ cwist_mux_route

◆ cwist_mux_router

Function Documentation

◆ 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 route handler.

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.

◆ cwist_mux_router_create()

cwist_mux_router * cwist_mux_router_create ( void  )

Create a new router.

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 router.

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 
)

Dispatch a request.

Returns
True if a route was found and executed, false otherwise (404).

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.