Zephyr API Documentation 4.4.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
22
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
37
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#if defined(CONFIG_HTTP_SERVER_CAPTURE_HEADERS)
53#define HTTP_SERVER_CAPTURE_HEADER_BUFFER_SIZE CONFIG_HTTP_SERVER_CAPTURE_HEADER_BUFFER_SIZE
54#define HTTP_SERVER_CAPTURE_HEADER_COUNT CONFIG_HTTP_SERVER_CAPTURE_HEADER_COUNT
55#else
56#define HTTP_SERVER_CAPTURE_HEADER_BUFFER_SIZE 0
57#define HTTP_SERVER_CAPTURE_HEADER_COUNT 0
58#endif
59
60#define HTTP2_PREFACE "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
61
63
84
104
106BUILD_ASSERT(NUM_BITS(
107 sizeof(((struct http_resource_detail *)0)->bitmask_of_supported_http_methods))
108 >= (HTTP_METHOD_END_VALUE - 1));
110
124
126/* Make sure that the common is the first in the struct. */
127BUILD_ASSERT(offsetof(struct http_resource_detail_static, common) == 0);
129
140
150
152/* Make sure that the common is the first in the struct. */
153BUILD_ASSERT(offsetof(struct http_resource_detail_static_fs, common) == 0);
155
157 const char *extension;
159 const char *content_type;
160};
161
162#define HTTP_SERVER_CONTENT_TYPE(_extension, _content_type) \
163 const STRUCT_SECTION_ITERABLE(http_content_type, _extension) = { \
164 .extension = STRINGIFY(_extension), \
165 .extension_len = sizeof(STRINGIFY(_extension)) - 1, \
166 .content_type = _content_type, \
167 };
168
169#define HTTP_SERVER_CONTENT_TYPE_FOREACH(_it) STRUCT_SECTION_FOREACH(http_content_type, _it)
170
171struct http_client_ctx;
172
184
191
194 const char *name;
195 const char *value;
196};
197
217
227
248typedef int (*http_resource_dynamic_cb_t)(struct http_client_ctx *client,
249 enum http_transaction_status status,
250 const struct http_request_ctx *request_ctx,
251 struct http_response_ctx *response_ctx,
252 void *user_data);
253
274
276BUILD_ASSERT(offsetof(struct http_resource_detail_dynamic, common) == 0);
278
294typedef int (*http_resource_websocket_cb_t)(int ws_socket, struct http_request_ctx *request_ctx,
295 void *user_data);
296
321
323BUILD_ASSERT(offsetof(struct http_resource_detail_websocket, common) == 0);
324
325enum http2_stream_state {
326 HTTP2_STREAM_IDLE,
327 HTTP2_STREAM_RESERVED_LOCAL,
328 HTTP2_STREAM_RESERVED_REMOTE,
329 HTTP2_STREAM_OPEN,
330 HTTP2_STREAM_HALF_CLOSED_LOCAL,
331 HTTP2_STREAM_HALF_CLOSED_REMOTE,
332 HTTP2_STREAM_CLOSED
333};
334
335enum http_server_state {
336 HTTP_SERVER_FRAME_HEADER_STATE,
337 HTTP_SERVER_PREFACE_STATE,
338 HTTP_SERVER_REQUEST_STATE,
339 HTTP_SERVER_FRAME_DATA_STATE,
340 HTTP_SERVER_FRAME_HEADERS_STATE,
341 HTTP_SERVER_FRAME_SETTINGS_STATE,
342 HTTP_SERVER_FRAME_PRIORITY_STATE,
343 HTTP_SERVER_FRAME_WINDOW_UPDATE_STATE,
344 HTTP_SERVER_FRAME_CONTINUATION_STATE,
345 HTTP_SERVER_FRAME_PING_STATE,
346 HTTP_SERVER_FRAME_RST_STREAM_STATE,
347 HTTP_SERVER_FRAME_GOAWAY_STATE,
348 HTTP_SERVER_FRAME_PADDING_STATE,
349 HTTP_SERVER_DONE_STATE,
350 HTTP_SERVER_H3_STREAM_STATE,
351};
352
353enum http1_parser_state {
354 HTTP1_INIT_HEADER_STATE,
355 HTTP1_WAITING_HEADER_STATE,
356 HTTP1_RECEIVING_HEADER_STATE,
357 HTTP1_RECEIVED_HEADER_STATE,
358 HTTP1_RECEIVING_DATA_STATE,
359 HTTP1_MESSAGE_COMPLETE_STATE,
360};
361
362#define HTTP_SERVER_INITIAL_WINDOW_SIZE 65536
363#define HTTP_SERVER_WS_MAX_SEC_KEY_LEN 32
364#define HTTP_SERVER_WS_MAX_SEC_PROTOCOL_LEN 64
365
367struct http2_stream_ctx {
368 int stream_id;
369 enum http2_stream_state stream_state;
370 int window_size;
371
373 struct http_resource_detail *current_detail;
374
376 bool headers_sent : 1;
377
379 bool end_stream_sent : 1;
380};
381
383struct http2_frame {
384 uint32_t length;
385 uint32_t stream_identifier;
386 uint8_t type;
387 uint8_t flags;
388 uint8_t padding_len;
389};
390
392struct http_header_capture_ctx {
394 unsigned char buffer[HTTP_SERVER_CAPTURE_HEADER_BUFFER_SIZE];
395
397 struct http_header headers[HTTP_SERVER_CAPTURE_HEADER_COUNT];
398
400 enum http_header_status status;
401
403 size_t count;
404
406 size_t cursor;
407
409 struct http2_stream_ctx *current_stream;
410
412 bool store_next_value;
413};
414
416struct http3_stream_ctx {
418 unsigned char buffer[HTTP_SERVER_CLIENT_BUFFER_SIZE];
419
421 size_t data_len;
422
424 struct http_resource_detail *current_detail;
425
427 unsigned char url_buffer[HTTP_SERVER_MAX_URL_LENGTH];
428
430 enum http_method method;
431
433 struct http_header_capture_ctx header_capture_ctx;
434
436 bool request_headers_pending;
437};
438
440struct http_header_name {
441 const char *name;
442};
444
451 int fd;
452
454 const struct http_service_desc *service;
455
457 unsigned char buffer[HTTP_SERVER_CLIENT_BUFFER_SIZE];
458
460 unsigned char *cursor;
461
463 size_t data_len;
464
466 int window_size;
467
469 enum http_server_state server_state;
470
472 struct http2_frame current_frame;
473
475 struct http_resource_detail *current_detail;
476
478 struct http2_stream_ctx *current_stream;
479
481 struct http_hpack_header_buf header_field;
482
484 struct http2_stream_ctx streams[HTTP_SERVER_MAX_STREAMS];
485
487 struct http_parser_settings parser_settings;
488
490 struct http_parser parser;
491
493 struct http_header_capture_ctx header_capture_ctx;
494
496 unsigned char url_buffer[HTTP_SERVER_MAX_URL_LENGTH];
497
499 unsigned char content_type[HTTP_SERVER_MAX_CONTENT_TYPE_LEN];
500
502 unsigned char header_buffer[HTTP_SERVER_MAX_HEADER_LEN];
504
507
510
513 enum http1_parser_state parser_state;
514
518 int http1_frag_data_len;
519
523 struct k_work_delayable inactivity_timer;
524
526 IF_ENABLED(CONFIG_WEBSOCKET, (uint8_t ws_sec_key[HTTP_SERVER_WS_MAX_SEC_KEY_LEN]));
527
533 IF_ENABLED(CONFIG_WEBSOCKET,
534 (char ws_sec_protocol[HTTP_SERVER_WS_MAX_SEC_PROTOCOL_LEN]));
535
537 IF_ENABLED(CONFIG_HTTP_SERVER_COMPRESSION, (uint8_t supported_compression));
538
540 struct {
541#if defined(CONFIG_HTTP_SERVER_VERSION_3)
545 int conn_sock;
546
547#define HTTP3_SERVER_MAX_STREAMS (CONFIG_QUIC_MAX_STREAMS_UNI + CONFIG_QUIC_MAX_STREAMS_BIDI)
548
551 int stream_sock[HTTP3_SERVER_MAX_STREAMS];
552
554 bool headers_sent[HTTP3_SERVER_MAX_STREAMS];
555
557 struct http3_stream_ctx streams[HTTP3_SERVER_MAX_STREAMS];
558#else
559 /* This is here in order to avoid adding #ifdefs in the code related
560 * to the HTTP/3.
561 */
562 union {
563 int conn_sock;
564 int stream_sock[0];
565 bool headers_sent[0];
566 struct http3_stream_ctx streams[0];
567 };
568
569#define HTTP3_SERVER_MAX_STREAMS 0
570#endif /* CONFIG_HTTP_SERVER_VERSION_3 */
571 } h3;
573
575 bool is_h3 : 1;
576
578 bool preface_sent : 1;
579
582
585
588
591
594 bool websocket_sec_key_next : 1;
595
597 bool websocket_sec_protocol_next : 1;
598
600 IF_ENABLED(CONFIG_HTTP_SERVER_COMPRESSION, (bool accept_encoding_next: 1));
601
603 bool expect_continuation : 1;
605};
606
613#define HTTP_SERVER_REGISTER_HEADER_CAPTURE(_id, _header) \
614 BUILD_ASSERT(sizeof(_header) <= CONFIG_HTTP_SERVER_MAX_HEADER_LEN, \
615 "Header is too long to be captured, try increasing " \
616 "CONFIG_HTTP_SERVER_MAX_HEADER_LEN"); \
617 static const char *const _id##_str = _header; \
618 static const STRUCT_SECTION_ITERABLE(http_header_name, _id) = { \
619 .name = _id##_str, \
620 }
621
629
635
636#ifdef __cplusplus
637}
638#endif
639
643
644#endif
http_method
HTTP Request Methods.
Definition method.h:28
http_resource_type
HTTP server resource type.
Definition server.h:67
http_transaction_status
Indicates the status of the currently processed piece of data.
Definition server.h:174
http_header_status
Status of captured request headers.
Definition server.h:186
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, struct http_request_ctx *request_ctx, void *user_data)
Callback used when a Websocket connection is setup.
Definition server.h:294
int(* http_resource_dynamic_cb_t)(struct http_client_ctx *client, enum http_transaction_status status, const struct http_request_ctx *request_ctx, struct http_response_ctx *response_ctx, void *user_data)
Callback used when data is received.
Definition server.h:248
http_compression
HTTP compressions.
Definition server.h:142
@ HTTP_RESOURCE_TYPE_STATIC_FS
serves static gzipped files from a filesystem
Definition server.h:72
@ HTTP_RESOURCE_TYPE_STATIC
Static resource, cannot be modified on runtime.
Definition server.h:69
@ HTTP_RESOURCE_TYPE_WEBSOCKET
Websocket resource, application takes control over Websocket connection after and upgrade.
Definition server.h:82
@ HTTP_RESOURCE_TYPE_DYNAMIC
Dynamic resource, server interacts with the application via registered http_resource_dynamic_cb_t.
Definition server.h:77
@ HTTP_SERVER_REQUEST_DATA_MORE
Transaction incomplete, more data expected.
Definition server.h:178
@ HTTP_SERVER_TRANSACTION_ABORTED
Transaction aborted, data incomplete.
Definition server.h:176
@ HTTP_SERVER_TRANSACTION_COMPLETE
Transaction completed, response sent completely.
Definition server.h:182
@ HTTP_SERVER_REQUEST_DATA_FINAL
Final data fragment in current transaction.
Definition server.h:180
@ HTTP_HEADER_STATUS_DROPPED
One or more headers were dropped due to lack of space.
Definition server.h:188
@ HTTP_HEADER_STATUS_NONE
No header status is available.
Definition server.h:189
@ HTTP_HEADER_STATUS_OK
All available headers were successfully captured.
Definition server.h:187
@ HTTP_NONE
NONE.
Definition server.h:143
@ HTTP_GZIP
GZIP.
Definition server.h:144
@ HTTP_ZSTD
ZSTD.
Definition server.h:148
@ HTTP_DEFLATE
DEFLATE.
Definition server.h:146
@ HTTP_COMPRESS
COMPRESS.
Definition server.h:145
@ HTTP_BR
BR.
Definition server.h:147
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:278
HTTP HPACK.
Public kernel APIs.
BSD Sockets compatible API definitions.
flags
Definition parser.h:97
HTTP response status codes.
__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:448
bool is_h3
Flag indicating this is an HTTP/3 (QUIC stream) client.
Definition server.h:575
bool preface_sent
Flag indicating that HTTP2 preface was sent.
Definition server.h:578
enum http_method method
Request method.
Definition server.h:509
bool http1_headers_sent
Flag indicating that HTTP1 headers were sent.
Definition server.h:581
size_t content_len
Request content length.
Definition server.h:506
bool websocket_upgrade
Flag indicating Websocket upgrade takes place.
Definition server.h:590
bool has_upgrade_header
Flag indicating that upgrade header was present in the request.
Definition server.h:584
bool http2_upgrade
Flag indicating HTTP/2 upgrade takes place.
Definition server.h:587
Definition server.h:156
const char * content_type
Definition server.h:159
size_t extension_len
Definition server.h:158
const char * extension
Definition server.h:157
HTTP header representation.
Definition server.h:193
const char * value
Pointer to header value NULL-terminated string.
Definition server.h:195
const char * name
Pointer to header name NULL-terminated string.
Definition server.h:194
HTTP2 header field with decoding buffer.
Definition hpack.h:110
Definition parser.h:200
Definition parser.h:155
HTTP request context.
Definition server.h:210
enum http_header_status headers_status
Status of HTTP request headers.
Definition server.h:215
size_t header_count
Array length of HTTP request headers.
Definition server.h:214
size_t data_len
Length of HTTP request data.
Definition server.h:212
struct http_header * headers
Array of HTTP request headers.
Definition server.h:213
uint8_t * data
HTTP request data.
Definition server.h:211
Representation of a dynamic server resource.
Definition server.h:257
void * user_data
A pointer to the user data registered by the application.
Definition server.h:272
http_resource_dynamic_cb_t cb
Resource callback used by the server to interact with the application.
Definition server.h:264
struct http_client_ctx * holder
A pointer to the client currently processing resource, used to prevent concurrent access to the resou...
Definition server.h:269
struct http_resource_detail common
Common resource details.
Definition server.h:259
Representation of a static filesystem server resource.
Definition server.h:133
struct http_resource_detail common
Common resource details.
Definition server.h:135
const char * fs_path
Path in the local filesystem.
Definition server.h:138
Representation of a static server resource.
Definition server.h:114
size_t static_data_len
Size of the static resource.
Definition server.h:122
const void * static_data
Content of the static resource.
Definition server.h:119
struct http_resource_detail common
Common resource details.
Definition server.h:116
Representation of a websocket server resource.
Definition server.h:298
size_t data_buffer_len
Length of the data in the data buffer.
Definition server.h:316
void * user_data
A pointer to the user data registered by the application.
Definition server.h:319
struct http_resource_detail common
Common resource details.
Definition server.h:300
http_resource_websocket_cb_t cb
Resource callback used by the server to interact with the application.
Definition server.h:308
int ws_sock
Websocket socket value.
Definition server.h:303
uint8_t * data_buffer
Data buffer used to exchanged data between server and the, application.
Definition server.h:313
Representation of a server resource, common for all resource types.
Definition server.h:88
int path_len
Length of the URL path.
Definition server.h:96
const char * content_type
Content type of the resource.
Definition server.h:102
uint32_t bitmask_of_supported_http_methods
Bitmask of supported HTTP methods (http_method).
Definition server.h:90
const char * content_encoding
Content encoding of the resource.
Definition server.h:99
enum http_resource_type type
Resource type.
Definition server.h:93
HTTP response context.
Definition server.h:219
bool final_chunk
Flag set to true when the application has no more data to send.
Definition server.h:225
const struct http_header * headers
Array of HTTP headers.
Definition server.h:221
size_t header_count
Length of headers array.
Definition server.h:222
size_t body_len
Length of body data.
Definition server.h:224
const uint8_t * body
Pointer to body data.
Definition server.h:223
enum http_status status
HTTP status code to include in response.
Definition server.h:220
A structure used to submit work after a delay.
Definition kernel.h:4643
Iterable sections helpers.
#define NUM_BITS(t)
Number of bits that make up a type.
Definition util.h:34