8#ifndef ZEPHYR_INCLUDE_NET_HTTP_SERVER_H_
9#define ZEPHYR_INCLUDE_NET_HTTP_SERVER_H_
39#if defined(CONFIG_HTTP_SERVER)
40#define HTTP_SERVER_CLIENT_BUFFER_SIZE CONFIG_HTTP_SERVER_CLIENT_BUFFER_SIZE
41#define HTTP_SERVER_MAX_STREAMS CONFIG_HTTP_SERVER_MAX_STREAMS
42#define HTTP_SERVER_MAX_CONTENT_TYPE_LEN CONFIG_HTTP_SERVER_MAX_CONTENT_TYPE_LENGTH
43#define HTTP_SERVER_MAX_URL_LENGTH CONFIG_HTTP_SERVER_MAX_URL_LENGTH
44#define HTTP_SERVER_MAX_HEADER_LEN CONFIG_HTTP_SERVER_MAX_HEADER_LEN
46#define HTTP_SERVER_CLIENT_BUFFER_SIZE 0
47#define HTTP_SERVER_MAX_STREAMS 0
48#define HTTP_SERVER_MAX_CONTENT_TYPE_LEN 0
49#define HTTP_SERVER_MAX_URL_LENGTH 0
50#define HTTP_SERVER_MAX_HEADER_LEN 0
53#if defined(CONFIG_HTTP_SERVER_CAPTURE_HEADERS)
54#define HTTP_SERVER_CAPTURE_HEADER_BUFFER_SIZE CONFIG_HTTP_SERVER_CAPTURE_HEADER_BUFFER_SIZE
55#define HTTP_SERVER_CAPTURE_HEADER_COUNT CONFIG_HTTP_SERVER_CAPTURE_HEADER_COUNT
57#define HTTP_SERVER_CAPTURE_HEADER_BUFFER_SIZE 0
58#define HTTP_SERVER_CAPTURE_HEADER_COUNT 0
61#define HTTP2_PREFACE "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
109 >= (HTTP_METHOD_END_VALUE - 1));
170#define HTTP_SERVER_CONTENT_TYPE(_extension, _content_type) \
171 const STRUCT_SECTION_ITERABLE(http_content_type, _extension) = { \
172 .extension = STRINGIFY(_extension), \
173 .extension_len = sizeof(STRINGIFY(_extension)) - 1, \
174 .content_type = _content_type, \
177#define HTTP_SERVER_CONTENT_TYPE_FOREACH(_it) STRUCT_SECTION_FOREACH(http_content_type, _it)
333enum http2_stream_state {
335 HTTP2_STREAM_RESERVED_LOCAL,
336 HTTP2_STREAM_RESERVED_REMOTE,
338 HTTP2_STREAM_HALF_CLOSED_LOCAL,
339 HTTP2_STREAM_HALF_CLOSED_REMOTE,
343enum http_server_state {
344 HTTP_SERVER_FRAME_HEADER_STATE,
345 HTTP_SERVER_PREFACE_STATE,
346 HTTP_SERVER_REQUEST_STATE,
347 HTTP_SERVER_FRAME_DATA_STATE,
348 HTTP_SERVER_FRAME_HEADERS_STATE,
349 HTTP_SERVER_FRAME_SETTINGS_STATE,
350 HTTP_SERVER_FRAME_PRIORITY_STATE,
351 HTTP_SERVER_FRAME_WINDOW_UPDATE_STATE,
352 HTTP_SERVER_FRAME_CONTINUATION_STATE,
353 HTTP_SERVER_FRAME_PING_STATE,
354 HTTP_SERVER_FRAME_UNKNOWN_STATE,
355 HTTP_SERVER_FRAME_RST_STREAM_STATE,
356 HTTP_SERVER_FRAME_GOAWAY_STATE,
357 HTTP_SERVER_FRAME_PADDING_STATE,
358 HTTP_SERVER_DONE_STATE,
359 HTTP_SERVER_H3_STREAM_STATE,
362enum http1_parser_state {
363 HTTP1_INIT_HEADER_STATE,
364 HTTP1_WAITING_HEADER_STATE,
365 HTTP1_RECEIVING_HEADER_STATE,
366 HTTP1_RECEIVED_HEADER_STATE,
367 HTTP1_RECEIVING_DATA_STATE,
368 HTTP1_MESSAGE_COMPLETE_STATE,
371#define HTTP_SERVER_INITIAL_WINDOW_SIZE 65536
372#define HTTP_SERVER_WS_MAX_SEC_KEY_LEN 32
373#define HTTP_SERVER_WS_MAX_SEC_PROTOCOL_LEN 64
375#define HTTP_SERVER_HTTP2_SETTINGS_MAX_LEN 64
378struct http2_stream_ctx {
380 enum http2_stream_state stream_state;
389 struct http_resource_detail *current_detail;
392 bool headers_sent : 1;
395 bool end_stream_sent : 1;
399struct http_header_capture_ctx {
401 unsigned char buffer[HTTP_SERVER_CAPTURE_HEADER_BUFFER_SIZE];
404 struct http_header headers[HTTP_SERVER_CAPTURE_HEADER_COUNT];
416 struct http2_stream_ctx *current_stream;
419 bool store_next_value;
423struct http3_stream_ctx {
425 unsigned char buffer[HTTP_SERVER_CLIENT_BUFFER_SIZE];
431 struct http_resource_detail *current_detail;
434 unsigned char url_buffer[HTTP_SERVER_MAX_URL_LENGTH];
440 struct http_header_capture_ctx header_capture_ctx;
443 bool request_headers_pending;
450struct http_header_name {
464 const struct http_service_desc *service;
467 unsigned char buffer[HTTP_SERVER_CLIENT_BUFFER_SIZE];
470 unsigned char *cursor;
485 enum http_server_state server_state;
494 struct http2_stream_ctx *current_stream;
499 struct http_hpack_dynamic_table *hpack_table;
502 struct http2_stream_ctx streams[HTTP_SERVER_MAX_STREAMS];
511 struct http_header_capture_ctx header_capture_ctx;
514 unsigned char url_buffer[HTTP_SERVER_MAX_URL_LENGTH];
517 unsigned char content_type[HTTP_SERVER_MAX_CONTENT_TYPE_LEN];
520 unsigned char header_buffer[HTTP_SERVER_MAX_HEADER_LEN];
531 enum http1_parser_state parser_state;
536 int http1_frag_data_len;
541 size_t http1_frag_data_offset;
549 IF_ENABLED(CONFIG_WEBSOCKET, (
uint8_t ws_sec_key[HTTP_SERVER_WS_MAX_SEC_KEY_LEN]));
557 (
char ws_sec_protocol[HTTP_SERVER_WS_MAX_SEC_PROTOCOL_LEN]));
573#if defined(CONFIG_HTTP_SERVER_VERSION_3)
579#define HTTP3_SERVER_MAX_STREAMS (CONFIG_QUIC_MAX_STREAMS_UNI + CONFIG_QUIC_MAX_STREAMS_BIDI)
583 int stream_sock[HTTP3_SERVER_MAX_STREAMS];
586 bool headers_sent[HTTP3_SERVER_MAX_STREAMS];
589 struct http3_stream_ctx streams[HTTP3_SERVER_MAX_STREAMS];
597 bool headers_sent[0];
598 struct http3_stream_ctx streams[0];
601#define HTTP3_SERVER_MAX_STREAMS 0
632 bool websocket_sec_key_next : 1;
635 bool websocket_sec_protocol_next : 1;
638 bool http2_settings_next : 1;
641 bool http2_settings_present : 1;
644 bool http2_settings_invalid : 1;
647 IF_ENABLED(CONFIG_HTTP_SERVER_COMPRESSION, (
bool accept_encoding_next: 1));
650 bool headers_end_stream : 1;
653 bool expect_continuation : 1;
657 bool hpack_unresolved : 1;
662 bool hpack_table_missed : 1;
672#define HTTP_SERVER_REGISTER_HEADER_CAPTURE(_id, _header) \
673 BUILD_ASSERT(sizeof(_header) <= CONFIG_HTTP_SERVER_MAX_HEADER_LEN, \
674 "Header is too long to be captured, try increasing " \
675 "CONFIG_HTTP_SERVER_MAX_HEADER_LEN"); \
676 static const char *const _id##_str = _header; \
677 static const STRUCT_SECTION_ITERABLE(http_header_name, _id) = { \
http2_settings
HTTP2 settings.
Definition frame.h:105
http_method
HTTP Request Methods.
Definition method.h:28
http_resource_type
HTTP server resource type.
Definition server.h:68
http_transaction_status
Indicates the status of the currently processed piece of data.
Definition server.h:182
http_header_status
Status of captured request headers.
Definition server.h:194
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:302
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:256
http_compression
HTTP compressions.
Definition server.h:150
@ HTTP_RESOURCE_TYPE_STATIC_FS
serves static gzipped files from a filesystem
Definition server.h:73
@ HTTP_RESOURCE_TYPE_STATIC
Static resource, cannot be modified on runtime.
Definition server.h:70
@ HTTP_RESOURCE_TYPE_WEBSOCKET
Websocket resource, application takes control over Websocket connection after and upgrade.
Definition server.h:83
@ HTTP_RESOURCE_TYPE_DYNAMIC
Dynamic resource, server interacts with the application via registered http_resource_dynamic_cb_t.
Definition server.h:78
@ HTTP_SERVER_REQUEST_DATA_MORE
Transaction incomplete, more data expected.
Definition server.h:186
@ HTTP_SERVER_TRANSACTION_ABORTED
Transaction aborted, data incomplete.
Definition server.h:184
@ HTTP_SERVER_TRANSACTION_COMPLETE
Transaction completed, response sent completely.
Definition server.h:190
@ HTTP_SERVER_REQUEST_DATA_FINAL
Final data fragment in current transaction.
Definition server.h:188
@ HTTP_HEADER_STATUS_DROPPED
One or more headers were dropped due to lack of space.
Definition server.h:196
@ HTTP_HEADER_STATUS_NONE
No header status is available.
Definition server.h:197
@ HTTP_HEADER_STATUS_OK
All available headers were successfully captured.
Definition server.h:195
@ HTTP_NONE
NONE.
Definition server.h:151
@ HTTP_GZIP
GZIP.
Definition server.h:152
@ HTTP_ZSTD
ZSTD.
Definition server.h:156
@ HTTP_DEFLATE
DEFLATE.
Definition server.h:154
@ HTTP_COMPRESS
COMPRESS.
Definition server.h:153
@ HTTP_BR
BR.
Definition server.h:155
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:290
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
__INT64_TYPE__ int64_t
Definition stdint.h:75
HTTP/2 frame representation.
Definition frame.h:90
Representation of an HTTP client connected to the server.
Definition server.h:458
bool settings_acked
Flag indicating that the client acknowledged the server's SETTINGS.
Definition server.h:613
bool http1_no_body
Flag indicating that the HTTP1 response status does not allow a body.
Definition server.h:619
bool is_h3
Flag indicating this is an HTTP/3 (QUIC stream) client.
Definition server.h:607
bool preface_sent
Flag indicating that HTTP2 preface was sent.
Definition server.h:610
enum http_method method
Request method.
Definition server.h:527
bool http1_headers_sent
Flag indicating that HTTP1 headers were sent.
Definition server.h:616
size_t content_len
Request content length.
Definition server.h:524
bool websocket_upgrade
Flag indicating Websocket upgrade takes place.
Definition server.h:628
bool has_upgrade_header
Flag indicating that upgrade header was present in the request.
Definition server.h:622
bool http2_upgrade
Flag indicating HTTP/2 upgrade takes place.
Definition server.h:625
const char * content_type
Definition server.h:167
size_t extension_len
Definition server.h:166
const char * extension
Definition server.h:165
HTTP request context.
Definition server.h:218
enum http_header_status headers_status
Status of HTTP request headers.
Definition server.h:223
size_t header_count
Array length of HTTP request headers.
Definition server.h:222
size_t data_len
Length of HTTP request data.
Definition server.h:220
struct http_header * headers
Array of HTTP request headers.
Definition server.h:221
uint8_t * data
HTTP request data.
Definition server.h:219
Representation of a dynamic server resource.
Definition server.h:265
void * user_data
A pointer to the user data registered by the application.
Definition server.h:280
http_resource_dynamic_cb_t cb
Resource callback used by the server to interact with the application.
Definition server.h:272
struct http_client_ctx * holder
A pointer to the client currently processing resource, used to prevent concurrent access to the resou...
Definition server.h:277
struct http_resource_detail common
Common resource details.
Definition server.h:267
Representation of a static filesystem server resource.
Definition server.h:134
struct http_resource_detail common
Common resource details.
Definition server.h:136
const char * fs_path
Path in the local filesystem.
Definition server.h:139
const char * fallback_file
File under fs_path served instead of a 404 when the requested file does not exist and the last segmen...
Definition server.h:146
Representation of a static server resource.
Definition server.h:115
size_t static_data_len
Size of the static resource.
Definition server.h:123
const void * static_data
Content of the static resource.
Definition server.h:120
struct http_resource_detail common
Common resource details.
Definition server.h:117
Representation of a websocket server resource.
Definition server.h:306
size_t data_buffer_len
Length of the data in the data buffer.
Definition server.h:324
void * user_data
A pointer to the user data registered by the application.
Definition server.h:327
struct http_resource_detail common
Common resource details.
Definition server.h:308
http_resource_websocket_cb_t cb
Resource callback used by the server to interact with the application.
Definition server.h:316
int ws_sock
Websocket socket value.
Definition server.h:311
uint8_t * data_buffer
Data buffer used to exchanged data between server and the, application.
Definition server.h:321
Representation of a server resource, common for all resource types.
Definition server.h:89
int path_len
Length of the URL path.
Definition server.h:97
const char * content_type
Content type of the resource.
Definition server.h:103
uint32_t bitmask_of_supported_http_methods
Bitmask of supported HTTP methods (http_method).
Definition server.h:91
const char * content_encoding
Content encoding of the resource.
Definition server.h:100
enum http_resource_type type
Resource type.
Definition server.h:94
HTTP response context.
Definition server.h:227
bool final_chunk
Flag set to true when the application has no more data to send.
Definition server.h:233
const struct http_header * headers
Array of HTTP headers.
Definition server.h:229
size_t header_count
Length of headers array.
Definition server.h:230
size_t body_len
Length of body data.
Definition server.h:232
const uint8_t * body
Pointer to body data.
Definition server.h:231
enum http_status status
HTTP status code to include in response.
Definition server.h:228
A structure used to submit work after a delay.
Definition kernel.h:4965
Iterable sections helpers.
#define NUM_BITS(t)
Number of bits that make up a type.
Definition util.h:34