Zephyr API 3.6.99
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>
28#include <zephyr/net/socket.h>
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
37#if defined(CONFIG_HTTP_SERVER)
38#define HTTP_SERVER_CLIENT_BUFFER_SIZE CONFIG_HTTP_SERVER_CLIENT_BUFFER_SIZE
39#define HTTP_SERVER_MAX_STREAMS CONFIG_HTTP_SERVER_MAX_STREAMS
40#define HTTP_SERVER_MAX_CONTENT_TYPE_LEN CONFIG_HTTP_SERVER_MAX_CONTENT_TYPE_LENGTH
41#define HTTP_SERVER_MAX_URL_LENGTH CONFIG_HTTP_SERVER_MAX_URL_LENGTH
42#define HTTP_SERVER_MAX_HEADER_LEN CONFIG_HTTP_SERVER_MAX_HEADER_LEN
43#else
44#define HTTP_SERVER_CLIENT_BUFFER_SIZE 0
45#define HTTP_SERVER_MAX_STREAMS 0
46#define HTTP_SERVER_MAX_CONTENT_TYPE_LEN 0
47#define HTTP_SERVER_MAX_URL_LENGTH 0
48#define HTTP_SERVER_MAX_HEADER_LEN 0
49#endif
50
51#define HTTP2_PREFACE "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
52
75
95
97BUILD_ASSERT(NUM_BITS(
98 sizeof(((struct http_resource_detail *)0)->bitmask_of_supported_http_methods))
99 >= (HTTP_METHOD_END_VALUE - 1));
115
117/* Make sure that the common is the first in the struct. */
118BUILD_ASSERT(offsetof(struct http_resource_detail_static, common) == 0);
131
133/* Make sure that the common is the first in the struct. */
134BUILD_ASSERT(offsetof(struct http_resource_detail_static_fs, common) == 0);
138 const char *extension;
140 const char *content_type;
141};
142
143#define HTTP_SERVER_CONTENT_TYPE(_extension, _content_type) \
144 const STRUCT_SECTION_ITERABLE(http_content_type, _extension) = { \
145 .extension = STRINGIFY(_extension), \
146 .extension_len = sizeof(STRINGIFY(_extension)) - 1, \
147 .content_type = _content_type, \
148 };
149
150#define HTTP_SERVER_CONTENT_TYPE_FOREACH(_it) STRUCT_SECTION_FOREACH(http_content_type, _it)
151
152struct http_client_ctx;
153
163
180typedef int (*http_resource_dynamic_cb_t)(struct http_client_ctx *client,
181 enum http_data_status status,
182 uint8_t *data_buffer,
183 size_t data_len,
184 void *user_data);
185
214
216BUILD_ASSERT(offsetof(struct http_resource_detail_dynamic, common) == 0);
233typedef int (*http_resource_websocket_cb_t)(int ws_socket,
234 void *user_data);
235
260
262BUILD_ASSERT(offsetof(struct http_resource_detail_websocket, common) == 0);
267enum http2_stream_state {
268 HTTP2_STREAM_IDLE,
269 HTTP2_STREAM_RESERVED_LOCAL,
270 HTTP2_STREAM_RESERVED_REMOTE,
271 HTTP2_STREAM_OPEN,
272 HTTP2_STREAM_HALF_CLOSED_LOCAL,
273 HTTP2_STREAM_HALF_CLOSED_REMOTE,
274 HTTP2_STREAM_CLOSED
275};
276
277enum http_server_state {
278 HTTP_SERVER_FRAME_HEADER_STATE,
279 HTTP_SERVER_PREFACE_STATE,
280 HTTP_SERVER_REQUEST_STATE,
281 HTTP_SERVER_FRAME_DATA_STATE,
282 HTTP_SERVER_FRAME_HEADERS_STATE,
283 HTTP_SERVER_FRAME_SETTINGS_STATE,
284 HTTP_SERVER_FRAME_PRIORITY_STATE,
285 HTTP_SERVER_FRAME_WINDOW_UPDATE_STATE,
286 HTTP_SERVER_FRAME_CONTINUATION_STATE,
287 HTTP_SERVER_FRAME_PING_STATE,
288 HTTP_SERVER_FRAME_RST_STREAM_STATE,
289 HTTP_SERVER_FRAME_GOAWAY_STATE,
290 HTTP_SERVER_FRAME_PADDING_STATE,
291 HTTP_SERVER_DONE_STATE,
292};
293
294enum http1_parser_state {
295 HTTP1_INIT_HEADER_STATE,
296 HTTP1_WAITING_HEADER_STATE,
297 HTTP1_RECEIVING_HEADER_STATE,
298 HTTP1_RECEIVED_HEADER_STATE,
299 HTTP1_RECEIVING_DATA_STATE,
300 HTTP1_MESSAGE_COMPLETE_STATE,
301};
302
303#define HTTP_SERVER_INITIAL_WINDOW_SIZE 65536
304#define HTTP_SERVER_WS_MAX_SEC_KEY_LEN 32
305
311 enum http2_stream_state stream_state;
315 bool headers_sent : 1;
316
319};
320
329
330#if defined(CONFIG_HTTP_SERVER_CAPTURE_HEADERS)
332struct http_header {
333 const char *name;
334 const char *value;
335};
336
338enum http_header_status {
339 HTTP_HEADER_STATUS_OK,
340 HTTP_HEADER_STATUS_DROPPED,
341};
342
344struct http_header_capture_ctx {
346 unsigned char buffer[CONFIG_HTTP_SERVER_CAPTURE_HEADER_BUFFER_SIZE];
347
349 struct http_header headers[CONFIG_HTTP_SERVER_CAPTURE_HEADER_COUNT];
350
352 enum http_header_status status;
353
355 size_t count;
356
358 size_t cursor;
359
361 bool store_next_value;
362};
363
365struct http_header_name {
366 const char *name;
367};
368#endif /* defined(CONFIG_HTTP_SERVER_CAPTURE_HEADERS) */
369
375 int fd;
376
378 unsigned char buffer[HTTP_SERVER_CLIENT_BUFFER_SIZE];
379
381 unsigned char *cursor;
382
384 size_t data_len;
385
388
390 enum http_server_state server_state;
391
394
397
400
403
405 struct http2_stream_ctx streams[HTTP_SERVER_MAX_STREAMS];
406
409
412
413#if defined(CONFIG_HTTP_SERVER_CAPTURE_HEADERS)
415 struct http_header_capture_ctx header_capture_ctx;
416#endif /* defined(CONFIG_HTTP_SERVER_CAPTURE_HEADERS) */
417
419 unsigned char url_buffer[HTTP_SERVER_MAX_URL_LENGTH];
420
422 unsigned char content_type[HTTP_SERVER_MAX_CONTENT_TYPE_LEN];
423
425 unsigned char header_buffer[HTTP_SERVER_MAX_HEADER_LEN];
426
429
432
434 enum http1_parser_state parser_state;
435
440
445
448 IF_ENABLED(CONFIG_WEBSOCKET, (uint8_t ws_sec_key[HTTP_SERVER_WS_MAX_SEC_KEY_LEN]));
452 bool preface_sent : 1;
453
456
459
462
465
468
471};
472
473#if defined(CONFIG_HTTP_SERVER_CAPTURE_HEADERS)
480#define HTTP_SERVER_REGISTER_HEADER_CAPTURE(_id, _header) \
481 BUILD_ASSERT(sizeof(_header) <= CONFIG_HTTP_SERVER_MAX_HEADER_LEN, \
482 "Header is too long to be captured, try increasing " \
483 "CONFIG_HTTP_SERVER_MAX_HEADER_LEN"); \
484 static const char *const _id##_str = _header; \
485 static const STRUCT_SECTION_ITERABLE(http_header_name, _id) = { \
486 .name = _id##_str, \
487 }
488#endif /* defined(CONFIG_HTTP_SERVER_CAPTURE_HEADERS) */
489
497
503
504#ifdef __cplusplus
505}
506#endif
507
512#endif
http_method
HTTP Request Methods.
Definition method.h:28
http_resource_type
HTTP server resource type.
Definition server.h:58
http_data_status
Indicates the status of the currently processed piece of data.
Definition server.h:155
int(* http_resource_websocket_cb_t)(int ws_socket, void *user_data)
Callback used when a Websocket connection is setup.
Definition server.h:233
int http_server_stop(void)
Stop the HTTP2 server.
int http_server_start(void)
Start the HTTP2 server.
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:180
@ HTTP_RESOURCE_TYPE_STATIC_FS
serves static gzipped files from a filesystem
Definition server.h:63
@ 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:73
@ HTTP_RESOURCE_TYPE_DYNAMIC
Dynamic resource, server interacts with the application via registered http_resource_dynamic_cb_t.
Definition server.h:68
@ HTTP_SERVER_DATA_FINAL
Final data fragment in current transaction.
Definition server.h:161
@ HTTP_SERVER_DATA_MORE
Transaction incomplete, more data expected.
Definition server.h:159
@ HTTP_SERVER_DATA_ABORTED
Transaction aborted, data incomplete.
Definition server.h:157
#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
HTTP/2 frame representation.
Definition server.h:322
uint32_t stream_identifier
Stream ID the frame belongs to.
Definition server.h:324
uint8_t type
Frame type.
Definition server.h:325
uint8_t flags
Frame flags.
Definition server.h:326
uint8_t padding_len
Frame padding length.
Definition server.h:327
uint32_t length
Frame payload length.
Definition server.h:323
HTTP/2 stream representation.
Definition server.h:309
bool headers_sent
Flag indicating that headers were sent in the reply.
Definition server.h:315
enum http2_stream_state stream_state
Stream state.
Definition server.h:311
int window_size
Stream-level window size.
Definition server.h:312
int stream_id
Stream identifier.
Definition server.h:310
bool end_stream_sent
Flag indicating that END_STREAM flag was sent.
Definition server.h:318
Representation of an HTTP client connected to the server.
Definition server.h:373
struct http2_stream_ctx streams[HTTP_SERVER_MAX_STREAMS]
HTTP/2 streams context.
Definition server.h:405
struct k_work_delayable inactivity_timer
Client inactivity timer.
Definition server.h:444
struct http_parser_settings parser_settings
HTTP/1 parser configuration.
Definition server.h:408
unsigned char * cursor
Cursor indicating currently processed byte.
Definition server.h:381
int window_size
Connection-level window size.
Definition server.h:387
enum http1_parser_state parser_state
HTTP/1 parser state.
Definition server.h:434
bool expect_continuation
The next frame on the stream is expectd to be a continuation frame.
Definition server.h:470
struct http_parser parser
HTTP/1 parser context.
Definition server.h:411
int http1_frag_data_len
Length of the payload length in the currently processed request fragment (HTTP/1 only).
Definition server.h:439
bool websocket_sec_key_next
Flag indicating Websocket key is being processed.
Definition server.h:467
enum http_server_state server_state
Server state for the associated client.
Definition server.h:390
struct http_hpack_header_buf header_field
HTTP/2 header parser context.
Definition server.h:402
struct http_resource_detail * current_detail
Currently processed resource detail.
Definition server.h:396
unsigned char url_buffer[HTTP_SERVER_MAX_URL_LENGTH]
Request URL.
Definition server.h:419
unsigned char header_buffer[HTTP_SERVER_MAX_HEADER_LEN]
Temp buffer for currently processed header (HTTP/1 only).
Definition server.h:425
unsigned char content_type[HTTP_SERVER_MAX_CONTENT_TYPE_LEN]
Request content type.
Definition server.h:422
bool preface_sent
Flag indicating that HTTP2 preface was sent.
Definition server.h:452
enum http_method method
Request method.
Definition server.h:431
int fd
Socket descriptor associated with the server.
Definition server.h:375
bool http1_headers_sent
Flag indicating that HTTP1 headers were sent.
Definition server.h:455
size_t content_len
Request content length.
Definition server.h:428
unsigned char buffer[HTTP_SERVER_CLIENT_BUFFER_SIZE]
Client data buffer.
Definition server.h:378
struct http2_frame current_frame
Currently processed HTTP/2 frame.
Definition server.h:393
struct http2_stream_ctx * current_stream
Currently processed stream.
Definition server.h:399
bool websocket_upgrade
Flag indicating Websocket upgrade takes place.
Definition server.h:464
bool has_upgrade_header
Flag indicating that upgrade header was present in the request.
Definition server.h:458
bool http2_upgrade
Flag indicating HTTP/2 upgrade takes place.
Definition server.h:461
size_t data_len
Data left to process in the buffer.
Definition server.h:384
Definition server.h:137
const char * content_type
Definition server.h:140
size_t extension_len
Definition server.h:139
const char * extension
Definition server.h:138
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:189
uint8_t * data_buffer
Data buffer used to exchanged data between server and the, application.
Definition server.h:201
size_t data_buffer_len
Length of the data in the data buffer.
Definition server.h:204
void * user_data
A pointer to the user data registered by the application.
Definition server.h:212
http_resource_dynamic_cb_t cb
Resource callback used by the server to interact with the application.
Definition server.h:196
struct http_client_ctx * holder
A pointer to the client currently processing resource, used to prevent concurrent access to the resou...
Definition server.h:209
struct http_resource_detail common
Common resource details.
Definition server.h:191
Representation of a static filesystem server resource.
Definition server.h:124
struct http_resource_detail common
Common resource details.
Definition server.h:126
const char * fs_path
Path in the local filesystem.
Definition server.h:129
Representation of a static server resource.
Definition server.h:105
size_t static_data_len
Size of the static resource.
Definition server.h:113
const void * static_data
Content of the static resource.
Definition server.h:110
struct http_resource_detail common
Common resource details.
Definition server.h:107
Representation of a websocket server resource.
Definition server.h:237
size_t data_buffer_len
Length of the data in the data buffer.
Definition server.h:255
void * user_data
A pointer to the user data registered by the application.
Definition server.h:258
struct http_resource_detail common
Common resource details.
Definition server.h:239
http_resource_websocket_cb_t cb
Resource callback used by the server to interact with the application.
Definition server.h:247
int ws_sock
Websocket socket value.
Definition server.h:242
uint8_t * data_buffer
Data buffer used to exchanged data between server and the, application.
Definition server.h:252
Representation of a server resource, common for all resource types.
Definition server.h:79
int path_len
Length of the URL path.
Definition server.h:87
const char * content_type
Content type of the resource.
Definition server.h:93
uint32_t bitmask_of_supported_http_methods
Bitmask of supported HTTP methods (http_method).
Definition server.h:81
const char * content_encoding
Content encoding of the resource.
Definition server.h:90
enum http_resource_type type
Resource type.
Definition server.h:84
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