ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
http.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/**
5 * @file http.h
6 * @brief The HTTP root: the parts of the protocol that do not belong to one version.
7 *
8 * A status code, a method token, a route path match and an Allow list read the same on HTTP/1.1,
9 * HTTP/2 and HTTP/3, so they sit here and each version module calls them.
10 *
11 * The one symbol this module exports is @ref Http.
12 */
13
14#ifndef PROTOCORE_HTTP_H
15#define PROTOCORE_HTTP_H
16
18
20
21/** @brief The request methods a route binds to. */
22typedef enum
23{
24 HTTP_GET, ///< Safe, idempotent read
25 HTTP_POST, ///< Non-idempotent create / action
26 HTTP_PUT, ///< Idempotent replace
27 HTTP_DELETE, ///< Idempotent delete
28 HTTP_PATCH, ///< Partial update
29 HTTP_HEAD, ///< Same as GET but no response body
30 HTTP_OPTIONS, ///< Capability query / CORS preflight
31 HTTP_METHOD_UNKNOWN ///< Unrecognized method token, answered 501
33
34/** @brief A route's request handler. */
35typedef void (*Handler)(uint8_t slot_id, HttpReq *request);
36
37/** @brief RFC 9110 sec 9: a method, held either as the enum or as its token. */
38typedef struct
39{
40 HttpMethod method; ///< the method a name lookup names
41 const char *token; ///< a method token, or the one an Allow list appends
43
44/** @brief RFC 9110 sec 7.1 request target: the route pattern, and the path tested against it. */
45typedef struct
46{
47 const char *route; ///< the route path a match tests
48 proto_bool is_wildcard; ///< that route ends in a trailing star
49 const char *path; ///< the request path a match tests against
50 HttpReq *req; ///< the request a param capture writes into
52
53/** @brief RFC 9110 sec 10.2.1 Allow: the comma-separated list an append builds. */
54typedef struct
55{
56 char *buf; ///< where the list is built
57 size_t cap; ///< how much room it has
59
60/**
61 * @brief The version-agnostic HTTP surface.
62 *
63 * A caller sets the members a call takes, invokes it through ::Http, and reads the outcome off the
64 * same handle.
65 *
66 * @var HttpNs::slot the connection a call acts on
67 * @var HttpNs::code the status code a lookup names
68 * @var HttpNs::method_args a method, as an enum or as a token
69 * @var HttpNs::route_args the target a route is matched against
70 * @var HttpNs::allow the Allow list an append builds
71 * @var HttpNs::cb the handler a request runs when no route matched
72 * @var HttpNs::edge_poll the edge-cache origin fetch that owns a slot while it is in flight
73 * @var HttpNs::ok a call's true/false outcome
74 * @var HttpNs::text the reason phrase or method token a lookup reports
75 * @var HttpNs::method_of the enum a token parses to
76 * @var HttpNs::status_text the reason phrase for @c code; "Unknown" for one it has none for
77 * @var HttpNs::parse_method the enum for @c method_args.token, HTTP_METHOD_UNKNOWN for an unimplemented one
78 * @var HttpNs::method_name the canonical token for @c method_args.method, empty for HTTP_METHOD_UNKNOWN
79 * @var HttpNs::path_matches whether @c route_args.route matches @c route_args.path, exact or trailing star
80 * @var HttpNs::match_path_params whether a `:name` route matches, capturing each segment into @c route_args.req
81 * @var HttpNs::req_is_head whether the request on @c slot used HEAD
82 * @var HttpNs::allow_append add @c method_args.token to a comma-separated Allow list, skipping a repeat
83 * @var HttpNs::match_and_execute run a completed request on @c slot through the route table
84 * @var HttpNs::set_not_found install the handler a request runs when no route matched
85 * @var HttpNs::poll_slot the ProtoHandler on_poll for an HTTP slot: pumps, drains, dispatches
86 * @var HttpNs::reset drop every handler registered here, back to the built-in answers
87 * @var HttpNs::set_edge_poll install the edge-cache origin fetch
88 */
89
90typedef struct
91{
92 uint8_t slot; ///< the connection every call names
93 int code; ///< the status code a lookup names
94 Handler cb; ///< the handler a request runs when no route matched
95 HttpMethodArgs method_args; ///< a method, as an enum or as a token
96 HttpRouteArgs route_args; ///< the target a route is matched against
97 HttpAllowArgs allow; ///< the Allow list an append builds
98#if PROTOCORE_ENABLE_EDGE_CACHE
99 proto_bool (*edge_poll)(uint8_t slot);
100#endif
102 const char *text;
104#if PROTOCORE_ENABLE_EDGE_CACHE
105#endif
106} HttpVars;
107
108/** @brief The operands and the outcome. */
109extern HttpVars HttpV;
110
111/** @brief The entries. */
112typedef struct
113{
114 void (*const status_text)(uint8_t *work);
115 void (*const parse_method)(uint8_t *work);
116 void (*const method_name)(uint8_t *work);
117 void (*const path_matches)(uint8_t *work);
118 void (*const match_path_params)(uint8_t *work);
119 void (*const req_is_head)(uint8_t *work);
120 void (*const allow_append)(uint8_t *work);
121 void (*const match_and_execute)(uint8_t *work);
122 void (*const set_not_found)(uint8_t *work);
123 void (*const poll_slot)(uint8_t *work);
124 void (*const reset)(uint8_t *work);
125 void (*const set_edge_poll)(uint8_t *work);
126} HttpNs;
127
128// What the table binds, defined once in the .c and taking one parameter each: everything
129// else an entry needs is an operand in HttpV or a region of the borrow at a fixed offset.
130void protocore_http_status_text(uint8_t *work);
131void protocore_http_parse_method(uint8_t *work);
132void protocore_http_method_name(uint8_t *work);
133void protocore_http_path_matches(uint8_t *work);
135void protocore_http_req_is_head(uint8_t *work);
136void protocore_http_allow_append(uint8_t *work);
139void protocore_http_poll_slot(uint8_t *work);
140void protocore_http_reset(uint8_t *work);
142
143// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
144// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
145// `Http.status_text(work)` resolves to a named function and becomes a DIRECT call. An extern table
146// leaves the call indirect and the symbol live at every level, -O2 -flto included.
147static const HttpNs Http __attribute__((unused)) = {
149 .parse_method = protocore_http_parse_method,
150 .method_name = protocore_http_method_name,
151 .path_matches = protocore_http_path_matches,
152 .match_path_params = protocore_http_match_path_params,
153 .req_is_head = protocore_http_req_is_head,
154 .allow_append = protocore_http_allow_append,
155 .match_and_execute = protocore_http_match_and_execute,
156 .set_not_found = protocore_http_set_not_found,
157 .poll_slot = protocore_http_poll_slot,
158 .reset = protocore_http_reset,
159 .set_edge_poll = protocore_http_set_edge_poll,
160};
161
162/**
163 * @brief The PROTOCORE_HTTP_BORROW bytes this module's state lives in.
164 *
165 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
166 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
167 * walks, so the state lasts the life of the program.
168 *
169 * @return the span.
170 */
171uint8_t *protocore_http_span(void);
172
174
175#endif // PROTOCORE_HTTP_H
void protocore_http_path_matches(uint8_t *work)
void protocore_http_match_path_params(uint8_t *work)
void protocore_http_set_not_found(uint8_t *work)
void protocore_http_poll_slot(uint8_t *work)
HttpMethod
The request methods a route binds to.
Definition http.h:23
@ HTTP_PUT
Idempotent replace.
Definition http.h:26
@ HTTP_DELETE
Idempotent delete.
Definition http.h:27
@ HTTP_GET
Safe, idempotent read.
Definition http.h:24
@ HTTP_METHOD_UNKNOWN
Unrecognized method token, answered 501.
Definition http.h:31
@ HTTP_OPTIONS
Capability query / CORS preflight.
Definition http.h:30
@ HTTP_POST
Non-idempotent create / action.
Definition http.h:25
@ HTTP_HEAD
Same as GET but no response body.
Definition http.h:29
@ HTTP_PATCH
Partial update.
Definition http.h:28
uint8_t * protocore_http_span(void)
The PROTOCORE_HTTP_BORROW bytes this module's state lives in.
void(* Handler)(uint8_t slot_id, HttpReq *request)
A route's request handler.
Definition http.h:35
void protocore_http_method_name(uint8_t *work)
void protocore_http_allow_append(uint8_t *work)
void protocore_http_req_is_head(uint8_t *work)
void protocore_http_match_and_execute(uint8_t *work)
void protocore_http_parse_method(uint8_t *work)
HttpVars HttpV
The operands and the outcome.
void protocore_http_reset(uint8_t *work)
void protocore_http_set_edge_poll(uint8_t *work)
void protocore_http_status_text(uint8_t *work)
HttpParser..
RFC 9110 sec 10.2.1 Allow: the comma-separated list an append builds.
Definition http.h:55
char * buf
where the list is built
Definition http.h:56
size_t cap
how much room it has
Definition http.h:57
RFC 9110 sec 9: a method, held either as the enum or as its token.
Definition http.h:39
HttpMethod method
the method a name lookup names
Definition http.h:40
const char * token
a method token, or the one an Allow list appends
Definition http.h:41
The entries.
Definition http.h:113
void(*const status_text)(uint8_t *work)
Definition http.h:114
RFC 9110 sec 7.1 request target: the route pattern, and the path tested against it.
Definition http.h:46
const char * route
the route path a match tests
Definition http.h:47
const char * path
the request path a match tests against
Definition http.h:49
proto_bool is_wildcard
that route ends in a trailing star
Definition http.h:48
HttpReq * req
the request a param capture writes into
Definition http.h:50
HttpRouteArgs route_args
the target a route is matched against
Definition http.h:96
int code
the status code a lookup names
Definition http.h:93
HttpMethodArgs method_args
a method, as an enum or as a token
Definition http.h:95
HttpAllowArgs allow
the Allow list an append builds
Definition http.h:97
uint8_t slot
the connection every call names
Definition http.h:92
const char * text
Definition http.h:102
Handler cb
the handler a request runs when no route matched
Definition http.h:94
HttpMethod method_of
Definition http.h:103
proto_bool ok
Definition http.h:101
#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