This commit is contained in:
yhirose 2017-09-07 14:24:33 -04:00
parent 5f1ff58ed2
commit 2a45bdcd3b
2 changed files with 34 additions and 12 deletions

View file

@ -175,14 +175,14 @@ public:
protected: protected:
bool process_request(Stream& strm, const Request& req, Response& res); bool process_request(Stream& strm, const Request& req, Response& res);
const std::string host_;
const int port_;
private: private:
bool read_response_line(Stream& strm, Response& res); bool read_response_line(Stream& strm, Response& res);
void add_default_headers(Request& req); void add_default_headers(Request& req);
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);
const std::string host_;
const int port_;
}; };
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
@ -1152,14 +1152,16 @@ inline std::shared_ptr<Response> Client::post(
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT #ifdef CPPHTTPLIB_OPENSSL_SUPPORT
namespace detail { namespace detail {
template <typename U, typename T> 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, 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);
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);
setup(ssl);
SSL_connect_or_accept(ssl); SSL_connect_or_accept(ssl);
SSLSocketStream strm(ssl); SSLSocketStream strm(ssl);
@ -1239,10 +1241,14 @@ inline SSLServer::~SSLServer()
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(sock, ctx_, SSL_accept, [this](Stream& strm) { return detail::read_and_close_socket_ssl(
process_request(strm); sock, ctx_,
return true; SSL_accept,
}); [](SSL* ssl) {},
[this](Stream& strm) {
process_request(strm);
return true;
});
} }
// SSL HTTP client implementation // 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) 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 detail::read_and_close_socket_ssl(
return process_request(strm, req, res); sock, ctx_,
}); SSL_connect,
[&](SSL* ssl) {
SSL_set_tlsext_host_name(ssl, host_.c_str());
},
[&](Stream& strm) {
return process_request(strm, req, res);
});
} }
#endif #endif

View file

@ -357,6 +357,16 @@ TEST_F(ServerTestWithAI_PASSIVE, GetMethod200)
EXPECT_EQ("Hello World!", res->body); 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 #ifdef _WIN32
TEST(CleanupTest, WSACleanup) TEST(CleanupTest, WSACleanup)
{ {