Skip to content

Commit a0dbf21

Browse files
net: http: service: extend HTTP service with config
Update the HTTP service API to allow setting per-service configuration, currently it is only custom socket creation callback, but in the future it can be extended with other parameters Signed-off-by: Andrey Dodonov <Andrey.Dodonov@endress.com>
1 parent 0f8b7b7 commit a0dbf21

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed

include/zephyr/net/http/service.h

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ struct http_service_runtime_data {
6969
int num_clients;
7070
};
7171

72+
struct http_service_desc;
73+
74+
/** Custom socket creation function type */
75+
typedef int (*http_socket_create_fn)(const struct http_service_desc *svc, int af, int proto);
76+
77+
/** HTTP service configuration */
78+
struct http_service_config {
79+
/** Custom socket creation for the service if needed */
80+
http_socket_create_fn socket_create;
81+
/* If any more service-specific configuration is needed, it can be added here. */
82+
};
83+
7284
struct http_service_desc {
7385
const char *host;
7486
uint16_t *port;
@@ -80,14 +92,15 @@ struct http_service_desc {
8092
struct http_resource_desc *res_begin;
8193
struct http_resource_desc *res_end;
8294
struct http_resource_detail *res_fallback;
95+
const struct http_service_config *config;
8396
#if defined(CONFIG_NET_SOCKETS_SOCKOPT_TLS)
8497
const sec_tag_t *sec_tag_list;
8598
size_t sec_tag_list_size;
8699
#endif
87100
};
88101

89102
#define __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
90-
_res_fallback, _res_begin, _res_end, ...) \
103+
_res_fallback, _res_begin, _res_end, _config, ...) \
91104
BUILD_ASSERT(_concurrent <= CONFIG_HTTP_SERVER_MAX_CLIENTS, \
92105
"can't accept more then MAX_CLIENTS"); \
93106
BUILD_ASSERT(_backlog > 0, "backlog can't be 0"); \
@@ -104,6 +117,7 @@ struct http_service_desc {
104117
.res_begin = (_res_begin), \
105118
.res_end = (_res_end), \
106119
.res_fallback = (_res_fallback), \
120+
.config = (_config), \
107121
COND_CODE_1(CONFIG_NET_SOCKETS_SOCKOPT_TLS, \
108122
(.sec_tag_list = COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), (NULL), \
109123
(GET_ARG_N(1, __VA_ARGS__))),), ()) \
@@ -133,11 +147,12 @@ struct http_service_desc {
133147
* @param _backlog Maximum number of queued connections. (min. 1)
134148
* @param _detail User-defined detail associated with the service.
135149
* @param _res_fallback Fallback resource to be served if no other resource matches path
150+
* @param _config Pointer to http_service_config structure (can be NULL for default behavior)
136151
*/
137152
#define HTTP_SERVICE_DEFINE_EMPTY(_name, _host, _port, _concurrent, _backlog, _detail, \
138-
_res_fallback) \
153+
_res_fallback, _config) \
139154
__z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
140-
_res_fallback, NULL, NULL)
155+
_res_fallback, NULL, NULL, _config)
141156

