Browse Source

Fixed SSL server problem with bad key.pem and cert.pem

yhirose 8 years ago
parent
commit
a83dcefe86
2 changed files with 41 additions and 1 deletions
  1. 5 0
      example/server.cc
  2. 36 1
      httplib.h

+ 5 - 0
example/server.cc

@@ -73,6 +73,11 @@ int main(void)
     Server svr;
     Server svr;
 #endif
 #endif
 
 
+    if (!svr.is_valid()) {
+        printf("server has an error...\n");
+        return -1;
+    }
+
     svr.get("/", [=](const auto& /*req*/, auto& res) {
     svr.get("/", [=](const auto& /*req*/, auto& res) {
         res.set_redirect("/hi");
         res.set_redirect("/hi");
     });
     });

+ 36 - 1
httplib.h

@@ -169,6 +169,8 @@ public:
     Server();
     Server();
     virtual ~Server();
     virtual ~Server();
 
 
+    virtual bool is_valid() const;
+
     Server& get(const char* pattern, Handler handler);
     Server& get(const char* pattern, Handler handler);
     Server& post(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);
     Client(const char* host, int port, HttpVersion http_version = HttpVersion::v1_0);
     virtual ~Client();
     virtual ~Client();
 
 
+    virtual bool is_valid() const;
+
     std::shared_ptr<Response> get(const char* path, Progress progress = nullptr);
     std::shared_ptr<Response> get(const char* path, Progress progress = nullptr);
     std::shared_ptr<Response> get(const char* path, const Headers& headers, Progress progress = nullptr);
     std::shared_ptr<Response> 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);
     SSLServer(const char* cert_path, const char* private_key_path);
     virtual ~SSLServer();
     virtual ~SSLServer();
 
 
+    virtual bool is_valid() const;
+
 private:
 private:
     virtual bool read_and_close_socket(socket_t sock);
     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);
     SSLClient(const char* host, int port, HttpVersion http_version = HttpVersion::v1_0);
     virtual ~SSLClient();
     virtual ~SSLClient();
 
 
+    virtual bool is_valid() const;
+
 private:
 private:
     virtual bool read_and_close_socket(socket_t sock, const Request& req, Response& res);
     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)
 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);
     svr_sock_ = detail::create_server_socket(host, port, socket_flags);
     if (svr_sock_ == -1) {
     if (svr_sock_ == -1) {
         return false;
         return false;
@@ -1405,6 +1417,11 @@ inline void Server::process_request(Stream& strm)
     write_response(strm, req, res);
     write_response(strm, req, res);
 }
 }
 
 
+inline bool Server::is_valid() const
+{
+    return true;
+}
+
 inline bool Server::read_and_close_socket(socket_t sock)
 inline bool Server::read_and_close_socket(socket_t sock)
 {
 {
     return detail::read_and_close_socket(sock, [this](Stream& strm) {
     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)
 inline bool Client::read_response_line(Stream& strm, Response& res)
 {
 {
     const auto bufsiz = 2048;
     const auto bufsiz = 2048;
@@ -1610,6 +1632,9 @@ template <typename U, typename V, typename T>
 inline bool read_and_close_socket_ssl(socket_t sock, SSL_CTX* ctx, U SSL_connect_or_accept, V setup, T callback)
 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);
     auto ssl = SSL_new(ctx);
+    if (!ssl) {
+        return false;
+    }
 
 
     auto bio = BIO_new_socket(sock, BIO_NOCLOSE);
     auto bio = BIO_new_socket(sock, BIO_NOCLOSE);
     SSL_set_bio(ssl, bio, bio);
     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)
 inline bool SSLServer::read_and_close_socket(socket_t sock)
 {
 {
     return detail::read_and_close_socket_ssl(
     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)
 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_,
         sock, ctx_,
         SSL_connect,
         SSL_connect,
         [&](SSL* ssl) {
         [&](SSL* ssl) {