Skip to content

Commit 5770cd8

Browse files
net: http: service: enhance HTTP service with optional custom socket create
Update the HTTP service API to allow custom socket creation through a function pointer in the service descriptor. The existing socket creation method is retained as a fallback if no custom function is provided. Signed-off-by: Andrey Dodonov <Andrey.Dodonov@endress.com>
1 parent 7da64e7 commit 5770cd8

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

include/zephyr/net/http/service.h

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

72+
typedef int (*http_socket_create_fn)(const struct http_service_desc *svc, int af, int proto);
73+
7274
struct http_service_desc {
7375
const char *host;
7476
uint16_t *port;
@@ -84,6 +86,7 @@ struct http_service_desc {
8486
const sec_tag_t *sec_tag_list;
8587
size_t sec_tag_list_size;
8688
#endif
89+
http_socket_create_fn socket_create;
8790
};
8891

8992
#define __z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
@@ -110,6 +113,7 @@ struct http_service_desc {
110113
COND_CODE_1(CONFIG_NET_SOCKETS_SOCKOPT_TLS, \
111114
(.sec_tag_list_size = COND_CODE_0(NUM_VA_ARGS_LESS_1(__VA_ARGS__), (0),\
112115
(GET_ARG_N(1, GET_ARGS_LESS_N(1, __VA_ARGS__))))), ())\
116+
.socket_create = GET_ARG_N(3, __VA_ARGS__, NULL, NULL, NULL), \
113117
}
114118

115119
/** @endcond */
@@ -135,9 +139,9 @@ struct http_service_desc {
135139
* @param _res_fallback Fallback resource to be served if no other resource matches path
136140
*/
137141
#define HTTP_SERVICE_DEFINE_EMPTY(_name, _host, _port, _concurrent, _backlog, _detail, \
138-
_res_fallback) \
142+
_res_fallback, ...) \
139143
__z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
140-
_res_fallback, NULL, NULL)
144+
_res_fallback, NULL, NULL, __VA_ARGS__)
141145

142146
/**
143147
* @brief Define an HTTPS service without static resources.
@@ -162,10 +166,10 @@ struct http_service_desc {
162166
* @param _sec_tag_list_size TLS security tag list size used to setup a HTTPS socket.
163167
*/
164168
#define HTTPS_SERVICE_DEFINE_EMPTY(_name, _host, _port, _concurrent, _backlog, _detail, \
165-
_res_fallback, _sec_tag_list, _sec_tag_list_size) \
169+
_res_fallback, _sec_tag_list, _sec_tag_list_size, ...) \
166170
__z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
167171
_res_fallback, NULL, NULL, \
168-
_sec_tag_list, _sec_tag_list_size); \
172+
_sec_tag_list, _sec_tag_list_size, __VA_ARGS__); \
169173
BUILD_ASSERT(IS_ENABLED(CONFIG_NET_SOCKETS_SOCKOPT_TLS), \
170174
"TLS is required for HTTP secure (CONFIG_NET_SOCKETS_SOCKOPT_TLS)")
171175

@@ -189,13 +193,14 @@ struct http_service_desc {
189193
* @param _detail User-defined detail associated with the service.
190194
* @param _res_fallback Fallback resource to be served if no other resource matches path
191195
*/
192-
#define HTTP_SERVICE_DEFINE(_name, _host, _port, _concurrent, _backlog, _detail, _res_fallback) \
196+
#define HTTP_SERVICE_DEFINE(_name, _host, _port, _concurrent, _backlog, \
197+
_detail, _res_fallback, _socket_create, ...) \
193198
extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_start)[]; \
194199
extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_end)[]; \
195200
__z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
196201
_res_fallback, \
197202
&_CONCAT(_http_resource_desc_##_name, _list_start)[0], \
198-
&_CONCAT(_http_resource_desc_##_name, _list_end)[0]);
203+
&_CONCAT(_http_resource_desc_##_name, _list_end)[0], __VA_ARGS__)
199204

200205
/**
201206
* @brief Define an HTTPS service with static resources.
@@ -220,14 +225,14 @@ struct http_service_desc {
220225
* @param _sec_tag_list_size TLS security tag list size used to setup a HTTPS socket.
221226
*/
222227
#define HTTPS_SERVICE_DEFINE(_name, _host, _port, _concurrent, _backlog, _detail, \
223-
_res_fallback, _sec_tag_list, _sec_tag_list_size) \
228+
_res_fallback, _sec_tag_list, _sec_tag_list_size, ...) \
224229
extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_start)[]; \
225230
extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_end)[]; \
226231
__z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
227232
_res_fallback, \
228233
&_CONCAT(_http_resource_desc_##_name, _list_start)[0], \
229234
&_CONCAT(_http_resource_desc_##_name, _list_end)[0], \
230-
_sec_tag_list, _sec_tag_list_size); \
235+
_sec_tag_list, _sec_tag_list_size, __VA_ARGS__); \
231236
BUILD_ASSERT(IS_ENABLED(CONFIG_NET_SOCKETS_SOCKOPT_TLS), \
232237
"TLS is required for HTTP secure (CONFIG_NET_SOCKETS_SOCKOPT_TLS)")
233238

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->socket_create != NULL) {
160+
fd = svc->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)