Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_http_router.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
66
67#ifndef __NILOREA_HTTP_ROUTER_HEADER
68#define __NILOREA_HTTP_ROUTER_HEADER
69
70#ifdef __cplusplus
71extern "C" {
72#endif
73
78
79#include "nilorea/n_http_req.h"
80
81#include <stddef.h>
82
84#define N_ROUTE_GET N_HTTP_REQ_METHOD_BIT(N_HTTP_REQ_GET)
86#define N_ROUTE_POST N_HTTP_REQ_METHOD_BIT(N_HTTP_REQ_POST)
88#define N_ROUTE_HEAD N_HTTP_REQ_METHOD_BIT(N_HTTP_REQ_HEAD)
89
91#define N_ROUTE_PARAMS_MAX 8
93#define N_ROUTE_NAME_MAX 31
94
95struct N_ROUTE_MATCH;
96
98typedef int (*n_route_handler)(void* ctx, const struct N_ROUTE_MATCH* match);
99
101typedef struct N_ROUTE {
103 unsigned methods;
105 const char* pattern;
109 const void* data;
110} N_ROUTE;
111
127
135
137typedef struct N_ROUTER N_ROUTER;
138
142
145void n_router_free(N_ROUTER** r);
146
159int n_router_add(N_ROUTER* r, const N_ROUTE* routes, size_t count);
160
167int n_router_check(const N_ROUTER* r, char* err, size_t errsz);
168
170size_t n_router_count(const N_ROUTER* r);
171
176const N_ROUTE* n_router_route(const N_ROUTER* r, size_t i);
177
187N_ROUTER_RESULT n_router_match(const N_ROUTER* r, n_http_req_method method, const char* path, size_t len, N_ROUTE_MATCH* match, unsigned* allowed);
188
194const char* n_route_param(const N_ROUTE_MATCH* m, const char* name, size_t* len);
195
201int n_router_allow_header(unsigned allowed, char* out, size_t size);
202
206
207#ifdef __cplusplus
208}
209#endif
210
211#endif /* __NILOREA_HTTP_ROUTER_HEADER */
LIST * routes
n_http_req_method
The methods accepted.
Definition n_http_req.h:77
#define N_HTTP_REQ_PATH_MAX
Longest origin-form request target ('/'-prefixed, query included).
Definition n_http_req.h:65
const char * names[8]
placeholder names, in pattern order
const char * pattern
the pattern, see the file documentation
size_t lens[8]
decoded value lengths
const char * values[8]
decoded values (NUL-terminated, inside buf)
n_route_handler handler
what the caller runs on a match
size_t nb_params
number of placeholders
char buf[2048u+8+1]
storage for the decoded values
unsigned methods
accepted methods, an OR of N_ROUTE_GET / N_ROUTE_POST / N_ROUTE_HEAD
const N_ROUTE * route
the route that matched
const void * data
caller data for this route (a policy: auth, CSRF, limits...)
N_ROUTER_RESULT
result of n_router_match()
N_ROUTER * n_router_new(void)
new, empty router
int n_router_allow_header(unsigned allowed, char *out, size_t size)
write an Allow header value ("GET, HEAD, POST") for a method mask
N_ROUTER_RESULT n_router_match(const N_ROUTER *r, n_http_req_method method, const char *path, size_t len, N_ROUTE_MATCH *match, unsigned *allowed)
find the route for a method and a path
int(* n_route_handler)(void *ctx, const struct N_ROUTE_MATCH *match)
a route handler: ctx is the caller's request context
int n_router_check(const N_ROUTER *r, char *err, size_t errsz)
refuse ambiguous routes: two routes sharing a method that can match the same path with the same speci...
void n_router_free(N_ROUTER **r)
free a router and set *r to NULL (the route tables are not touched)
size_t n_router_count(const N_ROUTER *r)
number of routes added
const N_ROUTE * n_router_route(const N_ROUTER *r, size_t i)
the i-th route added (to check caller policies at start-up)
int n_router_add(N_ROUTER *r, const N_ROUTE *routes, size_t count)
compile and add a route table
#define N_ROUTE_PARAMS_MAX
most placeholders in one route
const char * n_route_param(const N_ROUTE_MATCH *m, const char *name, size_t *len)
decoded value of a placeholder
@ N_ROUTER_BAD_REQUEST
the path itself is invalid: 400
@ N_ROUTER_FOUND
a route matched path and method
@ N_ROUTER_NOT_FOUND
no route matches the path: 404 or a fallback
@ N_ROUTER_METHOD_NOT_ALLOWED
the path matches, the method does not: 405
one route of a table
a successful match: the route and its decoded placeholder values
Pure, strict, bounded parser for an HTTP/1.1 request head.
a compiled router (opaque)