Give WebSocketClient the PemMemory client certificate constructor SSLClient has

Adds ws::WebSocketClient::PemMemory and a constructor overload that
installs an in-memory client certificate on the TLS context, enabling
mutual TLS for wss:// connections. The certificate is silently ignored
for ws:// URLs, consistent with the existing TLS-only setters such as
set_ca_cert_path().

Part of the interface alignment discussed in #2531.
This commit is contained in:
yhirose committed 2026-08-07 16:34:12 -04:00
1 parent bd02a50cbb
commit 86abc9a0ea
5 files changed
+155

No files matched your search

+28
View File
@@ -4300,6 +4300,16 @@ public:
void set_hostname_addr_map(std::map<std::string, std::string> addr_map);
#ifdef CPPHTTPLIB_SSL_ENABLED
struct PemMemory {
const char *cert_pem;
size_t cert_pem_len;
const char *key_pem;
size_t key_pem_len;
const char *private_key_password;
};
explicit WebSocketClient(const std::string &scheme_host_port_path,
const PemMemory &pem, const Headers &headers = {});
void set_ca_cert_path(const std::string &ca_cert_file_path,
const std::string &ca_cert_dir_path = std::string());
void set_ca_cert_store(tls::ca_store_t store);
@@ -21289,6 +21299,24 @@ inline WebSocketClient::WebSocketClient(
}
}
#ifdef CPPHTTPLIB_SSL_ENABLED
inline WebSocketClient::WebSocketClient(
const std::string &scheme_host_port_path, const PemMemory &pem,
const Headers &headers)
: WebSocketClient(scheme_host_port_path, headers) {
// For ws:// URLs the client certificate is silently ignored, consistent
// with the TLS-only setters such as set_ca_cert_path().
if (is_valid_ && is_ssl_ && pem.cert_pem && pem.key_pem) {
if (!tls::set_client_cert_pem(tls_ctx_, pem.cert_pem, pem.key_pem,
pem.private_key_password)) {
tls::free_context(tls_ctx_);
tls_ctx_ = nullptr;
is_valid_ = false;
}
}
}
#endif
inline WebSocketClient::~WebSocketClient() {
shutdown_and_close();
#ifdef CPPHTTPLIB_SSL_ENABLED