lnm/include/lnm/http/route.h

98 lines
2.7 KiB
C

#ifndef LNM_HTTP_ROUTE
#define LNM_HTTP_ROUTE
#include <stdlib.h>
#include "lnm/common.h"
#include "lnm/http/consts.h"
#define LNM_HTTP_MAX_KEY_SEGMENTS 4
typedef enum lnm_http_step_err {
lnm_http_step_err_done = 0,
lnm_http_step_err_io_needed,
lnm_http_step_err_close,
lnm_http_step_err_res,
} lnm_http_step_err;
typedef lnm_http_step_err (*lnm_http_step_fn)(lnm_http_conn *conn);
typedef struct lnm_http_route lnm_http_route;
typedef struct lnm_http_route_match_segment {
size_t start;
size_t len;
} lnm_http_route_match_segment;
typedef struct lnm_http_route_match {
const lnm_http_route *route;
lnm_http_method method;
lnm_http_route_match_segment key_segments[LNM_HTTP_MAX_KEY_SEGMENTS];
} lnm_http_route_match;
typedef struct lnm_http_router lnm_http_router;
typedef enum lnm_http_route_err {
lnm_http_route_err_match = 0,
lnm_http_route_err_unknown_route = 1,
lnm_http_route_err_unknown_method = 2,
} lnm_http_route_err;
/**
* Allocate and initialize a new http_router.
*/
lnm_err lnm_http_router_init(lnm_http_router **out);
void lnm_http_router_free(lnm_http_router *router);
/**
* Insert a new path & method in the given router, returning a handle to the
* newly created route struct.
*/
lnm_err lnm_http_router_add(lnm_http_route **out, lnm_http_router *http_router,
lnm_http_method method, const char *path);
/**
* Checks whether the two routers have any conflicting parts.
*/
bool lnm_http_router_conflicts(const lnm_http_router *r1,
const lnm_http_router *r2);
/**
* Merge two routers, with the result ending up in r1. This is equivalent to
* nesting a router on '/'.
*/
lnm_err lnm_http_router_merge(lnm_http_router *r1, lnm_http_router *r2);
/**
* Integrate the child router into the parent routing, mounting its paths on the
* given prefix.
*/
lnm_err lnm_http_router_nest(lnm_http_router *parent, lnm_http_router *child,
const char *prefix);
/**
* Route the given path & method.
*/
lnm_http_route_err lnm_http_router_route(lnm_http_route_match *out,
const lnm_http_router *router,
lnm_http_method method,
const char *path);
/**
* Retrieve a path segment using its name.
*
* @return NULL if not found, otherwise pointer to the match segment struct
* representing the key
*/
const lnm_http_route_match_segment *
lnm_http_route_match_get(lnm_http_route_match *match, const char *key);
/**
* Append the given step function to the route's step list.
*/
lnm_err lnm_http_route_step_append(lnm_http_route *route, lnm_http_step_fn fn,
bool blocking);
#endif