Zephyr Project API 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
120
121 /* If any more service-specific configuration is needed, it can be added here. */
122};
123
125
126struct http_service_desc {
127 const char *host;
128 uint16_t *port;
129 int *fd;
130 /* QUIC listening connection socket for HTTP/3. It is used to accept
131 * new connections for this service.
132 * Set to NULL if HTTP/3 is not supported, and set to -1 if HTTP/3 is
133 * not yet initialized or enabled for this service. If HTTP/3 is supported then
134 * this pointer cannot be null.
135 */
136 int *fd_h3;
137 void *detail;
138 size_t concurrent;
139 size_t backlog;
140 struct http_service_runtime_data *data;
141 struct http_resource_desc *res_begin;
142 struct http_resource_desc *res_end;
143 struct http_resource_detail *res_fallback;
144 const struct http_service_config *config;
145#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
146 const sec_tag_t *sec_tag_list;
147 size_t sec_tag_list_size;
148#endif
149};
150
151#define __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
152 _res_fallback, _res_begin, _res_end, _config, ...) \
153 BUILD_ASSERT(_concurrent <= CONFIG_HTTP_SERVER_MAX_CLIENTS, \
154 "can't accept more then MAX_CLIENTS"); \
155 BUILD_ASSERT(_backlog > 0, "backlog can't be 0"); \
156 static int _name##_fd = -1; \
157 IF_ENABLED(CONFIG_HTTP_SERVER_VERSION_3, (static int _name##_fd_h3 = -1;)) \
158 static struct http_service_runtime_data _name##_data = {0}; \
159 const STRUCT_SECTION_ITERABLE(http_service_desc, _name) = { \
160 .host = _host, \
161 .port = (uint16_t *)(_port), \
162 .fd = &_name##_fd, \
163 IF_ENABLED(CONFIG_HTTP_SERVER_VERSION_3, (.fd_h3 = &_name##_fd_h3,)) \
164 .detail = (void *)(_detail), \
165 .concurrent = (_concurrent), \
166 .backlog = (_backlog), \
167 .data = &_name##_data, \
168 .res_begin = (_res_begin), \
169 .res_end = (_res_end), \
170 .res_fallback = (_res_fallback), \
171 .config = (_config), \
172 COND_CODE_1(CONFIG_NET_SOCKETS_SOCKOPT_TLS, \
173 (.sec_tag_list = COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), (NULL), \
174 (GET_ARG_N(1, __VA_ARGS__))),), ()) \
175 COND_CODE_1(CONFIG_NET_SOCKETS_SOCKOPT_TLS, \
176 (.sec_tag_list_size = COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), (0),\
177 (GET_ARG_N(1, GET_ARGS_LESS_N(1, __VA_ARGS__))))), ())\
178 }
179
181
203#define HTTP_SERVICE_DEFINE_EMPTY(_name, _host, _port, _concurrent, _backlog, _detail, \
204 _res_fallback, _config) \
205 __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
206 _res_fallback, NULL, NULL, _config)
207
231#define HTTPS_SERVICE_DEFINE_EMPTY(_name, _host, _port, _concurrent, _backlog, _detail, \
232 _res_fallback, _config, _sec_tag_list, _sec_tag_list_size) \
233 __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
234 _res_fallback, NULL, NULL, _config, \
235 _sec_tag_list, _sec_tag_list_size); \
236 BUILD_ASSERT(IS_ENABLED(CONFIG_NET_SOCKETS_SOCKOPT_TLS), \
237 "TLS is required for HTTP secure (CONFIG_NET_SOCKETS_SOCKOPT_TLS)")
238
260#define HTTP_SERVICE_DEFINE(_name, _host, _port, _concurrent, _backlog, _detail, _res_fallback, \
261 _config) \
262 extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_start)[]; \
263 extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_end)[]; \
264 __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
265 _res_fallback, \
266 &_CONCAT(_http_resource_desc_##_name, _list_start)[0], \
267 &_CONCAT(_http_resource_desc_##_name, _list_end)[0], _config);
268
292#define HTTPS_SERVICE_DEFINE(_name, _host, _port, _concurrent, _backlog, _detail, \
293 _res_fallback, _config, _sec_tag_list, _sec_tag_list_size) \
294 extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_start)[]; \
295 extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_end)[]; \
296 __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
297 _res_fallback, \
298 &_CONCAT(_http_resource_desc_##_name, _list_start)[0], \
299 &_CONCAT(_http_resource_desc_##_name, _list_end)[0], _config, \
300 _sec_tag_list, _sec_tag_list_size); \
301 BUILD_ASSERT(IS_ENABLED(CONFIG_NET_SOCKETS_SOCKOPT_TLS), \
302 "TLS is required for HTTP secure (CONFIG_NET_SOCKETS_SOCKOPT_TLS)")
303
309#define HTTP_SERVICE_COUNT(_dst) STRUCT_SECTION_COUNT(http_service_desc, _dst)
310
316#define HTTP_SERVICE_RESOURCE_COUNT(_service) ((_service)->res_end - (_service)->res_begin)
317
323#define HTTP_SERVICE_FOREACH(_it) STRUCT_SECTION_FOREACH(http_service_desc, _it)
324
333#define HTTP_RESOURCE_FOREACH(_service, _it) \
334 STRUCT_SECTION_FOREACH_ALTERNATE(http_resource_desc_##_service, http_resource_desc, _it)
335
345#define HTTP_SERVICE_FOREACH_RESOURCE(_service, _it) \
346 for (struct http_resource_desc *_it = (_service)->res_begin; ({ \
347 __ASSERT(_it <= (_service)->res_end, "unexpected list end location"); \
348 _it < (_service)->res_end; \
349 }); \
350 _it++)
351
352#ifdef __cplusplus
353}
354#endif
355
359
360#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:78
HTTP server API.
__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:113
enum http_h3_alt_svc_policy h3_alt_svc_policy
HTTP/3 Alt-Svc advertisement policy for the service.
Definition service.h:119
http_socket_create_fn socket_create
Custom socket creation for the service if needed.
Definition service.h:115
enum http_version http_ver
What HTTP version to use for the service.
Definition service.h:117
TLS credentials management.
Macro utilities.