Compare commits

..
Author SHA1 Message Date
yhirose e5c767407a Tell service failures from client failures in the online tests
The no-exceptions CI job runs the *_Online tests, and over its last 40
runs 27 failed, each time on tests that never reached what they check:

- google.com answered CI runners with 302 or 429 instead of the 301 or
  200 the certificate verification tests asserted. Those tests only need
  a response, which proves the handshake and the verification passed, as
  UniversalClientRedirectTest.EnableSystemCaCarriesOver_Online already
  does. Issue2004_Online is a regression test for a crash, so a response
  is enough there too.
- badssl.com drops connections from CI runners before it sends its
  certificate, so the hostname and self-signed checks never ran. From
  elsewhere, the same OpenSSL 3.0.13 gets the certificate and the
  expected verification error.

Generalize retry_gateway_errors() to retry_service_failures(), which also
treats a handshake that failed before any verdict on the certificate as
the service's failure, and use it for the two badssl.com tests. Such a
handshake failure in the client itself is caught by the TLS tests that
run against local servers.
2026-11-26 13:00:58 -05:00
yhirose 4995333ef2 Stop waiting for a TLS handshake once the server is stopping
SSLServer::process_and_close_socket() waits for the handshake of an
accepted connection for up to the read timeout, and that wait ignored
stop(). A client that connects and never sends a ClientHello kept
listen() from returning for the full timeout (4.7 seconds with the
default 5-second read timeout).

Give tls::accept_nonblocking() the same `interrupted` parameter that
connect_nonblocking() already has, and pass `is_stopping()` from
start_session(). Keep-alive parking was not affected, since it waits for
the ClientHello in its event loop, which stop() wakes.
2026-11-24 23:58:31 -05:00
2 changed files with 161 additions and 105 deletions

No files matched your search

