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
9EMBED_BEGIN_DECLS
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
19EMBED_ENUM_BYTE(ParseState, PARSE_METHOD, PARSE_PATH, PARSE_QUERY, PARSE_VERSION, PARSE_HEADER_KEY, PARSE_HEADER_VAL,
20 PARSE_EXPECT_LF, PARSE_EXPECT_BODY_LF, PARSE_BODY, PARSE_COMPLETE, PARSE_ERROR, PARSE_ENTITY_TOO_LARGE,
21 PARSE_URI_TOO_LONG);
22
23EMBED_ENUM_BYTE(HttpVersion, HTTP_UNKNOWN = 0, HTTP_10, HTTP_11);
24
25typedef struct
26{
29} Header;
30
31typedef struct
32{
36
37typedef struct HttpReq
38{
39 uint8_t slot_id;
40 ParseState parse_state;
41 HttpVersion version;
42 uint32_t _version_hash;
43
46 size_t path_idx;
47
49 size_t query_idx;
51 uint8_t query_count;
52
55
56#if PROTOCORE_CAPTURE_AUTH_HEADER
57 char authorization[PROTOCORE_AUTH_HDR_CAP];
58 uint16_t auth_idx;
59 embed_bool cur_is_auth;
60#endif
61
63 uint8_t header_count;
65
68
71 uint8_t host_count;
73
74 uint8_t body[BODY_BUF_SIZE + 1];
75 size_t body_len;
76
77#if PROTOCORE_ENABLE_STREAM_BODY
78 embed_bool body_streaming;
79#endif
81
82typedef embed_bool (*HttpStreamBeginCb)(HttpReq *req);
83
84typedef void (*HttpStreamDataCb)(HttpReq *req, const uint8_t *data, size_t len);
85
86typedef void (*HttpStreamAbortCb)(HttpReq *req);
87
88/**
89 * @brief The per-slot request table every HTTP layer parses into and reads back out of.
90 *
91 * Not an entry: it is the shared cross-TU substrate a caller indexes by slot, not something the
92 * namespace hands out, so it is reached by name.
93 */
95
96/** @brief Dispatch table. Addressed by offset, so the layout is asserted below. */
97typedef struct
98{
100 void (*reset)(uint8_t *, HttpReq *);
101 void (*feed)(uint8_t *, HttpReq *, uint8_t);
102 const char *(*get_header)(const uint8_t *, const HttpReq *, const char *);
103 embed_bool (*get_cookie)(const uint8_t *, const HttpReq *, const char *, char *, size_t);
104 embed_bool (*forwarded_client)(const uint8_t *, const HttpReq *, char *, size_t, embed_bool *);
105 const char *(*get_query)(const uint8_t *, const HttpReq *, const char *);
106 embed_bool (*get_form)(const uint8_t *, const HttpReq *, const char *, char *, size_t);
107 const char *(*get_param)(const uint8_t *, const HttpReq *, const char *);
109PROTOCORE_NS_LAYOUT(HttpParserNs, set_stream_hooks, reset, feed, get_header, get_cookie, forwarded_client, get_query,
110 get_form, get_param);
111
112/**
113 * @brief Set_stream_hooks.
114 * @param work PROTOCORE_HTTP_PARSER_BORROW bytes the caller took. Not held past the call.
115 * @param begin Begin
116 * @param data Data
117 * @param abort Abort
118 */
120 HttpStreamAbortCb abort);
121/**
122 * @brief Reset.
123 * @param work PROTOCORE_HTTP_PARSER_BORROW bytes the caller took. Not held past the call.
124 * @param req Req
125 */
126void protocore_http_parser_reset(uint8_t *work, HttpReq *req);
127/**
128 * @brief Feed.
129 * @param work PROTOCORE_HTTP_PARSER_BORROW bytes the caller took. Not held past the call.
130 * @param req Req
131 * @param byte Byte
132 */
133void protocore_http_parser_feed(uint8_t *work, HttpReq *req, uint8_t byte);
134/**
135 * @brief Get_header.
136 * @param work PROTOCORE_HTTP_PARSER_BORROW bytes the caller took. Not held past the call.
137 * @param req Req
138 * @param key Key
139 * @return The const char *.
140 */
141const char *protocore_http_parser_get_header(const uint8_t *work, const HttpReq *req, const char *key);
142/**
143 * @brief Get_cookie.
144 * @param work PROTOCORE_HTTP_PARSER_BORROW bytes the caller took. Not held past the call.
145 * @param req Req
146 * @param name Name
147 * @param out Out
148 * @param out_size Out size
149 * @return EMBED_TRUE on success.
150 */
151embed_bool protocore_http_parser_get_cookie(const uint8_t *work, const HttpReq *req, const char *name, char *out,
152 size_t out_size);
153/**
154 * @brief Forwarded_client.
155 * @param work PROTOCORE_HTTP_PARSER_BORROW bytes the caller took. Not held past the call.
156 * @param req Req
157 * @param ip_out Ip out
158 * @param ip_cap Ip cap
159 * @param is_https Is https
160 * @return EMBED_TRUE on success.
161 */
162embed_bool protocore_http_parser_forwarded_client(const uint8_t *work, const HttpReq *req, char *ip_out, size_t ip_cap,
163 embed_bool *is_https);
164/**
165 * @brief Get_query.
166 * @param work PROTOCORE_HTTP_PARSER_BORROW bytes the caller took. Not held past the call.
167 * @param req Req
168 * @param key Key
169 * @return The const char *.
170 */
171const char *protocore_http_parser_get_query(const uint8_t *work, const HttpReq *req, const char *key);
172/**
173 * @brief Get_form.
174 * @param work PROTOCORE_HTTP_PARSER_BORROW bytes the caller took. Not held past the call.
175 * @param req Req
176 * @param key Key
177 * @param out Out
178 * @param out_size Out size
179 * @return EMBED_TRUE on success.
180 */
181embed_bool protocore_http_parser_get_form(const uint8_t *work, const HttpReq *req, const char *key, char *out,
182 size_t out_size);
183/**
184 * @brief Get_param.
185 * @param work PROTOCORE_HTTP_PARSER_BORROW bytes the caller took. Not held past the call.
186 * @param req Req
187 * @param key Key
188 * @return The const char *.
189 */
190const char *protocore_http_parser_get_param(const uint8_t *work, const HttpReq *req, const char *key);
191
192typedef embed_bool (*HttpStreamBeginCb)(HttpReq *req);
193typedef void (*HttpStreamDataCb)(HttpReq *req, const uint8_t *data, size_t len);
194/**
195 * @brief The PROTOCORE_HTTP_PARSER_BORROW bytes this module's state lives in.
196 *
197 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
198 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
199 * walks, so the state lasts the life of the program.
200 *
201 * @return the span.
202 */
204
205/** @brief Module namespace. */
215
216EMBED_END_DECLS
217
218#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
#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.
embed_bool protocore_http_parser_get_form(const uint8_t *work, const HttpReq *req, const char *key, char *out, size_t out_size)
Get_form.
void protocore_http_parser_reset(uint8_t *work, HttpReq *req)
Reset.
const char * protocore_http_parser_get_param(const uint8_t *work, const HttpReq *req, const char *key)
Get_param.
embed_bool(* HttpStreamBeginCb)(HttpReq *req)
Definition http_parser.h:82
const char * protocore_http_parser_get_query(const uint8_t *work, const HttpReq *req, const char *key)
Get_query.
embed_bool protocore_http_parser_forwarded_client(const uint8_t *work, const HttpReq *req, char *ip_out, size_t ip_cap, embed_bool *is_https)
Forwarded_client.
void(* HttpStreamDataCb)(HttpReq *req, const uint8_t *data, size_t len)
Definition http_parser.h:84
EMBED_ENUM_BYTE(ParseState, PARSE_METHOD, PARSE_PATH, PARSE_QUERY, PARSE_VERSION, PARSE_HEADER_KEY, PARSE_HEADER_VAL, PARSE_EXPECT_LF, PARSE_EXPECT_BODY_LF, PARSE_BODY, PARSE_COMPLETE, PARSE_ERROR, PARSE_ENTITY_TOO_LARGE, PARSE_URI_TOO_LONG)
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)
Definition http_parser.h:86
HttpReq http_pool[CONN_POOL_SLOTS]
The per-slot request table every HTTP layer parses into and reads back out of.
embed_bool protocore_http_parser_get_cookie(const uint8_t *work, const HttpReq *req, const char *name, char *out, size_t out_size)
Get_cookie.
const char * protocore_http_parser_get_header(const uint8_t *work, const HttpReq *req, const char *key)
Get_header.
uint8_t * protocore_http_parser_span(void)
The PROTOCORE_HTTP_PARSER_BORROW bytes this module's state lives in.
#define PROTOCORE_UNUSED
Suppress the unused warning. Every module ends in a namespace most callers only partly use.
Definition ns_contract.h:36
#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.
char val[MAX_VAL_LEN]
Definition http_parser.h:28
char key[MAX_KEY_LEN]
Definition http_parser.h:27
Dispatch table. Addressed by offset, so the layout is asserted below.
Definition http_parser.h:98
void(* feed)(uint8_t *, HttpReq *, uint8_t)
void(* set_stream_hooks)(uint8_t *, HttpStreamBeginCb, HttpStreamDataCb, HttpStreamAbortCb)
Definition http_parser.h:99
embed_bool(* get_cookie)(const uint8_t *, const HttpReq *, const char *, char *, size_t)
embed_bool(* forwarded_client)(const uint8_t *, const HttpReq *, char *, size_t, embed_bool *)
void(* reset)(uint8_t *, HttpReq *)
embed_bool(* get_form)(const uint8_t *, const HttpReq *, const char *, char *, size_t)
QueryParam query_params[MAX_QUERY_PARAMS]
Definition http_parser.h:50
Header headers[MAX_HEADERS]
Definition http_parser.h:62
char query[MAX_QUERY_LEN]
Definition http_parser.h:48
uint32_t _version_hash
Definition http_parser.h:42
uint8_t body[BODY_BUF_SIZE+1]
Definition http_parser.h:74
QueryParam path_params[MAX_PATH_PARAMS]
Definition http_parser.h:53
uint8_t header_count
Definition http_parser.h:63
size_t current_token_idx
Definition http_parser.h:64
ParseState parse_state
Definition http_parser.h:40
size_t path_idx
Definition http_parser.h:46
size_t content_length
Definition http_parser.h:69
char cur_key[MAX_KEY_LEN]
Definition http_parser.h:66
uint8_t path_param_count
Definition http_parser.h:54
uint8_t slot_id
Definition http_parser.h:39
uint8_t content_length_count
Definition http_parser.h:70
size_t body_len
Definition http_parser.h:75
HttpVersion version
Definition http_parser.h:41
uint8_t host_count
Definition http_parser.h:71
char cur_val[MAX_VAL_LEN]
Definition http_parser.h:67
char path[MAX_PATH_LEN]
Definition http_parser.h:45
uint8_t query_count
Definition http_parser.h:51
size_t body_bytes_read
Definition http_parser.h:72
size_t query_idx
Definition http_parser.h:49
char method[PROTOCORE_METHOD_BUF_SIZE]
Definition http_parser.h:44
char val[QUERY_VAL_LEN]
Definition http_parser.h:34
char key[QUERY_KEY_LEN]
Definition http_parser.h:33