Skip to content

Commit 521529d

Browse files
authored
Fix #1481 (with content provider) (#1527)
* Fix #1481 (with content provider) * Improve shutdown performance * Make shutdown action more stable * Move some tests up * Simplified * Simplified
1 parent ed0719f commit 521529d

File tree

2 files changed

+235
-164
lines changed

2 files changed

+235
-164
lines changed

httplib.h

+21-4
Original file line numberDiff line numberDiff line change
@@ -8541,13 +8541,29 @@ inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex,
85418541
return ssl;
85428542
}
85438543

8544-
inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl,
8544+
inline void ssl_delete(std::mutex &ctx_mutex, SSL *ssl, socket_t sock,
85458545
bool shutdown_gracefully) {
85468546
// sometimes we may want to skip this to try to avoid SIGPIPE if we know
85478547
// the remote has closed the network connection
85488548
// Note that it is not always possible to avoid SIGPIPE, this is merely a
85498549
// best-efforts.
8550-
if (shutdown_gracefully) { SSL_shutdown(ssl); }
8550+
if (shutdown_gracefully) {
8551+
#ifdef _WIN32
8552+
SSL_shutdown(ssl);
8553+
#else
8554+
timeval tv;
8555+
tv.tv_sec = 1;
8556+
tv.tv_usec = 0;
8557+
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO,
8558+
reinterpret_cast<const void *>(&tv), sizeof(tv));
8559+
8560+
auto ret = SSL_shutdown(ssl);
8561+
while (ret == 0) {
8562+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
8563+
ret = SSL_shutdown(ssl);
8564+
}
8565+
#endif
8566+
}
85518567

85528568
std::lock_guard<std::mutex> guard(ctx_mutex);
85538569
SSL_free(ssl);
@@ -8826,7 +8842,7 @@ inline bool SSLServer::process_and_close_socket(socket_t sock) {
88268842
// Shutdown gracefully if the result seemed successful, non-gracefully if
88278843
// the connection appeared to be closed.
88288844
const bool shutdown_gracefully = ret;
8829-
detail::ssl_delete(ctx_mutex_, ssl, shutdown_gracefully);
8845+
detail::ssl_delete(ctx_mutex_, ssl, sock, shutdown_gracefully);
88308846
}
88318847

88328848
detail::shutdown_socket(sock);
@@ -9109,7 +9125,8 @@ inline void SSLClient::shutdown_ssl_impl(Socket &socket,
91099125
return;
91109126
}
91119127
if (socket.ssl) {
9112-
detail::ssl_delete(ctx_mutex_, socket.ssl, shutdown_gracefully);
9128+
detail::ssl_delete(ctx_mutex_, socket.ssl, socket.sock,
9129+
shutdown_gracefully);
91139130
socket.ssl = nullptr;
91149131
}
91159132
assert(socket.ssl == nullptr);

0 commit comments

Comments
 (0)