142157
/**
143158
* @brief Define an HTTPS service without static resources.
@@ -158,13 +173,14 @@ struct http_service_desc {
158173
* @param _backlog Maximum number of queued connections. (min. 1)
159174
* @param _detail User-defined detail associated with the service.
160175
* @param _res_fallback Fallback resource to be served if no other resource matches path
176+
* @param _config Pointer to http_service_config structure (can be NULL for default behavior)
161177
* @param _sec_tag_list TLS security tag list used to setup a HTTPS socket.
162178
* @param _sec_tag_list_size TLS security tag list size used to setup a HTTPS socket.
163179
*/
164180
#define HTTPS_SERVICE_DEFINE_EMPTY(_name, _host, _port, _concurrent, _backlog, _detail, \
165-
_res_fallback, _sec_tag_list, _sec_tag_list_size) \
181+
_res_fallback, _config, _sec_tag_list, _sec_tag_list_size) \
166182
__z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
167-
_res_fallback, NULL, NULL, \
183+
_res_fallback, NULL, NULL, _config, \
168184
_sec_tag_list, _sec_tag_list_size); \
169185
BUILD_ASSERT(IS_ENABLED(CONFIG_NET_SOCKETS_SOCKOPT_TLS), \
170186
"TLS is required for HTTP secure (CONFIG_NET_SOCKETS_SOCKOPT_TLS)")
@@ -188,14 +204,16 @@ struct http_service_desc {
188204
* @param _backlog Maximum number of queued connections. (min. 1)
189205
* @param _detail User-defined detail associated with the service.
190206
* @param _res_fallback Fallback resource to be served if no other resource matches path
207+
* @param _config Pointer to http_service_config structure (can be NULL for default behavior)
191208
*/
192-
#define HTTP_SERVICE_DEFINE(_name, _host, _port, _concurrent, _backlog, _detail, _res_fallback) \
209+
#define HTTP_SERVICE_DEFINE(_name, _host, _port, _concurrent, _backlog, _detail, _res_fallback, \
210+
_config) \
193211
extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_start)[]; \
194212
extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_end)[]; \
195213
__z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
196214
_res_fallback, \
197215
&_CONCAT(_http_resource_desc_##_name, _list_start)[0], \
198-
&_CONCAT(_http_resource_desc_##_name, _list_end)[0]);
216+
&_CONCAT(_http_resource_desc_##_name, _list_end)[0], _config);
199217

200218
/**
201219
* @brief Define an HTTPS service with static resources.
@@ -216,17 +234,18 @@ struct http_service_desc {
216234
* @param _backlog Maximum number of queued connections. (min. 1)
217235
* @param _detail User-defined detail associated with the service.
218236
* @param _res_fallback Fallback resource to be served if no other resource matches path
237+
* @param _config Pointer to http_service_config structure (can be NULL for default behavior)
219238
* @param _sec_tag_list TLS security tag list used to setup a HTTPS socket.
220239
* @param _sec_tag_list_size TLS security tag list size used to setup a HTTPS socket.
221240
*/
222241
#define HTTPS_SERVICE_DEFINE(_name, _host, _port, _concurrent, _backlog, _detail, \
223-
_res_fallback, _sec_tag_list, _sec_tag_list_size) \
242+
_res_fallback, _config, _sec_tag_list, _sec_tag_list_size) \
224243
extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_start)[]; \
225244
extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_end)[]; \
226245
__z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
227246
_res_fallback, \
228247
&_CONCAT(_http_resource_desc_##_name, _list_start)[0], \
229-
&_CONCAT(_http_resource_desc_##_name, _list_end)[0], \
248+
&_CONCAT(_http_resource_desc_##_name, _list_end)[0], _config, \
230249
_sec_tag_list, _sec_tag_list_size); \
231250
BUILD_ASSERT(IS_ENABLED(CONFIG_NET_SOCKETS_SOCKOPT_TLS), \
232251
"TLS is required for HTTP secure (CONFIG_NET_SOCKETS_SOCKOPT_TLS)")

subsys/net/lib/http/http_server_core.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,11 @@ int http_server_init(struct http_server_ctx *ctx)
156156
proto = IPPROTO_TCP;
157157
}
158158

159-
fd = zsock_socket(af, SOCK_STREAM, proto);
159+
if (svc->config != NULL && svc->config->socket_create != NULL) {
160+
fd = svc->config->socket_create(svc, af, proto);
161+
} else {
162+
fd = zsock_socket(af, SOCK_STREAM, proto);
163+
}
160164
if (fd < 0) {
161165
LOG_ERR("socket: %d", errno);
162166
failed++;

0 commit comments

Comments
 (0)