mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-25 20:38:26 +00:00
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.
This commit is contained in:
1 parent
baf27c5993
commit
4995333ef2
2 files changed
+64
-15
No files matched your search
@@ -5647,7 +5647,8 @@ bool connect_nonblocking(session_t session, socket_t sock, time_t timeout_sec,
|
||||
time_t timeout_usec, TlsError *err,
|
||||
const std::function<bool()> &interrupted = nullptr);
|
||||
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)
|
||||
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_);
|
||||
if (--handshakes_in_progress_ == 0) { ctx_cv_.notify_all(); }
|
||||
});
|
||||
accepted = accept_nonblocking(session, sock, read_timeout_sec_,
|
||||
read_timeout_usec_, &tls_err);
|
||||
accepted =
|
||||
accept_nonblocking(session, sock, read_timeout_sec_, read_timeout_usec_,
|
||||
&tls_err, [this]() { return is_stopping(); });
|
||||
}
|
||||
if (!accepted) {
|
||||
#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,
|
||||
time_t timeout_sec, time_t timeout_usec,
|
||||
TlsError *err) {
|
||||
TlsError *err,
|
||||
const std::function<bool()> &interrupted) {
|
||||
if (!session) {
|
||||
if (err) { err->code = ErrorCode::Fatal; }
|
||||
return false;
|
||||
@@ -21719,10 +21722,14 @@ inline bool accept_nonblocking(session_t session, socket_t sock,
|
||||
auto ssl_err = SSL_get_error(ssl, res);
|
||||
switch (ssl_err) {
|
||||
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;
|
||||
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;
|
||||
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,
|
||||
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
|
||||
impl::mbedpending_sni().clear();
|
||||
|
||||
// Same implementation as connect for Mbed TLS
|
||||
bool result =
|
||||
connect_nonblocking(session, sock, timeout_sec, timeout_usec, err);
|
||||
bool result = connect_nonblocking(session, sock, timeout_sec, timeout_usec,
|
||||
err, interrupted);
|
||||
|
||||
// After successful handshake, capture SNI from thread-local storage
|
||||
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,
|
||||
time_t timeout_sec, time_t timeout_usec,
|
||||
TlsError *err) {
|
||||
TlsError *err,
|
||||
const std::function<bool()> &interrupted) {
|
||||
if (!session) {
|
||||
if (err) { err->code = ErrorCode::Fatal; }
|
||||
return false;
|
||||
@@ -24248,9 +24257,13 @@ inline bool accept_nonblocking(session_t session, socket_t sock,
|
||||
while ((ret = wolfSSL_accept(wsession->ssl)) != SSL_SUCCESS) {
|
||||
int ssl_error = wolfSSL_get_error(wsession->ssl, ret);
|
||||
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) {
|
||||
if (detail::select_until(sock, POLLOUT, deadline) > 0) { continue; }
|
||||
if (detail::select_until(sock, POLLOUT, deadline, interrupted) > 0) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Error or timeout
|
||||
|
||||
Reference in new issue
Block a user