From d7b0d4bedbc8ee2331391f1d63c99e246a571dc0 Mon Sep 17 00:00:00 2001 From: Graeme Smecher Date: Mon, 16 Nov 2026 21:53:56 -0700 Subject: [PATCH] Drain the socket before closing a TLS connection (#2673) Server::process_and_close_socket() half-closes the socket and drains what the peer is still sending before the final close (#2534), so that a close with unread data, or with bytes still in flight, is not answered with a RST. A RST can make the peer's stack discard a response it has received but not yet read, as Windows does. SSLServer::process_and_close_socket() still closed outright. After a completed handshake, shut down TLS as before (sending close_notify when the exchange completed) and then drain the same way. A failed handshake closes outright as before. Fix #2672 --- httplib.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/httplib.h b/httplib.h index 42181dff..2e13e2dc 100644 --- a/httplib.h +++ b/httplib.h @@ -19160,8 +19160,12 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) { auto cleanup = detail::scope_exit([&] { if (handshake_done) { shutdown(session, !websocket_upgraded && ret); } free_session(session); - detail::shutdown_socket(sock); - detail::close_socket(sock); + if (handshake_done) { + detail::drain_and_close_socket(sock); + } else { + detail::shutdown_socket(sock); + detail::close_socket(sock); + } }); // Perform TLS accept handshake with timeout