ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
http_route.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4#ifndef PROTOCORE_HTTP_ROUTE_H
5#define PROTOCORE_HTTP_ROUTE_H
6
7#include "network_drivers/presentation/http/http.h" // the complete type a public struct below holds by value
8#include "protocore_config.h" // the entry point: protocore_types.h for the widths
9
11
12/**
13 * @file http_route.h
14 * @brief The route table: where a request goes.
15 *
16 * A row names a path pattern, a method, a handler, and the ws / sse / mount / credential id the
17 * request needs. Every one of those is HTTP, so the table sits under the HTTP root rather than at
18 * the network layer, which routes datagrams.
19 *
20 * The module exports one symbol, @ref HttpRoutes. Everything in route.c has internal linkage.
21 *
22 * @c work is bytes the CALLER holds. This module reads none of them: it carries nothing
23 * between calls, so there is no state to keep and nothing to wipe. The parameter is there so
24 * a caller drives every namespace the same way.
25 */
26
27// PROTOCORE_HTTP_ROUTES_BORROW - the bytes this module runs out of - is stated in protocore_config.h, which sums
28// it into its arena. Its size and its offset are each a static_assert, so a feature
29// combination that does not fit fails to compile rather than overrunning at run time.
30
31/** @brief Discriminates between HTTP, WebSocket, and SSE route entries. */
32typedef enum
33{
34 ROUTE_HTTP, ///< Standard HTTP request/response.
35#if PROTOCORE_ENABLE_WEBSOCKET
36 ROUTE_WS, ///< WebSocket upgrade route.
37#endif
38#if PROTOCORE_ENABLE_SSE
39 ROUTE_SSE, ///< Server-Sent Events route.
40#endif
41#if PROTOCORE_ENABLE_FILE_SERVING
42 ROUTE_STATIC, ///< Static-file subtree mount (serve_static()).
43#endif
44#if PROTOCORE_ENABLE_WEBDAV
45 ROUTE_DAV, ///< WebDAV subtree mount (dav()).
46#endif
48
49/**
50 * @brief Internal route entry stored in the routing table.
51 *
52 * Populated by on(), on_ws(), or on_sse().
53 * Application code does not interact with this struct directly.
54 */
55typedef struct HttpRoute
56{
57 char path[MAX_PATH_LEN]; ///< Null-terminated path pattern.
58 HttpRouteType type; ///< HTTP, WS, or SSE.
59 HttpMethod method; ///< HTTP method (ROUTE_HTTP only).
60 Handler callback; ///< HTTP handler (ROUTE_HTTP only).
61
62#if PROTOCORE_ENABLE_WEBSOCKET
63 /// The handler set this route serves, or PROTOCORE_WS_NONE. The handlers belong to the websocket
64 /// module: a route decides where a request goes, and what runs once the socket is open is not
65 /// routing's business.
66 uint8_t ws_id;
67#endif
68
69#if PROTOCORE_ENABLE_SSE
70 /// The handler this route serves, or PROTOCORE_SSE_NONE. The handler belongs to the sse module: a
71 /// route decides where a request goes, not what runs once a client subscribes.
72 uint8_t sse_id;
73#endif
74
75#if PROTOCORE_ENABLE_FILE_SERVING
76 /// The mount point this route serves, or PROTOCORE_MNT_NONE. The backend and subtree belong to mnt:
77 /// two registrars describe a mount the same way, so the description lives with mounting.
78 uint8_t mnt_id;
79#endif
80
81#if PROTOCORE_ENABLE_AUTH
82 /// The credential set this route needs, or PROTOCORE_AUTH_NONE. The credentials themselves belong to
83 /// the auth module: a route decides where a request goes, and key material in a routing entry is
84 /// a copy of a secret in a place that has no reason to hold one.
85 uint8_t auth_id; ///< Required password.
86#endif
87
88 proto_bool is_active; ///< `false` for unused table slots.
89 proto_bool is_wildcard; ///< `true` when path ends with `*`.
90 proto_bool is_param; ///< `true` when the path contains a `:name` segment.
91 proto_bool is_regex; ///< `true` when the path is a regex (see on_regex()).
92 protocore_if_kind iface_filter; ///< Interface gate; PROTOCORE_IF_ANY (0) = match any interface.
94
95/** @brief The table's storage. Declared, never defined here: the layout stays in route.c. */
97
98/** @brief Dispatch table. Addressed by offset, so the layout is asserted below. */
99typedef struct
100{
101 HttpRoute *(*add)(uint8_t *);
102 uint8_t (*count)(uint8_t *);
103 HttpRoute *(*at)(uint8_t *, uint8_t);
104 void (*reset)(uint8_t *);
106PROTOCORE_NS_LAYOUT(HttpRouteNs, add, count, at, reset);
107
108/**
109 * @brief Take the next free entry, zeroed and ready to fill, or NULL when .
110 * @param work PROTOCORE_HTTP_ROUTES_BORROW bytes the caller took. Not held past the call.
111 * @return The HttpRoute *.
112 */
114/**
115 * @brief Entries currently registered.
116 * @param work PROTOCORE_HTTP_ROUTES_BORROW bytes the caller took. Not held past the call.
117 * @return The uint8_t.
118 */
119uint8_t protocore_http_routes_count(uint8_t *work);
120/**
121 * @brief Entry i, or NULL if i is past the end.
122 * @param work PROTOCORE_HTTP_ROUTES_BORROW bytes the caller took. Not held past the call.
123 * @param i I
124 * @return The HttpRoute *.
125 */
126HttpRoute *protocore_http_routes_at(uint8_t *work, uint8_t i);
127/**
128 * @brief Empty the table. For tests: a case that does not reset matches .
129 * @param work PROTOCORE_HTTP_ROUTES_BORROW bytes the caller took. Not held past the call.
130 */
131void protocore_http_routes_reset(uint8_t *work);
132
133/**
134 * @brief The bytes every entry here runs out of: the one route table.
135 *
136 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where that
137 * borrow comes from. Every registrar and every reader drives the same table, so the bytes belong to
138 * this module rather than to any one caller. Taken once from the end of the secure pool, which no
139 * mark and no release walks, so the table lasts the life of the program.
140 *
141 * @return the span.
142 */
144
145/** @brief Module namespace. */
150
152
153#endif // PROTOCORE_HTTP_ROUTE_H
enum PROTO_ENUM_PACKED protocore_if_kind
What an interface is, and the filter that selects one.
#define MAX_PATH_LEN
Maximum URL path length (including leading /).
The HTTP root: the parts of the protocol that do not belong to one version.
HttpMethod
The request methods a route binds to.
Definition http.h:23
void(* Handler)(uint8_t slot_id, HttpReq *request)
A route's request handler.
Definition http.h:35
PROTOCORE_NS HttpRouteNs HttpRoutes PROTOCORE_UNUSED
Module namespace.
Definition http_route.h:146
HttpRoute * protocore_http_routes_at(uint8_t *work, uint8_t i)
Entry i, or NULL if i is past the end.
uint8_t protocore_http_routes_count(uint8_t *work)
Entries currently registered.
void protocore_http_routes_reset(uint8_t *work)
Empty the table. For tests: a case that does not reset matches .
struct HttpRouteCtx HttpRouteCtx
The table's storage. Declared, never defined here: the layout stays in route.c.
Definition http_route.h:96
HttpRoute * protocore_http_routes_add(uint8_t *work)
Take the next free entry, zeroed and ready to fill, or NULL when .
uint8_t * protocore_http_route_span(void)
The bytes every entry here runs out of: the one route table.
HttpRouteType
Discriminates between HTTP, WebSocket, and SSE route entries.
Definition http_route.h:33
@ ROUTE_HTTP
Standard HTTP request/response.
Definition http_route.h:34
#define PROTOCORE_NS_LAYOUT(T,...)
Pin every dispatch slot of a table that is nothing but function pointers.
#define PROTOCORE_NS
Storage for a dispatch table. The const is load bearing.
Dispatch table. Addressed by offset, so the layout is asserted below.
Definition http_route.h:100
HttpRoute *(* add)(uint8_t *)
Definition http_route.h:101
Internal route entry stored in the routing table.
Definition http_route.h:56
HttpMethod method
HTTP method (ROUTE_HTTP only).
Definition http_route.h:59
Handler callback
HTTP handler (ROUTE_HTTP only).
Definition http_route.h:60
protocore_if_kind iface_filter
Interface gate; PROTOCORE_IF_ANY (0) = match any interface.
Definition http_route.h:92
HttpRouteType type
HTTP, WS, or SSE.
Definition http_route.h:58
proto_bool is_wildcard
true when path ends with *.
Definition http_route.h:89
proto_bool is_regex
true when the path is a regex (see on_regex()).
Definition http_route.h:91
char path[MAX_PATH_LEN]
Null-terminated path pattern.
Definition http_route.h:57
proto_bool is_active
false for unused table slots.
Definition http_route.h:88
proto_bool is_param
true when the path contains a :name segment.
Definition http_route.h:90
#define PROTOCORE_BEGIN_DECLS
Give a header's declarations C linkage, so their symbol names carry no parameter types.
Definition types.h:96
_Bool proto_bool
The truth value.
Definition types.h:64
#define PROTOCORE_END_DECLS
Definition types.h:97