From a83dcefe8665789f8b94377749595a04962321b4 Mon Sep 17 00:00:00 2001 From: yhirose Date: Sun, 17 Dec 2017 22:23:05 -0500 Subject: [PATCH] Fixed SSL server problem with bad key.pem and cert.pem --- example/server.cc | 5 +++++ httplib.h | 37 ++++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/example/server.cc b/example/server.cc index 8106fa5..a18bfc6 100644 --- a/example/server.cc +++ b/example/server.cc @@ -73,6 +73,11 @@ int main(void) Server svr; #endif + if (!svr.is_valid()) { + printf("server has an error...\n"); + return -1; + } + svr.get("/", [=](const auto& /*req*/, auto& res) { res.set_redirect("/hi"); }); diff --git a/httplib.h b/httplib.h index ab79cc5..8630559 100644 --- a/httplib.h +++ b/httplib.h @@ -169,6 +169,8 @@ public: Server(); virtual ~Server(); + virtual bool is_valid() const; + Server& get(const char* pattern, Handler handler); Server& post(const char* pattern, Handler handler); @@ -208,6 +210,8 @@ public: Client(const char* host, int port, HttpVersion http_version = HttpVersion::v1_0); virtual ~Client(); + virtual bool is_valid() const; + std::shared_ptr get(const char* path, Progress progress = nullptr); std::shared_ptr get(const char* path, const Headers& headers, Progress progress = nullptr); @@ -256,6 +260,8 @@ public: SSLServer(const char* cert_path, const char* private_key_path); virtual ~SSLServer(); + virtual bool is_valid() const; + private: virtual bool read_and_close_socket(socket_t sock); @@ -267,6 +273,8 @@ public: SSLClient(const char* host, int port, HttpVersion http_version = HttpVersion::v1_0); virtual ~SSLClient(); + virtual bool is_valid() const; + private: virtual bool read_and_close_socket(socket_t sock, const Request& req, Response& res); @@ -1216,6 +1224,10 @@ inline void Server::set_logger(Logger logger) inline bool Server::listen(const char* host, int port, int socket_flags) { + if (!is_valid()) { + return false; + } + svr_sock_ = detail::create_server_socket(host, port, socket_flags); if (svr_sock_ == -1) { return false; @@ -1405,6 +1417,11 @@ inline void Server::process_request(Stream& strm) write_response(strm, req, res); } +inline bool Server::is_valid() const +{ + return true; +} + inline bool Server::read_and_close_socket(socket_t sock) { return detail::read_and_close_socket(sock, [this](Stream& strm) { @@ -1426,6 +1443,11 @@ inline Client::~Client() { } +inline bool Client::is_valid() const +{ + return true; +} + inline bool Client::read_response_line(Stream& strm, Response& res) { const auto bufsiz = 2048; @@ -1610,6 +1632,9 @@ template inline bool read_and_close_socket_ssl(socket_t sock, SSL_CTX* ctx, U SSL_connect_or_accept, V setup, T callback) { auto ssl = SSL_new(ctx); + if (!ssl) { + return false; + } auto bio = BIO_new_socket(sock, BIO_NOCLOSE); SSL_set_bio(ssl, bio, bio); @@ -1693,6 +1718,11 @@ inline SSLServer::~SSLServer() } } +inline bool SSLServer::is_valid() const +{ + return ctx_; +} + inline bool SSLServer::read_and_close_socket(socket_t sock) { return detail::read_and_close_socket_ssl( @@ -1719,9 +1749,14 @@ inline SSLClient::~SSLClient() } } +inline bool SSLClient::is_valid() const +{ + return ctx_; +} + inline bool SSLClient::read_and_close_socket(socket_t sock, const Request& req, Response& res) { - return detail::read_and_close_socket_ssl( + return is_valid() && detail::read_and_close_socket_ssl( sock, ctx_, SSL_connect, [&](SSL* ssl) {