Zephyr API Documentation  3.7.0-rc1
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
21#include <stdint.h>
22
23#include <zephyr/kernel.h>
26#include <zephyr/net/socket.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
34#if defined(CONFIG_HTTP_SERVER)
35#define HTTP_SERVER_CLIENT_BUFFER_SIZE CONFIG_HTTP_SERVER_CLIENT_BUFFER_SIZE
36#define HTTP_SERVER_MAX_STREAMS CONFIG_HTTP_SERVER_MAX_STREAMS
37#define HTTP_SERVER_MAX_CONTENT_TYPE_LEN CONFIG_HTTP_SERVER_MAX_CONTENT_TYPE_LENGTH
38#define HTTP_SERVER_MAX_URL_LENGTH CONFIG_HTTP_SERVER_MAX_URL_LENGTH
39#else
40#define HTTP_SERVER_CLIENT_BUFFER_SIZE 0
41#define HTTP_SERVER_MAX_STREAMS 0
42#define HTTP_SERVER_MAX_CONTENT_TYPE_LEN 0
43#define HTTP_SERVER_MAX_URL_LENGTH 0
44#endif
45
46/* Maximum header field name / value length. This is only used to detect Upgrade and
47 * websocket header fields and values in the http1 server so the value is quite short.
48 */
49#define HTTP_SERVER_MAX_HEADER_LEN 32
50
51#define HTTP2_PREFACE "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
52
61
66
71};
72
79
82
85
87 const char *content_encoding;
88
90 const char *content_type;
91};
92
94BUILD_ASSERT(NUM_BITS(
95 sizeof(((struct http_resource_detail *)0)->bitmask_of_supported_http_methods))
96 >= (HTTP_METHOD_END_VALUE - 1));
105
107 const void *static_data;
108
111};
112
114/* Make sure that the common is the first in the struct. */
115BUILD_ASSERT(offsetof(struct http_resource_detail_static, common) == 0);
118struct http_client_ctx;
119
128};
129
146typedef int (*http_resource_dynamic_cb_t)(struct http_client_ctx *client,
147 enum http_data_status status,
148 uint8_t *data_buffer,
149 size_t data_len,
150 void *user_data);
151
158
163
168
171
176
179};
180
182BUILD_ASSERT(offsetof(struct http_resource_detail_dynamic, common) == 0);
199typedef int (*http_resource_websocket_cb_t)(int ws_socket,
200 void *user_data);
201
206
209
214
219
222
225};
226
228BUILD_ASSERT(offsetof(struct http_resource_detail_websocket, common) == 0);
233enum http_stream_state {
234 HTTP_SERVER_STREAM_IDLE,
235 HTTP_SERVER_STREAM_RESERVED_LOCAL,
236 HTTP_SERVER_STREAM_RESERVED_REMOTE,
237 HTTP_SERVER_STREAM_OPEN,
238 HTTP_SERVER_STREAM_HALF_CLOSED_LOCAL,
239 HTTP_SERVER_STREAM_HALF_CLOSED_REMOTE,
240 HTTP_SERVER_STREAM_CLOSED
241};
242
243enum http_server_state {
244 HTTP_SERVER_FRAME_HEADER_STATE,
245 HTTP_SERVER_PREFACE_STATE,
246 HTTP_SERVER_REQUEST_STATE,
247 HTTP_SERVER_FRAME_DATA_STATE,
248 HTTP_SERVER_FRAME_HEADERS_STATE,
249 HTTP_SERVER_FRAME_SETTINGS_STATE,
250 HTTP_SERVER_FRAME_PRIORITY_STATE,
251 HTTP_SERVER_FRAME_WINDOW_UPDATE_STATE,
252 HTTP_SERVER_FRAME_CONTINUATION_STATE,
253 HTTP_SERVER_FRAME_PING_STATE,
254 HTTP_SERVER_FRAME_RST_STREAM_STATE,
255 HTTP_SERVER_FRAME_GOAWAY_STATE,
256 HTTP_SERVER_DONE_STATE,
257};
258
259enum http1_parser_state {
260 HTTP1_INIT_HEADER_STATE,
261 HTTP1_WAITING_HEADER_STATE,
262 HTTP1_RECEIVING_HEADER_STATE,
263 HTTP1_RECEIVED_HEADER_STATE,
264 HTTP1_RECEIVING_DATA_STATE,
265 HTTP1_MESSAGE_COMPLETE_STATE,
266};
267
268#define HTTP_SERVER_INITIAL_WINDOW_SIZE 65536
269#define HTTP_SERVER_WS_MAX_SEC_KEY_LEN 32
270
276 enum http_stream_state stream_state;
278};
279
287};
288
294 int fd;
295
297 unsigned char buffer[HTTP_SERVER_CLIENT_BUFFER_SIZE];
298
300 unsigned char *cursor;
301
303 size_t data_len;
304
307
309 enum http_server_state server_state;
310
313
316
319
321 struct http_stream_ctx streams[HTTP_SERVER_MAX_STREAMS];
322
325
328
330 unsigned char url_buffer[HTTP_SERVER_MAX_URL_LENGTH];
331
333 unsigned char content_type[HTTP_SERVER_MAX_CONTENT_TYPE_LEN];
334
336 unsigned char header_buffer[HTTP_SERVER_MAX_HEADER_LEN];
337
340
343
345 enum http1_parser_state parser_state;
346
351
356
359 IF_ENABLED(CONFIG_WEBSOCKET, (uint8_t ws_sec_key[HTTP_SERVER_WS_MAX_SEC_KEY_LEN]));
363 bool headers_sent : 1;
364
366 bool preface_sent : 1;
367
370
373
376
379};
380
388
394
395#ifdef __cplusplus
396}
397#endif
398
403#endif
http_method
HTTP Request Methods.
Definition: method.h:26
http_resource_type
HTTP server resource type.
Definition: server.h:58
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:146
http_data_status
Indicates the status of the currently processed piece of data.
Definition: server.h:121
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:199
@ HTTP_RESOURCE_TYPE_STATIC
Static resource, cannot be modified on runtime.
Definition: server.h:60
@ HTTP_RESOURCE_TYPE_WEBSOCKET
Websocket resource, application takes control over Websocket connection after and upgrade.
Definition: server.h:70
@ HTTP_RESOURCE_TYPE_DYNAMIC
Dynamic resource, server interacts with the application via registered http_resource_dynamic_cb_t.
Definition: server.h:65
@ HTTP_SERVER_DATA_FINAL
Final data fragment in current transaction.
Definition: server.h:127
@ HTTP_SERVER_DATA_MORE
Transaction incomplete, more data expected.
Definition: server.h:125
@ HTTP_SERVER_DATA_ABORTED
Transaction aborted, data incomplete.
Definition: server.h:123
#define IF_ENABLED(_flag, _code)
Insert code if _flag is defined and equals 1.
Definition: util_macro.h:223
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:292
struct k_work_delayable inactivity_timer
Client inactivity timer.
Definition: server.h:355
struct http_parser_settings parser_settings
HTTP/1 parser configuration.
Definition: server.h:324
unsigned char * cursor
Cursor indicating currently processed byte.
Definition: server.h:300
int window_size
Connection-level window size.
Definition: server.h:306
struct http_stream_ctx streams[HTTP_SERVER_MAX_STREAMS]
HTTP/2 streams context.
Definition: server.h:321
enum http1_parser_state parser_state
HTTP/1 parser state.
Definition: server.h:345
struct http_parser parser
HTTP/1 parser context.
Definition: server.h:327
int http1_frag_data_len
Length of the payload length in the currently processed request fragment (HTTP/1 only).
Definition: server.h:350
bool websocket_sec_key_next
Flag indicating Websocket key is being processed.
Definition: server.h:378
enum http_server_state server_state
Server state for the associated client.
Definition: server.h:309
struct http_hpack_header_buf header_field
HTTP/2 header parser context.
Definition: server.h:318
bool headers_sent
Flag indicating that headers were sent in the reply.
Definition: server.h:363
struct http_resource_detail * current_detail
Currently processed resource detail.
Definition: server.h:315
unsigned char url_buffer[HTTP_SERVER_MAX_URL_LENGTH]
Request URL.
Definition: server.h:330
struct http_frame current_frame
Currently processed HTTP/2 frame.
Definition: server.h:312
unsigned char header_buffer[HTTP_SERVER_MAX_HEADER_LEN]
Temp buffer for currently processed header (HTTP/1 only).
Definition: server.h:336
unsigned char content_type[HTTP_SERVER_MAX_CONTENT_TYPE_LEN]
Request content type.
Definition: server.h:333
bool preface_sent
Flag indicating that HTTP2 preface was sent.
Definition: server.h:366
enum http_method method
Request method.
Definition: server.h:342
int fd
Socket descriptor associated with the server.
Definition: server.h:294
size_t content_len
Request content length.
Definition: server.h:339
unsigned char buffer[HTTP_SERVER_CLIENT_BUFFER_SIZE]
Client data buffer.
Definition: server.h:297
bool websocket_upgrade
Flag indicating Websocket upgrade takes place.
Definition: server.h:375
bool has_upgrade_header
Flag indicating that upgrade header was present in the request.
Definition: server.h:369
bool http2_upgrade
Flag indicating HTTP/2 upgrade takes place.
Definition: server.h:372
size_t data_len
Data left to process in the buffer.
Definition: server.h:303
HTTP/2 frame representation.
Definition: server.h:281
uint8_t type
Frame type.
Definition: server.h:284
uint8_t * payload
A pointer to the frame payload.
Definition: server.h:286
uint8_t flags
Frame flags.
Definition: server.h:285
uint32_t length
Frame payload length.
Definition: server.h:282
uint32_t stream_identifier
Stream ID the frame belongs to.
Definition: server.h:283
HTTP2 header field with decoding buffer.
Definition: hpack.h:108
Definition: parser.h:190
Definition: parser.h:147
Representation of a dynamic server resource.
Definition: server.h:155
uint8_t * data_buffer
Data buffer used to exchanged data between server and the, application.
Definition: server.h:167
size_t data_buffer_len
Length of the data in the data buffer.
Definition: server.h:170
void * user_data
A pointer to the user data registered by the application.
Definition: server.h:178
http_resource_dynamic_cb_t cb
Resource callback used by the server to interact with the application.
Definition: server.h:162
struct http_client_ctx * holder
A pointer to the client currently processing resource, used to prevent concurrent access to the resou...
Definition: server.h:175
struct http_resource_detail common
Common resource details.
Definition: server.h:157
Representation of a static server resource.
Definition: server.h:102
size_t static_data_len
Size of the static resource.
Definition: server.h:110
const void * static_data
Content of the static resource.
Definition: server.h:107
struct http_resource_detail common
Common resource details.
Definition: server.h:104
Representation of a websocket server resource.
Definition: server.h:203
size_t data_buffer_len
Length of the data in the data buffer.
Definition: server.h:221
void * user_data
A pointer to the user data registered by the application.
Definition: server.h:224
struct http_resource_detail common
Common resource details.
Definition: server.h:205
http_resource_websocket_cb_t cb
Resource callback used by the server to interact with the application.
Definition: server.h:213
int ws_sock
Websocket socket value.
Definition: server.h:208
uint8_t * data_buffer
Data buffer used to exchanged data between server and the, application.
Definition: server.h:218
Representation of a server resource, common for all resource types.
Definition: server.h:76
int path_len
Length of the URL path.
Definition: server.h:84
const char * content_type
Content type of the resource.
Definition: server.h:90
uint32_t bitmask_of_supported_http_methods
Bitmask of supported HTTP methods (http_method).
Definition: server.h:78
const char * content_encoding
Content encoding of the resource.
Definition: server.h:87
enum http_resource_type type
Resource type.
Definition: server.h:81
HTTP/2 stream representation.
Definition: server.h:274
int window_size
Stream-level window size.
Definition: server.h:277
enum http_stream_state stream_state
Stream state.
Definition: server.h:276
int stream_id
Stream identifier.
Definition: server.h:275
A structure used to submit work after a delay.
Definition: kernel.h:3905
#define NUM_BITS(t)
Number of bits that make up a type.
Definition: util.h:33