KEEL 1.0.0
Minimal C11 HTTP client/server library built on epoll/kqueue/io_uring/poll
Loading...
Searching...
No Matches
router.h
Go to the documentation of this file.
1#ifndef KEEL_ROUTER_H
2#define KEEL_ROUTER_H
3
4#include <keel/allocator.h>
5#include <keel/request.h>
6#include <keel/response.h>
7#include <keel/body_reader.h>
8#include <stddef.h>
9
11typedef void (*KlHandler)(KlRequest *req, KlResponse *res, void *user_data);
12
18typedef int (*KlMiddleware)(KlRequest *req, KlResponse *res, void *user_data);
19
21
32
33typedef struct {
34 const char *method;
35 const char *pattern;
36 size_t method_len;
37 size_t pattern_len;
39 void *user_data;
41
57
65
76int kl_router_add(KlRouter *r, const char *method, const char *pattern,
77 KlHandler handler, void *user_data,
78 KlBodyReaderFactory body_reader);
79
93int kl_router_match(KlRouter *r, const char *method, size_t method_len,
94 const char *path, size_t path_len,
95 KlRoute **matched, KlParam *params, int *num_params);
96
106int kl_router_use(KlRouter *r, const char *method, const char *pattern,
107 KlMiddleware fn, void *user_data);
108
122int kl_router_use_post(KlRouter *r, const char *method, const char *pattern,
123 KlMiddleware fn, void *user_data);
124
130
136
139
140#endif
KlBodyReader *(* KlBodyReaderFactory)(KlAllocator *alloc, const KlRequest *req, void *user_data)
Factory creates a body reader for a given request.
Definition body_reader.h:34
int kl_router_match(KlRouter *r, const char *method, size_t method_len, const char *path, size_t path_len, KlRoute **matched, KlParam *params, int *num_params)
Match a request against registered routes. HEAD requests automatically fall back to GET routes.
int kl_router_use_post(KlRouter *r, const char *method, const char *pattern, KlMiddleware fn, void *user_data)
Register post-body middleware that runs after body reading.
int kl_router_run_post_middleware(KlRouter *r, KlRequest *req, KlResponse *res)
Run all matching post-body middleware in registration order.
int kl_router_add(KlRouter *r, const char *method, const char *pattern, KlHandler handler, void *user_data, KlBodyReaderFactory body_reader)
Register a route. Pattern supports :param segments (e.g. "/users/:id").
void(* KlHandler)(KlRequest *req, KlResponse *res, void *user_data)
Route handler function.
Definition router.h:11
void kl_router_free(KlRouter *r)
Free router resources.
int kl_router_run_middleware(KlRouter *r, KlRequest *req, KlResponse *res)
Run all matching pre-body middleware in registration order.
int(* KlMiddleware)(KlRequest *req, KlResponse *res, void *user_data)
Middleware function signature.
Definition router.h:18
int kl_router_init(KlRouter *r, KlAllocator *alloc)
Initialize a router with an empty route table.
int kl_router_use(KlRouter *r, const char *method, const char *pattern, KlMiddleware fn, void *user_data)
Register pre-body middleware that runs before body reading.
Bring-your-own allocator vtable.
Definition allocator.h:12
Definition router.h:33
size_t pattern_len
Definition router.h:37
void * user_data
Definition router.h:39
size_t method_len
Definition router.h:36
const char * method
Definition router.h:34
KlMiddleware fn
Definition router.h:38
const char * pattern
Definition router.h:35
Definition request.h:14
Definition request.h:26
Definition response.h:21
Definition router.h:22
const char * pattern
Definition router.h:24
KlWsServerConfig * ws_config
Definition router.h:30
size_t pattern_len
Definition router.h:26
const char * method
Definition router.h:23
size_t method_len
Definition router.h:25
void * user_data
Definition router.h:28
KlBodyReaderFactory body_reader
Definition router.h:29
KlHandler handler
Definition router.h:27
Definition router.h:42
int post_mw_count
Definition router.h:52
int capacity
Definition router.h:45
KlRoute * routes
Definition router.h:43
KlMiddlewareEntry * middleware
Definition router.h:47
KlMiddlewareEntry * post_middleware
Definition router.h:51
int mw_count
Definition router.h:48
int count
Definition router.h:44
int mw_capacity
Definition router.h:49
KlAllocator * alloc
Definition router.h:55
int post_mw_capacity
Definition router.h:53
Definition websocket_server.h:34