Sagui library v3.5.0
Cross-platform library which helps to develop web servers or frameworks.
Loading...
Searching...
No Matches
Data Structures | Typedefs | Functions
Path routing

Data Structures

struct  sg_entrypoint
 
struct  sg_entrypoints
 
struct  sg_route
 
struct  sg_router
 

Typedefs

typedef int(* sg_entrypoints_iter_cb) (void *cls, struct sg_entrypoint *entrypoint)
 
typedef int(* sg_segments_iter_cb) (void *cls, unsigned int index, const char *segment)
 
typedef int(* sg_vars_iter_cb) (void *cls, const char *name, const char *val)
 
typedef void(* sg_route_cb) (void *cls, struct sg_route *route)
 
typedef int(* sg_routes_iter_cb) (void *cls, struct sg_route *route)
 
typedef int(* sg_router_dispatch_cb) (void *cls, const char *path, struct sg_route *route)
 
typedef int(* sg_router_match_cb) (void *cls, struct sg_route *route)
 

Functions

const char * sg_entrypoint_name (struct sg_entrypoint *entrypoint)
 
int sg_entrypoint_set_user_data (struct sg_entrypoint *entrypoint, void *data)
 
void * sg_entrypoint_user_data (struct sg_entrypoint *entrypoint)
 
struct sg_entrypointssg_entrypoints_new (void) __attribute__((malloc))
 
void sg_entrypoints_free (struct sg_entrypoints *entrypoints)
 
int sg_entrypoints_add (struct sg_entrypoints *entrypoints, const char *path, void *user_data)
 
int sg_entrypoints_rm (struct sg_entrypoints *entrypoints, const char *path)
 
int sg_entrypoints_iter (struct sg_entrypoints *entrypoints, sg_entrypoints_iter_cb cb, void *cls)
 
int sg_entrypoints_clear (struct sg_entrypoints *entrypoints)
 
int sg_entrypoints_find (struct sg_entrypoints *entrypoints, struct sg_entrypoint **entrypoint, const char *path)
 
void * sg_route_handle (struct sg_route *route)
 
void * sg_route_match (struct sg_route *route)
 
const char * sg_route_rawpattern (struct sg_route *route)
 
char * sg_route_pattern (struct sg_route *route) __attribute__((malloc))
 
const char * sg_route_path (struct sg_route *route)
 
int sg_route_segments_iter (struct sg_route *route, sg_segments_iter_cb cb, void *cls)
 
int sg_route_vars_iter (struct sg_route *route, sg_vars_iter_cb cb, void *cls)
 
void * sg_route_user_data (struct sg_route *route)
 
int sg_routes_add2 (struct sg_route **routes, struct sg_route **route, const char *pattern, char *errmsg, size_t errlen, sg_route_cb cb, void *cls)
 
bool sg_routes_add (struct sg_route **routes, const char *pattern, sg_route_cb cb, void *cls)
 
int sg_routes_rm (struct sg_route **routes, const char *pattern)
 
int sg_routes_iter (struct sg_route *routes, sg_routes_iter_cb cb, void *cls)
 
int sg_routes_next (struct sg_route **route)
 
unsigned int sg_routes_count (struct sg_route *routes)
 
int sg_routes_cleanup (struct sg_route **routes)
 
struct sg_routersg_router_new (struct sg_route *routes) __attribute__((malloc))
 
void sg_router_free (struct sg_router *router)
 
int sg_router_dispatch2 (struct sg_router *router, const char *path, void *user_data, sg_router_dispatch_cb dispatch_cb, void *cls, sg_router_match_cb match_cb)
 
int sg_router_dispatch (struct sg_router *router, const char *path, void *user_data)
 

Detailed Description

High-performance path routing.

Typedef Documentation

◆ sg_entrypoints_iter_cb

typedef int(* sg_entrypoints_iter_cb) (void *cls, struct sg_entrypoint *entrypoint)

Callback signature used by sg_entrypoints_iter() to iterate entry-point items.

Parameters
[out]clsUser-defined closure.
[out]entrypointCurrent iterated entry-point.
Return values
0Success.
E<ERROR>User-defined error to stop the items iteration.

◆ sg_segments_iter_cb

typedef int(* sg_segments_iter_cb) (void *cls, unsigned int index, const char *segment)

Callback signature used by sg_route_segments_iter() to iterate the path segments.

Parameters
[out]clsUser-defined closure.
[out]indexCurrent iterated item index.
[out]segmentCurrent iterated segment.
Return values
0Success.
E<ERROR>User-defined error to stop the segments iteration.

◆ sg_vars_iter_cb

typedef int(* sg_vars_iter_cb) (void *cls, const char *name, const char *val)

Callback signature used by sg_route_vars_iter() to iterate the path variables.

