mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-19 11:19:36 +00:00
Support HTTP proxies in WebSocketClient (#2635)
WebSocketClient gets the proxy setters Client has: set_proxy(), set_proxy_basic_auth(), set_proxy_bearer_token_auth(), set_no_proxy() and, in SSL builds, set_proxy_digest_auth(). connect() opens a CONNECT tunnel for ws:// as well as wss:// (RFC 6455 4.1) and sends the proxy credentials on the CONNECT request only. A proxy that does not answer 200 is reported as Error::ProxyConnection, with its status and headers in Result.
This commit is contained in:
1 parent
7220bdb8b2
commit
94cf1b5c9a
5 files changed
+683
-221
No files matched your search
@@ -69,6 +69,20 @@ Hostname matching is case-insensitive and uses a dot-boundary rule, so an entry
|
||||
|
||||
Malformed entries are silently dropped. Port-specific entries such as `example.com:8080` are not supported (cpp-httplib's other host-keyed APIs are also keyed on hostname only).
|
||||
|
||||
## Use a proxy for WebSocket
|
||||
|
||||
`ws::WebSocketClient` has the same proxy setters.
|
||||
|
||||
```cpp
|
||||
httplib::ws::WebSocketClient cli("wss://ws.example.com/chat");
|
||||
cli.set_proxy("proxy.internal", 8080);
|
||||
cli.set_proxy_basic_auth("user", "password");
|
||||
|
||||
auto res = cli.connect();
|
||||
```
|
||||
|
||||
For both `ws://` and `wss://`, the client opens a tunnel with the CONNECT method and then performs the WebSocket handshake through it. `set_no_proxy()` works the same way.
|
||||
|
||||
## Read proxy settings from the environment
|
||||
|
||||
cpp-httplib doesn't touch `HTTP_PROXY` / `HTTPS_PROXY` / `NO_PROXY` on its own — the config API is always explicit, the same way `set_ca_cert_path()` is. If you'd like that behavior, read the variables in your application and feed them to `set_proxy()` and `set_no_proxy()`.
|
||||
|
||||
@@ -69,6 +69,20 @@ cli.set_no_proxy({"internal.corp", "10.0.0.0/8", ".dev.local"});
|
||||
|
||||
不正な書式のエントリは黙って捨てられます。`example.com:8080`のようなポート指定エントリはサポート外です(cpp-httplibの他のホストキーAPIもホスト名のみを扱う設計のため)。
|
||||
|
||||
## WebSocketでもプロキシを使う
|
||||
|
||||
`ws::WebSocketClient`にも同じプロキシ設定があります。
|
||||
|
||||
```cpp
|
||||
httplib::ws::WebSocketClient cli("wss://ws.example.com/chat");
|
||||
cli.set_proxy("proxy.internal", 8080);
|
||||
cli.set_proxy_basic_auth("user", "password");
|
||||
|
||||
auto res = cli.connect();
|
||||
```
|
||||
|
||||
`ws://`でも`wss://`でも、CONNECTメソッドでトンネルを張ってからWebSocketのハンドシェイクを行います。`set_no_proxy()`も同じように使えます。
|
||||
|
||||
## 環境変数からプロキシ設定を読み込む
|
||||
|
||||
cpp-httplib本体は`HTTP_PROXY` / `HTTPS_PROXY` / `NO_PROXY`を読みません。`set_ca_cert_path()`と同じで、設定APIは常に明示的にしています。環境変数を反映させたい場合は、アプリ側で読んで`set_proxy()`や`set_no_proxy()`に渡してください。
|
||||
|
||||
Reference in new issue
Block a user