Zephyr API Documentation 4.0.0-rc2
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
23#include <stdint.h>
24
25#include <zephyr/kernel.h>
29#include <zephyr/net/socket.h>
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
38#if defined(CONFIG_HTTP_SERVER)
39#define HTTP_SERVER_CLIENT_BUFFER_SIZE CONFIG_HTTP_SERVER_CLIENT_BUFFER_SIZE
40#define HTTP_SERVER_MAX_STREAMS CONFIG_HTTP_SERVER_MAX_STREAMS
41#define HTTP_SERVER_MAX_CONTENT_TYPE_LEN CONFIG_HTTP_SERVER_MAX_CONTENT_TYPE_LENGTH
42#define HTTP_SERVER_MAX_URL_LENGTH CONFIG_HTTP_SERVER_MAX_URL_LENGTH
43#define HTTP_SERVER_MAX_HEADER_LEN CONFIG_HTTP_SERVER_MAX_HEADER_LEN
44#else
45#define HTTP_SERVER_CLIENT_BUFFER_SIZE 0
46#define HTTP_SERVER_MAX_STREAMS 0
47#define HTTP_SERVER_MAX_CONTENT_TYPE_LEN 0
48#define HTTP_SERVER_MAX_URL_LENGTH 0
49#define HTTP_SERVER_MAX_HEADER_LEN 0
50#endif
51
52#define HTTP2_PREFACE "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
53
76
96
98BUILD_ASSERT(NUM_BITS(
99 sizeof(((struct http_resource_detail *)0)->bitmask_of_supported_http_methods))
100 >= (HTTP_METHOD_END_VALUE - 1));
116
118/* Make sure that the common is the first in the struct. */
119BUILD_ASSERT(offsetof(struct http_resource_detail_static, common) == 0);
132
134/* Make sure that the common is the first in the struct. */
135BUILD_ASSERT(offsetof(struct http_resource_detail_static_fs, common) == 0);
139 const char *extension;
141 const char *content_type;
142};
143
144#define HTTP_SERVER_CONTENT_TYPE(_extension, _content_type) \
145 const STRUCT_SECTION_ITERABLE(http_content_type, _extension) = { \
146 .extension = STRINGIFY(_extension), \
147 .extension_len = sizeof(STRINGIFY(_extension)) - 1, \
148 .content_type = _content_type, \
149 };
150
151#define HTTP_SERVER_CONTENT_TYPE_FOREACH(_it) STRUCT_SECTION_FOREACH(http_content_type, _it)
152
153struct http_client_ctx;
154
164
167 const char *name;
168 const char *value;
169};
170
180
198typedef int (*http_resource_dynamic_cb_t)(struct http_client_ctx *client,
199 enum http_data_status status,
200 uint8_t *data_buffer,
201 size_t data_len,
202 struct http_response_ctx *response_ctx,
203 void *user_data);
204
225
227BUILD_ASSERT(offsetof(struct http_resource_detail_dynamic, common) == 0);
244typedef int (*http_resource_websocket_cb_t)(int ws_socket,
245 void *user_data);
246
271
273BUILD_ASSERT(offsetof(struct http_resource_detail_websocket, common) == 0);
278enum http2_stream_state {
279 HTTP2_STREAM_IDLE,
280 HTTP2_STREAM_RESERVED_LOCAL,
281 HTTP2_STREAM_RESERVED_REMOTE,
282 HTTP2_STREAM_OPEN,
283 HTTP2_STREAM_HALF_CLOSED_LOCAL,
284 HTTP2_STREAM_HALF_CLOSED_REMOTE,
285 HTTP2_STREAM_CLOSED
286};
287
288enum http_server_state {
289 HTTP_SERVER_FRAME_HEADER_STATE,
290 HTTP_SERVER_PREFACE_STATE,
291 HTTP_SERVER_REQUEST_STATE,
292 HTTP_SERVER_FRAME_DATA_STATE,
293 HTTP_SERVER_FRAME_HEADERS_STATE,
294 HTTP_SERVER_FRAME_SETTINGS_STATE,
295 HTTP_SERVER_FRAME_PRIORITY_STATE,
296 HTTP_SERVER_FRAME_WINDOW_UPDATE_STATE,
297 HTTP_SERVER_FRAME_CONTINUATION_STATE,
298 HTTP_SERVER_FRAME_PING_STATE,
299 HTTP_SERVER_FRAME_RST_STREAM_STATE,
300 HTTP_SERVER_FRAME_GOAWAY_STATE,
301 HTTP_SERVER_FRAME_PADDING_STATE,
302 HTTP_SERVER_DONE_STATE,
303};
304
305enum http1_parser_state {
306 HTTP1_INIT_HEADER_STATE,
307 HTTP1_WAITING_HEADER_STATE,
308 HTTP1_RECEIVING_HEADER_STATE,
309 HTTP1_RECEIVED_HEADER_STATE,
310 HTTP1_RECEIVING_DATA_STATE,
311 HTTP1_MESSAGE_COMPLETE_STATE,
312};
313
314#define HTTP_SERVER_INITIAL_WINDOW_SIZE 65536
315#define HTTP_SERVER_WS_MAX_SEC_KEY_LEN 32
316
322 enum http2_stream_state stream_state;
326 bool headers_sent : 1;
327
330};
331
340
341#if defined(CONFIG_HTTP_SERVER_CAPTURE_HEADERS)
343enum http_header_status {
344 HTTP_HEADER_STATUS_OK,
345 HTTP_HEADER_STATUS_DROPPED,
346};
347
349struct http_header_capture_ctx {
351 unsigned char buffer[CONFIG_HTTP_SERVER_CAPTURE_HEADER_BUFFER_SIZE];
352
354 struct http_header headers[CONFIG_HTTP_SERVER_CAPTURE_HEADER_COUNT];
355
357 enum http_header_status status;
358
360 size_t count;
361
363 size_t cursor;
364
366 bool store_next_value;
367};
368
370struct http_header_name {
371 const char *name;
372};
373#endif /* defined(CONFIG_HTTP_SERVER_CAPTURE_HEADERS) */
374
380 int fd;
381
383 unsigned char buffer[HTTP_SERVER_CLIENT_BUFFER_SIZE];
384
386 unsigned char *cursor;
387
389 size_t data_len;
390
393
395 enum http_server_state server_state;
396
399
402
405
408
410 struct http2_stream_ctx streams[HTTP_SERVER_MAX_STREAMS];
411
414
417
418#if defined(CONFIG_HTTP_SERVER_CAPTURE_HEADERS)
420 struct http_header_capture_ctx header_capture_ctx;
421#endif /* defined(CONFIG_HTTP_SERVER_CAPTURE_HEADERS) */
422
424 unsigned char url_buffer[HTTP_SERVER_MAX_URL_LENGTH];
425
427 unsigned char content_type[HTTP_SERVER_MAX_CONTENT_TYPE_LEN];
428
430 unsigned char header_buffer[HTTP_SERVER_MAX_HEADER_LEN];
431
434
437
439 enum http1_parser_state parser_state;
440
445
450
453 IF_ENABLED(CONFIG_WEBSOCKET, (uint8_t ws_sec_key[HTTP_SERVER_WS_MAX_SEC_KEY_LEN]));
457 bool preface_sent : 1;
458
461
464
467
470
473
476};
477
478#if defined(CONFIG_HTTP_SERVER_CAPTURE_HEADERS)
485#define HTTP_SERVER_REGISTER_HEADER_CAPTURE(_id, _header) \
486 BUILD_ASSERT(sizeof(_header) <= CONFIG_HTTP_SERVER_MAX_HEADER_LEN, \
487 "Header is too long to be captured, try increasing " \
488 "CONFIG_HTTP_SERVER_MAX_HEADER_LEN"); \
489 static const char *const _id##_str = _header; \
490 static const STRUCT_SECTION_ITERABLE(http_header_name, _id) = { \
491 .name = _id##_str, \
492 }
493#endif /* defined(CONFIG_HTTP_SERVER_CAPTURE_HEADERS) */
494
502
508
509#ifdef __cplusplus
510}
511#endif
512
517#endif
http_method
HTTP Request Methods.
Definition method.h:28
http_resource_type
HTTP server resource type.
Definition server.h:59
http_data_status
Indicates the status of the currently processed piece of data.
Definition server.h:156
int(* http_resource_dynamic_cb_t)(struct http_client_ctx *client, enum http_data_status status, uint8_t *data_buffer, size_t data_len, struct http_response_ctx *response_ctx, void *user_data)
Callback used when data is received.
Definition server.h:198
int(* http_resource_websocket_cb_t)(int ws_socket, void *user_data)
Callback used when a Websocket connection is setup.
Definition server.h:244
int http_server_stop(void)
Stop the HTTP2 server.
int http_server_start(void)
Start the HTTP2 server.
@ HTTP_RESOURCE_TYPE_STATIC_FS
serves static gzipped files from a filesystem
Definition server.h:64
@ HTTP_RESOURCE_TYPE_STATIC
Static resource, cannot be modified on runtime.
Definition server.h:61
@ HTTP_RESOURCE_TYPE_WEBSOCKET
Websocket resource, application takes control over Websocket connection after and upgrade.
Definition server.h:74
@ HTTP_RESOURCE_TYPE_DYNAMIC
Dynamic resource, server interacts with the application via registered http_resource_dynamic_cb_t.
Definition server.h:69
@ HTTP_SERVER_DATA_FINAL
Final data fragment in current transaction.
Definition server.h:162
@ HTTP_SERVER_DATA_MORE
Transaction incomplete, more data expected.
Definition server.h:160
@ HTTP_SERVER_DATA_ABORTED
Transaction aborted, data incomplete.
Definition server.h:158
http_status
HTTP response status codes.
Definition status.h:36
#define IF_ENABLED(_flag, _code)
Insert code if _flag is defined and equals 1.
Definition util_macro.h:239
HTTP HPACK.
Public kernel APIs.
BSD Sockets compatible API definitions.
HTTP response status codes.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT8_TYPE__ uint8_t
Definition stdint.h:88
HTTP/2 frame representation.
Definition server.h:333
uint32_t stream_identifier
Stream ID the frame belongs to.
Definition server.h:335
uint8_t type
Frame type.
Definition server.h:336
uint8_t flags
Frame flags.
Definition server.h:337
uint8_t padding_len
Frame padding length.
Definition server.h:338
uint32_t length
Frame payload length.
Definition server.h:334
HTTP/2 stream representation.
Definition server.h:320
bool headers_sent
Flag indicating that headers were sent in the reply.
Definition server.h:326
enum http2_stream_state stream_state
Stream state.
Definition server.h:322
int window_size
Stream-level window size.
Definition server.h:323
int stream_id
Stream identifier.
Definition server.h:321
bool end_stream_sent
Flag indicating that END_STREAM flag was sent.
Definition server.h:329
Representation of an HTTP client connected to the server.
Definition server.h:378
struct http2_stream_ctx streams[HTTP_SERVER_MAX_STREAMS]
HTTP/2 streams context.
Definition server.h:410
struct k_work_delayable inactivity_timer
Client inactivity timer.
Definition server.h:449
struct http_parser_settings parser_settings
HTTP/1 parser configuration.
Definition server.h:413
unsigned char * cursor
Cursor indicating currently processed byte.
Definition server.h:386
int window_size
Connection-level window size.
Definition server.h:392
enum http1_parser_state parser_state
HTTP/1 parser state.
Definition server.h:439
bool expect_continuation
The next frame on the stream is expectd to be a continuation frame.
Definition server.h:475
struct http_parser parser
HTTP/1 parser context.
Definition server.h:416
int http1_frag_data_len
Length of the payload length in the currently processed request fragment (HTTP/1 only).
Definition server.h:444
bool websocket_sec_key_next
Flag indicating Websocket key is being processed.
Definition server.h:472
enum http_server_state server_state
Server state for the associated client.
Definition server.h:395
struct http_hpack_header_buf header_field
HTTP/2 header parser context.
Definition server.h:407
struct http_resource_detail * current_detail
Currently processed resource detail.
Definition server.h:401
unsigned char url_buffer[HTTP_SERVER_MAX_URL_LENGTH]
Request URL.
Definition server.h:424
unsigned char header_buffer[HTTP_SERVER_MAX_HEADER_LEN]
Temp buffer for currently processed header (HTTP/1 only).
Definition server.h:430
unsigned char content_type[HTTP_SERVER_MAX_CONTENT_TYPE_LEN]
Request content type.
Definition server.h:427
bool preface_sent
Flag indicating that HTTP2 preface was sent.
Definition server.h:457
enum http_method method
Request method.
Definition server.h:436
int fd
Socket descriptor associated with the server.
Definition server.h:380
bool http1_headers_sent
Flag indicating that HTTP1 headers were sent.
Definition server.h:460
size_t content_len
Request content length.
Definition server.h:433
unsigned char buffer[HTTP_SERVER_CLIENT_BUFFER_SIZE]
Client data buffer.
Definition server.h:383
struct http2_frame current_frame
Currently processed HTTP/2 frame.
Definition server.h:398
struct http2_stream_ctx * current_stream
Currently processed stream.
Definition server.h:404
bool websocket_upgrade
Flag indicating Websocket upgrade takes place.
Definition server.h:469
bool has_upgrade_header
Flag indicating that upgrade header was present in the request.
Definition server.h:463
bool http2_upgrade
Flag indicating HTTP/2 upgrade takes place.
Definition server.h:466
size_t data_len
Data left to process in the buffer.
Definition server.h:389
Definition server.h:138
const char * content_type
Definition server.h:141
size_t extension_len
Definition server.h:140
const char * extension
Definition server.h:139
HTTP header representation.
Definition server.h:166
const char * value
Pointer to header value NULL-terminated string.
Definition server.h:168
const char * name
Pointer to header name NULL-terminated string.
Definition server.h:167
HTTP2 header field with decoding buffer.
Definition hpack.h:110
Definition parser.h:190
Definition parser.h:147
Representation of a dynamic server resource.
Definition server.h:208
void * user_data
A pointer to the user data registered by the application.
Definition server.h:223
http_resource_dynamic_cb_t cb
Resource callback used by the server to interact with the application.
Definition server.h:215
struct http_client_ctx * holder
A pointer to the client currently processing resource, used to prevent concurrent access to the resou...
Definition server.h:220
struct http_resource_detail common
Common resource details.
Definition server.h:210
Representation of a static filesystem server resource.
Definition server.h:125
struct http_resource_detail common
Common resource details.
Definition server.h:127
const char * fs_path
Path in the local filesystem.
Definition server.h:130
Representation of a static server resource.
Definition server.h:106
size_t static_data_len
Size of the static resource.
Definition server.h:114
const void * static_data
Content of the static resource.
Definition server.h:111
struct http_resource_detail common
Common resource details.
Definition server.h:108
Representation of a websocket server resource.
Definition server.h:248
size_t data_buffer_len
Length of the data in the data buffer.
Definition server.h:266
void * user_data
A pointer to the user data registered by the application.
Definition server.h:269
struct http_resource_detail common
Common resource details.
Definition server.h:250
http_resource_websocket_cb_t cb
Resource callback used by the server to interact with the application.
Definition server.h:258
int ws_sock
Websocket socket value.
Definition server.h:253
uint8_t * data_buffer
Data buffer used to exchanged data between server and the, application.
Definition server.h:263
Representation of a server resource, common for all resource types.
Definition server.h:80
int path_len
Length of the URL path.
Definition server.h:88
const char * content_type
Content type of the resource.
Definition server.h:94
uint32_t bitmask_of_supported_http_methods
Bitmask of supported HTTP methods (http_method).
Definition server.h:82
const char * content_encoding
Content encoding of the resource.
Definition server.h:91
enum http_resource_type type
Resource type.
Definition server.h:85
HTTP response context.
Definition server.h:172
bool final_chunk
Length of body data.
Definition server.h:178
const struct http_header * headers
HTTP status code to include in response.
Definition server.h:174
size_t header_count
Array of HTTP headers.
Definition server.h:175
size_t body_len
Pointer to body data.
Definition server.h:177
const uint8_t * body
Length of headers array.
Definition server.h:176
enum http_status status
Definition server.h:173
A structure used to submit work after a delay.
Definition kernel.h:3985
#define NUM_BITS(t)
Number of bits that make up a type.
Definition util.h:33