Zephyr API Documentation 4.0.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
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#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
84
104
106BUILD_ASSERT(NUM_BITS(
107 sizeof(((struct http_resource_detail *)0)->bitmask_of_supported_http_methods))
108 >= (HTTP_METHOD_END_VALUE - 1));
124
126/* Make sure that the common is the first in the struct. */
127BUILD_ASSERT(offsetof(struct http_resource_detail_static, common) == 0);
140
142/* Make sure that the common is the first in the struct. */
143BUILD_ASSERT(offsetof(struct http_resource_detail_static_fs, common) == 0);
147 const char *extension;
149 const char *content_type;
150};
151
152#define HTTP_SERVER_CONTENT_TYPE(_extension, _content_type) \
153 const STRUCT_SECTION_ITERABLE(http_content_type, _extension) = { \
154 .extension = STRINGIFY(_extension), \
155 .extension_len = sizeof(STRINGIFY(_extension)) - 1, \
156 .content_type = _content_type, \
157 };
158
159#define HTTP_SERVER_CONTENT_TYPE_FOREACH(_it) STRUCT_SECTION_FOREACH(http_content_type, _it)
160
161struct http_client_ctx;
162
172
179
182 const char *name;
183 const char *value;
184};
185
194
204
219typedef int (*http_resource_dynamic_cb_t)(struct http_client_ctx *client,
220 enum http_data_status status,
221 const struct http_request_ctx *request_ctx,
222 struct http_response_ctx *response_ctx,
223 void *user_data);
224
245
247BUILD_ASSERT(offsetof(struct http_resource_detail_dynamic, common) == 0);
265typedef int (*http_resource_websocket_cb_t)(int ws_socket, struct http_request_ctx *request_ctx,
266 void *user_data);
267
292
294BUILD_ASSERT(offsetof(struct http_resource_detail_websocket, common) == 0);
299enum http2_stream_state {
300 HTTP2_STREAM_IDLE,
301 HTTP2_STREAM_RESERVED_LOCAL,
302 HTTP2_STREAM_RESERVED_REMOTE,
303 HTTP2_STREAM_OPEN,
304 HTTP2_STREAM_HALF_CLOSED_LOCAL,
305 HTTP2_STREAM_HALF_CLOSED_REMOTE,
306 HTTP2_STREAM_CLOSED
307};
308
309enum http_server_state {
310 HTTP_SERVER_FRAME_HEADER_STATE,
311 HTTP_SERVER_PREFACE_STATE,
312 HTTP_SERVER_REQUEST_STATE,
313 HTTP_SERVER_FRAME_DATA_STATE,
314 HTTP_SERVER_FRAME_HEADERS_STATE,
315 HTTP_SERVER_FRAME_SETTINGS_STATE,
316 HTTP_SERVER_FRAME_PRIORITY_STATE,
317 HTTP_SERVER_FRAME_WINDOW_UPDATE_STATE,
318 HTTP_SERVER_FRAME_CONTINUATION_STATE,
319 HTTP_SERVER_FRAME_PING_STATE,
320 HTTP_SERVER_FRAME_RST_STREAM_STATE,
321 HTTP_SERVER_FRAME_GOAWAY_STATE,
322 HTTP_SERVER_FRAME_PADDING_STATE,
323 HTTP_SERVER_DONE_STATE,
324};
325
326enum http1_parser_state {
327 HTTP1_INIT_HEADER_STATE,
328 HTTP1_WAITING_HEADER_STATE,
329 HTTP1_RECEIVING_HEADER_STATE,
330 HTTP1_RECEIVED_HEADER_STATE,
331 HTTP1_RECEIVING_DATA_STATE,
332 HTTP1_MESSAGE_COMPLETE_STATE,
333};
334
335#define HTTP_SERVER_INITIAL_WINDOW_SIZE 65536
336#define HTTP_SERVER_WS_MAX_SEC_KEY_LEN 32
337
343 enum http2_stream_state stream_state;
348
350 bool headers_sent : 1;
351
354};
355
364
367struct http_header_capture_ctx {
369 unsigned char buffer[HTTP_SERVER_CAPTURE_HEADER_BUFFER_SIZE];
370
372 struct http_header headers[HTTP_SERVER_CAPTURE_HEADER_COUNT];
373
375 enum http_header_status status;
376
378 size_t count;
379
381 size_t cursor;
382
384 struct http2_stream_ctx *current_stream;
385
387 bool store_next_value;
388};
393 const char *name;
394};
395
401 int fd;
402
404 const struct http_service_desc *service;
405
407 unsigned char buffer[HTTP_SERVER_CLIENT_BUFFER_SIZE];
408
410 unsigned char *cursor;
411
413 size_t data_len;
414
417
419 enum http_server_state server_state;
420
423
426
429
432
434 struct http2_stream_ctx streams[HTTP_SERVER_MAX_STREAMS];
435
438
441
443 struct http_header_capture_ctx header_capture_ctx;
444
446 unsigned char url_buffer[HTTP_SERVER_MAX_URL_LENGTH];
447
449 unsigned char content_type[HTTP_SERVER_MAX_CONTENT_TYPE_LEN];
450
452 unsigned char header_buffer[HTTP_SERVER_MAX_HEADER_LEN];
453
456
459
461 enum http1_parser_state parser_state;
462
467
472
475 IF_ENABLED(CONFIG_WEBSOCKET, (uint8_t ws_sec_key[HTTP_SERVER_WS_MAX_SEC_KEY_LEN]));
479 bool preface_sent : 1;
480
483
486
489
492
495
498};
499
506#define HTTP_SERVER_REGISTER_HEADER_CAPTURE(_id, _header) \
507 BUILD_ASSERT(sizeof(_header) <= CONFIG_HTTP_SERVER_MAX_HEADER_LEN, \
508 "Header is too long to be captured, try increasing " \
509 "CONFIG_HTTP_SERVER_MAX_HEADER_LEN"); \
510 static const char *const _id##_str = _header; \
511 static const STRUCT_SECTION_ITERABLE(http_header_name, _id) = { \
512 .name = _id##_str, \
513 }
514
522
528
529#ifdef __cplusplus
530}
531#endif
532
537#endif
http_method
HTTP Request Methods.
Definition method.h:28
http_resource_type
HTTP server resource type.
Definition server.h:67
http_data_status
Indicates the status of the currently processed piece of data.
Definition server.h:164
int(* http_resource_dynamic_cb_t)(struct http_client_ctx *client, enum http_data_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:219
http_header_status
Status of captured request headers.
Definition server.h:174
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:265
@ 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_DATA_FINAL
Final data fragment in current transaction.
Definition server.h:170
@ HTTP_SERVER_DATA_MORE
Transaction incomplete, more data expected.
Definition server.h:168
@ HTTP_SERVER_DATA_ABORTED
Transaction aborted, data incomplete.
Definition server.h:166
@ HTTP_HEADER_STATUS_DROPPED
One or more headers were dropped due to lack of space.
Definition server.h:176
@ HTTP_HEADER_STATUS_NONE
No header status is available.
Definition server.h:177
@ HTTP_HEADER_STATUS_OK
All available headers were successfully captured.
Definition server.h:175
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:247
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:357
uint32_t stream_identifier
Stream ID the frame belongs to.
Definition server.h:359
uint8_t type
Frame type.
Definition server.h:360
uint8_t flags
Frame flags.
Definition server.h:361
uint8_t padding_len
Frame padding length.
Definition server.h:362
uint32_t length
Frame payload length.
Definition server.h:358
HTTP/2 stream representation.
Definition server.h:341
bool headers_sent
Flag indicating that headers were sent in the reply.
Definition server.h:350
enum http2_stream_state stream_state
Stream state.
Definition server.h:343
int window_size
Stream-level window size.
Definition server.h:344
int stream_id
Stream identifier.
Definition server.h:342
struct http_resource_detail * current_detail
Currently processed resource detail.
Definition server.h:347
bool end_stream_sent
Flag indicating that END_STREAM flag was sent.
Definition server.h:353
Representation of an HTTP client connected to the server.
Definition server.h:399
struct http2_stream_ctx streams[HTTP_SERVER_MAX_STREAMS]
HTTP/2 streams context.
Definition server.h:434
struct k_work_delayable inactivity_timer
Client inactivity timer.
Definition server.h:471
struct http_parser_settings parser_settings
HTTP/1 parser configuration.
Definition server.h:437
unsigned char * cursor
Cursor indicating currently processed byte.
Definition server.h:410
int window_size
Connection-level window size.
Definition server.h:416
enum http1_parser_state parser_state
HTTP/1 parser state.
Definition server.h:461
bool expect_continuation
The next frame on the stream is expectd to be a continuation frame.
Definition server.h:497
struct http_parser parser
HTTP/1 parser context.
Definition server.h:440
int http1_frag_data_len
Length of the payload length in the currently processed request fragment (HTTP/1 only).
Definition server.h:466
bool websocket_sec_key_next
Flag indicating Websocket key is being processed.
Definition server.h:494
enum http_server_state server_state
Server state for the associated client.
Definition server.h:419
struct http_hpack_header_buf header_field
HTTP/2 header parser context.
Definition server.h:431
struct http_resource_detail * current_detail
Currently processed resource detail.
Definition server.h:425
unsigned char url_buffer[HTTP_SERVER_MAX_URL_LENGTH]
Request URL.
Definition server.h:446
unsigned char header_buffer[HTTP_SERVER_MAX_HEADER_LEN]
Temp buffer for currently processed header (HTTP/1 only).
Definition server.h:452
unsigned char content_type[HTTP_SERVER_MAX_CONTENT_TYPE_LEN]
Request content type.
Definition server.h:449
bool preface_sent
Flag indicating that HTTP2 preface was sent.
Definition server.h:479
enum http_method method
Request method.
Definition server.h:458
int fd
Socket descriptor associated with the server.
Definition server.h:401
bool http1_headers_sent
Flag indicating that HTTP1 headers were sent.
Definition server.h:482
size_t content_len
Request content length.
Definition server.h:455
unsigned char buffer[HTTP_SERVER_CLIENT_BUFFER_SIZE]
Client data buffer.
Definition server.h:407
struct http2_frame current_frame
Currently processed HTTP/2 frame.
Definition server.h:422
struct http2_stream_ctx * current_stream
Currently processed stream.
Definition server.h:428
bool websocket_upgrade
Flag indicating Websocket upgrade takes place.
Definition server.h:491
bool has_upgrade_header
Flag indicating that upgrade header was present in the request.
Definition server.h:485
const struct http_service_desc * service
HTTP service on which the client is connected.
Definition server.h:404
struct http_header_capture_ctx header_capture_ctx
Header capture context.
Definition server.h:443
bool http2_upgrade
Flag indicating HTTP/2 upgrade takes place.
Definition server.h:488
size_t data_len
Data left to process in the buffer.
Definition server.h:413
Definition server.h:146
const char * content_type
Definition server.h:149
size_t extension_len
Definition server.h:148
const char * extension
Definition server.h:147
HTTP header name representation.
Definition server.h:392
const char * name
Pointer to header name NULL-terminated string.
Definition server.h:393
HTTP header representation.
Definition server.h:181
const char * value
Pointer to header value NULL-terminated string.
Definition server.h:183
const char * name
Pointer to header name NULL-terminated string.
Definition server.h:182
HTTP2 header field with decoding buffer.
Definition hpack.h:110
Definition parser.h:200
Definition parser.h:155
HTTP request context.
Definition server.h:187
enum http_header_status headers_status
Status of HTTP request headers.
Definition server.h:192
size_t header_count
Array length of HTTP request headers.
Definition server.h:191
size_t data_len
Length of HTTP request data.
Definition server.h:189
struct http_header * headers
Array of HTTP request headers.
Definition server.h:190
uint8_t * data
HTTP request data.
Definition server.h:188
Representation of a dynamic server resource.
Definition server.h:228
void * user_data
A pointer to the user data registered by the application.
Definition server.h:243
http_resource_dynamic_cb_t cb
Resource callback used by the server to interact with the application.
Definition server.h:235
struct http_client_ctx * holder
A pointer to the client currently processing resource, used to prevent concurrent access to the resou...
Definition server.h:240
struct http_resource_detail common
Common resource details.
Definition server.h:230
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:269
size_t data_buffer_len
Length of the data in the data buffer.
Definition server.h:287
void * user_data
A pointer to the user data registered by the application.
Definition server.h:290
struct http_resource_detail common
Common resource details.
Definition server.h:271
http_resource_websocket_cb_t cb
Resource callback used by the server to interact with the application.
Definition server.h:279
int ws_sock
Websocket socket value.
Definition server.h:274
uint8_t * data_buffer
Data buffer used to exchanged data between server and the, application.
Definition server.h:284
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:196
bool final_chunk
Flag set to true when the application has no more data to send.
Definition server.h:202
const struct http_header * headers
Array of HTTP headers.
Definition server.h:198
size_t header_count
Length of headers array.
Definition server.h:199
size_t body_len
Length of body data.
Definition server.h:201
const uint8_t * body
Pointer to body data.
Definition server.h:200
enum http_status status
HTTP status code to include in response.
Definition server.h:197
A structure used to submit work after a delay.
Definition kernel.h:4034
#define NUM_BITS(t)
Number of bits that make up a type.
Definition util.h:33