Parameters
[out]clsUser-defined closure.
[out]nameCurrent iterated variable name.
[out]valCurrent iterated variable value.
Return values
0Success.
E<ERROR>User-defined error to stop the variables iteration.

◆ sg_route_cb

typedef void(* sg_route_cb) (void *cls, struct sg_route *route)

Callback signature used to handle the path routing.

Parameters
[out]clsUser-defined closure.
[out]routeRoute handle.

◆ sg_routes_iter_cb

typedef int(* sg_routes_iter_cb) (void *cls, struct sg_route *route)

Callback signature used by sg_routes_iter() to iterate route items.

Parameters
[out]clsUser-defined closure.
[out]routeCurrent iterated route.
Return values
0Success.
E<ERROR>User-defined error to stop the route items iteration.

◆ sg_router_dispatch_cb

typedef int(* sg_router_dispatch_cb) (void *cls, const char *path, struct sg_route *route)

Callback signature used by sg_router_dispatch2 in the route dispatching loop.

Parameters
[out]clsUser-defined closure.
[out]pathRoute path as a null-terminated string.
[out]routeRoute handle.
Return values
0Success.
E<ERROR>User-defined error to stop the route dispatching loop.

◆ sg_router_match_cb

typedef int(* sg_router_match_cb) (void *cls, struct sg_route *route)

Callback signature used by sg_router_dispatch2 when the path matches the pattern before the route dispatching.

Parameters
[out]clsUser-defined closure.
[out]routeRoute handle.
Return values
0Success.
E<ERROR>User-defined error to stop the route dispatching.

Function Documentation

◆ sg_entrypoint_name()

const char * sg_entrypoint_name ( struct sg_entrypoint entrypoint)

Returns the name of the entry-point handle entrypoint.

Parameters
[in]entrypointEntry-point handle.
Returns
Entry-point name as a null-terminated string.
Return values
NULLIf the entrypoint is null and set the errno to EINVAL.

◆ sg_entrypoint_set_user_data()

int sg_entrypoint_set_user_data ( struct sg_entrypoint entrypoint,
void *  data 
)

Sets user data to the entry-point handle.

Parameters
[in]entrypointEntry-point handle.
[in]dataUser data pointer.
Return values
0Success.
EINVALInvalid argument.

◆ sg_entrypoint_user_data()

void * sg_entrypoint_user_data ( struct sg_entrypoint entrypoint)

Gets user data from the entry-point handle.

Parameters
[in]entrypointEntry-point handle.
Returns
User data pointer.
Return values
NULLIf entrypoint is null and set the errno to EINVAL.
Examples
example_entrypoint.c.

◆ sg_entrypoints_new()

struct sg_entrypoints * sg_entrypoints_new ( void  )

Creates a new entry-points handle.

Returns
Entry-points handle.
Return values
NULLIf no memory space is available.
Examples
example_entrypoint.c.

◆ sg_entrypoints_free()

void sg_entrypoints_free ( struct sg_entrypoints entrypoints)

Frees the entry-points handle previously allocated by sg_entrypoints_new().

Parameters
[in]entrypointsPointer of the entry-points to be freed.
Examples
example_entrypoint.c.

◆ sg_entrypoints_add()

int sg_entrypoints_add ( struct sg_entrypoints entrypoints,
const char *  path,
void *  user_data 
)

Adds a new entry-point item to the entry-points entrypoints.

Parameters
[in]entrypointsEntry-points handle.
[in]pathEntry-point path.
[in]user_dataUser data pointer.
Return values
0Success.
EINVALInvalid argument.
ENOMEMOut of memory.
EALREADYEntry-point already added.
Examples
example_entrypoint.c.

◆ sg_entrypoints_rm()

int sg_entrypoints_rm ( struct sg_entrypoints entrypoints,
const char *  path 
)

Removes an entry-point item from the entry-points entrypoints.

Parameters
[in]entrypointsEntry-points handle.
[in]pathEntry-point path to be removed.
Return values
0Success.
EINVALInvalid argument.
ENOMEMOut of memory.
ENOENTEntry-point already removed.

◆ sg_entrypoints_iter()

int sg_entrypoints_iter ( struct sg_entrypoints entrypoints,
sg_entrypoints_iter_cb  cb,
void *  cls 
)

Iterates over entry-point items.

Parameters
[in]entrypointsEntry-points handle.
[in]cbCallback to iterate the entry-point items.
[in,out]clsUser-specified value.
Return values
0Success.
EINVALInvalid argument.
Returns
Callback result when it is different from 0.

◆ sg_entrypoints_clear()

int sg_entrypoints_clear ( struct sg_entrypoints entrypoints)

Clears all existing entry-point items in the entry-points entrypoints.

