Skip to content

Commit

Permalink
Fixed SSL server problem with bad key.pem and cert.pem
Browse files Browse the repository at this point in the history
  • Loading branch information
yhirose committed Dec 18, 2017
1 parent 95b22a9 commit a83dcef
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
5 changes: 5 additions & 0 deletions example/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
});
Expand Down
37 changes: 36 additions & 1 deletion httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ class Server {
Server();
virtual ~Server();

virtual bool is_valid() const;

Server& get(const char* pattern, Handler handler);
Server& post(const char* pattern, Handler handler);

Expand Down Expand Up @@ -208,6 +210,8 @@ class Client {
Client(const char* host, int port, HttpVersion http_version = HttpVersion::v1_0);
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, const Headers& headers, Progress progress = nullptr);

Expand Down Expand Up @@ -256,6 +260,8 @@ class SSLServer : public Server {
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);

Expand All @@ -267,6 +273,8 @@ class SSLClient : public Client {
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);

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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)
{
auto ssl = SSL_new(ctx);
if (!ssl) {
return false;
}

auto bio = BIO_new_socket(sock, BIO_NOCLOSE);
SSL_set_bio(ssl, bio, bio);
Expand Down Expand Up @@ -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(
Expand All @@ -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) {
Expand Down

0 comments on commit a83dcef

Please sign in to comment.