Skip to content

Commit a83dcef

Browse files
committed
Fixed SSL server problem with bad key.pem and cert.pem
1 parent 95b22a9 commit a83dcef

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

example/server.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,11 @@ int main(void)
7373
Server svr;
7474
#endif
7575

76+
if (!svr.is_valid()) {
77+
printf("server has an error...\n");
78+
return -1;
79+
}
80+
7681
svr.get("/", [=](const auto& /*req*/, auto& res) {
7782
res.set_redirect("/hi");
7883
});

httplib.h

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ class Server {
169169
Server();
170170
virtual ~Server();
171171

172+
virtual bool is_valid() const;
173+
172174
Server& get(const char* pattern, Handler handler);
173175
Server& post(const char* pattern, Handler handler);
174176

@@ -208,6 +210,8 @@ class Client {
208210
Client(const char* host, int port, HttpVersion http_version = HttpVersion::v1_0);
209211
virtual ~Client();
210212

213+
virtual bool is_valid() const;
214+
211215
std::shared_ptr<Response> get(const char* path, Progress progress = nullptr);
212216
std::shared_ptr<Response> get(const char* path, const Headers& headers, Progress progress = nullptr);
213217

@@ -256,6 +260,8 @@ class SSLServer : public Server {
256260
SSLServer(const char* cert_path, const char* private_key_path);
257261
virtual ~SSLServer();
258262

263+
virtual bool is_valid() const;
264+
259265
private:
260266
virtual bool read_and_close_socket(socket_t sock);
261267

@@ -267,6 +273,8 @@ class SSLClient : public Client {
267273
SSLClient(const char* host, int port, HttpVersion http_version = HttpVersion::v1_0);
268274
virtual ~SSLClient();
269275

276+
virtual bool is_valid() const;
277+
270278
private:
271279
virtual bool read_and_close_socket(socket_t sock, const Request& req, Response& res);
272280

@@ -1216,6 +1224,10 @@ inline void Server::set_logger(Logger logger)
12161224

12171225
inline bool Server::listen(const char* host, int port, int socket_flags)
12181226
{
1227+
if (!is_valid()) {
1228+
return false;
1229+
}
1230+
12191231
svr_sock_ = detail::create_server_socket(host, port, socket_flags);
12201232
if (svr_sock_ == -1) {
12211233
return false;
@@ -1405,6 +1417,11 @@ inline void Server::process_request(Stream& strm)
14051417
write_response(strm, req, res);
14061418
}
14071419

1420+
inline bool Server::is_valid() const
1421+
{
1422+
return true;
1423+
}
1424+
14081425
inline bool Server::read_and_close_socket(socket_t sock)
14091426
{
14101427
return detail::read_and_close_socket(sock, [this](Stream& strm) {
@@ -1426,6 +1443,11 @@ inline Client::~Client()
14261443
{
14271444
}
14281445

1446+
inline bool Client::is_valid() const
1447+
{
1448+
return true;
1449+
}
1450+
14291451
inline bool Client::read_response_line(Stream& strm, Response& res)
14301452
{
14311453
const auto bufsiz = 2048;
@@ -1610,6 +1632,9 @@ template <typename U, typename V, typename T>
16101632
inline bool read_and_close_socket_ssl(socket_t sock, SSL_CTX* ctx, U SSL_connect_or_accept, V setup, T callback)
16111633
{
16121634
auto ssl = SSL_new(ctx);
1635+
if (!ssl) {
1636+
return false;
1637+
}
16131638

16141639
auto bio = BIO_new_socket(sock, BIO_NOCLOSE);
16151640
SSL_set_bio(ssl, bio, bio);
@@ -1693,6 +1718,11 @@ inline SSLServer::~SSLServer()
16931718
}
16941719
}
16951720

1721+
inline bool SSLServer::is_valid() const
1722+
{
1723+
return ctx_;
1724+
}
1725+
16961726
inline bool SSLServer::read_and_close_socket(socket_t sock)
16971727
{
16981728
return detail::read_and_close_socket_ssl(
@@ -1719,9 +1749,14 @@ inline SSLClient::~SSLClient()
17191749
}
17201750
}
17211751

1752+
inline bool SSLClient::is_valid() const
1753+
{
1754+
return ctx_;
1755+
}
1756+
17221757
inline bool SSLClient::read_and_close_socket(socket_t sock, const Request& req, Response& res)
17231758
{
1724-
return detail::read_and_close_socket_ssl(
1759+
return is_valid() && detail::read_and_close_socket_ssl(
17251760
sock, ctx_,
17261761
SSL_connect,
17271762
[&](SSL* ssl) {

0 commit comments

Comments
 (0)