56 lines
1.6 KiB
C
56 lines
1.6 KiB
C
#ifndef ROUTER_H
|
|
#define ROUTER_H
|
|
|
|
#include "reactor/conn.h"
|
|
|
|
typedef struct router router_t;
|
|
typedef struct node node_t;
|
|
typedef void (*handler_t)(conn_t *c);
|
|
|
|
// Router node for tree-based matching
|
|
struct node {
|
|
char *segment; // path segment
|
|
handler_t handler; // handler for this path
|
|
node_t **children; // child nodes
|
|
int child_count; // number of children
|
|
int child_cap; // capacity of children array
|
|
int is_dynamic; // :param
|
|
int is_wildcard; // *path
|
|
char **param_names; // parameter names for this route
|
|
int param_count; // number of parameters
|
|
};
|
|
|
|
// HTTP router
|
|
struct router {
|
|
node_t *get;
|
|
node_t *post;
|
|
node_t *put;
|
|
node_t *patch;
|
|
node_t *delete;
|
|
|
|
// Parameter extraction buffer
|
|
char **param_values;
|
|
char **param_names;
|
|
int param_cap;
|
|
};
|
|
|
|
// Router operations
|
|
router_t* router_new(void);
|
|
void router_free(router_t *r);
|
|
|
|
// Route registration
|
|
int router_add(router_t *r, const char *method, const char *path, handler_t handler);
|
|
int router_get(router_t *r, const char *path, handler_t handler);
|
|
int router_post(router_t *r, const char *path, handler_t handler);
|
|
int router_put(router_t *r, const char *path, handler_t handler);
|
|
int router_delete(router_t *r, const char *path, handler_t handler);
|
|
|
|
// Route lookup
|
|
handler_t router_lookup(router_t *r, const char *method, const char *path, char ***params, char ***names, int *count);
|
|
|
|
// Parameter extraction from matched route
|
|
const char* router_param(conn_t *c, const char *name);
|
|
const char* router_param_idx(conn_t *c, int index);
|
|
|
|
#endif // ROUTER_H
|