ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
http_parser.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_PARSER_H
5#define PROTOCORE_HTTP_PARSER_H
6
7#include "protocore_config.h" // the entry point: protocore_types.h for the widths
8
10
11/**
12 * @file http_parser.h
13 * @brief HttpParser..
14 *
15 * @c work is PROTOCORE_HTTP_PARSER_BORROW bytes the CALLER took, at an address it knows. It is not held past the call,
16 * so nothing here aliases it. How those bytes are carved is this module's and is never named here.
17 */
18
35
42
43typedef struct
44{
45 char key[MAX_KEY_LEN];
46 char val[MAX_VAL_LEN];
47} Header;
48
49typedef struct
50{
51 char key[QUERY_KEY_LEN];
52 char val[QUERY_VAL_LEN];
54
55typedef struct HttpReq
56{
57 uint8_t slot_id;
60 uint32_t _version_hash;
61
64 size_t path_idx;
65
67 size_t query_idx;
69 uint8_t query_count;
70
73
74#if PROTOCORE_CAPTURE_AUTH_HEADER
75 char authorization[PROTOCORE_AUTH_HDR_CAP];
76 uint16_t auth_idx;
77 proto_bool cur_is_auth;
78#endif
79
81 uint8_t header_count;
83
86
89 uint8_t host_count;
91
92 uint8_t body[BODY_BUF_SIZE + 1];
93 size_t body_len;
94
95#if PROTOCORE_ENABLE_STREAM_BODY
96 proto_bool body_streaming;
97#endif
99
101
102typedef void (*HttpStreamDataCb)(HttpReq *req, const uint8_t *data, size_t len);
103
104typedef void (*HttpStreamAbortCb)(HttpReq *req);
105
106/**
107 * @brief The per-slot request table every HTTP layer parses into and reads back out of.
108 *
109 * Not an entry: it is the shared cross-TU substrate a caller indexes by slot, not something the
110 * namespace hands out, so it is reached by name.
111 */
113
114/** @brief Dispatch table. Addressed by offset, so the layout is asserted below. */
115typedef struct
116{
117 void (*set_stream_hooks)(uint8_t *, HttpStreamBeginCb, HttpStreamDataCb, HttpStreamAbortCb);
118 void (*reset)(uint8_t *, HttpReq *);
119 void (*feed)(uint8_t *, HttpReq *, uint8_t);
120 const char *(*get_header)(uint8_t *, const HttpReq *, const char *);
121 proto_bool (*get_cookie)(uint8_t *, const HttpReq *, const char *, char *, size_t);
122 proto_bool (*forwarded_client)(uint8_t *, const HttpReq *, char *, size_t, proto_bool *);
123 const char *(*get_query)(uint8_t *, const HttpReq *, const char *);
124 proto_bool (*get_form)(uint8_t *, const HttpReq *, const char *, char *, size_t);
125 const char *(*get_param)(uint8_t *, const HttpReq *, const char *);
127PROTOCORE_NS_LAYOUT(HttpParserNs, set_stream_hooks, reset, feed, get_header, get_cookie, forwarded_client, get_query,
128 get_form, get_param);
129
130/**
131 * @brief Set_stream_hooks.
132 * @param work PROTOCORE_HTTP_PARSER_BORROW bytes the caller took. Not held past the call.
133 * @param begin Begin
134 * @param data Data
135 * @param abort Abort
136 */
138 HttpStreamAbortCb abort);
139/**
140 * @brief Reset.
141 * @param work PROTOCORE_HTTP_PARSER_BORROW bytes the caller took. Not held past the call.
142 * @param req Req
143 */
144void protocore_http_parser_reset(uint8_t *work, HttpReq *req);
145/**
146 * @brief Feed.
147 * @param work PROTOCORE_HTTP_PARSER_BORROW bytes the caller took. Not held past the call.
148 * @param req Req
149 * @param byte Byte
150 */
151void protocore_http_parser_feed(uint8_t *work, HttpReq *req, uint8_t byte);
152/**
153 * @brief Get_header.
154 * @param work PROTOCORE_HTTP_PARSER_BORROW bytes the caller took. Not held past the call.
155 * @param req Req
156 * @param key Key
157 * @return The const char *.
158 */
159const char *protocore_http_parser_get_header(uint8_t *work, const HttpReq *req, const char *key);
160/**
161 * @brief Get_cookie.
162 * @param work PROTOCORE_HTTP_PARSER_BORROW bytes the caller took. Not held past the call.
163 * @param req Req
164 * @param name Name
165 * @param out Out
166 * @param out_size Out size
167 * @return PROTO_TRUE on success.
168 */
169proto_bool protocore_http_parser_get_cookie(uint8_t *work, const HttpReq *req, const char *name, char *out,
170 size_t out_size);
171/**
172 * @brief Forwarded_client.
173 * @param work PROTOCORE_HTTP_PARSER_BORROW bytes the caller took. Not held past the call.
174 * @param req Req
175 * @param ip_out Ip out
176 * @param ip_cap Ip cap
177 * @param is_https Is https
178 * @return PROTO_TRUE on success.
179 */
180proto_bool protocore_http_parser_forwarded_client(uint8_t *work, const HttpReq *req, char *ip_out, size_t ip_cap,
181 proto_bool *is_https);
182/**
183 * @brief Get_query.
184 * @param work PROTOCORE_HTTP_PARSER_BORROW bytes the caller took. Not held past the call.
185 * @param req Req
186 * @param key Key
187 * @return The const char *.
188 */
189const char *protocore_http_parser_get_query(uint8_t *work, const HttpReq *req, const char *key);
190/**
191 * @brief Get_form.
192 * @param work PROTOCORE_HTTP_PARSER_BORROW bytes the caller took. Not held past the call.
193 * @param req Req
194 * @param key Key
195 * @param out Out
196 * @param out_size Out size
197 * @return PROTO_TRUE on success.
198 */
199proto_bool protocore_http_parser_get_form(uint8_t *work, const HttpReq *req, const char *key, char *out,
200 size_t out_size);
201/**
202 * @brief Get_param.
203 * @param work PROTOCORE_HTTP_PARSER_BORROW bytes the caller took. Not held past the call.
204 * @param req Req
205 * @param key Key
206 * @return The const char *.
207 */
208const char *protocore_http_parser_get_param(uint8_t *work, const HttpReq *req, const char *key);
209
210typedef proto_bool (*HttpStreamBeginCb)(HttpReq *req);
211typedef void (*HttpStreamDataCb)(HttpReq *req, const uint8_t *data, size_t len);
212/**
213 * @brief The PROTOCORE_HTTP_PARSER_BORROW bytes this module's state lives in.
214 *
215 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
216 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
217 * walks, so the state lasts the life of the program.
218 *
219 * @return the span.
220 */
222
223/** @brief Module namespace. */
233
235
236#endif // PROTOCORE_HTTP_PARSER_H
#define CONN_POOL_SLOTS
#define MAX_VAL_LEN
Maximum header field-value length.
#define MAX_QUERY_PARAMS
Maximum number of parsed query-string parameters.
#define MAX_QUERY_LEN
Maximum raw query-string length (everything after ?).
#define QUERY_VAL_LEN
Maximum query-parameter value length.
#define BODY_BUF_SIZE
Maximum request body bytes stored in HttpReq::body.
#define PROTOCORE_AUTH_HDR_CAP
PROTO_ENUM_PACKED
Application protocol spoken on a listener port or connection slot.
#define MAX_HEADERS
Maximum HTTP headers stored per request.
#define MAX_PATH_LEN
Maximum URL path length (including leading /).
#define QUERY_KEY_LEN
Maximum query-parameter key length.
#define MAX_KEY_LEN
Maximum header field-name length (e.g. "Content-Type").
#define PROTOCORE_METHOD_BUF_SIZE
HTTP method-token buffer size (bytes, including the NUL).
#define MAX_PATH_PARAMS
Maximum number of :name path parameters captured per route match.
const char * protocore_http_parser_get_param(uint8_t *work, const HttpReq *req, const char *key)
Get_param.
proto_bool protocore_http_parser_get_cookie(uint8_t *work, const HttpReq *req, const char *name, char *out, size_t out_size)
Get_cookie.
void protocore_http_parser_reset(uint8_t *work, HttpReq *req)
Reset.
const char * protocore_http_parser_get_query(uint8_t *work, const HttpReq *req, const char *key)
Get_query.
proto_bool protocore_http_parser_get_form(uint8_t *work, const HttpReq *req, const char *key, char *out, size_t out_size)
Get_form.
enum PROTO_ENUM_PACKED ParseState
const char * protocore_http_parser_get_header(uint8_t *work, const HttpReq *req, const char *key)
Get_header.
void(* HttpStreamDataCb)(HttpReq *req, const uint8_t *data, size_t len)
PROTOCORE_NS HttpParserNs HttpParser PROTOCORE_UNUSED
Module namespace.
@ PARSE_BODY
Definition http_parser.h:29
@ PARSE_HEADER_VAL
Definition http_parser.h:26
@ PARSE_QUERY
Definition http_parser.h:23
@ PARSE_COMPLETE
Definition http_parser.h:30
@ HTTP_UNKNOWN
Definition http_parser.h:38
@ PARSE_HEADER_KEY
Definition http_parser.h:25
@ PARSE_EXPECT_BODY_LF
Definition http_parser.h:28
@ PARSE_VERSION
Definition http_parser.h:24
@ PARSE_URI_TOO_LONG
Definition http_parser.h:33
@ HTTP_11
Definition http_parser.h:40
@ PARSE_EXPECT_LF
Definition http_parser.h:27
@ HTTP_10
Definition http_parser.h:39
@ PARSE_ENTITY_TOO_LARGE
Definition http_parser.h:32
@ PARSE_PATH
Definition http_parser.h:22
@ PARSE_ERROR
Definition http_parser.h:31
@ PARSE_METHOD
Definition http_parser.h:21
void protocore_http_parser_set_stream_hooks(uint8_t *work, HttpStreamBeginCb begin, HttpStreamDataCb data, HttpStreamAbortCb abort)
Set_stream_hooks.
void protocore_http_parser_feed(uint8_t *work, HttpReq *req, uint8_t byte)
Feed.
void(* HttpStreamAbortCb)(HttpReq *req)
HttpReq http_pool[CONN_POOL_SLOTS]
The per-slot request table every HTTP layer parses into and reads back out of.
proto_bool protocore_http_parser_forwarded_client(uint8_t *work, const HttpReq *req, char *ip_out, size_t ip_cap, proto_bool *is_https)
Forwarded_client.
enum PROTO_ENUM_PACKED HttpVersion
proto_bool(* HttpStreamBeginCb)(HttpReq *req)
uint8_t * protocore_http_parser_span(void)
The PROTOCORE_HTTP_PARSER_BORROW bytes this module's state lives in.
#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.
void(* set_stream_hooks)(uint8_t *, HttpStreamBeginCb, HttpStreamDataCb, HttpStreamAbortCb)
QueryParam query_params[MAX_QUERY_PARAMS]
Definition http_parser.h:68
Header headers[MAX_HEADERS]
Definition http_parser.h:80
char query[MAX_QUERY_LEN]
Definition http_parser.h:66
uint32_t _version_hash
Definition http_parser.h:60
uint8_t body[BODY_BUF_SIZE+1]
Definition http_parser.h:92
QueryParam path_params[MAX_PATH_PARAMS]
Definition http_parser.h:71
uint8_t header_count
Definition http_parser.h:81
size_t current_token_idx
Definition http_parser.h:82
ParseState parse_state
Definition http_parser.h:58
size_t path_idx
Definition http_parser.h:64
size_t content_length
Definition http_parser.h:87
char cur_key[MAX_KEY_LEN]
Definition http_parser.h:84
uint8_t path_param_count
Definition http_parser.h:72
uint8_t slot_id
Definition http_parser.h:57
uint8_t content_length_count
Definition http_parser.h:88
size_t body_len
Definition http_parser.h:93
HttpVersion version
Definition http_parser.h:59
uint8_t host_count
Definition http_parser.h:89
char cur_val[MAX_VAL_LEN]
Definition http_parser.h:85
char path[MAX_PATH_LEN]
Definition http_parser.h:63
uint8_t query_count
Definition http_parser.h:69
size_t body_bytes_read
Definition http_parser.h:90
size_t query_idx
Definition http_parser.h:67
char method[PROTOCORE_METHOD_BUF_SIZE]
Definition http_parser.h:62
#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