mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-08-23 10:27:16 +00:00
Document WebSocketClient/SSLClient TLS parity gaps in the READMEs
WebSocketClient::set_connection_timeout (both the time_t and chrono overloads) was missing from README-websocket.md's API reference and the timeout example, even though set_read_timeout and set_write_timeout were both listed. README.md never documented the PemMemory in-memory constructor that SSLServer and SSLClient both have, so mTLS setup only showed the file-path form. Add a "Mutual TLS (mTLS)" section covering both forms for server and client, and note that ws::WebSocketClient's wss:// constructor takes the same PemMemory struct. Also note, next to Client::set_interface, that WebSocketClient has the same method, matching the existing cross-reference for set_hostname_addr_map right below it.
This commit is contained in:
@@ -168,10 +168,13 @@ bool is_open() const;
|
|||||||
// Timeouts
|
// Timeouts
|
||||||
void set_read_timeout(time_t sec, time_t usec = 0);
|
void set_read_timeout(time_t sec, time_t usec = 0);
|
||||||
void set_write_timeout(time_t sec, time_t usec = 0);
|
void set_write_timeout(time_t sec, time_t usec = 0);
|
||||||
|
void set_connection_timeout(time_t sec, time_t usec = 0);
|
||||||
template <class Rep, class Period>
|
template <class Rep, class Period>
|
||||||
void set_read_timeout(const std::chrono::duration<Rep, Period> &duration);
|
void set_read_timeout(const std::chrono::duration<Rep, Period> &duration);
|
||||||
template <class Rep, class Period>
|
template <class Rep, class Period>
|
||||||
void set_write_timeout(const std::chrono::duration<Rep, Period> &duration);
|
void set_write_timeout(const std::chrono::duration<Rep, Period> &duration);
|
||||||
|
template <class Rep, class Period>
|
||||||
|
void set_connection_timeout(const std::chrono::duration<Rep, Period> &duration);
|
||||||
|
|
||||||
// SSL configuration (wss:// only, requires CPPHTTPLIB_OPENSSL_SUPPORT)
|
// SSL configuration (wss:// only, requires CPPHTTPLIB_OPENSSL_SUPPORT)
|
||||||
void set_ca_cert_path(const std::string &ca_cert_file_path,
|
void set_ca_cert_path(const std::string &ca_cert_file_path,
|
||||||
@@ -304,8 +307,14 @@ httplib::Headers headers = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
httplib::ws::WebSocketClient ws("ws://localhost:8080/ws", headers);
|
httplib::ws::WebSocketClient ws("ws://localhost:8080/ws", headers);
|
||||||
ws.set_read_timeout(30, 0); // 30 seconds
|
ws.set_connection_timeout(5, 0); // 5 seconds
|
||||||
ws.set_write_timeout(10, 0); // 10 seconds
|
ws.set_read_timeout(30, 0); // 30 seconds
|
||||||
|
ws.set_write_timeout(10, 0); // 10 seconds
|
||||||
|
|
||||||
|
// std::chrono is also supported
|
||||||
|
ws.set_connection_timeout(std::chrono::seconds(5));
|
||||||
|
ws.set_read_timeout(std::chrono::seconds(30));
|
||||||
|
ws.set_write_timeout(std::chrono::seconds(10));
|
||||||
|
|
||||||
if (ws.connect()) {
|
if (ws.connect()) {
|
||||||
std::string msg;
|
std::string msg;
|
||||||
|
|||||||
37
README.md
37
README.md
@@ -168,6 +168,41 @@ cli.set_server_certificate_verifier(
|
|||||||
});
|
});
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Mutual TLS (mTLS)
|
||||||
|
|
||||||
|
Regular TLS only verifies the server certificate. With mTLS, the client also presents a certificate that the server verifies.
|
||||||
|
|
||||||
|
```c++
|
||||||
|
// Server: pass a CA to verify client certificates against
|
||||||
|
httplib::SSLServer svr("./cert.pem", "./key.pem", "./client-ca-cert.pem");
|
||||||
|
|
||||||
|
// Client: present a certificate
|
||||||
|
httplib::SSLClient cli("api.example.com", 443,
|
||||||
|
"./client-cert.pem", "./client-key.pem");
|
||||||
|
```
|
||||||
|
|
||||||
|
Both `SSLServer` and `SSLClient` also accept an in-memory `PemMemory` struct instead of file paths — handy when certs come from an environment variable or a secrets manager:
|
||||||
|
|
||||||
|
```c++
|
||||||
|
httplib::SSLServer::PemMemory server_pem{};
|
||||||
|
server_pem.cert_pem = server_cert.data();
|
||||||
|
server_pem.cert_pem_len = server_cert.size();
|
||||||
|
server_pem.key_pem = server_key.data();
|
||||||
|
server_pem.key_pem_len = server_key.size();
|
||||||
|
server_pem.client_ca_pem = client_ca.data();
|
||||||
|
server_pem.client_ca_pem_len = client_ca.size();
|
||||||
|
httplib::SSLServer svr(server_pem);
|
||||||
|
|
||||||
|
httplib::SSLClient::PemMemory client_pem{};
|
||||||
|
client_pem.cert_pem = client_cert.data();
|
||||||
|
client_pem.cert_pem_len = client_cert.size();
|
||||||
|
client_pem.key_pem = client_key.data();
|
||||||
|
client_pem.key_pem_len = client_key.size();
|
||||||
|
httplib::SSLClient cli("api.example.com", 443, client_pem);
|
||||||
|
```
|
||||||
|
|
||||||
|
`httplib::ws::WebSocketClient` has the same `PemMemory` constructor for `wss://` connections. See [README-websocket.md](README-websocket.md) for details.
|
||||||
|
|
||||||
### Peer Certificate Inspection
|
### Peer Certificate Inspection
|
||||||
|
|
||||||
On the server side, you can inspect the client's peer certificate from a request handler:
|
On the server side, you can inspect the client's peer certificate from a request handler:
|
||||||
@@ -1290,6 +1325,8 @@ res->status; // 200
|
|||||||
cli.set_interface("eth0"); // Interface name, IP address or host name
|
cli.set_interface("eth0"); // Interface name, IP address or host name
|
||||||
```
|
```
|
||||||
|
|
||||||
|
The same method is available on `httplib::ws::WebSocketClient`.
|
||||||
|
|
||||||
### Override the connection target for a hostname
|
### Override the connection target for a hostname
|
||||||
|
|
||||||
`set_hostname_addr_map` redirects where the socket connects, without changing
|
`set_hostname_addr_map` redirects where the socket connects, without changing
|
||||||
|
|||||||
Reference in New Issue
Block a user