+25 -12
View File
@@ -5647,7 +5647,8 @@ bool connect_nonblocking(session_t session, socket_t sock, time_t timeout_sec,
time_t timeout_usec, TlsError *err, time_t timeout_usec, TlsError *err,
const std::function<bool()> &interrupted = nullptr); const std::function<bool()> &interrupted = nullptr);
bool accept_nonblocking(session_t session, socket_t sock, time_t timeout_sec, bool accept_nonblocking(session_t session, socket_t sock, time_t timeout_sec,
time_t timeout_usec, TlsError *err); time_t timeout_usec, TlsError *err,
const std::function<bool()> &interrupted = nullptr);
// I/O (non-blocking capable) // I/O (non-blocking capable)
ssize_t read(session_t session, void *buf, size_t len, TlsError &err); ssize_t read(session_t session, void *buf, size_t len, TlsError &err);
@@ -20278,8 +20279,9 @@ inline tls::session_t SSLServer::start_session(socket_t sock) {
std::lock_guard<std::mutex> guard(ctx_mutex_); std::lock_guard<std::mutex> guard(ctx_mutex_);
if (--handshakes_in_progress_ == 0) { ctx_cv_.notify_all(); } if (--handshakes_in_progress_ == 0) { ctx_cv_.notify_all(); }
}); });
accepted = accept_nonblocking(session, sock, read_timeout_sec_, accepted =
read_timeout_usec_, &tls_err); accept_nonblocking(session, sock, read_timeout_sec_, read_timeout_usec_,
&tls_err, [this]() { return is_stopping(); });
} }
if (!accepted) { if (!accepted) {
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
@@ -21694,7 +21696,8 @@ inline bool connect_nonblocking(session_t session, socket_t sock,
inline bool accept_nonblocking(session_t session, socket_t sock, inline bool accept_nonblocking(session_t session, socket_t sock,
time_t timeout_sec, time_t timeout_usec, time_t timeout_sec, time_t timeout_usec,
TlsError *err) { TlsError *err,
const std::function<bool()> &interrupted) {
if (!session) { if (!session) {
if (err) { err->code = ErrorCode::Fatal; } if (err) { err->code = ErrorCode::Fatal; }
return false; return false;
@@ -21719,10 +21722,14 @@ inline bool accept_nonblocking(session_t session, socket_t sock,
auto ssl_err = SSL_get_error(ssl, res); auto ssl_err = SSL_get_error(ssl, res);
switch (ssl_err) { switch (ssl_err) {
case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_READ:
if (detail::select_until(sock, POLLIN, deadline) > 0) { continue; } if (detail::select_until(sock, POLLIN, deadline, interrupted) > 0) {
continue;
}
break; break;
case SSL_ERROR_WANT_WRITE: case SSL_ERROR_WANT_WRITE:
if (detail::select_until(sock, POLLOUT, deadline) > 0) { continue; } if (detail::select_until(sock, POLLOUT, deadline, interrupted) > 0) {
continue;
}
break; break;
default: break; default: break;
} }
@@ -23049,13 +23056,14 @@ inline bool connect_nonblocking(session_t session, socket_t sock,
inline bool accept_nonblocking(session_t session, socket_t sock, inline bool accept_nonblocking(session_t session, socket_t sock,
time_t timeout_sec, time_t timeout_usec, time_t timeout_sec, time_t timeout_usec,
TlsError *err) { TlsError *err,
const std::function<bool()> &interrupted) {
// A failed handshake leaves its SNI behind, so start without it // A failed handshake leaves its SNI behind, so start without it
impl::mbedpending_sni().clear(); impl::mbedpending_sni().clear();
// Same implementation as connect for Mbed TLS // Same implementation as connect for Mbed TLS
bool result = bool result = connect_nonblocking(session, sock, timeout_sec, timeout_usec,
connect_nonblocking(session, sock, timeout_sec, timeout_usec, err); err, interrupted);
// After successful handshake, capture SNI from thread-local storage // After successful handshake, capture SNI from thread-local storage
if (result && session) { if (result && session) {
@@ -24230,7 +24238,8 @@ inline bool connect_nonblocking(session_t session, socket_t sock,
inline bool accept_nonblocking(session_t session, socket_t sock, inline bool accept_nonblocking(session_t session, socket_t sock,
time_t timeout_sec, time_t timeout_usec, time_t timeout_sec, time_t timeout_usec,
TlsError *err) { TlsError *err,
const std::function<bool()> &interrupted) {
if (!session) { if (!session) {
if (err) { err->code = ErrorCode::Fatal; } if (err) { err->code = ErrorCode::Fatal; }
return false; return false;
@@ -24248,9 +24257,13 @@ inline bool accept_nonblocking(session_t session, socket_t sock,
while ((ret = wolfSSL_accept(wsession->ssl)) != SSL_SUCCESS) { while ((ret = wolfSSL_accept(wsession->ssl)) != SSL_SUCCESS) {
int ssl_error = wolfSSL_get_error(wsession->ssl, ret); int ssl_error = wolfSSL_get_error(wsession->ssl, ret);
if (ssl_error == SSL_ERROR_WANT_READ) { if (ssl_error == SSL_ERROR_WANT_READ) {
if (detail::select_until(sock, POLLIN, deadline) > 0) { continue; } if (detail::select_until(sock, POLLIN, deadline, interrupted) > 0) {
continue;
}
} else if (ssl_error == SSL_ERROR_WANT_WRITE) { } else if (ssl_error == SSL_ERROR_WANT_WRITE) {
if (detail::select_until(sock, POLLOUT, deadline) > 0) { continue; } if (detail::select_until(sock, POLLOUT, deadline, interrupted) > 0) {
continue;
}
} }
// Error or timeout // Error or timeout
+136 -93
View File
@@ -84,7 +84,8 @@ bool connect_nonblocking(session_t session, socket_t sock, time_t timeout_sec,
time_t timeout_usec, TlsError *err, time_t timeout_usec, TlsError *err,
const std::function<bool()> &interrupted); const std::function<bool()> &interrupted);
bool accept_nonblocking(session_t session, socket_t sock, time_t timeout_sec, bool accept_nonblocking(session_t session, socket_t sock, time_t timeout_sec,
time_t timeout_usec, TlsError *err); time_t timeout_usec, TlsError *err,
const std::function<bool()> &interrupted);
ssize_t read(session_t session, void *buf, size_t len, TlsError &err); ssize_t read(session_t session, void *buf, size_t len, TlsError &err);
ssize_t write(session_t session, const void *buf, size_t len, TlsError &err); ssize_t write(session_t session, const void *buf, size_t len, TlsError &err);
void shutdown(session_t session, bool graceful); void shutdown(session_t session, bool graceful);
@@ -156,28 +157,38 @@ static void read_file(const std::string &path, std::string &out) {
fs.read(&out[0], static_cast<std::streamsize>(size)); fs.read(&out[0], static_cast<std::streamsize>(size));
} }
// httpbingo.org intermittently answers with gateway errors unrelated to the // Failures of the services that the *_Online tests reach, unrelated to the
// client under test. // client under test: httpbingo.org intermittently answers with gateway errors,
static bool is_gateway_error(const Result &res) { // and badssl.com drops connections from CI runners before it sends its
return res && (res->status == StatusCode::BadGateway_502 || // certificate. A handshake that fails before any verdict on the certificate is
res->status == StatusCode::ServiceUnavailable_503 || // left to the tests against local servers.
res->status == StatusCode::GatewayTimeout_504); static bool is_service_failure(const Result &res) {
if (res) {
return res->status == StatusCode::BadGateway_502 ||
res->status == StatusCode::ServiceUnavailable_503 ||
res->status == StatusCode::GatewayTimeout_504;
}
return res.error() == Error::SSLConnection;
} }
template <typename F> static Result retry_gateway_errors(F &&send) { template <typename F> static Result retry_service_failures(F &&send) {
auto res = send(); auto res = send();
for (auto wait : {1, 2}) { for (auto wait : {1, 2}) {
if (!is_gateway_error(res)) { break; } if (!is_service_failure(res)) { break; }
std::this_thread::sleep_for(std::chrono::seconds(wait)); std::this_thread::sleep_for(std::chrono::seconds(wait));
res = send(); res = send();
} }
return res; return res;
} }
#define SKIP_ON_GATEWAY_ERROR(res) \ #define SKIP_ON_SERVICE_FAILURE(res) \
do { \ do { \
if (is_gateway_error(res)) { \ if (is_service_failure(res)) { \
GTEST_SKIP() << "gateway error " << (res)->status; \ if (res) { \
GTEST_SKIP() << "service failure: status " << (res)->status; \
} else { \
GTEST_SKIP() << "service failure: " << to_string((res).error()); \
} \
} \ } \
} while (0) } while (0)
@@ -2852,8 +2863,8 @@ TEST(RangeTest, FromHTTPBin_Online) {
cli.set_connection_timeout(5); cli.set_connection_timeout(5);
{ {
auto res = retry_gateway_errors([&] { return cli.Get(path); }); auto res = retry_service_failures([&] { return cli.Get(path); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
@@ -2861,8 +2872,8 @@ TEST(RangeTest, FromHTTPBin_Online) {
{ {
Headers headers = {make_range_header({{1, -1}})}; Headers headers = {make_range_header({{1, -1}})};
auto res = retry_gateway_errors([&] { return cli.Get(path, headers); }); auto res = retry_service_failures([&] { return cli.Get(path, headers); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ("bcdefghijklmnopqrstuvwxyzabcdef", res->body); EXPECT_EQ("bcdefghijklmnopqrstuvwxyzabcdef", res->body);
EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ(StatusCode::PartialContent_206, res->status);
@@ -2870,8 +2881,8 @@ TEST(RangeTest, FromHTTPBin_Online) {
{ {
Headers headers = {make_range_header({{1, 10}})}; Headers headers = {make_range_header({{1, 10}})};
auto res = retry_gateway_errors([&] { return cli.Get(path, headers); }); auto res = retry_service_failures([&] { return cli.Get(path, headers); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ("bcdefghijk", res->body); EXPECT_EQ("bcdefghijk", res->body);
EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ(StatusCode::PartialContent_206, res->status);
@@ -2882,8 +2893,8 @@ TEST(RangeTest, FromHTTPBin_Online) {
// acceptable per RFC 9110 §15.3.7, so we accept either status code. // acceptable per RFC 9110 §15.3.7, so we accept either status code.
{ {
Headers headers = {make_range_header({{0, 31}})}; Headers headers = {make_range_header({{0, 31}})};
auto res = retry_gateway_errors([&] { return cli.Get(path, headers); }); auto res = retry_service_failures([&] { return cli.Get(path, headers); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
EXPECT_TRUE(res->status == StatusCode::OK_200 || EXPECT_TRUE(res->status == StatusCode::OK_200 ||
@@ -2892,8 +2903,8 @@ TEST(RangeTest, FromHTTPBin_Online) {
{ {
Headers headers = {make_range_header({{0, -1}})}; Headers headers = {make_range_header({{0, -1}})};
auto res = retry_gateway_errors([&] { return cli.Get(path, headers); }); auto res = retry_service_failures([&] { return cli.Get(path, headers); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
EXPECT_TRUE(res->status == StatusCode::OK_200 || EXPECT_TRUE(res->status == StatusCode::OK_200 ||
@@ -2905,8 +2916,8 @@ TEST(RangeTest, FromHTTPBin_Online) {
// in real servers, so we only verify the request succeeds. // in real servers, so we only verify the request succeeds.
{ {
Headers headers = {make_range_header({{0, 32}})}; Headers headers = {make_range_header({{0, 32}})};
auto res = retry_gateway_errors([&] { return cli.Get(path, headers); }); auto res = retry_service_failures([&] { return cli.Get(path, headers); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
} }
} }
@@ -3221,9 +3232,9 @@ TEST(CancelTest, NoCancel_Online) {
#endif #endif
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
auto res = retry_gateway_errors( auto res = retry_service_failures(
[&] { return cli.Get(path, [](uint64_t, uint64_t) { return true; }); }); [&] { return cli.Get(path, [](uint64_t, uint64_t) { return true; }); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body); EXPECT_EQ("abcdefghijklmnopqrstuvwxyzabcdef", res->body);
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
@@ -3244,9 +3255,9 @@ TEST(CancelTest, WithCancelSmallPayload_Online) {
Client cli(host, port); Client cli(host, port);
#endif #endif
auto res = retry_gateway_errors( auto res = retry_service_failures(
[&] { return cli.Get(path, [](uint64_t, uint64_t) { return false; }); }); [&] { return cli.Get(path, [](uint64_t, uint64_t) { return false; }); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
ASSERT_TRUE(!res); ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error()); EXPECT_EQ(Error::Canceled, res.error());
@@ -3265,12 +3276,12 @@ TEST(CancelTest, WithCancelLargePayload_Online) {
#endif #endif
cli.set_connection_timeout(std::chrono::seconds(5)); cli.set_connection_timeout(std::chrono::seconds(5));
auto res = retry_gateway_errors([&] { auto res = retry_service_failures([&] {
auto count = 0; auto count = 0;
return cli.Get(path, return cli.Get(path,
[&count](uint64_t, uint64_t) { return (count++ == 0); }); [&count](uint64_t, uint64_t) { return (count++ == 0); });
}); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(!res); ASSERT_TRUE(!res);
EXPECT_EQ(Error::Canceled, res.error()); EXPECT_EQ(Error::Canceled, res.error());
} }
@@ -3860,18 +3871,18 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) {
#endif #endif
{ {
auto res = retry_gateway_errors([&] { return cli.Get(path); }); auto res = retry_service_failures([&] { return cli.Get(path); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::Unauthorized_401, res->status); EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
} }
{ {
auto res = retry_gateway_errors([&] { auto res = retry_service_failures([&] {
return cli.Get( return cli.Get(
path, Headers{make_basic_authentication_header("hello", "world")}); path, Headers{make_basic_authentication_header("hello", "world")});
}); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
auto body = remove_whitespace(res->body); auto body = remove_whitespace(res->body);
EXPECT_TRUE(body.find("\"authenticated\":true") != std::string::npos); EXPECT_TRUE(body.find("\"authenticated\":true") != std::string::npos);
@@ -3881,8 +3892,8 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) {
{ {
cli.set_basic_auth("hello", "world"); cli.set_basic_auth("hello", "world");
auto res = retry_gateway_errors([&] { return cli.Get(path); }); auto res = retry_service_failures([&] { return cli.Get(path); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
auto body = remove_whitespace(res->body); auto body = remove_whitespace(res->body);
EXPECT_TRUE(body.find("\"authenticated\":true") != std::string::npos); EXPECT_TRUE(body.find("\"authenticated\":true") != std::string::npos);
@@ -3892,16 +3903,16 @@ TEST(BaseAuthTest, FromHTTPWatch_Online) {
{ {
cli.set_basic_auth("hello", "bad"); cli.set_basic_auth("hello", "bad");
auto res = retry_gateway_errors([&] { return cli.Get(path); }); auto res = retry_service_failures([&] { return cli.Get(path); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::Unauthorized_401, res->status); EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
} }
{ {
cli.set_basic_auth("bad", "world"); cli.set_basic_auth("bad", "world");
auto res = retry_gateway_errors([&] { return cli.Get(path); }); auto res = retry_service_failures([&] { return cli.Get(path); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::Unauthorized_401, res->status); EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
} }
@@ -3920,8 +3931,8 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) {
SSLClient cli(host, port); SSLClient cli(host, port);
{ {
auto res = retry_gateway_errors([&] { return cli.Get(unauth_path); }); auto res = retry_service_failures([&] { return cli.Get(unauth_path); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::Unauthorized_401, res->status); EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
} }
@@ -3930,8 +3941,8 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) {
cli.set_digest_auth("hello", "world"); cli.set_digest_auth("hello", "world");
for (const auto &path : paths) { for (const auto &path : paths) {
auto res = retry_gateway_errors([&] { return cli.Get(path.c_str()); }); auto res = retry_service_failures([&] { return cli.Get(path.c_str()); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
auto body = remove_whitespace(res->body); auto body = remove_whitespace(res->body);
EXPECT_TRUE(body.find("\"authenticated\":true") != std::string::npos); EXPECT_TRUE(body.find("\"authenticated\":true") != std::string::npos);
@@ -3941,8 +3952,8 @@ TEST(DigestAuthTest, FromHTTPWatch_Online) {
cli.set_digest_auth("hello", "bad"); cli.set_digest_auth("hello", "bad");
for (const auto &path : paths) { for (const auto &path : paths) {
auto res = retry_gateway_errors([&] { return cli.Get(path.c_str()); }); auto res = retry_service_failures([&] { return cli.Get(path.c_str()); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::Unauthorized_401, res->status); EXPECT_EQ(StatusCode::Unauthorized_401, res->status);
} }
@@ -4154,8 +4165,8 @@ TEST(SpecifyServerIPAddressTest, AnotherHostname_Online) {
cli.set_direct_hostname_addr_map({{another_host, wrong_ip}}); cli.set_direct_hostname_addr_map({{another_host, wrong_ip}});
auto res = cli.Get("/"); auto res = cli.Get("/");
// Any status: Google may redirect or rate-limit CI runners.
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
ASSERT_EQ(StatusCode::MovedPermanently_301, res->status);
} }
TEST(SpecifyServerIPAddressTest, RealHostname_Online) { TEST(SpecifyServerIPAddressTest, RealHostname_Online) {
@@ -4340,8 +4351,8 @@ TEST(AbsoluteRedirectTest, Redirect_Online) {
#endif #endif
cli.set_follow_location(true); cli.set_follow_location(true);
auto res = retry_gateway_errors([&] { return cli.Get(path); }); auto res = retry_service_failures([&] { return cli.Get(path); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
} }
@@ -4357,8 +4368,8 @@ TEST(RedirectTest, Redirect_Online) {
#endif #endif
cli.set_follow_location(true); cli.set_follow_location(true);
auto res = retry_gateway_errors([&] { return cli.Get(path); }); auto res = retry_service_failures([&] { return cli.Get(path); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
} }
@@ -4374,8 +4385,8 @@ TEST(RelativeRedirectTest, Redirect_Online) {
#endif #endif
cli.set_follow_location(true); cli.set_follow_location(true);
auto res = retry_gateway_errors([&] { return cli.Get(path); }); auto res = retry_service_failures([&] { return cli.Get(path); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
} }
@@ -4391,8 +4402,8 @@ TEST(TooManyRedirectTest, Redirect_Online) {
#endif #endif
cli.set_follow_location(true); cli.set_follow_location(true);
auto res = retry_gateway_errors([&] { return cli.Get(path); }); auto res = retry_service_failures([&] { return cli.Get(path); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(!res); ASSERT_TRUE(!res);
EXPECT_EQ(Error::ExceedRedirectCount, res.error()); EXPECT_EQ(Error::ExceedRedirectCount, res.error());
} }
@@ -4419,10 +4430,10 @@ TEST(YahooRedirectTest, Redirect_Online) {
TEST(HttpsToHttpRedirectTest, Redirect_Online) { TEST(HttpsToHttpRedirectTest, Redirect_Online) {
SSLClient cli(REDIR_HOST); SSLClient cli(REDIR_HOST);
cli.set_follow_location(true); cli.set_follow_location(true);
auto res = retry_gateway_errors([&] { auto res = retry_service_failures([&] {
return cli.Get(REDIR_PATH "?url=http%3A%2F%2Fexample.com&status_code=302"); return cli.Get(REDIR_PATH "?url=http%3A%2F%2Fexample.com&status_code=302");
}); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
} }
@@ -4435,9 +4446,9 @@ TEST(HttpsToHttpRedirectTest2, Redirect_Online) {
params.emplace("url", "http://example.com"); params.emplace("url", "http://example.com");
params.emplace("status_code", "302"); params.emplace("status_code", "302");
auto res = retry_gateway_errors( auto res = retry_service_failures(
[&] { return cli.Get(REDIR_PATH, params, Headers{}); }); [&] { return cli.Get(REDIR_PATH, params, Headers{}); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
} }
@@ -4449,10 +4460,10 @@ TEST(HttpsToHttpRedirectTest3, Redirect_Online) {
Params params; Params params;
params.emplace("url", "http://example.com"); params.emplace("url", "http://example.com");
auto res = retry_gateway_errors([&] { auto res = retry_service_failures([&] {
return cli.Get(REDIR_PATH "?status_code=302", params, Headers{}); return cli.Get(REDIR_PATH "?status_code=302", params, Headers{});
}); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
} }
@@ -5968,6 +5979,40 @@ TEST(SSLClientServerTest, UpdateCertsPemWaitsForHandshakeInProgress) {
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
} }
TEST(SSLClientServerTest, StopDoesNotWaitForHandshakeInProgress) {
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
ASSERT_TRUE(svr.is_valid());
// Keep-alive parking would wait for the ClientHello without a handshake.
svr.set_keep_alive_parking(false);
svr.set_read_timeout(10, 0);
thread t = thread([&]() { ASSERT_TRUE(svr.listen(HOST, PORT)); });
auto se = detail::scope_exit([&] {
svr.stop();
if (t.joinable()) { t.join(); }
});
svr.wait_until_ready();
// A connection that sends no ClientHello keeps its handshake in progress,
// once the server has had a moment to start it.
auto error = Error::Success;
auto sock = detail::create_client_socket(
HOST, "", PORT, AF_UNSPEC, false, false, nullptr,
/*connection_timeout_sec=*/5, 0,
/*read_timeout_sec=*/5, 0,
/*write_timeout_sec=*/5, 0, std::string(), error);
ASSERT_NE(INVALID_SOCKET, sock);
std::this_thread::sleep_for(std::chrono::milliseconds(300));
auto start = std::chrono::steady_clock::now();
svr.stop();
t.join();
auto elapsed = std::chrono::steady_clock::now() - start;
detail::close_socket(sock);
EXPECT_LT(elapsed, std::chrono::seconds(3));
}
#endif #endif
TEST(ErrorHandlerTest, ContentLength) { TEST(ErrorHandlerTest, ContentLength) {
@@ -13802,7 +13847,8 @@ static void expect_no_reuse_with_unread_data(bool use_tls, bool same_write) {
if (use_tls) { if (use_tls) {
peer.session = tls::create_session(ctx, peer.sock); peer.session = tls::create_session(ctx, peer.sock);
tls::TlsError err; tls::TlsError err;
if (!tls::accept_nonblocking(peer.session, peer.sock, 5, 0, &err)) { if (!tls::accept_nonblocking(peer.session, peer.sock, 5, 0, &err,
nullptr)) {
close_peer(peer); close_peer(peer);
return Peer(); return Peer();
} }
@@ -15089,16 +15135,16 @@ TEST(ClientDefaultHeadersTest, DefaultHeaders_Online) {
cli.set_connection_timeout(5); cli.set_connection_timeout(5);
{ {
auto res = retry_gateway_errors([&] { return cli.Get(path); }); auto res = retry_service_failures([&] { return cli.Get(path); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ("bcdefghijk", res->body); EXPECT_EQ("bcdefghijk", res->body);
EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ(StatusCode::PartialContent_206, res->status);
} }
{ {
auto res = retry_gateway_errors([&] { return cli.Get(path); }); auto res = retry_service_failures([&] { return cli.Get(path); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ("bcdefghijk", res->body); EXPECT_EQ("bcdefghijk", res->body);
EXPECT_EQ(StatusCode::PartialContent_206, res->status); EXPECT_EQ(StatusCode::PartialContent_206, res->status);
@@ -16782,10 +16828,8 @@ TEST(SSLClientTest, UpdateCAStoreWithPem_Online) {
// Verify client is still valid and can make connections // Verify client is still valid and can make connections
httplib_client.enable_server_certificate_verification(true); httplib_client.enable_server_certificate_verification(true);
auto res = httplib_client.Get("/"); auto res = httplib_client.Get("/");
// Any status: Google may redirect or rate-limit CI runners.
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
// Google may return 200 or 301 depending on various factors
EXPECT_TRUE(res->status == StatusCode::OK_200 ||
res->status == StatusCode::MovedPermanently_301);
} }
TEST(SSLClientTest, ServerNameIndication_Online) { TEST(SSLClientTest, ServerNameIndication_Online) {
@@ -16793,8 +16837,8 @@ TEST(SSLClientTest, ServerNameIndication_Online) {
auto path = std::string{"/get"}; auto path = std::string{"/get"};
SSLClient cli(host, 443); SSLClient cli(host, 443);
auto res = retry_gateway_errors([&] { return cli.Get(path); }); auto res = retry_service_failures([&] { return cli.Get(path); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
ASSERT_EQ(StatusCode::OK_200, res->status); ASSERT_EQ(StatusCode::OK_200, res->status);
} }
@@ -16803,7 +16847,8 @@ TEST(SSLClientTest, ServerCertificateVerificationError_Online) {
// Use a site that will cause SSL verification failure due to self-signed cert // Use a site that will cause SSL verification failure due to self-signed cert
SSLClient cli("self-signed.badssl.com", 443); SSLClient cli("self-signed.badssl.com", 443);
cli.enable_server_certificate_verification(true); cli.enable_server_certificate_verification(true);
auto res = cli.Get("/"); auto res = retry_service_failures([&] { return cli.Get("/"); });
SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(!res); ASSERT_TRUE(!res);
EXPECT_EQ(Error::SSLServerVerification, res.error()); EXPECT_EQ(Error::SSLServerVerification, res.error());
@@ -16833,7 +16878,8 @@ TEST(SSLClientTest, ServerHostnameVerificationError_Online) {
cli.enable_server_certificate_verification(true); cli.enable_server_certificate_verification(true);
cli.enable_server_hostname_verification(true); cli.enable_server_hostname_verification(true);
auto res = cli.Get("/"); auto res = retry_service_failures([&] { return cli.Get("/"); });
SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(!res); ASSERT_TRUE(!res);
EXPECT_EQ(Error::SSLServerHostnameVerification, res.error()); EXPECT_EQ(Error::SSLServerHostnameVerification, res.error());
@@ -16941,16 +16987,16 @@ TEST(WebSocketSSLVerifyTest, WindowsCertificateVerification) {
TEST(SSLClientTest, ServerCertificateVerification1_Online) { TEST(SSLClientTest, ServerCertificateVerification1_Online) {
Client cli("https://google.com"); Client cli("https://google.com");
auto res = cli.Get("/"); auto res = cli.Get("/");
// Any status: Google may redirect or rate-limit CI runners.
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
ASSERT_EQ(StatusCode::MovedPermanently_301, res->status);
} }
TEST(SSLClientTest, ServerCertificateVerification2_Online) { TEST(SSLClientTest, ServerCertificateVerification2_Online) {
SSLClient cli("google.com"); SSLClient cli("google.com");
cli.set_ca_cert_path(CA_CERT_FILE); cli.set_ca_cert_path(CA_CERT_FILE);
auto res = cli.Get("/"); auto res = cli.Get("/");
// Any status: Google may redirect or rate-limit CI runners.
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
ASSERT_EQ(StatusCode::MovedPermanently_301, res->status);
} }
TEST(SSLClientTest, ServerCertificateVerification3_Online) { TEST(SSLClientTest, ServerCertificateVerification3_Online) {
@@ -17015,8 +17061,8 @@ TEST(SSLClientTest, ServerCertificateVerification5_Online) {
SSLClient cli("google.com"); SSLClient cli("google.com");
cli.load_ca_cert_store(cert.data(), cert.size()); cli.load_ca_cert_store(cert.data(), cert.size());
const auto res = cli.Get("/"); const auto res = cli.Get("/");
// Any status: Google may redirect or rate-limit CI runners.
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
ASSERT_EQ(StatusCode::MovedPermanently_301, res->status);
} }
TEST(SSLClientTest, ServerCertificateVerification6_Online) { TEST(SSLClientTest, ServerCertificateVerification6_Online) {
@@ -17047,8 +17093,8 @@ TEST(SSLClientTest, ServerCertificateVerification6_Online) {
SSLClient cli("google.com"); SSLClient cli("google.com");
cli.load_ca_cert_store(cert, sizeof(cert)); cli.load_ca_cert_store(cert, sizeof(cert));
const auto res = cli.Get("/"); const auto res = cli.Get("/");
// Any status: Google may redirect or rate-limit CI runners.
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
ASSERT_EQ(StatusCode::MovedPermanently_301, res->status);
} }
TEST(SSLClientTest, WildcardHostNameMatch_Online) { TEST(SSLClientTest, WildcardHostNameMatch_Online) {
@@ -17059,8 +17105,8 @@ TEST(SSLClientTest, WildcardHostNameMatch_Online) {
cli.set_follow_location(true); cli.set_follow_location(true);
auto res = cli.Get("/"); auto res = cli.Get("/");
// Any status: Google may redirect or rate-limit CI runners.
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
ASSERT_EQ(StatusCode::OK_200, res->status);
} }
TEST(SSLClientTest, WildcardHostNameMatchCase_Online) { TEST(SSLClientTest, WildcardHostNameMatchCase_Online) {
@@ -17072,8 +17118,8 @@ TEST(SSLClientTest, WildcardHostNameMatchCase_Online) {
cli.set_follow_location(true); cli.set_follow_location(true);
auto res = cli.Get("/"); auto res = cli.Get("/");
// Any status: Google may redirect or rate-limit CI runners.
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
ASSERT_EQ(StatusCode::OK_200, res->status);
} }
TEST(SSLClientTest, HostNameMatchCase_Online) { TEST(SSLClientTest, HostNameMatchCase_Online) {
@@ -17084,8 +17130,8 @@ TEST(SSLClientTest, HostNameMatchCase_Online) {
cli.set_follow_location(true); cli.set_follow_location(true);
auto res = cli.Get("/"); auto res = cli.Get("/");
// Any status: Google may redirect or rate-limit CI runners.
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
ASSERT_EQ(StatusCode::OK_200, res->status);
} }
TEST(SSLClientTest, Issue2004_Online) { TEST(SSLClientTest, Issue2004_Online) {
@@ -17093,11 +17139,8 @@ TEST(SSLClientTest, Issue2004_Online) {
client.set_follow_location(true); client.set_follow_location(true);
auto res = client.Get("/"); auto res = client.Get("/");
// Any status: Google may redirect or rate-limit CI runners.
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
ASSERT_EQ(StatusCode::OK_200, res->status);
auto body = res->body;
EXPECT_EQ(body.substr(0, 15), "<!doctype html>");
} }
TEST(SSLClientTest, ErrorReportingWhenInvalid) { TEST(SSLClientTest, ErrorReportingWhenInvalid) {
@@ -18841,10 +18884,10 @@ TEST(DecodeWithChunkedEncoding, BrotliEncoding_Online) {
TEST(HttpsToHttpRedirectTest, SimpleInterface_Online) { TEST(HttpsToHttpRedirectTest, SimpleInterface_Online) {
Client cli(REDIR_HOST); Client cli(REDIR_HOST);
cli.set_follow_location(true); cli.set_follow_location(true);
auto res = retry_gateway_errors([&] { auto res = retry_service_failures([&] {
return cli.Get(REDIR_PATH "?url=http%3A%2F%2Fexample.com&status_code=302"); return cli.Get(REDIR_PATH "?url=http%3A%2F%2Fexample.com&status_code=302");
}); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
@@ -18858,9 +18901,9 @@ TEST(HttpsToHttpRedirectTest2, SimpleInterface_Online) {
params.emplace("url", "http://example.com"); params.emplace("url", "http://example.com");
params.emplace("status_code", "302"); params.emplace("status_code", "302");
auto res = retry_gateway_errors( auto res = retry_service_failures(
[&] { return cli.Get(REDIR_PATH, params, Headers{}); }); [&] { return cli.Get(REDIR_PATH, params, Headers{}); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
} }
@@ -18872,10 +18915,10 @@ TEST(HttpsToHttpRedirectTest3, SimpleInterface_Online) {
Params params; Params params;
params.emplace("url", "http://example.com"); params.emplace("url", "http://example.com");
auto res = retry_gateway_errors([&] { auto res = retry_service_failures([&] {
return cli.Get(REDIR_PATH "?status_code=302", params, Headers{}); return cli.Get(REDIR_PATH "?status_code=302", params, Headers{});
}); });
SKIP_ON_GATEWAY_ERROR(res); SKIP_ON_SERVICE_FAILURE(res);
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
EXPECT_EQ(StatusCode::OK_200, res->status); EXPECT_EQ(StatusCode::OK_200, res->status);
} }
@@ -19251,8 +19294,8 @@ TEST(SSLClientTest, EnableSystemCaWithCustomCa_Online) {
cli.enable_server_certificate_verification(true); cli.enable_server_certificate_verification(true);
auto res = cli.Get("/"); auto res = cli.Get("/");
// Any status: Google may redirect or rate-limit CI runners.
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
ASSERT_EQ(StatusCode::MovedPermanently_301, res->status);
} }
// The custom CA remains effective when combined with the system CA // The custom CA remains effective when combined with the system CA
@@ -19555,8 +19598,8 @@ TEST(UniversalClientTest, EnableSystemCaWithCustomCa_Online) {
cli.enable_server_certificate_verification(true); cli.enable_server_certificate_verification(true);
auto res = cli.Get("/"); auto res = cli.Get("/");
// Any status: Google may redirect or rate-limit CI runners.
ASSERT_TRUE(res) << "Error: " << to_string(res.error()); ASSERT_TRUE(res) << "Error: " << to_string(res.error());
ASSERT_EQ(StatusCode::MovedPermanently_301, res->status);
} }
// The system CA policy must carry over to the client created internally for // The system CA policy must carry over to the client created internally for
@@ -25182,7 +25225,7 @@ static void with_single_tls_response(const std::string &response,
nullptr); nullptr);
auto session = tls::create_session(ctx, cli); auto session = tls::create_session(ctx, cli);
tls::TlsError err; tls::TlsError err;
if (tls::accept_nonblocking(session, cli, 5, 0, &err)) { if (tls::accept_nonblocking(session, cli, 5, 0, &err, nullptr)) {
// The request head first, so that closing the socket sends a FIN // The request head first, so that closing the socket sends a FIN
// rather than an RST for the unread bytes. // rather than an RST for the unread bytes.
std::string req; std::string req;