mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-08-22 01:55:00 +00:00
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:
@@ -57,6 +57,26 @@ auto res = cli.Get("/");
|
||||
|
||||
Note you're using `SSLClient` directly, not `Client`. If the private key has a password, pass it as the fifth argument.
|
||||
|
||||
## WebSocket clients
|
||||
|
||||
`ws::WebSocketClient` has the same `PemMemory` struct, so `wss://` connections can present a client certificate too.
|
||||
|
||||
```cpp
|
||||
httplib::ws::WebSocketClient::PemMemory pem{};
|
||||
pem.cert_pem = client_cert.data();
|
||||
pem.cert_pem_len = client_cert.size();
|
||||
pem.key_pem = client_key.data();
|
||||
pem.key_pem_len = client_key.size();
|
||||
|
||||
httplib::ws::WebSocketClient ws("wss://api.example.com/ws", pem);
|
||||
|
||||
if (ws.connect()) {
|
||||
ws.send("hello");
|
||||
}
|
||||
```
|
||||
|
||||
Passing `PemMemory` to a `ws://` (non-TLS) URL is silently ignored. There's no constructor that reads the cert files directly, so unlike `SSLClient` you always load the PEM into memory yourself before passing it in.
|
||||
|
||||
## Read client info from a handler
|
||||
|
||||
To see which client connected from inside a handler, use `req.peer_cert()`. Details in [T05. Access the peer certificate on the server](../t05-peer-cert).
|
||||
|
||||
Reference in New Issue
Block a user