Zephyr API Documentation  3.6.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
server.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023, Emna Rekik
3 * Copyright (c) 2024 Nordic Semiconductor ASA
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef ZEPHYR_INCLUDE_NET_HTTP_SERVER_H_
9#define ZEPHYR_INCLUDE_NET_HTTP_SERVER_H_
10
18#include <stdint.h>
19
20#include <zephyr/kernel.h>
23#include <zephyr/net/socket.h>
24
25#ifdef __cplusplus
26extern "C" {
27#endif
28
31#if defined(CONFIG_HTTP_SERVER)
32#define HTTP_SERVER_CLIENT_BUFFER_SIZE CONFIG_HTTP_SERVER_CLIENT_BUFFER_SIZE
33#define HTTP_SERVER_MAX_STREAMS CONFIG_HTTP_SERVER_MAX_STREAMS
34#define HTTP_SERVER_MAX_CONTENT_TYPE_LEN CONFIG_HTTP_SERVER_MAX_CONTENT_TYPE_LENGTH
35#define HTTP_SERVER_MAX_URL_LENGTH CONFIG_HTTP_SERVER_MAX_URL_LENGTH
36#else
37#define HTTP_SERVER_CLIENT_BUFFER_SIZE 0
38#define HTTP_SERVER_MAX_STREAMS 0
39#define HTTP_SERVER_MAX_CONTENT_TYPE_LEN 0
40#define HTTP_SERVER_MAX_URL_LENGTH 0
41#endif
42
43/* Maximum header field name / value length. This is only used to detect Upgrade and
44 * websocket header fields and values in the http1 server so the value is quite short.
45 */
46#define HTTP_SERVER_MAX_HEADER_LEN 32
47
48#define HTTP2_PREFACE "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
49
58
63
68};
69
76
79
82
84 const char *content_encoding;
85
87 const char *content_type;
88};
89
91BUILD_ASSERT(NUM_BITS(
92 sizeof(((struct http_resource_detail *)0)->bitmask_of_supported_http_methods))
93 >= (HTTP_METHOD_END_VALUE - 1));
102
104 const void *static_data;
105
108};
109
111/* Make sure that the common is the first in the struct. */
112BUILD_ASSERT(offsetof(struct http_resource_detail_static, common) == 0);
115struct http_client_ctx;
116
125};
126
143typedef int (*http_resource_dynamic_cb_t)(struct http_client_ctx *client,
144 enum http_data_status status,
145 uint8_t *data_buffer,
146 size_t data_len,
147 void *user_data);
148
155
160
165
168
173
176};
177
179BUILD_ASSERT(offsetof(struct http_resource_detail_dynamic, common) == 0);
196typedef int (*http_resource_websocket_cb_t)(int ws_socket,
197 void *user_data);
198
206};
207
209BUILD_ASSERT(offsetof(struct http_resource_detail_websocket, common) == 0);
214enum http_stream_state {
215 HTTP_SERVER_STREAM_IDLE,
216 HTTP_SERVER_STREAM_RESERVED_LOCAL,
217 HTTP_SERVER_STREAM_RESERVED_REMOTE,
218 HTTP_SERVER_STREAM_OPEN,
219 HTTP_SERVER_STREAM_HALF_CLOSED_LOCAL,
220 HTTP_SERVER_STREAM_HALF_CLOSED_REMOTE,
221 HTTP_SERVER_STREAM_CLOSED
222};
223
224enum http_server_state {
225 HTTP_SERVER_FRAME_HEADER_STATE,
226 HTTP_SERVER_PREFACE_STATE,
227 HTTP_SERVER_REQUEST_STATE,
228 HTTP_SERVER_FRAME_DATA_STATE,
229 HTTP_SERVER_FRAME_HEADERS_STATE,
230 HTTP_SERVER_FRAME_SETTINGS_STATE,
231 HTTP_SERVER_FRAME_PRIORITY_STATE,
232 HTTP_SERVER_FRAME_WINDOW_UPDATE_STATE,
233 HTTP_SERVER_FRAME_CONTINUATION_STATE,
234 HTTP_SERVER_FRAME_PING_STATE,
235 HTTP_SERVER_FRAME_RST_STREAM_STATE,
236 HTTP_SERVER_FRAME_GOAWAY_STATE,
237 HTTP_SERVER_DONE_STATE,
238};
239
240enum http1_parser_state {
241 HTTP1_INIT_HEADER_STATE,
242 HTTP1_WAITING_HEADER_STATE,
243 HTTP1_RECEIVING_HEADER_STATE,
244 HTTP1_RECEIVED_HEADER_STATE,
245 HTTP1_RECEIVING_DATA_STATE,
246 HTTP1_MESSAGE_COMPLETE_STATE,
247};
248
249#define HTTP_SERVER_INITIAL_WINDOW_SIZE 65536
250#define HTTP_SERVER_WS_MAX_SEC_KEY_LEN 32
251
257 enum http_stream_state stream_state;
259};
260
268};
269
275 int fd;
276
278 unsigned char buffer[HTTP_SERVER_CLIENT_BUFFER_SIZE];
279
281 unsigned char *cursor;
282
284 size_t data_len;
285
288
290 enum http_server_state server_state;
291
294
297
300
302 struct http_stream_ctx streams[HTTP_SERVER_MAX_STREAMS];
303
306
309
311 unsigned char url_buffer[HTTP_SERVER_MAX_URL_LENGTH];
312
314 unsigned char content_type[HTTP_SERVER_MAX_CONTENT_TYPE_LEN];
315
317 unsigned char header_buffer[HTTP_SERVER_MAX_HEADER_LEN];
318
321
324
326 enum http1_parser_state parser_state;
327
332
337
338 /* Websocket security key. */
339 IF_ENABLED(CONFIG_WEBSOCKET, (uint8_t ws_sec_key[HTTP_SERVER_WS_MAX_SEC_KEY_LEN]));
340
342 bool headers_sent : 1;
343
345 bool preface_sent : 1;
346
349
352
355
358};
359
367
373
374#ifdef __cplusplus
375}
376#endif
377
382#endif
http_method
HTTP Request Methods.
Definition: method.h:26
@ HTTP_METHOD_END_VALUE
Definition: method.h:61
http_resource_type
HTTP server resource type.
Definition: server.h:55
int(* http_resource_dynamic_cb_t)(struct http_client_ctx *client, enum http_data_status status, uint8_t *data_buffer, size_t data_len, void *user_data)
Callback used when data is received.
Definition: server.h:143
http_data_status
Indicates the status of the currently processed piece of data.
Definition: server.h:118
int http_server_stop(void)
Stop the HTTP2 server.
int http_server_start(void)
Start the HTTP2 server.
int(* http_resource_websocket_cb_t)(int ws_socket, void *user_data)
Callback used when a Websocket connection is setup.
Definition: server.h:196
@ HTTP_RESOURCE_TYPE_STATIC
Static resource, cannot be modified on runtime.
Definition: server.h:57
@ HTTP_RESOURCE_TYPE_WEBSOCKET
Websocket resource, application takes control over Websocket connection after and upgrade.
Definition: server.h:67
@ HTTP_RESOURCE_TYPE_DYNAMIC
Dynamic resource, server interacts with the application via registered http_resource_dynamic_cb_t.
Definition: server.h:62
@ HTTP_SERVER_DATA_FINAL
Final data fragment in current transaction.
Definition: server.h:124
@ HTTP_SERVER_DATA_MORE
Transaction incomplete, more data expected.
Definition: server.h:122
@ HTTP_SERVER_DATA_ABORTED
Transaction aborted, data incomplete.
Definition: server.h:120
HTTP HPACK.
Public kernel APIs.
BSD Sockets compatible API definitions.
__UINT32_TYPE__ uint32_t
Definition: stdint.h:90
__UINT8_TYPE__ uint8_t
Definition: stdint.h:88
Representation of an HTTP client connected to the server.
Definition: server.h:273
struct k_work_delayable inactivity_timer
Client inactivity timer.
Definition: server.h:336
struct http_parser_settings parser_settings
HTTP/1 parser configuration.
Definition: server.h:305
unsigned char * cursor
Cursor indicating currently processed byte.
Definition: server.h:281
int window_size
Connection-level window size.
Definition: server.h:287
struct http_stream_ctx streams[HTTP_SERVER_MAX_STREAMS]
HTTP/2 streams context.
Definition: server.h:302
enum http1_parser_state parser_state
HTTP/1 parser state.
Definition: server.h:326
struct http_parser parser
HTTP/1 parser context.
Definition: server.h:308
int http1_frag_data_len
Length of the payload length in the currently processed request fragment (HTTP/1 only).
Definition: server.h:331
bool websocket_sec_key_next
Flag indicating Websocket key is being processed.
Definition: server.h:357
IF_ENABLED(CONFIG_WEBSOCKET,(uint8_t ws_sec_key[HTTP_SERVER_WS_MAX_SEC_KEY_LEN]))
enum http_server_state server_state
Server state for the associated client.
Definition: server.h:290
struct http_hpack_header_buf header_field
HTTP/2 header parser context.
Definition: server.h:299
bool headers_sent
Flag indicating that headers were sent in the reply.
Definition: server.h:342
struct http_resource_detail * current_detail
Currently processed resource detail.
Definition: server.h:296
unsigned char url_buffer[HTTP_SERVER_MAX_URL_LENGTH]
Request URL.
Definition: server.h:311
struct http_frame current_frame
Currently processed HTTP/2 frame.
Definition: server.h:293
unsigned char header_buffer[HTTP_SERVER_MAX_HEADER_LEN]
Temp buffer for currently processed header (HTTP/1 only).
Definition: server.h:317
unsigned char content_type[HTTP_SERVER_MAX_CONTENT_TYPE_LEN]
Request content type.
Definition: server.h:314
bool preface_sent
Flag indicating that HTTP2 preface was sent.
Definition: server.h:345
enum http_method method
Request method.
Definition: server.h:323
int fd
Socket descriptor associated with the server.
Definition: server.h:275
size_t content_len
Request content length.
Definition: server.h:320
unsigned char buffer[HTTP_SERVER_CLIENT_BUFFER_SIZE]
Client data buffer.
Definition: server.h:278
bool websocket_upgrade
Flag indicating Websocket upgrade takes place.
Definition: server.h:354
bool has_upgrade_header
Flag indicating that upgrade header was present in the request.
Definition: server.h:348
bool http2_upgrade
Flag indicating HTTP/2 upgrade takes place.
Definition: server.h:351
size_t data_len
Data left to process in the buffer.
Definition: server.h:284
HTTP/2 frame representation.
Definition: server.h:262
uint8_t type
Frame type.
Definition: server.h:265
uint8_t * payload
A pointer to the frame payload.
Definition: server.h:267
uint8_t flags
Frame flags.
Definition: server.h:266
uint32_t length
Frame payload length.
Definition: server.h:263
uint32_t stream_identifier
Stream ID the frame belongs to.
Definition: server.h:264
HTTP2 header field with decoding buffer.
Definition: hpack.h:104
Definition: parser.h:190
Definition: parser.h:147
Representation of a dynamic server resource.
Definition: server.h:152
uint8_t * data_buffer
Data buffer used to exchanged data between server and the, application.
Definition: server.h:164
size_t data_buffer_len
Length of the data in the data buffer.
Definition: server.h:167
void * user_data
A pointer to the user data registered by the application.
Definition: server.h:175
http_resource_dynamic_cb_t cb
Resource callback used by the server to interact with the application.
Definition: server.h:159
struct http_client_ctx * holder
A pointer to the client currently processing resource, used to prevent concurrent access to the resou...
Definition: server.h:172
struct http_resource_detail common
Common resource details.
Definition: server.h:154
Representation of a static server resource.
Definition: server.h:99
size_t static_data_len
Size of the static resource.
Definition: server.h:107
const void * static_data
Content of the static resource.
Definition: server.h:104
struct http_resource_detail common
Common resource details.
Definition: server.h:101
Definition: server.h:199
size_t data_buffer_len
Definition: server.h:204
void * user_data
Definition: server.h:205
struct http_resource_detail common
Definition: server.h:200
http_resource_websocket_cb_t cb
Definition: server.h:202
int ws_sock
Definition: server.h:201
uint8_t * data_buffer
Definition: server.h:203
Representation of a server resource, common for all resource types.
Definition: server.h:73
int path_len
Length of the URL path.
Definition: server.h:81
const char * content_type
Content type of the resource.
Definition: server.h:87
uint32_t bitmask_of_supported_http_methods
Bitmask of supported HTTP methods (http_method).
Definition: server.h:75
const char * content_encoding
Content encoding of the resource.
Definition: server.h:84
enum http_resource_type type
Resource type.
Definition: server.h:78
HTTP/2 stream representation.
Definition: server.h:255
int window_size
Stream-level window size.
Definition: server.h:258
enum http_stream_state stream_state
Stream state.
Definition: server.h:257
int stream_id
Stream identifier.
Definition: server.h:256
A structure used to submit work after a delay.
Definition: kernel.h:3903
#define NUM_BITS(t)
Number of bits that make up a type.
Definition: util.h:33