Skip to content

Commit 4285d33

Browse files
authored
Fix #2223 (#2224)
* Fix #2223 * Fix build error
1 parent 92b4f53 commit 4285d33

File tree

2 files changed

+58
-5
lines changed

2 files changed

+58
-5
lines changed

httplib.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10429,6 +10429,13 @@ inline void ClientImpl::set_error_logger(ErrorLogger error_logger) {
1042910429
#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
1043010430
namespace detail {
1043110431

10432+
inline bool is_ip_address(const std::string &host) {
10433+
struct in_addr addr4;
10434+
struct in6_addr addr6;
10435+
return inet_pton(AF_INET, host.c_str(), &addr4) == 1 ||
10436+
inet_pton(AF_INET6, host.c_str(), &addr6) == 1;
10437+
}
10438+
1043210439
template <typename U, typename V>
1043310440
inline SSL *ssl_new(socket_t sock, SSL_CTX *ctx, std::mutex &ctx_mutex,
1043410441
U SSL_connect_or_accept, V setup) {
@@ -11087,14 +11094,18 @@ inline bool SSLClient::initialize_ssl(Socket &socket, Error &error) {
1108711094
return true;
1108811095
},
1108911096
[&](SSL *ssl2) {
11097+
// Set SNI only if host is not IP address
11098+
if (!detail::is_ip_address(host_)) {
1109011099
#if defined(OPENSSL_IS_BORINGSSL)
11091-
SSL_set_tlsext_host_name(ssl2, host_.c_str());
11100+
SSL_set_tlsext_host_name(ssl2, host_.c_str());
1109211101
#else
11093-
// NOTE: Direct call instead of using the OpenSSL macro to suppress
11094-
// -Wold-style-cast warning
11095-
SSL_ctrl(ssl2, SSL_CTRL_SET_TLSEXT_HOSTNAME, TLSEXT_NAMETYPE_host_name,
11096-
static_cast<void *>(const_cast<char *>(host_.c_str())));
11102+
// NOTE: Direct call instead of using the OpenSSL macro to suppress
11103+
// -Wold-style-cast warning
11104+
SSL_ctrl(ssl2, SSL_CTRL_SET_TLSEXT_HOSTNAME,
11105+
TLSEXT_NAMETYPE_host_name,
11106+
static_cast<void *>(const_cast<char *>(host_.c_str())));
1109711107
#endif
11108+
}
1109811109
return true;
1109911110
});
1110011111

test/test.cc

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7366,6 +7366,48 @@ TEST(KeepAliveTest, SSLClientReconnectionPost) {
73667366
ASSERT_TRUE(result);
73677367
EXPECT_EQ(200, result->status);
73687368
}
7369+
7370+
TEST(SNI_AutoDetectionTest, SNI_Logic) {
7371+
{
7372+
SSLServer svr(SERVER_CERT_FILE, SERVER_PRIVATE_KEY_FILE);
7373+
ASSERT_TRUE(svr.is_valid());
7374+
7375+
svr.Get("/sni", [&](const Request &req, Response &res) {
7376+
std::string expected;
7377+
if (req.ssl) {
7378+
if (const char *sni =
7379+
SSL_get_servername(req.ssl, TLSEXT_NAMETYPE_host_name)) {
7380+
expected = sni;
7381+
}
7382+
}
7383+
EXPECT_EQ(expected, req.get_param_value("expected"));
7384+
res.set_content("ok", "text/plain");
7385+
});
7386+
7387+
auto listen_thread = std::thread([&svr] { svr.listen(HOST, PORT); });
7388+
auto se = detail::scope_exit([&] {
7389+
svr.stop();
7390+
listen_thread.join();
7391+
ASSERT_FALSE(svr.is_running());
7392+
});
7393+
7394+
svr.wait_until_ready();
7395+
7396+
{
7397+
SSLClient cli("localhost", PORT);
7398+
cli.enable_server_certificate_verification(false);
7399+
auto res = cli.Get("/sni?expected=localhost");
7400+
ASSERT_TRUE(res);
7401+
}
7402+
7403+
{
7404+
SSLClient cli("::1", PORT);
7405+
cli.enable_server_certificate_verification(false);
7406+
auto res = cli.Get("/sni?expected=");
7407+
ASSERT_TRUE(res);
7408+
}
7409+
}
7410+
}
73697411
#endif
73707412

73717413
TEST(ClientProblemDetectionTest, ContentProvider) {

0 commit comments

Comments
 (0)