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:
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<Response> Client::post(
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
namespace detail {
template <typename U, typename T>
inline bool read_and_close_socket_ssl(socket_t sock, SSL_CTX* ctx, U SSL_connect_or_accept, T callback)
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);
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,7 +1241,11 @@ 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) {
return detail::read_and_close_socket_ssl(
sock, ctx_,
SSL_accept,
[](SSL* ssl) {},
[this](Stream& strm) {
process_request(strm);
return true;
});
@ -1261,7 +1267,13 @@ 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 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);
});
}

View file

@ -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)
{