Skip to content

Commit b8f1388

Browse files
authored
feat: add auto port assignment when setPort(0) is called
1 parent 1ad2e50 commit b8f1388

5 files changed

Lines changed: 31 additions & 26 deletions

File tree

config.mk

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,23 @@
1+
12
PREFIX=/usr/local
23
INSTALL_INCDIR=$(PREFIX)/include/hv
34
INSTALL_LIBDIR=$(PREFIX)/lib
4-
55
BUILD_SHARED=yes
66
BUILD_STATIC=yes
7-
8-
# modules
9-
# include icmp dns ftp smtp
107
WITH_PROTOCOL=no
11-
128
WITH_EVPP=yes
139
WITH_HTTP=yes
1410
WITH_HTTP_SERVER=yes
1511
WITH_HTTP_CLIENT=yes
1612
WITH_MQTT=no
17-
18-
# features
19-
# base/hsocket.h: Unix Domain Socket
2013
ENABLE_UDS=no
21-
# base/RAII.cpp: Windows MiniDumpWriteDump
2214
ENABLE_WINDUMP=no
23-
# http/http_content.h: KeyValue,QueryParams,MultiPart
2415
USE_MULTIMAP=no
25-
26-
# dependencies
27-
# for http/client
2816
WITH_CURL=no
29-
# for http2
3017
WITH_NGHTTP2=no
31-
# for SSL/TLS
3218
WITH_OPENSSL=no
3319
WITH_GNUTLS=no
3420
WITH_MBEDTLS=no
35-
36-
# rudp
3721
WITH_KCP=no
22+
WITH_IO_URING=no
23+
CONFIG_DATE=20260710

docs/cn/HttpServer.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ class HttpServer {
1010

1111
// 设置监听主机
1212
void setHost(const char* host = "0.0.0.0");
13-
// 设置监听端口
14-
void setPort(int port = 0, int ssl_port = 0);
13+
// 设置监听端口,port=0 表示自动分配空闲端口
14+
void setPort(int port = 0, int ssl_port = -1);
1515
// 设置监听文件描述符
1616
void setListenFD(int fd = -1, int ssl_fd = -1);
1717

@@ -34,7 +34,7 @@ class HttpServer {
3434
// 占用当前线程运行
3535
int run(bool wait = true);
3636

37-
// 不占用当前线程运行
37+
// 不占用当前线程运行,返回实际监听的HTTP端口号(port=0时为OS分配的端口)
3838
int start();
3939

4040
// 停止服务

examples/httpd/httpd.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ int parse_confile(const char* confile) {
129129
if (HV_WITH_SSL) {
130130
g_http_server.https_port = ini.Get<int>("https_port");
131131
}
132-
if (g_http_server.port == 0 && g_http_server.https_port == 0) {
132+
if (g_http_server.port < 0 && g_http_server.https_port < 0) {
133133
printf("Please config listen port!\n");
134134
exit(-10);
135135
}

http/server/HttpServer.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "herr.h"
55
#include "hlog.h"
66
#include "htime.h"
7+
#include "hsocket.h"
78

89
#include "EventLoop.h"
910
using namespace hv;
@@ -178,15 +179,31 @@ static void WINAPI loop_thread_stdcall(void* userdata) {
178179
*/
179180
int http_server_run(http_server_t* server, int wait) {
180181
// http_port
181-
if (server->port > 0) {
182+
if (server->port >= 0) {
182183
server->listenfd[0] = Listen(server->port, server->host);
183184
if (server->listenfd[0] < 0) return server->listenfd[0];
185+
if (server->port == 0) {
186+
// port=0: OS auto-assigned a free port, retrieve the actual port
187+
sockaddr_u localaddr;
188+
socklen_t addrlen = sizeof(localaddr);
189+
if (getsockname(server->listenfd[0], &localaddr.sa, &addrlen) == 0) {
190+
server->port = sockaddr_port(&localaddr);
191+
}
192+
}
184193
hlogi("http server listening on %s:%d", server->host, server->port);
185194
}
186195
// https_port
187-
if (server->https_port > 0 && HV_WITH_SSL) {
196+
if (server->https_port >= 0 && HV_WITH_SSL) {
188197
server->listenfd[1] = Listen(server->https_port, server->host);
189198
if (server->listenfd[1] < 0) return server->listenfd[1];
199+
if (server->https_port == 0) {
200+
// https_port=0: OS auto-assigned a free port, retrieve the actual port
201+
sockaddr_u localaddr;
202+
socklen_t addrlen = sizeof(localaddr);
203+
if (getsockname(server->listenfd[1], &localaddr.sa, &addrlen) == 0) {
204+
server->https_port = sockaddr_port(&localaddr);
205+
}
206+
}
190207
hlogi("https server listening on %s:%d", server->host, server->https_port);
191208
}
192209
// SSL_CTX

http/server/HttpServer.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ typedef struct http_server_s {
4040
// https_port = DEFAULT_HTTPS_PORT;
4141
// port = 8080;
4242
// https_port = 8443;
43-
port = https_port = 0;
43+
port = https_port = -1;
4444
http_version = 1;
4545
worker_processes = 0;
4646
worker_threads = 0;
@@ -102,7 +102,7 @@ class HV_EXPORT HttpServer : public http_server_t {
102102
if (host) strcpy(this->host, host);
103103
}
104104

105-
void setPort(int port = 0, int ssl_port = 0) {
105+
void setPort(int port = 0, int ssl_port = -1) {
106106
if (port >= 0) this->port = port;
107107
if (ssl_port >= 0) this->https_port = ssl_port;
108108
}
@@ -150,7 +150,9 @@ class HV_EXPORT HttpServer : public http_server_t {
150150
}
151151

152152
int start(const char* ip_port = NULL) {
153-
return run(ip_port, false);
153+
int ret = run(ip_port, false);
154+
if (ret != 0) return ret;
155+
return this->port;
154156
}
155157

156158
int stop() {

0 commit comments

Comments
 (0)