mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-08-11 04:41:23 +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).
|
||||
|
||||
@@ -57,6 +57,26 @@ auto res = cli.Get("/");
|
||||
|
||||
`Client`ではなく`SSLClient`を直接使う点に注意してください。秘密鍵にパスワードがある場合は第5引数で渡せます。
|
||||
|
||||
## WebSocketクライアントの場合
|
||||
|
||||
`ws::WebSocketClient`にも同じ`PemMemory`構造体があり、`wss://`接続でクライアント証明書を使えます。
|
||||
|
||||
```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");
|
||||
}
|
||||
```
|
||||
|
||||
`ws://`(非TLS)のURLに`PemMemory`を渡した場合は黙って無視されます。ファイルパスから直接読み込むコンストラクタは用意されていないので、`SSLClient`と違いPEMをメモリ上に読み込んでから渡す必要があります。
|
||||
|
||||
## ハンドラからクライアント情報を取得する
|
||||
|
||||
ハンドラの中で、どのクライアントが接続してきたかを確認したいときは`req.peer_cert()`を使います。詳しくは[T05. サーバー側でピア証明書を参照する](../t05-peer-cert)を参照してください。
|
||||
|
||||
Reference in New Issue
Block a user