diff --git a/capi/libuwebsockets.cpp b/capi/libuwebsockets.cpp index a332e8eff..12d60f5a9 100644 --- a/capi/libuwebsockets.cpp +++ b/capi/libuwebsockets.cpp @@ -338,17 +338,36 @@ extern "C" void uws_app_listen_with_config(int ssl, uws_app_t *app, uws_app_listen_config_t config, uws_listen_handler handler, void *user_data) { + /* branching is getting untidy */ if (ssl) { uWS::SSLApp *uwsApp = (uWS::SSLApp *)app; - uwsApp->listen(config.host, config.port, config.options, [handler, config, user_data](struct us_listen_socket_t *listen_socket) - { handler((struct us_listen_socket_t *)listen_socket, config, user_data); }); + if (config.fd) { + uwsApp->listen( + config.options, + [handler, config, user_data](struct us_listen_socket_t *listen_socket) { + handler((struct us_listen_socket_t *)listen_socket, config, user_data); + }, + config.fd); + } else { + uwsApp->listen(config.host, config.port, config.options, [handler, config, user_data](struct us_listen_socket_t *listen_socket) + { handler((struct us_listen_socket_t *)listen_socket, config, user_data); }); + } } else { uWS::App *uwsApp = (uWS::App *)app; - uwsApp->listen(config.host, config.port, config.options, [handler, config, user_data](struct us_listen_socket_t *listen_socket) - { handler((struct us_listen_socket_t *)listen_socket, config, user_data); }); + if (config.fd) { + uwsApp->listen( + config.options, + [handler, config, user_data](struct us_listen_socket_t *listen_socket) { + handler((struct us_listen_socket_t *)listen_socket, config, user_data); + }, + config.fd); + } else { + uwsApp->listen(config.host, config.port, config.options, [handler, config, user_data](struct us_listen_socket_t *listen_socket) + { handler((struct us_listen_socket_t *)listen_socket, config, user_data); }); + } } } diff --git a/capi/libuwebsockets.h b/capi/libuwebsockets.h index 784db8f34..f58877780 100644 --- a/capi/libuwebsockets.h +++ b/capi/libuwebsockets.h @@ -90,7 +90,7 @@ extern "C" DLL_EXPORT typedef struct { - + LIBUS_SOCKET_DESCRIPTOR fd; int port; const char *host; int options; diff --git a/src/App.h b/src/App.h index 587abdc3d..2741516d7 100644 --- a/src/App.h +++ b/src/App.h @@ -21,6 +21,7 @@ #include #include #include +#include "libusockets.h" namespace uWS { /* Safari 15.0 - 15.3 has a completely broken compression implementation (client_no_context_takeover not @@ -561,6 +562,18 @@ struct TemplatedApp { handler(httpContext ? httpContext->listen(nullptr, port, options) : nullptr); return std::move(*this); } + + /* callback, fd */ + TemplatedApp &&listen(MoveOnlyFunction &&handler, LIBUS_SOCKET_DESCRIPTOR fd) { + handler(httpContext ? httpContext->listen(fd, 0) : nullptr); + return std::move(*this); + } + + /* options, callback, fd */ + TemplatedApp &&listen(int options, MoveOnlyFunction &&handler, LIBUS_SOCKET_DESCRIPTOR fd) { + handler(httpContext ? httpContext->listen(fd, options) : nullptr); + return std::move(*this); + } /* options, callback, path to unix domain socket */ TemplatedApp &&listen(int options, MoveOnlyFunction &&handler, std::string path) { diff --git a/src/HttpContext.h b/src/HttpContext.h index 5b4c87581..aa087abdc 100644 --- a/src/HttpContext.h +++ b/src/HttpContext.h @@ -25,6 +25,7 @@ #include "HttpResponseData.h" #include "AsyncSocket.h" #include "WebSocketData.h" +#include "libusockets.h" #include #include @@ -466,6 +467,12 @@ struct HttpContext { us_listen_socket_t *listen(const char *path, int options) { return us_socket_context_listen_unix(SSL, getSocketContext(), path, options, sizeof(HttpResponseData)); } + + /* Attach to bound and listening socket (fd) using this HttpContext */ + us_listen_socket_t *listen(LIBUS_SOCKET_DESCRIPTOR fd, int options) { + return us_socket_context_listen_direct(SSL, getSocketContext(), fd, options, sizeof(HttpResponseData)); + } + }; }