Skip to content

Commit 1c3c8f2

Browse files
net: http: srvice: enhance HTTP service API with optional custom socket creation
Updated the HTTP service API to allow custom socket creation through a function pointer in the service descriptor. This change enables more flexible socket management during HTTP server initialization. The existing socket creation method is retained as a fallback if no custom function is provided.
1 parent 7da64e7 commit 1c3c8f2

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

include/zephyr/net/http/service.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ struct http_resource_desc {
6868
struct http_service_runtime_data {
6969
int num_clients;
7070
};
71+
72+
typedef int (*http_socket_create_fn)(const struct http_service_desc *svc, int af, int proto);
7173

7274
struct http_service_desc {
7375
const char *host;
@@ -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,13 @@ 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, _detail, _res_fallback, ...) \
193197
extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_start)[]; \
194198
extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_end)[]; \
195199
__z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
196200
_res_fallback, \
197201
&_CONCAT(_http_resource_desc_##_name, _list_start)[0], \
198-
&_CONCAT(_http_resource_desc_##_name, _list_end)[0]);
202+
&_CONCAT(_http_resource_desc_##_name, _list_end)[0], __VA_ARGS__)
199203

200204
/**
201205
* @brief Define an HTTPS service with static resources.
@@ -220,14 +224,14 @@ struct http_service_desc {
220224
* @param _sec_tag_list_size TLS security tag list size used to setup a HTTPS socket.
221225
*/
222226
#define HTTPS_SERVICE_DEFINE(_name, _host, _port, _concurrent, _backlog, _detail, \
223-
_res_fallback, _sec_tag_list, _sec_tag_list_size) \
227+
_res_fallback, _sec_tag_list, _sec_tag_list_size, ...) \
224228
extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_start)[]; \
225229
extern struct http_resource_desc _CONCAT(_http_resource_desc_##_name, _list_end)[]; \
226230
__z_http_service_define(_name, _host, _port, _concurrent, _backlog, _detail, \
227231
_res_fallback, \
228232
&_CONCAT(_http_resource_desc_##_name, _list_start)[0], \
229233
&_CONCAT(_http_resource_desc_##_name, _list_end)[0], \
230-
_sec_tag_list, _sec_tag_list_size); \
234+
_sec_tag_list, _sec_tag_list_size, __VA_ARGS__); \
231235
BUILD_ASSERT(IS_ENABLED(CONFIG_NET_SOCKETS_SOCKOPT_TLS), \
232236
"TLS is required for HTTP secure (CONFIG_NET_SOCKETS_SOCKOPT_TLS)")
233237

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)