Made a temporary fix for OpenSSL thread problem
This commit is contained in:
parent
4320d7ba3e
commit
5574d82eb3
22
httplib.h
22
httplib.h
@ -2029,15 +2029,23 @@ inline std::shared_ptr<Response> Client::post(const char* path, const Headers& h
|
|||||||
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
|
||||||
namespace detail {
|
namespace detail {
|
||||||
|
|
||||||
|
// TODO: OpenSSL 1.0.2 occasionally crashes... The upcoming 1.1.0 is going to be thread safe.
|
||||||
|
static std::mutex ssl_ctx_mutex_;
|
||||||
|
|
||||||
template <typename U, typename V, typename T>
|
template <typename U, typename V, typename T>
|
||||||
inline bool read_and_close_socket_ssl(
|
inline bool read_and_close_socket_ssl(
|
||||||
socket_t sock, bool keep_alive,
|
socket_t sock, bool keep_alive,
|
||||||
SSL_CTX* ctx, U SSL_connect_or_accept, V setup,
|
SSL_CTX* ctx, U SSL_connect_or_accept, V setup,
|
||||||
T callback)
|
T callback)
|
||||||
{
|
{
|
||||||
auto ssl = SSL_new(ctx);
|
SSL* ssl = nullptr;
|
||||||
if (!ssl) {
|
{
|
||||||
return false;
|
std::lock_guard<std::mutex> guard(ssl_ctx_mutex_);
|
||||||
|
|
||||||
|
ssl = SSL_new(ctx);
|
||||||
|
if (!ssl) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto bio = BIO_new_socket(sock, BIO_NOCLOSE);
|
auto bio = BIO_new_socket(sock, BIO_NOCLOSE);
|
||||||
@ -2069,8 +2077,14 @@ inline bool read_and_close_socket_ssl(
|
|||||||
}
|
}
|
||||||
|
|
||||||
SSL_shutdown(ssl);
|
SSL_shutdown(ssl);
|
||||||
SSL_free(ssl);
|
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> guard(ssl_ctx_mutex_);
|
||||||
|
SSL_free(ssl);
|
||||||
|
}
|
||||||
|
|
||||||
close_socket(sock);
|
close_socket(sock);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
14
test/test.cc
14
test/test.cc
@ -263,7 +263,7 @@ protected:
|
|||||||
res.set_content("Hello World!", "text/plain");
|
res.set_content("Hello World!", "text/plain");
|
||||||
})
|
})
|
||||||
.get("/slow", [&](const Request& /*req*/, Response& res) {
|
.get("/slow", [&](const Request& /*req*/, Response& res) {
|
||||||
msleep(3000);
|
msleep(2000);
|
||||||
res.set_content("slow", "text/plain");
|
res.set_content("slow", "text/plain");
|
||||||
})
|
})
|
||||||
.get("/remote_addr", [&](const Request& req, Response& res) {
|
.get("/remote_addr", [&](const Request& req, Response& res) {
|
||||||
@ -368,6 +368,9 @@ protected:
|
|||||||
|
|
||||||
virtual void TearDown() {
|
virtual void TearDown() {
|
||||||
svr_.stop();
|
svr_.stop();
|
||||||
|
for (auto& t: request_threads_) {
|
||||||
|
t.join();
|
||||||
|
}
|
||||||
t_.join();
|
t_.join();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -380,6 +383,7 @@ protected:
|
|||||||
Server svr_;
|
Server svr_;
|
||||||
#endif
|
#endif
|
||||||
thread t_;
|
thread t_;
|
||||||
|
std::vector<thread> request_threads_;
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_F(ServerTest, GetMethod200)
|
TEST_F(ServerTest, GetMethod200)
|
||||||
@ -736,10 +740,10 @@ TEST_F(ServerTest, GetMethodRemoteAddr)
|
|||||||
|
|
||||||
TEST_F(ServerTest, SlowRequest)
|
TEST_F(ServerTest, SlowRequest)
|
||||||
{
|
{
|
||||||
std::thread([=]() { auto res = cli_.get("/slow"); }).detach();
|
request_threads_.push_back(std::thread([=]() { auto res = cli_.get("/slow"); }));
|
||||||
std::thread([=]() { auto res = cli_.get("/slow"); }).detach();
|
request_threads_.push_back(std::thread([=]() { auto res = cli_.get("/slow"); }));
|
||||||
std::thread([=]() { auto res = cli_.get("/slow"); }).detach();
|
request_threads_.push_back(std::thread([=]() { auto res = cli_.get("/slow"); }));
|
||||||
msleep(1000);
|
msleep(100);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
|
#ifdef CPPHTTPLIB_ZLIB_SUPPORT
|
||||||
|
Loading…
x
Reference in New Issue
Block a user