Zephyr API Documentation 4.4.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
service.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2022 Meta
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_NET_HTTP_SERVICE_H_
8#define ZEPHYR_INCLUDE_NET_HTTP_SERVICE_H_
9
21
23#include <stdint.h>
24#include <stddef.h>
25
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
37 const char *resource;
39 void *detail;
40};
41
59#define HTTP_RESOURCE_DEFINE(_name, _service, _resource, _detail) \
60 const STRUCT_SECTION_ITERABLE_ALTERNATE(http_resource_desc_##_service, http_resource_desc, \
61 _name) = { \
62 .resource = _resource, \
63 .detail = (void *)(_detail), \
64 }
65
67
68struct http_service_runtime_data {
69 int num_clients;
70};
71
72struct http_service_desc;
73
75
88typedef int (*http_socket_create_fn)(const struct http_service_desc *svc, int af, int proto);
89
101
111
127
136
137 /* If any more service-specific configuration is needed, it can be added here. */
138};
139
141
142struct http_service_desc {
143 const char *host;
144 uint16_t *port;
145 int *fd;
146 /* QUIC listening connection socket for HTTP/3. It is used to accept
147 * new connections for this service.
148 * Set to NULL if HTTP/3 is not supported, and set to -1 if HTTP/3 is
149 * not yet initialized or enabled for this service. If HTTP/3 is supported then
150 * this pointer cannot be null.
151 */
152 int *fd_h3;
153 void *detail;
154 size_t concurrent;
155 size_t backlog;
156 struct http_service_runtime_data *data;
157 struct http_resource_desc *res_begin;
158 struct http_resource_desc *res_end;
159 struct http_resource_detail *res_fallback;
160 const struct http_service_config *config;
161#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
162 const sec_tag_t *sec_tag_list;
163 size_t sec_tag_list_size;
164#endif
165};
166
167#define __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
168 _res_fallback, _res_begin, _res_end, _config, ...) \
169 BUILD_ASSERT(_concurrent <= CONFIG_HTTP_SERVER_MAX_CLIENTS, \
170 "can't accept more then MAX_CLIENTS"); \
171 BUILD_ASSERT(_backlog > 0, "backlog can't be 0"); \
172 static int _name##_fd = -1; \
173 IF_ENABLED(CONFIG_HTTP_SERVER_VERSION_3, (static int _name##_fd_h3 = -1;)) \
174 static struct http_service_runtime_data _name##_data = {0}; \
175 const STRUCT_SECTION_ITERABLE(http_service_desc, _name) = { \
176 .host = _host, \
177 .port = (uint16_t *)(_port), \
178 .fd = &_name##_fd, \
179 IF_ENABLED(CONFIG_HTTP_SERVER_VERSION_3, (.fd_h3 = &_name##_fd_h3,)) \
180 .detail = (void *)(_detail), \
181 .concurrent = (_concurrent), \
182 .backlog = (_backlog), \
183 .data = &_name##_data, \
184 .res_begin = (_res_begin), \
185 .res_end = (_res_end), \
186 .res_fallback = (_res_fallback), \
187 .config = (_config), \
188 COND_CODE_1(CONFIG_NET_SOCKETS_SOCKOPT_TLS, \
189 (.sec_tag_list = COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), (NULL), \
190 (GET_ARG_N(1, __VA_ARGS__))),), ()) \
191 COND_CODE_1(CONFIG_NET_SOCKETS_SOCKOPT_TLS, \
192 (.sec_tag_list_size = COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), (0),\
193 (GET_ARG_N(1, GET_ARGS_LESS_N(1, __VA_ARGS__))))), ())\
194 }
195
197
219#define HTTP_SERVICE_DEFINE_EMPTY(_name, _host, _port, _concurrent, _backlog, _detail, \
220 _res_fallback, _config) \
221 __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
222 _res_fallback, NULL, NULL, _config)
223
247#define HTTPS_SERVICE_DEFINE_EMPTY(_name, _host, _port, _concurrent, _backlog, _detail, \
248 _res_fallback, _config, _sec_tag_list, _sec_tag_list_size) \
249 __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
250 _res_fallback, NULL, NULL, _config, \
251 _sec_tag_list, _sec_tag_list_size); \
252 BUILD_ASSERT(IS_ENABLED(CONFIG_NET_SOCKETS_SOCKOPT_TLS), \
253 "TLS is required for HTTP secure (CONFIG_NET_SOCKETS_SOCKOPT_TLS)")
254
276#define HTTP_SERVICE_DEFINE(_name, _host, _port, _concurrent, _backlog, _detail, _res_fallback, \
277 _config) \
278 extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_start)[]; \
279 extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_end)[]; \
280 __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
281 _res_fallback, \
282 &_CONCAT(_http_resource_desc_##_name, _list_start)[0], \
283 &_CONCAT(_http_resource_desc_##_name, _list_end)[0], _config);
284
308#define HTTPS_SERVICE_DEFINE(_name, _host, _port, _concurrent, _backlog, _detail, \
309 _res_fallback, _config, _sec_tag_list, _sec_tag_list_size) \
310 extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_start)[]; \
311 extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_end)[]; \
312 __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
313 _res_fallback, \
314 &_CONCAT(_http_resource_desc_##_name, _list_start)[0], \
315 &_CONCAT(_http_resource_desc_##_name, _list_end)[0], _config, \
316 _sec_tag_list, _sec_tag_list_size); \
317 BUILD_ASSERT(IS_ENABLED(CONFIG_NET_SOCKETS_SOCKOPT_TLS), \
318 "TLS is required for HTTP secure (CONFIG_NET_SOCKETS_SOCKOPT_TLS)")
319
325#define HTTP_SERVICE_COUNT(_dst) STRUCT_SECTION_COUNT(http_service_desc, _dst)
326
332#define HTTP_SERVICE_RESOURCE_COUNT(_service) ((_service)->res_end - (_service)->res_begin)
333
339#define HTTP_SERVICE_FOREACH(_it) STRUCT_SECTION_FOREACH(http_service_desc, _it)
340
349#define HTTP_RESOURCE_FOREACH(_service, _it) \
350 STRUCT_SECTION_FOREACH_ALTERNATE(http_resource_desc_##_service, http_resource_desc, _it)
351
361#define HTTP_SERVICE_FOREACH_RESOURCE(_service, _it) \
362 for (struct http_resource_desc *_it = (_service)->res_begin; ({ \
363 __ASSERT(_it <= (_service)->res_end, "unexpected list end location"); \
364 _it < (_service)->res_end; \
365 }); \
366 _it++)
367
368#ifdef __cplusplus
369}
370#endif
371
375
376#endif /* ZEPHYR_INCLUDE_NET_HTTP_SERVICE_H_ */
http_h3_alt_svc_policy
HTTP/3 Alt-Svc advertisement policy for this service.
Definition service.h:103
int(* http_socket_create_fn)(const struct http_service_desc *svc, int af, int proto)
Custom socket creation function type.
Definition service.h:88
http_version
Supported HTTP version for this service.
Definition service.h:95
@ HTTP_H3_ALT_SVC_ENABLE
Always advertise HTTP/3 via Alt-Svc for this service when available.
Definition service.h:109
@ HTTP_H3_ALT_SVC_DISABLE
Never advertise HTTP/3 via Alt-Svc for this service.
Definition service.h:107
@ HTTP_H3_ALT_SVC_DEFAULT
Use the global default policy configured in Kconfig.
Definition service.h:105
@ HTTP_VERSION_2
Support HTTP/2.
Definition service.h:98
@ HTTP_VERSION_1
Support HTTP/1.0 and HTTP/1.1.
Definition service.h:97
@ HTTP_VERSION_3
Support HTTP/3.
Definition service.h:99
@ HTTP_VERSION_ANY
Support any HTTP version configured in the system.
Definition service.h:96
int sec_tag_t
Secure tag, a reference to TLS credential.
Definition tls_credentials.h:90
HTTP server API.
__UINT32_TYPE__ uint32_t
Definition stdint.h:90
__UINT16_TYPE__ uint16_t
Definition stdint.h:89
HTTP resource description.
Definition service.h:35
const char * resource
Resource name.
Definition service.h:37
void * detail
Detail associated with this resource.
Definition service.h:39
Representation of a server resource, common for all resource types.
Definition server.h:88
HTTP service configuration.
Definition service.h:129
struct http_service_h3_config h3
HTTP/3-specific configuration.
Definition service.h:135
http_socket_create_fn socket_create
Custom socket creation for the service if needed.
Definition service.h:131
enum http_version http_ver
What HTTP version to use for the service.
Definition service.h:133
HTTP/3 configuration for a service.
Definition service.h:113
enum http_h3_alt_svc_policy alt_svc_policy
HTTP/3 Alt-Svc advertisement policy for the service.
Definition service.h:115
bool enable_session_tickets
Enable QUIC session tickets on the HTTP/3 listener for this service.
Definition service.h:117
uint32_t max_early_data_size
HTTP/3 listener early-data limit in bytes.
Definition service.h:125
Iterable sections helpers.
TLS credentials management.
Macro utilities.