ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
http_client.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_client.h
6 * @brief Layer 7 - the user agent (RFC 9110 sec 3.5): one outbound HTTP/1.1 message exchange.
7 *
8 * RFC 9110 sec 3.5: "the term 'user agent' refers to any of the various client programs that
9 * initiate a request." This is that side of HTTP. The serving side is network_drivers/presentation/http.
10 *
11 * The exchange runs in three steps, each a call on ::HttpClient:
12 *
13 * - @ref HttpClientNs::parse_target_uri splits the target URI (RFC 9110 sec 7.1) into the authority
14 * it dials and the origin-form request target it sends (RFC 9112 sec 3.2.1).
15 * - @ref HttpClientNs::build_request writes the request-line and field lines of an HTTP-message
16 * (RFC 9112 sec 2.1, sec 3).
17 * - @ref HttpClientNs::parse_response reads the status-line (RFC 9112 sec 4) and frames the message
18 * body by the rules of RFC 9112 sec 6.3.
19 *
20 * Those three touch no socket and are unit-tested on the host. @ref HttpClientNs::get and
21 * @ref HttpClientNs::post run all three over the shared outbound transport (::TcpClient), with the
22 * TLS record layer under them for an https target URI (RFC 9112 sec 9.7).
23 *
24 * The content is returned by pointer into the module's receive buffer and is valid until the next
25 * call. One exchange at a time: the module holds one connection and one buffer.
26 *
27 * @author Douglas Quigg (dstroy0)
28 * @date 2026
29 */
30
31#ifndef PROTOCORE_HTTP_CLIENT_H
32#define PROTOCORE_HTTP_CLIENT_H
33
34#include "protocore_config.h" // the entry point: protocore_types.h for the widths
35
36#if PROTOCORE_ENABLE_HTTP_CLIENT
37
38EMBED_BEGIN_DECLS
39
40/**
41 * @brief What went wrong below the status code.
42 *
43 * RFC 9110 sec 15: every status code is in the range 100..599, so a negative value can never
44 * collide with one and @ref HttpClientNs::status carries both.
45 */
46EMBED_ENUM_BYTE(HttpClientError,
47 HTTP_CLIENT_ERR_URL = -1, ///< the target URI does not parse (RFC 9110 sec 4.2.1 / 4.2.2)
48 HTTP_CLIENT_ERR_DNS = -2, ///< the uri-host did not resolve; the transport reports that as a close, so
49 ///< an exchange that fails to resolve reports ::HTTP_CLIENT_ERR_CONNECT
50 HTTP_CLIENT_ERR_CONNECT = -3, ///< the connection did not come up (RFC 9112 sec 9.1)
51 HTTP_CLIENT_ERR_TIMEOUT = -4, ///< no complete message arrived before the deadline
52 HTTP_CLIENT_ERR_SEND = -5, ///< the request message did not go out
53 HTTP_CLIENT_ERR_RESPONSE = -6, ///< the response does not parse (RFC 9112 sec 4)
54 HTTP_CLIENT_ERR_TLS = -7, ///< an https target URI with no TLS, or a failed handshake
55);
56
57/**
58 * @brief RFC 9110 sec 7.1: the target URI, split into the parts a request sends.
59 *
60 * A parse writes @c host, @c path, @c port and @c https out of @c url; a build reads them back.
61 * Scheme defaults are RFC 9110 sec 4.2.1 (http, TCP port 80) and sec 4.2.2 (https, TCP port 443).
62 */
63typedef struct
64{
65 const char *url; ///< the absolute target URI, "http://..." or "https://..."
66 char *host; ///< where the uri-host goes, and what the Host field carries (RFC 9110 sec 7.2)
67 size_t host_cap; ///< how much room it has
68 char *path; ///< where the origin-form request target goes (RFC 9112 sec 3.2.1)
69 size_t path_cap; ///< how much room it has
70 uint16_t port; ///< the authority's port; 80 for http, 443 for https when the URI omits it
71 embed_bool https; ///< the scheme is "https", so the exchange runs over TLS (RFC 9112 sec 9.7)
72} HttpTargetArgs;
73
74/** @brief RFC 9112 sec 3: the request-line's method, the content it encloses, and where it is built. */
75typedef struct
76{
77 const char *method; ///< the method token (RFC 9112 sec 3.1), "GET" or "POST" here
78 const char *content_type; ///< the Content-Type field value (RFC 9110 sec 8.3); null takes application/octet-stream
79 const uint8_t *body; ///< the content the message encloses, or null
80 size_t body_len; ///< its octet count, sent as Content-Length (RFC 9110 sec 8.6)
81 char *out; ///< where the request message is written
82 size_t cap; ///< how much room it has
83} HttpRequestArgs;
84
85/** @brief RFC 9112 sec 2.1: the received message a parse frames. Chunked decoding rewrites it. */
86typedef struct
87{
88 uint8_t *buf; ///< the octets received, start-line first
89 size_t len; ///< how many
90} HttpMessageArgs;
91
92/**
93 * @brief The HTTP user agent (RFC 9110 sec 3.5).
94 *
95 * A caller sets the members a call takes, invokes it through ::HttpClient, and reads the outcome off
96 * the same handle. There is no slot member: the module runs one exchange at a time, so no call names
97 * a row.
98 *
99 * An exchange sets @c target.host, @c target.path and @c request.out to the module's own buffers
100 * before it splits the target URI, so a caller sets only @c target.url and reads the parts back.
101 *
102 * @var HttpClientNs::target the target URI a call splits, dials, or names in a request-line
103 * @var HttpClientNs::request the method, the content, and where the request message is built
104 * @var HttpClientNs::message the received message a parse frames
105 * @var HttpClientNs::ok a call's true/false outcome
106 * @var HttpClientNs::status the status-code (RFC 9112 sec 4), or a negative ::HttpClientError
107 * @var HttpClientNs::n octets a build wrote; 0 when the message would not fit
108 * @var HttpClientNs::body_off where the content starts inside the parsed message
109 * @var HttpClientNs::body_len the content's octet count (RFC 9112 sec 6.3)
110 * @var HttpClientNs::body the content an exchange read, pointing into the module's receive buffer
111 * @var HttpClientNs::parse_target_uri split the target URI into authority, port and request target
112 * @var HttpClientNs::build_request write the request-line and field lines of one HTTP-message
113 * @var HttpClientNs::parse_response read the status-line and frame the message body
114 * @var HttpClientNs::get run one GET exchange (RFC 9110 sec 9.3.1)
115 * @var HttpClientNs::post run one POST exchange (RFC 9110 sec 9.3.3)
116 */
117typedef struct
118{
119 HttpTargetArgs target; ///< the target URI and its parts (RFC 9110 sec 7.1)
120 HttpRequestArgs request; ///< what a request-line and its field lines carry (RFC 9112 sec 3)
121 HttpMessageArgs message; ///< the received message a parse frames (RFC 9112 sec 2.1)
122 embed_bool ok;
123 int32_t status;
124 size_t n;
125 size_t body_off;
126 size_t body_len;
127 const uint8_t *body;
128} HttpClientVars;
129
130/** @brief The operands and the outcome. */
131extern HttpClientVars HttpClientV;
132
133/** @brief The entries. */
134typedef struct
135{
136 void (*const parse_target_uri)(const uint8_t *work);
137 void (*const build_request)(const uint8_t *work);
138 void (*const parse_response)(const uint8_t *work);
139 void (*const get)(uint8_t *work);
140 void (*const post)(uint8_t *work);
141} HttpClientNs;
142
143// What the table binds, defined once in the .c and taking one parameter each: everything
144// else an entry needs is an operand in HttpClientV or a region of the borrow at a fixed offset.
145void protocore_http_client_parse_target_uri(const uint8_t *work);
146void protocore_http_client_build_request(const uint8_t *work);
147void protocore_http_client_parse_response(const uint8_t *work);
148void protocore_http_client_get(uint8_t *work);
149void protocore_http_client_post(uint8_t *work);
150
151// `static const`, initialised HERE rather than `extern` against a definition in the .c: a
152// const object whose initializer every translation unit can see is a COMPILE-TIME FACT, so
153// `HttpClient.parse_target_uri(work)` resolves to a named function and becomes a DIRECT call. An extern table
154// leaves the call indirect and the symbol live at every level, -O2 -flto included.
155static const HttpClientNs HttpClient __attribute__((unused)) = {
156 .parse_target_uri = protocore_http_client_parse_target_uri,
157 .build_request = protocore_http_client_build_request,
158 .parse_response = protocore_http_client_parse_response,
159 .get = protocore_http_client_get,
160 .post = protocore_http_client_post,
161};
162
163/**
164 * @brief The PROTOCORE_HTTP_CLIENT_BORROW bytes this module's state lives in.
165 *
166 * Stated beside the namespace rather than on it: an entry takes a borrow, and this is where
167 * that borrow comes from. Taken once from the end of the pool, which no mark and no release
168 * walks, so the state lasts the life of the program.
169 *
170 * @return the span.
171 */
172// Not under PROTOCORE_HAS_NET_STACK: the borrow is the MODULE's, the same size and offset stack or
173// no stack, and http_client.c defines this above its own capability gate for that reason. Guarding
174// the declaration while the definition is unconditional hid a symbol that exists, so a caller
175// asking a stackless build for its borrow saw no declaration. Only the entries take the capability.
176uint8_t *protocore_http_client_span(void);
177
178EMBED_END_DECLS
179
180#endif // PROTOCORE_ENABLE_HTTP_CLIENT
181
182#endif // PROTOCORE_HTTP_CLIENT_H
EMBED_ENUM_BYTE(ProtoConn, PROTO_NONE=0, PROTO_HTTP=1, PROTO_TELNET=2, PROTO_SSH=3, PROTO_MODBUS=4, PROTO_OPCUA=5, PROTO_SSH_RFWD=6, PROTO_RELAY=7, PROTO_BRIDGE=8, PROTO_NTRIP_CASTER=9, PROTO_MESH=10, PROTO_UDP=11,)
Application protocol spoken on a listener port or connection slot.