Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_http_router.c File Reference

HTTP router (see n_http_router.h). More...

#include "nilorea/n_http_router.h"
#include "nilorea/n_common.h"
#include "nilorea/n_log.h"
#include "nilorea/n_str.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for n_http_router.c:

Go to the source code of this file.

Data Structures

struct  _compiled
struct  _seg
struct  N_ROUTER
 a compiled router (opaque) More...

Macros

#define N_ROUTER_SEGS_MAX   32

Enumerations

enum  _seg_rank {
  _SEG_LITERAL = 0 , _SEG_DIGITS = 1 , _SEG_HEX = 1 , _SEG_INT = 2 ,
  _SEG_SLUG = 3 , _SEG_ANY = 4 , _SEG_REST = 5
}
enum  _seg_type {
  _T_LITERAL , _T_DIGITS , _T_HEX , _T_INT ,
  _T_SLUG , _T_ANY , _T_REST
}

Functions

static int _compile (const N_ROUTE *route, _compiled *c, const char **why)
static size_t _decode (const char *s, size_t n, char *dst)
static int _hexval (char c)
static int _literal_byte_ok (unsigned char c)
static unsigned _methods (unsigned m)
static int _more_specific (const _compiled *a, const _compiled *b)
static int _parse_n (const char *s, size_t len, size_t lo, size_t hi, size_t *n)
static int _rank (_seg_type t)
static int _route_fits (const _compiled *c, const char **seg, const size_t *seglen, int nseg, int trailing)
static int _seg_fits (const _seg *g, const char *s, size_t n)
static int _split (const char *path, size_t len, const char **seg, size_t *seglen, int *trailing)
static int _types_overlap (const _seg *a, const _seg *b)
const char * n_route_param (const N_ROUTE_MATCH *m, const char *name, size_t *len)
 decoded value of a placeholder
int n_router_add (N_ROUTER *r, const N_ROUTE *routes, size_t count)
 compile and add a route table; nothing is added when a route is refused
int n_router_allow_header (unsigned allowed, char *out, size_t size)
 write an Allow header value for a method mask (GET implies HEAD)
int n_router_check (const N_ROUTER *r, char *err, size_t errsz)
 refuse two routes sharing a method that can match the same path with the same specificity
size_t n_router_count (const N_ROUTER *r)
 number of routes added
void n_router_free (N_ROUTER **r)
 free a router and set *r to NULL (the route tables are not touched)
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
N_ROUTER * n_router_new (void)
 new, empty router
const N_ROUTE * n_router_route (const N_ROUTER *r, size_t i)
 the i-th route added

Detailed Description

HTTP router (see n_http_router.h).

Author
Castagnier Mickael
Version
1.0
Date
2026

Definition in file n_http_router.c.


Data Structure Documentation

◆ _compiled

struct _compiled

Definition at line 70 of file n_http_router.c.

Collaboration diagram for _compiled:
Data Fields
char names[8][31+1]
size_t nb_params
size_t nb_segs
const N_ROUTE * route
_seg segs[32]
int trailing_slash

◆ _seg

struct _seg

Definition at line 62 of file n_http_router.c.

Collaboration diagram for _seg:
Data Fields
const char * lit
size_t lit_len
size_t n
int param
_seg_type type

◆ N_ROUTER

struct N_ROUTER

a compiled router (opaque)

Examples
ex_http_router.c.

Definition at line 79 of file n_http_router.c.

Collaboration diagram for N_ROUTER:
Data Fields
size_t nb
_compiled * routes

Macro Definition Documentation

◆ N_ROUTER_SEGS_MAX

#define N_ROUTER_SEGS_MAX   32

Definition at line 39 of file n_http_router.c.

Referenced by _compile(), _split(), and n_router_match().

Enumeration Type Documentation

◆ _seg_rank

enum _seg_rank
Enumerator
_SEG_LITERAL 
_SEG_DIGITS 
_SEG_HEX 
_SEG_INT 
_SEG_SLUG 
_SEG_ANY 
_SEG_REST 

Definition at line 42 of file n_http_router.c.

◆ _seg_type

enum _seg_type
Enumerator
_T_LITERAL 
_T_DIGITS 
_T_HEX 
_T_INT 
_T_SLUG 
_T_ANY 
_T_REST 

Definition at line 52 of file n_http_router.c.

Function Documentation

◆ _compile()

◆ _decode()

size_t _decode ( const char * s,
size_t n,
char * dst )
static

Definition at line 422 of file n_http_router.c.

References _hexval(), and n_str_utf8_valid().

Referenced by n_router_match().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _hexval()

int _hexval ( char c)
static

Definition at line 410 of file n_http_router.c.

Referenced by _decode().

Here is the caller graph for this function:

◆ _literal_byte_ok()

int _literal_byte_ok ( unsigned char c)
static

Definition at line 138 of file n_http_router.c.

Referenced by _compile().

Here is the caller graph for this function:

◆ _methods()

unsigned _methods ( unsigned m)
static

Definition at line 319 of file n_http_router.c.

References N_ROUTE_GET, and N_ROUTE_HEAD.

Referenced by n_router_allow_header(), n_router_check(), and n_router_match().

Here is the caller graph for this function:

◆ _more_specific()

int _more_specific ( const _compiled * a,
const _compiled * b )
static

Definition at line 444 of file n_http_router.c.

References _rank(), _compiled::nb_segs, _compiled::segs, and _seg::type.

Referenced by n_router_match().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _parse_n()

int _parse_n ( const char * s,
size_t len,
size_t lo,
size_t hi,
size_t * n )
static

Definition at line 125 of file n_http_router.c.

Referenced by _compile().

Here is the caller graph for this function:

◆ _rank()

int _rank ( _seg_type t)
static

Definition at line 84 of file n_http_router.c.

References _SEG_ANY, _SEG_DIGITS, _SEG_HEX, _SEG_INT, _SEG_LITERAL, _SEG_REST, _SEG_SLUG, _T_ANY, _T_DIGITS, _T_HEX, _T_INT, _T_LITERAL, and _T_SLUG.

Referenced by _more_specific(), _types_overlap(), and n_router_check().

Here is the caller graph for this function:

◆ _route_fits()

int _route_fits ( const _compiled * c,
const char ** seg,
const size_t * seglen,
int nseg,
int trailing )
static

Definition at line 480 of file n_http_router.c.

References _seg_fits(), _T_REST, _compiled::nb_segs, _compiled::segs, _compiled::trailing_slash, and _seg::type.

Referenced by n_router_match().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ _seg_fits()

int _seg_fits ( const _seg * g,
const char * s,
size_t n )
static

Definition at line 376 of file n_http_router.c.

References _T_ANY, _T_DIGITS, _T_HEX, _T_INT, _T_LITERAL, _T_SLUG, _seg::lit, _seg::lit_len, _seg::n, and _seg::type.

Referenced by _route_fits().

Here is the caller graph for this function:

◆ _split()

int _split ( const char * path,
size_t len,
const char ** seg,
size_t * seglen,
int * trailing )
static

Definition at line 455 of file n_http_router.c.

References N_ROUTER_SEGS_MAX.

Referenced by n_router_match().

Here is the caller graph for this function:

◆ _types_overlap()

int _types_overlap ( const _seg * a,
const _seg * b )
static

Definition at line 326 of file n_http_router.c.

References _rank(), _SEG_DIGITS, _seg::n, and _seg::type.

Referenced by n_router_check().

Here is the call graph for this function:
Here is the caller graph for this function: