Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
client_multiprotocol.h
Go to the documentation of this file.
1
6
7/*
8 * Copyright (c) 2026 Nordic Semiconductor ASA
9 *
10 * SPDX-License-Identifier: Apache-2.0
11 */
12
13#ifndef ZEPHYR_INCLUDE_NET_HTTP_CLIENT_MULTIPROTOCOL_H_
14#define ZEPHYR_INCLUDE_NET_HTTP_CLIENT_MULTIPROTOCOL_H_
15
28
29#include <zephyr/kernel.h>
33#include <zephyr/net/net_ip.h>
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
42
43#if defined(CONFIG_HTTP_CLIENT_MULTIPROTOCOL)
44#define HTTP_CLIENT_MAX_STREAMS CONFIG_HTTP_CLIENT_MAX_STREAMS
45#define HTTP_CLIENT_HEADERS_BUFFER_SIZE CONFIG_HTTP_CLIENT_HEADERS_BUFFER_SIZE
46#define HTTP_CLIENT_RX_BUFFER_SIZE CONFIG_HTTP_CLIENT_RX_BUFFER_SIZE
47#define HTTP_CLIENT_TX_BUFFER_SIZE CONFIG_HTTP_CLIENT_TX_BUFFER_SIZE
48#define HTTP_CLIENT_MAX_CONTENT_TYPE_LEN CONFIG_HTTP_CLIENT_MAX_CONTENT_TYPE_LENGTH
49#else
50#define HTTP_CLIENT_MAX_STREAMS 1
51#define HTTP_CLIENT_HEADERS_BUFFER_SIZE 1
52#define HTTP_CLIENT_RX_BUFFER_SIZE 1
53#define HTTP_CLIENT_TX_BUFFER_SIZE 1
54#define HTTP_CLIENT_MAX_CONTENT_TYPE_LEN 1
55#endif
56
58
59struct http_client;
62
64
65/* State kept by the protocol layers inside the public contexts below. */
66
70#define HTTP2_CLIENT_DEFERRED_PINGS 4
71
73struct http2_client_stream {
75 int window_size;
76
78 int send_window;
79
81 size_t body_received;
82
84 bool status_seen;
85
87 bool regular_field_seen;
88
92 uint32_t window_update_pending;
93};
94
98struct http2_client_deferred {
100 uint8_t settings_acks;
101
103 uint8_t ping_count;
104
106 uint8_t ping_data[HTTP2_CLIENT_DEFERRED_PINGS][HTTP2_PING_FRAME_LEN];
107
109 uint32_t conn_window;
110
112 struct {
113 uint32_t id;
114 enum http2_error error;
115 } rst[HTTP_CLIENT_MAX_STREAMS];
116
118 uint8_t rst_count;
119};
120
122enum http2_client_parser_state {
124 HTTP2_CLIENT_STATE_FRAME_HEADER,
125
127 HTTP2_CLIENT_STATE_HEADERS,
128
130 HTTP2_CLIENT_STATE_CONTINUATION,
131
133 HTTP2_CLIENT_STATE_DATA,
134
136 HTTP2_CLIENT_STATE_SETTINGS,
137
139 HTTP2_CLIENT_STATE_WINDOW_UPDATE,
140
142 HTTP2_CLIENT_STATE_RST_STREAM,
143
145 HTTP2_CLIENT_STATE_GOAWAY,
146
148 HTTP2_CLIENT_STATE_PING,
149
151 HTTP2_CLIENT_STATE_PRIORITY,
152
154 HTTP2_CLIENT_STATE_SKIP,
155};
156
158struct http2_client_parser {
160 enum http2_client_parser_state state;
161
163 struct http2_frame frame;
164
166 bool expect_continuation;
167
169 uint32_t headers_stream_id;
170
172 bool block_field_seen;
173
175 enum http2_error goaway_error;
176
178 bool headers_end_stream;
179
181 struct http_hpack_header_buf header_field;
182
186 size_t rx_headers_str_top;
187
189 int window_size;
190
192 int send_window;
193
195 uint32_t peer_initial_window;
196
198 uint32_t peer_max_frame_size;
199
201 uint32_t peer_max_streams;
202
204 bool going_away;
205
207 uint32_t last_stream_id;
208
210 bool preface_received;
211
213 bool settings_acked;
214
219 uint32_t table_size_changes;
220
222 uint32_t table_size_acked;
223
225 struct http2_client_deferred deferred;
226};
227
229
236
243
246 const char *name;
247 const char *value;
248};
249
255 bool secure;
256
261 void (*init)(void *transport_ctx);
262
275 int (*connect)(struct http_client *client);
276
293 int (*send)(struct http_client *client, void *stream, const uint8_t *data, size_t len,
294 bool end_stream);
295
320 int (*recv)(struct http_client *client, void **stream, uint8_t *buf, size_t bufsize,
321 bool *end_stream);
322
334 int (*wait)(struct http_client *client, k_timeout_t timeout);
335
344 void (*stop)(struct http_client *client);
345
358 int (*close)(struct http_client *client);
359};
360
372void http_client_received_data(struct http_client *client, int status);
373
387typedef int (*http_client_socket_create_cb_t)(struct http_client *client, int af,
388 int proto);
389
415typedef int (*http_client_payload_cb_t)(size_t offset, const uint8_t **payload,
416 size_t *len, bool *more, void *user_data);
417
447typedef int (*http_client_headers_cb_t)(const struct http_client_header **headers,
448 size_t *count, bool *more, void *user_data);
449
466
495
506
544typedef int (*http_client_response_cb_t)(struct http_client *client,
545 const struct http_client_request *request,
546 struct http_client_response *response,
548 void *user_data);
549
554
556 int error;
557
559 char content_type[HTTP_CLIENT_MAX_CONTENT_TYPE_LEN];
560
562 char content_encoding[HTTP_CLIENT_MAX_CONTENT_TYPE_LEN];
563
566
569
575
578
580 const uint8_t *body;
581
583 size_t body_len;
584};
585
637
642
645
650
652 union {
654 struct http2_client_stream http2;
656
661 uint8_t headers_buf[HTTP_CLIENT_HEADERS_BUFFER_SIZE] __aligned(sizeof(void *));
662
665
668
671
674
677
680
686
689
692};
693
700
705
707 struct net_sockaddr_storage server_addr;
708
711
713 const char *host;
714
716 bool async;
717
723
725 struct http_client_stream streams[HTTP_CLIENT_MAX_STREAMS];
726
728 uint8_t rx_buf[HTTP_CLIENT_RX_BUFFER_SIZE];
729
732
735
737 uint8_t tx_buf[HTTP_CLIENT_TX_BUFFER_SIZE];
738
740 union {
742 struct http2_client_parser http2;
744};
745
749 struct net_sockaddr_storage server_addr;
750
753
760 const char *host;
761
763 bool async;
764};
765
803
819int http_client_init(struct http_client *client, enum http_client_version version,
820 const struct http_client_transport_ops *transport_ops,
821 void *transport_ctx);
822
835int http_client_init_tcp(struct http_client *client, enum http_client_version version,
836 struct http_client_transport_tcp_ctx *transport_ctx);
837
853int http_client_init_tls(struct http_client *client, enum http_client_version version,
854 struct http_client_transport_tls_ctx *transport_ctx);
855
873 struct http_client_session_config *config);
874
934 const struct http_client_request *request,
935 int32_t timeout, void *user_data);
936
956
957#ifdef __cplusplus
958}
959#endif
960
964
965#endif /* ZEPHYR_INCLUDE_NET_HTTP_CLIENT_MULTIPROTOCOL_H_ */
HTTP2 frame information.
int http_client_init_tls(struct http_client *client, enum http_client_version version, struct http_client_transport_tls_ctx *transport_ctx)
Initialize HTTP client with TLS transport.
http_client_session_state
HTTP client session state.
Definition client_multiprotocol.h:695
int http_client_init_tcp(struct http_client *client, enum http_client_version version, struct http_client_transport_tcp_ctx *transport_ctx)
Initialize HTTP client with TCP transport.
int http_client_connect(struct http_client *client, struct http_client_session_config *config)
Connect HTTP client to a HTTP server.
int http_client_send_request(struct http_client *client, const struct http_client_request *request, int32_t timeout, void *user_data)
Send HTTP request to a HTTP server.
int(* http_client_socket_create_cb_t)(struct http_client *client, int af, int proto)
A function to offload socket creation for the transport layer.
Definition client_multiprotocol.h:387
http_client_version
HTTP protocol version.
Definition client_multiprotocol.h:231
http_client_transaction_status
State of the HTTP exchange passed to the callback.
Definition client_multiprotocol.h:238
int http_client_init(struct http_client *client, enum http_client_version version, const struct http_client_transport_ops *transport_ops, void *transport_ctx)
Initialize HTTP client with a custom transport.
int(* http_client_payload_cb_t)(size_t offset, const uint8_t **payload, size_t *len, bool *more, void *user_data)
Callback for providing payload for the HTTP request.
Definition client_multiprotocol.h:415
void http_client_received_data(struct http_client *client, int status)
Notify HTTP client about a transport reception event.
int(* http_client_headers_cb_t)(const struct http_client_header **headers, size_t *count, bool *more, void *user_data)
Callback for providing additional headers for the HTTP request.
Definition client_multiprotocol.h:447
int(* http_client_response_cb_t)(struct http_client *client, const struct http_client_request *request, struct http_client_response *response, enum http_client_transaction_status status, void *user_data)
Callback used when data is received from the server or an error occurred.
Definition client_multiprotocol.h:544
int http_client_disconnect(struct http_client *client)
Disconnect HTTP client from a HTTP server.
@ HTTP_CLIENT_SESSION_OPEN
Session open.
Definition client_multiprotocol.h:697
@ HTTP_CLIENT_SESSION_CLOSING
Session being torn down.
Definition client_multiprotocol.h:698
@ HTTP_CLIENT_SESSION_CLOSED
Session closed.
Definition client_multiprotocol.h:696
@ HTTP_CLIENT_VERSION_2
HTTP/2.
Definition client_multiprotocol.h:233
@ HTTP_CLIENT_VERSION_1_1
HTTP/1.1.
Definition client_multiprotocol.h:232
@ HTTP_CLIENT_VERSION_3
HTTP/3.
Definition client_multiprotocol.h:234
@ HTTP_CLIENT_TRANSACTION_ABORTED
Transaction aborted, data incomplete.
Definition client_multiprotocol.h:239
@ HTTP_CLIENT_RESPONSE_DATA_MORE
Transaction incomplete, more data to come.
Definition client_multiprotocol.h:240
@ HTTP_CLIENT_RESPONSE_DATA_FINAL
Transaction complete, end of data.
Definition client_multiprotocol.h:241
http_method
HTTP Request Methods.
Definition method.h:28
uint32_t net_socklen_t
Length of a socket address.
Definition net_ip.h:172
int sec_tag_t
Secure tag, a reference to TLS credential.
Definition tls_credentials.h:99
HTTP HPACK.
struct k_thread * k_tid_t
Definition thread.h:414
Public kernel APIs.
HTTP request methods.
IPv6 and IPv4 definitions.
state
Definition parser_state.h:29
HTTP response status codes.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__INT32_TYPE__ int32_t
Definition stdint.h:74
__UINT64_TYPE__ uint64_t
Definition stdint.h:91
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
__INT64_TYPE__ int64_t
Definition stdint.h:75
An HTTP header field, a name and value pair.
Definition client_multiprotocol.h:245
const char * name
Pointer to header name NULL-terminated string.
Definition client_multiprotocol.h:246
const char * value
Pointer to header value NULL-terminated string.
Definition client_multiprotocol.h:247
HTTP request context.
Definition client_multiprotocol.h:587
enum http_method method
The HTTP method: GET, HEAD, OPTIONS, POST, ...
Definition client_multiprotocol.h:589
size_t headers_count
Number of headers provided in http_client_request::header_fields.
Definition client_multiprotocol.h:635
size_t payload_len
Length of the payload provided in http_client_request::payload.
Definition client_multiprotocol.h:619
const char * content_type_value
The value of the Content-Type header field, may be NULL.
Definition client_multiprotocol.h:607
const struct http_client_header * header_fields
The HTTP header fields (application specific).
Definition client_multiprotocol.h:632
http_client_response_cb_t response_cb
User supplied callback function to call when response is received.
Definition client_multiprotocol.h:592
http_client_payload_cb_t payload_cb
User supplied callback function to call when payload needs to be sent.
Definition client_multiprotocol.h:613
const uint8_t * payload
Payload, may be NULL.
Definition client_multiprotocol.h:616
http_client_headers_cb_t headers_cb
User supplied callback function to call when optional headers need to be sent.
Definition client_multiprotocol.h:625
const char * url
The request target in origin form, that is the path and query, for example /index....
Definition client_multiprotocol.h:604
bool skip_response_headers
Do not report response headers to the application.
Definition client_multiprotocol.h:597
HTTP response context.
Definition client_multiprotocol.h:551
char content_encoding[HTTP_CLIENT_MAX_CONTENT_TYPE_LEN]
Content encoding, truncated to fit.
Definition client_multiprotocol.h:562
char content_type[HTTP_CLIENT_MAX_CONTENT_TYPE_LEN]
Content type, truncated to fit.
Definition client_multiprotocol.h:559
bool content_len_present
Whether the response carried a Content-Length header.
Definition client_multiprotocol.h:568
size_t header_count
Length of headers array.
Definition client_multiprotocol.h:577
uint16_t status_code
HTTP status code, 0 until the response headers are received.
Definition client_multiprotocol.h:553
size_t content_len
Content length, valid when content_len_present is set.
Definition client_multiprotocol.h:565
const struct http_client_header * headers
Headers of the header block being reported.
Definition client_multiprotocol.h:574
const uint8_t * body
Pointer to body data.
Definition client_multiprotocol.h:580
int error
Reason for an aborted transaction (negative errno), 0 otherwise.
Definition client_multiprotocol.h:556
size_t body_len
Body length (in current chunk).
Definition client_multiprotocol.h:583
HTTP session configuration.
Definition client_multiprotocol.h:747
net_socklen_t addrlen
Server address length.
Definition client_multiprotocol.h:752
bool async
Whether session should be synchronous or asynchronous.
Definition client_multiprotocol.h:763
struct net_sockaddr_storage server_addr
Server address.
Definition client_multiprotocol.h:749
const char * host
Server host name, IPv4 literal or bracketed IPv6 literal, without a port, used for the :authority pse...
Definition client_multiprotocol.h:760
HTTP session context.
Definition client_multiprotocol.h:702
uint8_t rx_buf[HTTP_CLIENT_RX_BUFFER_SIZE]
Receive buffer used to buffer incoming data from the transport.
Definition client_multiprotocol.h:728
bool close_pending
Whether the transport of the ended session is still to be closed, by the thread that ended it once it...
Definition client_multiprotocol.h:722
struct http_client_stream streams[HTTP_CLIENT_MAX_STREAMS]
HTTP streams for this session.
Definition client_multiprotocol.h:725
size_t rx_data_len
Number of valid bytes currently stored in rx_buf.
Definition client_multiprotocol.h:731
net_socklen_t addrlen
Server address length.
Definition client_multiprotocol.h:710
uint8_t tx_buf[HTTP_CLIENT_TX_BUFFER_SIZE]
Transmit buffer used when sending HTTP frames.
Definition client_multiprotocol.h:737
bool async
Whether session is synchronous or asynchronous.
Definition client_multiprotocol.h:716
void * rx_stream
Transport stream the data in rx_buf came from.
Definition client_multiprotocol.h:734
struct http2_client_parser http2
HTTP/2 parser context.
Definition client_multiprotocol.h:742
const char * host
Server host name, NULL if not configured.
Definition client_multiprotocol.h:713
struct net_sockaddr_storage server_addr
Server address.
Definition client_multiprotocol.h:707
enum http_client_session_state state
HTTP session state.
Definition client_multiprotocol.h:704
union http_client_session::@353115226307316274035330375366316012317213155020 parser
Protocol-specific incremental parser context.
HTTP stream context.
Definition client_multiprotocol.h:639
bool headers_received
Whether the headers of the final response were received.
Definition client_multiprotocol.h:688
void * transport_stream
Transport stream carrying this stream, NULL on a single-stream transport.
Definition client_multiprotocol.h:649
struct http2_client_stream http2
HTTP/2 stream state.
Definition client_multiprotocol.h:654
int64_t timeout
Response timeout for the stream (absolute time in milliseconds).
Definition client_multiprotocol.h:670
bool completed
Whether the transaction has completed.
Definition client_multiprotocol.h:676
bool sending
Whether the request is still being sent.
Definition client_multiprotocol.h:679
k_tid_t caller
Thread that took the stream in http_client_send_request().
Definition client_multiprotocol.h:685
int result
Result of the completed transaction (0, or a negative errno).
Definition client_multiprotocol.h:673
struct http_client_response response
Response data associated with the stream.
Definition client_multiprotocol.h:667
void * user_data
User data.
Definition client_multiprotocol.h:691
uint8_t headers_buf[HTTP_CLIENT_HEADERS_BUFFER_SIZE]
Buffer backing the response header array and strings delivered to the application.
Definition client_multiprotocol.h:661
const struct http_client_request * request
Request the stream carries, NULL when none.
Definition client_multiprotocol.h:664
union http_client_stream::@054264073225070071214264150217205037164344114171 proto
Protocol-specific stream state.
bool is_used
Whether the stream is in use.
Definition client_multiprotocol.h:641
uint64_t stream_id
Protocol stream identifier, as assigned by the protocol layer.
Definition client_multiprotocol.h:644
HTTP transport API.
Definition client_multiprotocol.h:251
int(* recv)(struct http_client *client, void **stream, uint8_t *buf, size_t bufsize, bool *end_stream)
Receive data from the transport layer.
Definition client_multiprotocol.h:320
void(* init)(void *transport_ctx)
Initialize transport context.
Definition client_multiprotocol.h:261
bool secure
Whether the transport is secured with TLS.
Definition client_multiprotocol.h:255
int(* send)(struct http_client *client, void *stream, const uint8_t *data, size_t len, bool end_stream)
Send data over the transport layer.
Definition client_multiprotocol.h:293
int(* connect)(struct http_client *client)
Connect the transport layer.
Definition client_multiprotocol.h:275
int(* close)(struct http_client *client)
Disconnect the transport layer and release resources.
Definition client_multiprotocol.h:358
void(* stop)(struct http_client *client)
Stop reporting reception events.
Definition client_multiprotocol.h:344
int(* wait)(struct http_client *client, k_timeout_t timeout)
Wait for data to become available.
Definition client_multiprotocol.h:334
TCP transport context for the HTTP client.
Definition client_multiprotocol.h:451
http_client_socket_create_cb_t socket_create
Optional callback function for socket creation.
Definition client_multiprotocol.h:464
const char * if_name
Name of the interface that the HTTP client instance should be bound to.
Definition client_multiprotocol.h:458
int sock
Socket file descriptor in use, managed by the transport.
Definition client_multiprotocol.h:453
TLS configuration for the HTTP client.
Definition client_multiprotocol.h:468
bool set_native_tls
Set socket to use native TLS (used with socket offloading).
Definition client_multiprotocol.h:493
int cert_nocopy
Indicates the preference for copying certificates to the heap.
Definition client_multiprotocol.h:490
const char * hostname
Peer hostname, sent as the TLS server name and used for certificate verification.
Definition client_multiprotocol.h:487
const int * cipher_list
The list of ciphers to be used for the session.
Definition client_multiprotocol.h:475
const sec_tag_t * sec_tag_list
The list of security tags to be used for the session.
Definition client_multiprotocol.h:481
uint32_t sec_tag_count
The number of entries in the sec tag list.
Definition client_multiprotocol.h:478
uint32_t cipher_count
The number of entries in the cipher list.
Definition client_multiprotocol.h:470
TLS transport context for the HTTP client.
Definition client_multiprotocol.h:497
struct http_client_transport_tcp_ctx tcp
Underlying TCP transport context.
Definition client_multiprotocol.h:501
struct http_client_transport_tls_config config
TLS configuration.
Definition client_multiprotocol.h:504
HTTP client context.
Definition client_multiprotocol.h:767
enum http_client_version version
HTTP version in use.
Definition client_multiprotocol.h:775
uint8_t callers
Threads inside http_client_send_request() that own a stream, which http_client_disconnect() waits for...
Definition client_multiprotocol.h:789
void * user_data
Opaque user data.
Definition client_multiprotocol.h:801
struct k_condvar tx_cond
Signaled when a thread blocked sending a request may be able to proceed.
Definition client_multiprotocol.h:784
void * transport_ctx
Context data for the transport layer.
Definition client_multiprotocol.h:772
struct http_client_session session
HTTP session context.
Definition client_multiprotocol.h:778
struct k_mutex tx_lock
Serializes the senders on the transport, so that a request frame or header block goes out in one piec...
Definition client_multiprotocol.h:795
const struct http_client_transport_ops * transport_ops
Transport API in use.
Definition client_multiprotocol.h:769
struct k_mutex lock
Client lock.
Definition client_multiprotocol.h:781
struct k_work_delayable timeout_work
Deadline enforcement for asynchronous transactions.
Definition client_multiprotocol.h:798
Kernel condition variable structure.
Definition kernel.h:3907
Kernel mutex structure.
Definition kernel.h:3774
Kernel timeout type.
Definition clock.h:65
A structure used to submit work after a delay.
Definition kernel.h:4965
TLS credentials management.