Skip to content

Commit 7d86835

Browse files
committed
Fix #1878
1 parent aa04fee commit 7d86835

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

httplib.h

+16-5
Original file line numberDiff line numberDiff line change
@@ -3278,7 +3278,8 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
32783278

32793279
if (socket_options) { socket_options(sock); }
32803280

3281-
if (!bind_or_connect(sock, hints)) {
3281+
bool dummy;
3282+
if (!bind_or_connect(sock, hints, dummy)) {
32823283
close_socket(sock);
32833284
sock = INVALID_SOCKET;
32843285
}
@@ -3363,12 +3364,17 @@ socket_t create_socket(const std::string &host, const std::string &ip, int port,
33633364
}
33643365

33653366
// bind or connect
3366-
if (bind_or_connect(sock, *rp)) {
3367+
auto quit = false;
3368+
if (bind_or_connect(sock, *rp, quit)) {
33673369
freeaddrinfo(result);
33683370
return sock;
33693371
}
33703372

33713373
close_socket(sock);
3374+
3375+
if (quit) {
3376+
break;
3377+
}
33723378
}
33733379

33743380
freeaddrinfo(result);
@@ -3469,7 +3475,7 @@ inline socket_t create_client_socket(
34693475
time_t write_timeout_usec, const std::string &intf, Error &error) {
34703476
auto sock = create_socket(
34713477
host, ip, port, address_family, 0, tcp_nodelay, std::move(socket_options),
3472-
[&](socket_t sock2, struct addrinfo &ai) -> bool {
3478+
[&](socket_t sock2, struct addrinfo &ai, bool& quit) -> bool {
34733479
if (!intf.empty()) {
34743480
#ifdef USE_IF2IP
34753481
auto ip_from_if = if2ip(address_family, intf);
@@ -3493,7 +3499,12 @@ inline socket_t create_client_socket(
34933499
}
34943500
error = wait_until_socket_is_ready(sock2, connection_timeout_sec,
34953501
connection_timeout_usec);
3496-
if (error != Error::Success) { return false; }
3502+
if (error != Error::Success) {
3503+
if (error == Error::ConnectionTimeout) {
3504+
quit = true;
3505+
}
3506+
return false;
3507+
}
34973508
}
34983509

34993510
set_nonblocking(sock2, false);
@@ -6470,7 +6481,7 @@ Server::create_server_socket(const std::string &host, int port,
64706481
return detail::create_socket(
64716482
host, std::string(), port, address_family_, socket_flags, tcp_nodelay_,
64726483
std::move(socket_options),
6473-
[](socket_t sock, struct addrinfo &ai) -> bool {
6484+
[](socket_t sock, struct addrinfo &ai, bool& quit) -> bool {
64746485
if (::bind(sock, ai.ai_addr, static_cast<socklen_t>(ai.ai_addrlen))) {
64756486
return false;
64766487
}

0 commit comments

Comments
 (0)