Parameters
[in]entrypointsEntry-points handle.
Return values
0Success.
EINVALInvalid argument.

◆ sg_entrypoints_find()

int sg_entrypoints_find ( struct sg_entrypoints entrypoints,
struct sg_entrypoint **  entrypoint,
const char *  path 
)

Finds an entry-point item by path.

Parameters
[in]entrypointsEntry-points handle.
[in,out]entrypointPointer of the variable to store the found entry-point.
[in]pathEntry-point path to be found.
Return values
0Success.
EINVALInvalid argument.
ENOMEMOut of memory.
ENOENTPair not found.
Examples
example_entrypoint.c.

◆ sg_route_handle()

void * sg_route_handle ( struct sg_route route)

Returns the PCRE2 handle containing the compiled regex code.

Parameters
[in]routeRoute handle.
Returns
PCRE2 handle containing the compiled regex code.
Return values
NULLIf route is null and set the errno to EINVAL.

◆ sg_route_match()

void * sg_route_match ( struct sg_route route)

Returns the PCRE2 match data created from the route pattern.

Parameters
[in]routeRoute handle.
Returns
PCRE2 match data.
Return values
NULLIf route is null and set the errno to EINVAL.

◆ sg_route_rawpattern()

const char * sg_route_rawpattern ( struct sg_route route)

Returns the raw route pattern. For example, given a pattern /foo, the raw pattern is ^/foo$.

Parameters
[in]routeRoute handle.
Returns
Raw pattern as a null-terminated string.
Return values
NULLIf route is null and set the errno to EINVAL.

◆ sg_route_pattern()

char * sg_route_pattern ( struct sg_route route)

Returns the route pattern.

Parameters
[in]routeRoute handle.
Returns
Pattern as a null-terminated string.
Return values
NULL
  • If route is null and set the errno to EINVAL.
  • If no memory space is available and set the errno to ENOMEM.
Warning
The caller must free the returned value.

◆ sg_route_path()

const char * sg_route_path ( struct sg_route route)

Returns the route path.

Parameters
[in]routeRoute handle.
Returns
Path component as a null-terminated string.
Return values
NULLIf route is null and set the errno to EINVAL.
Examples
example_entrypoint.c, example_router_segments.c, example_router_simple.c, and example_router_vars.c.

◆ sg_route_segments_iter()

int sg_route_segments_iter ( struct sg_route route,
sg_segments_iter_cb  cb,
void *  cls 
)

Iterates over path segments.

Parameters
[in]routeRoute handle.
[in]cbCallback to iterate the path segments.
[in,out]clsUser-specified value.
Return values
0Success.
EINVALInvalid argument.
Returns
Callback result when it is different from 0.
Examples
example_router_segments.c.

◆ sg_route_vars_iter()

int sg_route_vars_iter ( struct sg_route route,
sg_vars_iter_cb  cb,
void *  cls 
)

Iterates over path variables.

Parameters
[in]routeRoute handle.
[in]cbCallback to iterate the path variables.
[in,out]clsUser-specified value.
Return values
0Success.
EINVALInvalid argument.
ENOMEMOut of memory.
Returns
Callback result when it is different from 0.
Examples
example_router_srv.c, and example_router_vars.c.

◆ sg_route_user_data()

void * sg_route_user_data ( struct sg_route route)

Gets user data from the route handle.

Parameters
[in]routeRoute handle.
Returns
User data pointer.
Return values
NULLIf route is null and set the errno to EINVAL.
Examples
example_router_srv.c.

◆ sg_routes_add2()

int sg_routes_add2 ( struct sg_route **  routes,
struct sg_route **  route,
const char *  pattern,
char *  errmsg,
size_t  errlen,
sg_route_cb  cb,
void *  cls 
)

Adds a route item to the route list routes.

