Skip to content

Commit 390f2c4

Browse files
authored
Fix #1878 (#1893)
* Fix #1878
1 parent aa04fee commit 390f2c4

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

httplib.h

Lines changed: 12 additions & 5 deletions
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,15 @@ 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) { break; }
33723376
}
33733377

33743378
freeaddrinfo(result);
@@ -3469,7 +3473,7 @@ inline socket_t create_client_socket(
34693473
time_t write_timeout_usec, const std::string &intf, Error &error) {
34703474
auto sock = create_socket(
34713475
host, ip, port, address_family, 0, tcp_nodelay, std::move(socket_options),
3472-
[&](socket_t sock2, struct addrinfo &ai) -> bool {
3476+
[&](socket_t sock2, struct addrinfo &ai, bool &quit) -> bool {
34733477
if (!intf.empty()) {
34743478
#ifdef USE_IF2IP
34753479
auto ip_from_if = if2ip(address_family, intf);
@@ -3493,7 +3497,10 @@ inline socket_t create_client_socket(
34933497
}
34943498
error = wait_until_socket_is_ready(sock2, connection_timeout_sec,
34953499
connection_timeout_usec);
3496-
if (error != Error::Success) { return false; }
3500+
if (error != Error::Success) {
3501+
if (error == Error::ConnectionTimeout) { quit = true; }
3502+
return false;
3503+
}
34973504
}
34983505

34993506
set_nonblocking(sock2, false);
@@ -6470,7 +6477,7 @@ Server::create_server_socket(const std::string &host, int port,
64706477
return detail::create_socket(
64716478
host, std::string(), port, address_family_, socket_flags, tcp_nodelay_,
64726479
std::move(socket_options),
6473-
[](socket_t sock, struct addrinfo &ai) -> bool {
6480+
[](socket_t sock, struct addrinfo &ai, bool & /*quit*/) -> bool {
64746481
if (::bind(sock, ai.ai_addr, static_cast<socklen_t>(ai.ai_addrlen))) {
64756482
return false;
64766483
}

0 commit comments

Comments
 (0)