Zephyr Project API 3.7.0
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
21#include <stdint.h>
22
23#include <zephyr/kernel.h>
26#include <zephyr/net/socket.h>
27
28#ifdef __cplusplus
29extern "C" {
30#endif
31
34#if defined(CONFIG_HTTP_SERVER)
35#define HTTP_SERVER_CLIENT_BUFFER_SIZE CONFIG_HTTP_SERVER_CLIENT_BUFFER_SIZE
36#define HTTP_SERVER_MAX_STREAMS CONFIG_HTTP_SERVER_MAX_STREAMS
37#define HTTP_SERVER_MAX_CONTENT_TYPE_LEN CONFIG_HTTP_SERVER_MAX_CONTENT_TYPE_LENGTH
38#define HTTP_SERVER_MAX_URL_LENGTH CONFIG_HTTP_SERVER_MAX_URL_LENGTH
39#else
40#define HTTP_SERVER_CLIENT_BUFFER_SIZE 0
41#define HTTP_SERVER_MAX_STREAMS 0
42#define HTTP_SERVER_MAX_CONTENT_TYPE_LEN 0
43#define HTTP_SERVER_MAX_URL_LENGTH 0
44#endif
45
46/* Maximum header field name / value length. This is only used to detect Upgrade and
47 * websocket header fields and values in the http1 server so the value is quite short.
48 */
49#define HTTP_SERVER_MAX_HEADER_LEN 32
50
51#define HTTP2_PREFACE "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
52
72
92
94BUILD_ASSERT(NUM_BITS(
95 sizeof(((struct http_resource_detail *)0)->bitmask_of_supported_http_methods))
96 >= (HTTP_METHOD_END_VALUE - 1));
112
114/* Make sure that the common is the first in the struct. */
115BUILD_ASSERT(offsetof(struct http_resource_detail_static, common) == 0);
118struct http_client_ctx;
119
129
146typedef int (*http_resource_dynamic_cb_t)(struct http_client_ctx *client,
147 enum http_data_status status,
148 uint8_t *data_buffer,
149 size_t data_len,
150 void *user_data);
151
180
182BUILD_ASSERT(offsetof(struct http_resource_detail_dynamic, common) == 0);
199typedef int (*http_resource_websocket_cb_t)(int ws_socket,
200 void *user_data);
201
226
228BUILD_ASSERT(offsetof(struct http_resource_detail_websocket, common) == 0);
233enum http2_stream_state {
234 HTTP2_STREAM_IDLE,
235 HTTP2_STREAM_RESERVED_LOCAL,
236 HTTP2_STREAM_RESERVED_REMOTE,
237 HTTP2_STREAM_OPEN,
238 HTTP2_STREAM_HALF_CLOSED_LOCAL,
239 HTTP2_STREAM_HALF_CLOSED_REMOTE,
240 HTTP2_STREAM_CLOSED
241};
242
243enum http_server_state {
244 HTTP_SERVER_FRAME_HEADER_STATE,
245 HTTP_SERVER_PREFACE_STATE,
246 HTTP_SERVER_REQUEST_STATE,
247 HTTP_SERVER_FRAME_DATA_STATE,
248 HTTP_SERVER_FRAME_HEADERS_STATE,
249 HTTP_SERVER_FRAME_SETTINGS_STATE,
250 HTTP_SERVER_FRAME_PRIORITY_STATE,
251 HTTP_SERVER_FRAME_WINDOW_UPDATE_STATE,
252 HTTP_SERVER_FRAME_CONTINUATION_STATE,
253 HTTP_SERVER_FRAME_PING_STATE,
254 HTTP_SERVER_FRAME_RST_STREAM_STATE,
255 HTTP_SERVER_FRAME_GOAWAY_STATE,
256 HTTP_SERVER_FRAME_PADDING_STATE,
257 HTTP_SERVER_DONE_STATE,
258};
259
260enum http1_parser_state {
261 HTTP1_INIT_HEADER_STATE,
262 HTTP1_WAITING_HEADER_STATE,
263 HTTP1_RECEIVING_HEADER_STATE,
264 HTTP1_RECEIVED_HEADER_STATE,
265 HTTP1_RECEIVING_DATA_STATE,
266 HTTP1_MESSAGE_COMPLETE_STATE,
267};
268
269#define HTTP_SERVER_INITIAL_WINDOW_SIZE 65536
270#define HTTP_SERVER_WS_MAX_SEC_KEY_LEN 32
271
277 enum http2_stream_state stream_state;
281 bool headers_sent : 1;
282
285};
286
295
301 int fd;
302
304 unsigned char buffer[HTTP_SERVER_CLIENT_BUFFER_SIZE];
305
307 unsigned char *cursor;
308
310 size_t data_len;
311
314
316 enum http_server_state server_state;
317
320
323
326
329
331 struct http2_stream_ctx streams[HTTP_SERVER_MAX_STREAMS];
332
335
338
340 unsigned char url_buffer[HTTP_SERVER_MAX_URL_LENGTH];
341
343 unsigned char content_type[HTTP_SERVER_MAX_CONTENT_TYPE_LEN];
344
346 unsigned char header_buffer[HTTP_SERVER_MAX_HEADER_LEN];
347
350
353
355 enum http1_parser_state parser_state;
356
361
366
369 IF_ENABLED(CONFIG_WEBSOCKET, (uint8_t ws_sec_key[HTTP_SERVER_WS_MAX_SEC_KEY_LEN]));
373 bool preface_sent : 1;
374
377
380
383
386
389
392};
393
401
407
408#ifdef __cplusplus
409}
410#endif
411
416#endif
http_method
HTTP Request Methods.
Definition method.h:26
http_resource_type
HTTP server resource type.
Definition server.h:58
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:146
http_data_status
Indicates the status of the currently processed piece of data.
Definition server.h:121
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, void *user_data)
Callback used when a Websocket connection is setup.
Definition server.h:199
@ 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:70
@ HTTP_RESOURCE_TYPE_DYNAMIC
Dynamic resource, server interacts with the application via registered http_resource_dynamic_cb_t.
Definition server.h:65
@ HTTP_SERVER_DATA_FINAL
Final data fragment in current transaction.
Definition server.h:127
@ HTTP_SERVER_DATA_MORE
Transaction incomplete, more data expected.
Definition server.h:125
@ HTTP_SERVER_DATA_ABORTED
Transaction aborted, data incomplete.
Definition server.h:123
#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:288
uint32_t stream_identifier
Stream ID the frame belongs to.
Definition server.h:290
uint8_t type
Frame type.
Definition server.h:291
uint8_t flags
Frame flags.
Definition server.h:292
uint8_t padding_len
Frame padding length.
Definition server.h:293
uint32_t length
Frame payload length.
Definition server.h:289
HTTP/2 stream representation.
Definition server.h:275
bool headers_sent
Flag indicating that headers were sent in the reply.
Definition server.h:281
enum http2_stream_state stream_state
Stream state.
Definition server.h:277
int window_size
Stream-level window size.
Definition server.h:278
int stream_id
Stream identifier.
Definition server.h:276
bool end_stream_sent
Flag indicating that END_STREAM flag was sent.
Definition server.h:284
Representation of an HTTP client connected to the server.
Definition server.h:299
struct http2_stream_ctx streams[HTTP_SERVER_MAX_STREAMS]
HTTP/2 streams context.
Definition server.h:331
struct k_work_delayable inactivity_timer
Client inactivity timer.
Definition server.h:365
struct http_parser_settings parser_settings
HTTP/1 parser configuration.
Definition server.h:334
unsigned char * cursor
Cursor indicating currently processed byte.
Definition server.h:307
int window_size
Connection-level window size.
Definition server.h:313
enum http1_parser_state parser_state
HTTP/1 parser state.
Definition server.h:355
bool expect_continuation
The next frame on the stream is expectd to be a continuation frame.
Definition server.h:391
struct http_parser parser
HTTP/1 parser context.
Definition server.h:337
int http1_frag_data_len
Length of the payload length in the currently processed request fragment (HTTP/1 only).
Definition server.h:360
bool websocket_sec_key_next
Flag indicating Websocket key is being processed.
Definition server.h:388
enum http_server_state server_state
Server state for the associated client.
Definition server.h:316
struct http_hpack_header_buf header_field
HTTP/2 header parser context.
Definition server.h:328
struct http_resource_detail * current_detail
Currently processed resource detail.
Definition server.h:322
unsigned char url_buffer[HTTP_SERVER_MAX_URL_LENGTH]
Request URL.
Definition server.h:340
unsigned char header_buffer[HTTP_SERVER_MAX_HEADER_LEN]
Temp buffer for currently processed header (HTTP/1 only).
Definition server.h:346
unsigned char content_type[HTTP_SERVER_MAX_CONTENT_TYPE_LEN]
Request content type.
Definition server.h:343
bool preface_sent
Flag indicating that HTTP2 preface was sent.
Definition server.h:373
enum http_method method
Request method.
Definition server.h:352
int fd
Socket descriptor associated with the server.
Definition server.h:301
bool http1_headers_sent
Flag indicating that HTTP1 headers were sent.
Definition server.h:376
size_t content_len
Request content length.
Definition server.h:349
unsigned char buffer[HTTP_SERVER_CLIENT_BUFFER_SIZE]
Client data buffer.
Definition server.h:304
struct http2_frame current_frame
Currently processed HTTP/2 frame.
Definition server.h:319
struct http2_stream_ctx * current_stream
Currently processed stream.
Definition server.h:325
bool websocket_upgrade
Flag indicating Websocket upgrade takes place.
Definition server.h:385
bool has_upgrade_header
Flag indicating that upgrade header was present in the request.
Definition server.h:379
bool http2_upgrade
Flag indicating HTTP/2 upgrade takes place.
Definition server.h:382
size_t data_len
Data left to process in the buffer.
Definition server.h:310
HTTP2 header field with decoding buffer.
Definition hpack.h:108
Definition parser.h:190
Definition parser.h:147
Representation of a dynamic server resource.
Definition server.h:155
uint8_t * data_buffer
Data buffer used to exchanged data between server and the, application.
Definition server.h:167
size_t data_buffer_len
Length of the data in the data buffer.
Definition server.h:170
void * user_data
A pointer to the user data registered by the application.
Definition server.h:178
http_resource_dynamic_cb_t cb
Resource callback used by the server to interact with the application.
Definition server.h:162
struct http_client_ctx * holder
A pointer to the client currently processing resource, used to prevent concurrent access to the resou...
Definition server.h:175
struct http_resource_detail common
Common resource details.
Definition server.h:157
Representation of a static server resource.
Definition server.h:102
size_t static_data_len
Size of the static resource.
Definition server.h:110
const void * static_data
Content of the static resource.
Definition server.h:107
struct http_resource_detail common
Common resource details.
Definition server.h:104
Representation of a websocket server resource.
Definition server.h:203
size_t data_buffer_len
Length of the data in the data buffer.
Definition server.h:221
void * user_data
A pointer to the user data registered by the application.
Definition server.h:224
struct http_resource_detail common
Common resource details.
Definition server.h:205
http_resource_websocket_cb_t cb
Resource callback used by the server to interact with the application.
Definition server.h:213
int ws_sock
Websocket socket value.
Definition server.h:208
uint8_t * data_buffer
Data buffer used to exchanged data between server and the, application.
Definition server.h:218
Representation of a server resource, common for all resource types.
Definition server.h:76
int path_len
Length of the URL path.
Definition server.h:84
const char * content_type
Content type of the resource.
Definition server.h:90
uint32_t bitmask_of_supported_http_methods
Bitmask of supported HTTP methods (http_method).
Definition server.h:78
const char * content_encoding
Content encoding of the resource.
Definition server.h:87
enum http_resource_type type
Resource type.
Definition server.h:81
A structure used to submit work after a delay.
Definition kernel.h:3908
static const intptr_t user_data[5]
Definition main.c:588
#define NUM_BITS(t)
Number of bits that make up a type.
Definition util.h:33