Parameters
[in,out]routesRoute list pointer to add a new route item.
[in,out]routePointer of the variable to store the added route reference.
[in]patternPattern as a null-terminated string. It must be a valid regular expression in PCRE2 syntax.
[in,out]errmsgPointer of a string to store the error message.
[in]errlenLength of the error message.
[in]cbCallback to handle the path routing.
[in]clsUser-defined closure.
Return values
0Success.
EINVALInvalid argument.
EALREADYRoute already added.
ENOMEMOut of memory.
Note
The pattern is enclosed between ^ and $ automatically if it does not start with (.
The escape sequence \K is not supported. It causes EINVAL if used.
The pattern is compiled using just-in-time optimization (JIT) when it is supported.

◆ sg_routes_add()

bool sg_routes_add ( struct sg_route **  routes,
const char *  pattern,
sg_route_cb  cb,
void *  cls 
)

Adds a route item to the route list routes. It uses the stderr to print the validation errors.

Parameters
[in,out]routesRoute list pointer to add a new route item.
[in]patternPattern as a null-terminated string. It must be a valid regular expression in PCRE2 syntax.
[in]cbCallback to handle the path routing.
[in]clsUser-defined closure.
Return values
0Success.
EINVALInvalid argument.
EALREADYRoute already added.
ENOMEMOut of memory.
Note
The pattern is enclosed between ^ and $ automatically if it does not start with (.
The escape sequence \K is not supported. It causes EINVAL if used.
The pattern is compiled using just-in-time optimization (JIT) when it is supported.
Examples
example_entrypoint.c, example_router_segments.c, example_router_simple.c, example_router_srv.c, and example_router_vars.c.

◆ sg_routes_rm()

int sg_routes_rm ( struct sg_route **  routes,
const char *  pattern 
)

Removes a route item from the route list routes.

Parameters
[in,out]routesRoute list pointer to add a new route item.
[in]patternPattern as a null-terminated string of the route to be removed.
Return values
0Success.
EINVALInvalid argument.
ENOENTRoute already removed.

◆ sg_routes_iter()

int sg_routes_iter ( struct sg_route routes,
sg_routes_iter_cb  cb,
void *  cls 
)

Iterates over all the routes in the route list routes.

Parameters
[in]routesRoute list handle.
[in]cbCallback to iterate over route items.
[in]clsUser-defined closure.
Return values
0Success.
EINVALInvalid argument.
E<ERROR>User-defined error to abort the list iteration.

◆ sg_routes_next()

int sg_routes_next ( struct sg_route **  route)

Returns the next route into the route list.

Parameters
[in,out]routePointer to the next route item.
Return values
0Success.
EINVALInvalid argument.

◆ sg_routes_count()

unsigned int sg_routes_count ( struct sg_route routes)

Counts the total routes in the route list routes.

Parameters
[in]routesRoute list handle.
Returns
Total of route items.
Return values
0If the list is empty or null.

◆ sg_routes_cleanup()

int sg_routes_cleanup ( struct sg_route **  routes)

Cleans the entire route list.

Parameters
[in,out]routesPointer to the route list.
Examples
example_entrypoint.c, example_router_segments.c, example_router_simple.c, example_router_srv.c, and example_router_vars.c.

◆ sg_router_new()

struct sg_router * sg_router_new ( struct sg_route routes)

Creates a new path router handle. It requires a filled route list routes.

Parameters
[in]routesRoute list handle.
Returns
New router handle.
Return values
NULLIf the routes is null and set the errno to EINVAL or no memory space.
Examples
example_entrypoint.c, example_router_segments.c, example_router_simple.c, example_router_srv.c, and example_router_vars.c.

◆ sg_router_free()

void sg_router_free ( struct sg_router router)

Frees the router handle previously allocated by sg_router_new().

Parameters
[in]routerRouter handle.
Examples
example_entrypoint.c, example_router_segments.c, example_router_simple.c, example_router_srv.c, and example_router_vars.c.

◆ sg_router_dispatch2()

int sg_router_dispatch2 ( struct sg_router router,
const char *  path,
void *  user_data,
sg_router_dispatch_cb  dispatch_cb,
void *  cls,
sg_router_match_cb  match_cb 
)

Dispatches a route that its pattern matches the path passed in path.

Parameters
[in]routerRouter handle.
[in]pathPath to dispatch a route.
[in]user_dataUser data pointer to be held by the route.
[in]dispatch_cbCallback triggered for each route item in the route dispatching loop.
[in]clsUser-defined closure passed to the dispatch_cb and match_cb callbacks.
[in]match_cbCallback triggered when the path matches the route pattern.
Return values
0Success.
EINVALInvalid argument.
ENOENTRoute not found or path not matched.
E<ERROR>User-defined error in dispatch_cb or match_cb.
Note
The route callback sg_route_cb is triggered when the path matches the route pattern.
The match logic uses just-in-time optimization (JIT) when it is supported.

◆ sg_router_dispatch()

int sg_router_dispatch ( struct sg_router router,
const char *  path,
void *  user_data 
)

Dispatches a route that its pattern matches the path passed in path.

Parameters
[in]routerRouter handle.
[in]pathPath to dispatch a route.
[in]user_dataUser data pointer to be held by the route.
Return values
0Success.
EINVALInvalid argument.
ENOENTRoute not found or path not matched.
E<ERROR>User-defined error in dispatch_cb or match_cb.
Note
The route callback sg_route_cb is triggered when the path matches the route pattern.
The match logic uses just-in-time optimization (JIT) when it is supported.
Examples
example_entrypoint.c, example_router_segments.c, example_router_simple.c, example_router_srv.c, and example_router_vars.c.