From 2a45bdcd3b2ccc92e9099d58b85ad0208d0799ab Mon Sep 17 00:00:00 2001 From: yhirose Date: Thu, 7 Sep 2017 14:24:33 -0400 Subject: [PATCH] Fixed #14 --- httplib.h | 36 ++++++++++++++++++++++++------------ test/test.cc | 10 ++++++++++ 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/httplib.h b/httplib.h index 590f279..ac630db 100644 --- a/httplib.h +++ b/httplib.h @@ -175,14 +175,14 @@ public: protected: bool process_request(Stream& strm, const Request& req, Response& res); + const std::string host_; + const int port_; + private: bool read_response_line(Stream& strm, Response& res); void add_default_headers(Request& req); virtual bool read_and_close_socket(socket_t sock, const Request& req, Response& res); - - const std::string host_; - const int port_; }; #ifdef CPPHTTPLIB_OPENSSL_SUPPORT @@ -1152,14 +1152,16 @@ inline std::shared_ptr Client::post( #ifdef CPPHTTPLIB_OPENSSL_SUPPORT namespace detail { -template -inline bool read_and_close_socket_ssl(socket_t sock, SSL_CTX* ctx, U SSL_connect_or_accept, T callback) +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); auto bio = BIO_new_socket(sock, BIO_NOCLOSE); SSL_set_bio(ssl, bio, bio); + setup(ssl); + SSL_connect_or_accept(ssl); SSLSocketStream strm(ssl); @@ -1239,10 +1241,14 @@ inline SSLServer::~SSLServer() inline bool SSLServer::read_and_close_socket(socket_t sock) { - return detail::read_and_close_socket_ssl(sock, ctx_, SSL_accept, [this](Stream& strm) { - process_request(strm); - return true; - }); + return detail::read_and_close_socket_ssl( + sock, ctx_, + SSL_accept, + [](SSL* ssl) {}, + [this](Stream& strm) { + process_request(strm); + return true; + }); } // SSL HTTP client implementation @@ -1261,9 +1267,15 @@ inline SSLClient::~SSLClient() inline bool SSLClient::read_and_close_socket(socket_t sock, const Request& req, Response& res) { - return detail::read_and_close_socket_ssl(sock, ctx_, SSL_connect, [&](Stream& strm) { - return process_request(strm, req, res); - }); + return detail::read_and_close_socket_ssl( + sock, ctx_, + SSL_connect, + [&](SSL* ssl) { + SSL_set_tlsext_host_name(ssl, host_.c_str()); + }, + [&](Stream& strm) { + return process_request(strm, req, res); + }); } #endif diff --git a/test/test.cc b/test/test.cc index 97fe0a9..98f0954 100644 --- a/test/test.cc +++ b/test/test.cc @@ -357,6 +357,16 @@ TEST_F(ServerTestWithAI_PASSIVE, GetMethod200) EXPECT_EQ("Hello World!", res->body); } +#ifdef CPPHTTPLIB_OPENSSL_SUPPORT +TEST(SSLClientTest, ServerNameIndication) +{ + SSLClient cli("httpbin.org", 443); + auto res = cli.get("/get"); + ASSERT_TRUE(res != nullptr); + ASSERT_EQ(200, res->status); +} +#endif + #ifdef _WIN32 TEST(CleanupTest, WSACleanup) {