mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-21 20:01:46 +00:00
Compare commits
64
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
907ac7c546 | ||
|
|
dac2f48003 | ||
|
|
e1bc9c0140 | ||
|
|
114e3d0e46 | ||
|
|
7e32283df3 | ||
|
|
76e1a9c4c9 | ||
|
|
0289c0f528 | ||
|
|
b99ccee3db | ||
|
|
d730627815 | ||
|
|
2a6854c935 | ||
|
|
ddaeb5d83d | ||
|
|
f92079e633 | ||
|
|
7b6af186af | ||
|
|
58bd9a1900 | ||
|
|
1b914e1597 | ||
|
|
c51417f458 | ||
|
|
a69d6a0a35 | ||
|
|
726e4583ef | ||
|
|
a6ab971a3f | ||
|
|
9458c27c7c | ||
|
|
e7a004f509 | ||
|
|
3544f47edd | ||
|
|
7800c3dc5b | ||
|
|
baba35b8d3 | ||
|
|
92435b31b3 | ||
|
|
5a956da084 | ||
|
|
cd446a4e01 | ||
|
|
eb364f1167 | ||
|
|
748dbfade6 | ||
|
|
d7b0d4bedb | ||
|
|
4af42e1907 | ||
|
|
1b40ce850b | ||
|
|
f57672b390 | ||
|
|
654fd3546c | ||
|
|
546e821c65 | ||
|
|
53a7f4964e | ||
|
|
8c5fd6aa08 | ||
|
|
a2ebab183d | ||
|
|
a39e081386 | ||
|
|
07142256ed | ||
|
|
cc7a30f12f | ||
|
|
cdcd2ac6e6 | ||
|
|
bd23a7b179 | ||
|
|
6d8dec3c3e | ||
|
|
7e5d0683ac | ||
|
|
6df971434e | ||
|
|
39c98d4671 | ||
|
|
a1ba73a256 | ||
|
|
453b1b8613 | ||
|
|
a18895f42e | ||
|
|
24e58ce5b2 | ||
|
|
a0ab09fc0a | ||
|
|
290c4c0bbe | ||
|
|
25ba40f6fe | ||
|
|
578e4815b7 | ||
|
|
252dbaa505 | ||
|
|
8710fbd25d | ||
|
|
2f8b9f6f57 | ||
|
|
391de32115 | ||
|
|
ba32b468a4 | ||
|
|
76f84a5bfd | ||
|
|
d23d1b6029 | ||
|
|
7d349cbeed | ||
|
|
5ceef83c54 |
No files matched your search
+1
-1
@@ -100,7 +100,7 @@ sse.stop();
|
|||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
bool connected = sse.is_connected();
|
bool connected = sse.is_connected();
|
||||||
const std::string &id = sse.last_event_id();
|
std::string id = sse.last_event_id();
|
||||||
```
|
```
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|||||||
+29
-4
@@ -66,7 +66,7 @@ enum ReadResult : int {
|
|||||||
|
|
||||||
Returned by `read()`. Since `Fail` is `0`, the result works naturally in boolean contexts — `while (ws.read(msg))` continues until the connection closes. When you need to distinguish text from binary, check the return value directly.
|
Returned by `read()`. Since `Fail` is `0`, the result works naturally in boolean contexts — `while (ws.read(msg))` continues until the connection closes. When you need to distinguish text from binary, check the return value directly.
|
||||||
|
|
||||||
`Timeout` is only returned for a read timeout you set yourself with `set_read_timeout()`. It means the timeout elapsed on a message boundary: nothing was consumed and the connection is still open, so you can send on it and read again. The compile-time defaults (`CPPHTTPLIB_WEBSOCKET_SERVER_READ_TIMEOUT_SECOND`, 300 seconds on the server; a client waits forever) are a backstop against a peer that has gone quiet, not a request for control: when one of them elapses, `read()` returns `Fail` and closes the connection, so code that never calls `set_read_timeout()` can keep using `while (ws.read(msg))`.
|
`Timeout` is only returned for a read timeout you set yourself with `set_read_timeout()`. It means the timeout elapsed and the connection is still open, so you can send on it and read again. It can land anywhere, inside a message too: what was read of the message is kept, and the next `read()` goes on from there. The compile-time defaults (`CPPHTTPLIB_WEBSOCKET_SERVER_READ_TIMEOUT_SECOND`, 300 seconds on the server; a client waits forever) are a backstop against a peer that has gone quiet, not a request for control: when one of them elapses, `read()` returns `Fail` and closes the connection, so code that never calls `set_read_timeout()` can keep using `while (ws.read(msg))`.
|
||||||
|
|
||||||
**`msg` is left untouched on `Timeout`.** Because `Timeout` is non-zero, `while (ws.read(msg))` keeps looping — with the *previous* message still in `msg`. Once you set a read timeout, test the result instead:
|
**`msg` is left untouched on `Timeout`.** Because `Timeout` is non-zero, `while (ws.read(msg))` keeps looping — with the *previous* message still in `msg`. Once you set a read timeout, test the result instead:
|
||||||
|
|
||||||
@@ -200,6 +200,10 @@ void close(CloseStatus status = CloseStatus::Normal,
|
|||||||
const std::string &reason = "");
|
const std::string &reason = "");
|
||||||
bool is_open() const;
|
bool is_open() const;
|
||||||
|
|
||||||
|
// Authentication - the same setters Client has (Basic wins over Bearer).
|
||||||
|
void set_basic_auth(const std::string &username, const std::string &password);
|
||||||
|
void set_bearer_token_auth(const std::string &token);
|
||||||
|
|
||||||
// Proxy - the same setters Client has. connect() opens a CONNECT tunnel
|
// Proxy - the same setters Client has. connect() opens a CONNECT tunnel
|
||||||
// through the proxy for ws:// as well as wss:// (RFC 6455 4.1); the proxy
|
// through the proxy for ws:// as well as wss:// (RFC 6455 4.1); the proxy
|
||||||
// credentials are sent only on the CONNECT request, never through the tunnel.
|
// credentials are sent only on the CONNECT request, never through the tunnel.
|
||||||
@@ -227,13 +231,14 @@ void set_connection_timeout(const std::chrono::duration<Rep, Period> &duration);
|
|||||||
void set_websocket_ping_interval(time_t sec);
|
void set_websocket_ping_interval(time_t sec);
|
||||||
void set_websocket_max_missed_pongs(int count);
|
void set_websocket_max_missed_pongs(int count);
|
||||||
|
|
||||||
// Socket - the same setters Client has
|
// Socket - the same setters Client has (TCP_NODELAY is on by default here)
|
||||||
void set_tcp_nodelay(bool on);
|
void set_tcp_nodelay(bool on);
|
||||||
void set_address_family(int family);
|
void set_address_family(int family);
|
||||||
void set_ipv6_v6only(bool on);
|
void set_ipv6_v6only(bool on);
|
||||||
void set_socket_options(SocketOptions socket_options);
|
void set_socket_options(SocketOptions socket_options);
|
||||||
void set_interface(const std::string &intf);
|
void set_interface(const std::string &intf);
|
||||||
void set_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
void set_direct_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
||||||
|
void set_tunnel_hostname_addr_map(std::map<std::string, std::string> addr_map);
|
||||||
|
|
||||||
// SSL configuration (wss:// only, SSL builds only)
|
// SSL configuration (wss:// only, SSL builds only)
|
||||||
void set_ca_cert_path(const std::string &ca_cert_file_path,
|
void set_ca_cert_path(const std::string &ca_cert_file_path,
|
||||||
@@ -247,6 +252,9 @@ void enable_system_ca(bool enabled);
|
|||||||
void set_server_certificate_verifier(tls::VerifyCallback verifier);
|
void set_server_certificate_verifier(tls::VerifyCallback verifier);
|
||||||
void set_session_verifier(
|
void set_session_verifier(
|
||||||
std::function<SSLVerifierResponse(tls::session_t)> verifier);
|
std::function<SSLVerifierResponse(tls::session_t)> verifier);
|
||||||
|
// Windows only, as on SSLClient (see "Platform-specific Certificate Handling"
|
||||||
|
// in README.md)
|
||||||
|
void enable_windows_certificate_verification(bool enabled);
|
||||||
```
|
```
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
@@ -403,6 +411,22 @@ svr.set_pre_request_handler([](const httplib::Request &req, httplib::Response &r
|
|||||||
|
|
||||||
The server accepts a handshake from any `Origin`. A browser sends its cookies with a handshake another site starts, so if cookies authenticate the connection, also check `req.get_header_value("Origin")` against your own origins in one of these handlers (cross-site WebSocket hijacking).
|
The server accepts a handshake from any `Origin`. A browser sends its cookies with a handshake another site starts, so if cookies authenticate the connection, also check `req.get_header_value("Origin")` against your own origins in one of these handlers (cross-site WebSocket hijacking).
|
||||||
|
|
||||||
|
### Authentication
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
httplib::ws::WebSocketClient ws("wss://example.com/ws");
|
||||||
|
ws.set_bearer_token_auth("token123"); // Authorization: Bearer token123
|
||||||
|
// or
|
||||||
|
ws.set_basic_auth("user", "pass"); // Authorization: Basic dXNlcjpwYXNz
|
||||||
|
|
||||||
|
if (ws.connect()) { /* ... */ }
|
||||||
|
```
|
||||||
|
|
||||||
|
The credentials are read on every `connect()`, so a new token takes effect on
|
||||||
|
the next reconnect. An `Authorization` header passed to the constructor wins
|
||||||
|
over them. Through a proxy they are sent only inside the tunnel, never on
|
||||||
|
`CONNECT`.
|
||||||
|
|
||||||
### Custom Headers and Timeouts
|
### Custom Headers and Timeouts
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
@@ -490,6 +514,7 @@ if (ws.connect()) {
|
|||||||
| `CPPHTTPLIB_WEBSOCKET_CLOSE_TIMEOUT_SECOND` | `5` | Timeout for waiting peer's Close response (seconds) |
|
| `CPPHTTPLIB_WEBSOCKET_CLOSE_TIMEOUT_SECOND` | `5` | Timeout for waiting peer's Close response (seconds) |
|
||||||
| `CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND` | `30` | Automatic Ping interval for heartbeat (seconds) |
|
| `CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND` | `30` | Automatic Ping interval for heartbeat (seconds) |
|
||||||
| `CPPHTTPLIB_WEBSOCKET_MAX_MISSED_PONGS` | `0` (disabled) | Close the connection after N consecutive unacked pings |
|
| `CPPHTTPLIB_WEBSOCKET_MAX_MISSED_PONGS` | `0` (disabled) | Close the connection after N consecutive unacked pings |
|
||||||
|
| `CPPHTTPLIB_WEBSOCKET_TCP_NODELAY` | `true` | Set TCP_NODELAY on WebSocket connections, so small messages do not wait on Nagle's algorithm |
|
||||||
|
|
||||||
### Runtime Ping Interval
|
### Runtime Ping Interval
|
||||||
|
|
||||||
@@ -547,7 +572,7 @@ Choose sizes that account for both your expected HTTP load and the maximum numbe
|
|||||||
|
|
||||||
A single `WebSocket` (server-side) or `WebSocketClient` handle is shared by three potential callers: the thread running your handler (or holding the client), the heartbeat thread, and, if your code does its own thing, a separate thread calling `send()`/`close()` while another thread is blocked in `read()`.
|
A single `WebSocket` (server-side) or `WebSocketClient` handle is shared by three potential callers: the thread running your handler (or holding the client), the heartbeat thread, and, if your code does its own thing, a separate thread calling `send()`/`close()` while another thread is blocked in `read()`.
|
||||||
|
|
||||||
**Supported**: calling `read()` from one thread while calling `send()`/`close()` from another. This is the common pattern for a client that reads incoming messages in a loop on one thread and sends from elsewhere (e.g. a UI thread). A message that is in flight when `close()` is called still arrives intact; `close()` sends the Close frame and returns, leaving the connection's read side to the thread that owns it, so it does not block waiting for the peer's Close reply in that case. The heartbeat thread's automatic pings use the same `send()` path internally, so they are safe to run concurrently with your `read()` loop too — for `wss://` this requires every TLS call on a connection to be serialized internally, which cpp-httplib does for you.
|
**Supported**: calling `read()` from one thread while calling `send()`/`close()` from another. This is the common pattern for a client that reads incoming messages in a loop on one thread and sends from elsewhere (e.g. a UI thread). A message that is in flight when `close()` is called still arrives intact; `close()` sends the Close frame and returns, leaving the connection's read side to the thread that owns it, so it does not block waiting for the peer's Close reply in that case. That thread's `read()` keeps waiting for the reply until its own read timeout (by default none on a client, 300 seconds on a server); to bound it, set one with `set_read_timeout()` before the read starts. The heartbeat thread's automatic pings use the same `send()` path internally, so they are safe to run concurrently with your `read()` loop too — for `wss://` this requires every TLS call on a connection to be serialized internally, which cpp-httplib does for you.
|
||||||
|
|
||||||
**Not supported**: calling `read()` from two threads at the same time on the same handle. The calls are serialized rather than left to corrupt each other, but which thread receives which message is unspecified, so there is nothing useful to build on it.
|
**Not supported**: calling `read()` from two threads at the same time on the same handle. The calls are serialized rather than left to corrupt each other, but which thread receives which message is unspecified, so there is nothing useful to build on it.
|
||||||
|
|
||||||
|
|||||||
@@ -76,7 +76,10 @@ cpp-httplib supports multiple TLS backends through an abstraction layer:
|
|||||||
> A wolfSSL server that verifies client certificates does not resume TLS sessions: wolfSSL shares its session cache between contexts, so a resumed session could carry a certificate checked against another server's client CA.
|
> A wolfSSL server that verifies client certificates does not resume TLS sessions: wolfSSL shares its session cache between contexts, so a resumed session could carry a certificate checked against another server's client CA.
|
||||||
|
|
||||||
> [!NOTE]
|
> [!NOTE]
|
||||||
> **BoringSSL (best-effort):** BoringSSL builds under `CPPHTTPLIB_OPENSSL_SUPPORT` and is exercised by CI against current upstream. Because BoringSSL does not guarantee API stability, support is best-effort — breakage may occasionally land. Two known behavioral differences vs OpenSSL: (1) BoringSSL's public headers require C++14 or later, so consumers must compile accordingly; (2) hostname verification is SAN-only per RFC 6125 §6.4.4 (no CN fallback).
|
> **BoringSSL (best-effort):** BoringSSL builds under `CPPHTTPLIB_OPENSSL_SUPPORT` and is exercised by CI against current upstream. Because BoringSSL does not guarantee API stability, support is best-effort — breakage may occasionally land. One known difference vs OpenSSL: BoringSSL's public headers require C++14 or later, so consumers must compile accordingly.
|
||||||
|
|
||||||
|
> [!NOTE]
|
||||||
|
> **Hostname verification is SAN-only on every backend** (RFC 9525). A host is matched against the certificate's subjectAltName entries, never its Common Name, so a certificate that names its host only in the CN is rejected. Reissue it with a subjectAltName.
|
||||||
|
|
||||||
```c++
|
```c++
|
||||||
// Use either OpenSSL, Mbed TLS, or wolfSSL
|
// Use either OpenSSL, Mbed TLS, or wolfSSL
|
||||||
@@ -240,7 +243,7 @@ cpp-httplib automatically integrates with the OS certificate store on macOS and
|
|||||||
|
|
||||||
| Platform | Behavior | Disable (compile time) |
|
| Platform | Behavior | Disable (compile time) |
|
||||||
| :------- | :------- | :--------------------- |
|
| :------- | :------- | :--------------------- |
|
||||||
| macOS | Loads system certs from Keychain (link `CoreFoundation` and `Security` with `-framework`). Requires Apple Clang; GCC is not supported for this feature. | `CPPHTTPLIB_DISABLE_MACOSX_AUTOMATIC_ROOT_CERTIFICATES` |
|
| macOS | Loads system certs from Keychain, honouring their Trust Settings: a certificate set to "Never Trust", or trusted only for another policy such as S/MIME, is not loaded (link `CoreFoundation` and `Security` with `-framework`). Requires Apple Clang; GCC is not supported for this feature. | `CPPHTTPLIB_DISABLE_MACOSX_AUTOMATIC_ROOT_CERTIFICATES` |
|
||||||
| Windows | Verifies the certificate chain with CryptoAPI (`CertGetCertificateChain` / `CertVerifyCertificateChainPolicy`) instead of the TLS backend. Revocation checking is best-effort: a revoked server certificate is rejected, while one whose revocation status cannot be determined is accepted. Windows fetches missing roots and intermediates on demand. With a custom CA, the TLS backend verifies the chain instead; with `set_server_certificate_verifier()`, both do. | `CPPHTTPLIB_DISABLE_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE` |
|
| Windows | Verifies the certificate chain with CryptoAPI (`CertGetCertificateChain` / `CertVerifyCertificateChainPolicy`) instead of the TLS backend. Revocation checking is best-effort: a revoked server certificate is rejected, while one whose revocation status cannot be determined is accepted. Windows fetches missing roots and intermediates on demand. With a custom CA, the TLS backend verifies the chain instead; with `set_server_certificate_verifier()`, both do. | `CPPHTTPLIB_DISABLE_WINDOWS_AUTOMATIC_ROOT_CERTIFICATES_UPDATE` |
|
||||||
|
|
||||||
On Windows, the CryptoAPI check can also be turned off at runtime. The TLS backend then verifies the chain instead, as on other platforms:
|
On Windows, the CryptoAPI check can also be turned off at runtime. The TLS backend then verifies the chain instead, as on other platforms:
|
||||||
@@ -249,7 +252,7 @@ On Windows, the CryptoAPI check can also be turned off at runtime. The TLS backe
|
|||||||
cli.enable_windows_certificate_verification(false);
|
cli.enable_windows_certificate_verification(false);
|
||||||
```
|
```
|
||||||
|
|
||||||
`WebSocketClient` does not use CryptoAPI; the TLS backend always verifies its chain.
|
`WebSocketClient` verifies the same way and has the same `enable_windows_certificate_verification()`.
|
||||||
|
|
||||||
> [!NOTE]
|
> [!NOTE]
|
||||||
> When using SSL, it seems impossible to avoid SIGPIPE in all cases, since on some operating systems, SIGPIPE can only be suppressed on a per-message basis, but there is no way to make the OpenSSL library do so for its internal communications. If your program needs to avoid being terminated on SIGPIPE, the only fully general way might be to set up a signal handler for SIGPIPE to handle or ignore it yourself.
|
> When using SSL, it seems impossible to avoid SIGPIPE in all cases, since on some operating systems, SIGPIPE can only be suppressed on a per-message basis, but there is no way to make the OpenSSL library do so for its internal communications. If your program needs to avoid being terminated on SIGPIPE, the only fully general way might be to set up a signal handler for SIGPIPE to handle or ignore it yourself.
|
||||||
@@ -406,7 +409,7 @@ ret = svr.remove_mount_point("/");
|
|||||||
ret = svr.remove_mount_point("/public");
|
ret = svr.remove_mount_point("/public");
|
||||||
```
|
```
|
||||||
|
|
||||||
A mount point serves a path only when it has no `.`, `..` or empty segment other than a trailing `/`, and no segment ending in `.` or a space, so a check on `req.path` in `set_pre_routing_handler()` sees the path of the file that is served. A path ending in `/`, and the mount point itself such as `/public`, serve the `index.html` in that directory. The file system can still give a file other names: another case on a case-insensitive file system, such as the default ones on macOS and Windows, and on Windows an 8.3 short name such as `PRIVAT~1`. Compare paths without regard to case on such a file system, and on Windows keep files that need a guard out of mounted directories.
|
A mount point serves a path only when it has no `.`, `..` or empty segment other than a trailing `/`, no segment ending in `.` or a space and, on Windows, no `:` (it names an NTFS stream), so a check on `req.path` in `set_pre_routing_handler()` sees the path of the file that is served. A path ending in `/`, and the mount point itself such as `/public`, serve the `index.html` in that directory. The file system can still give a file other names: another case on a case-insensitive file system, such as the default ones on macOS and Windows, and on Windows an 8.3 short name such as `PRIVAT~1`. Compare paths without regard to case on such a file system, and on Windows keep files that need a guard out of mounted directories.
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
// User defined file extension and MIME type mappings
|
// User defined file extension and MIME type mappings
|
||||||
@@ -739,7 +742,7 @@ svr.Post("/multipart", [&](const Request& req, Response& res) {
|
|||||||
auto safe = httplib::sanitize_filename(file.filename);
|
auto safe = httplib::sanitize_filename(file.filename);
|
||||||
```
|
```
|
||||||
|
|
||||||
This function keeps only the part after the last `/` or `\`, removes null bytes, and trims leading and trailing spaces and tabs. It returns an empty string if what is left is empty, `.` or `..`.
|
This function keeps only the part after the last `/` or `\` (or, on Windows, `:`, which starts a drive prefix such as `C:` or an NTFS stream name), removes null bytes, and trims leading and trailing spaces and tabs. It returns an empty string if what is left is empty, `.` or `..`.
|
||||||
|
|
||||||
> [!NOTE]
|
> [!NOTE]
|
||||||
> `name` and `filename` are read as quoted-strings (RFC 9110 §5.6.4), where `\"` and `\\` stand for `"` and `\`. Browsers send a backslash as it is, so from a browser, a value with `\\` in it arrives with a single `\`, and one that ends in `\` is not read correctly.
|
> `name` and `filename` are read as quoted-strings (RFC 9110 §5.6.4), where `\"` and `\\` stand for `"` and `\`. Browsers send a backslash as it is, so from a browser, a value with `\\` in it arrives with a single `\`, and one that ends in `\` is not read correctly.
|
||||||
@@ -916,6 +919,23 @@ svr.set_keep_alive_max_count(2); // Default is 100, 0 for no limit
|
|||||||
svr.set_keep_alive_timeout(10); // Default is 5
|
svr.set_keep_alive_timeout(10); // Default is 5
|
||||||
```
|
```
|
||||||
|
|
||||||
|
#### Keep-alive parking
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
svr.set_keep_alive_parking(true); // Call it before listen()
|
||||||
|
```
|
||||||
|
|
||||||
|
By default, a kept-alive connection holds a worker thread while it waits for its next request, so connections beyond the thread count wait for one to be closed. With keep-alive parking, a connection waiting for its next request is handed to an event loop instead, and goes back to a worker when the request arrives. An idle connection then costs a socket and a few kilobytes instead of a thread, and connections that outnumber the threads take turns.
|
||||||
|
|
||||||
|
With it:
|
||||||
|
|
||||||
|
- Consecutive requests on a connection may run on different threads.
|
||||||
|
- When the [max queued requests](#max-queued-requests) limit is reached, only a connection that has not been served yet (or, with `SSLServer`, has not finished its TLS handshake) is closed. An established one waits for room.
|
||||||
|
- Handing the connection over to a worker adds roughly 10-20 µs to each request. It only shows when the client is as near as loopback and the handlers are very light: in our measurements, 2 connections on 2 threads served about a third fewer requests per second over loopback, but only a few percent fewer with a 250 µs round trip.
|
||||||
|
- A handler, a content provider and a WebSocket still hold their thread while they run.
|
||||||
|
- A subclass that overrides the private `process_and_close_socket()` is bypassed.
|
||||||
|
- `SSLServer` runs the TLS handshake on a worker when the client starts it. Until then the connection waits in the event loop and is closed at the keep-alive timeout, not the read timeout, and `ssl_last_error()` is not set for it.
|
||||||
|
|
||||||
### Client address behind a reverse proxy
|
### Client address behind a reverse proxy
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
@@ -959,7 +979,7 @@ Please see [Server example](https://github.com/yhirose/cpp-httplib/blob/master/e
|
|||||||
|
|
||||||
When a new task arrives and the queued tasks outnumber the idle threads, a temporary thread is spawned (up to the maximum). A dynamic thread exits automatically once it has waited for the idle timeout without receiving a task. The idle timeout defaults to 3 seconds, configurable via `CPPHTTPLIB_THREAD_POOL_IDLE_TIMEOUT`.
|
When a new task arrives and the queued tasks outnumber the idle threads, a temporary thread is spawned (up to the maximum). A dynamic thread exits automatically once it has waited for the idle timeout without receiving a task. The idle timeout defaults to 3 seconds, configurable via `CPPHTTPLIB_THREAD_POOL_IDLE_TIMEOUT`.
|
||||||
|
|
||||||
A connection holds its thread while it is served: between requests up to the keep-alive timeout, through a request body sent slowly but within the read timeout, and for the whole life of a WebSocket. Enough of them leave no thread for anyone else, so in front of untrusted clients, put a reverse proxy that limits connections per client, and reject unwanted WebSocket upgrades in the pre-request handler.
|
A connection holds its thread while it is served: between requests up to the keep-alive timeout (unless [keep-alive parking](#keep-alive-parking) is on), through a request body sent slowly but within the read timeout, and for the whole life of a WebSocket. Enough of them leave no thread for anyone else, so in front of untrusted clients, put a reverse proxy that limits connections per client, and reject unwanted WebSocket upgrades in the pre-request handler.
|
||||||
|
|
||||||
If you want to set the thread counts at runtime:
|
If you want to set the thread counts at runtime:
|
||||||
|
|
||||||
@@ -1169,7 +1189,7 @@ cli.set_default_headers({
|
|||||||
auto res = cli.Get("/hi");
|
auto res = cli.Get("/hi");
|
||||||
```
|
```
|
||||||
|
|
||||||
A `Content-Length` among these headers is sent in place of the one the library would send, so it has to match the body as written. A `Transfer-Encoding` is sent in addition to the library's own framing, so leave it out. Leave both out when passing on the headers of another message, as a proxy does.
|
A `Content-Length` among these headers is sent in place of the one the library would send, so it has to match the body as written. One passed with the request is dropped when the library compresses the body or sends it chunked. A `Transfer-Encoding` is sent in addition to the library's own framing, so leave it out. Leave both out when passing on the headers of another message, as a proxy does.
|
||||||
|
|
||||||
### POST
|
### POST
|
||||||
|
|
||||||
@@ -1474,17 +1494,17 @@ 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_direct_hostname_addr_map` redirects where the socket connects, without
|
||||||
the identity of the request. The hostname the client was constructed with keeps
|
changing the identity of the request. The hostname the client was constructed
|
||||||
supplying the `Host` header, the SNI, and the name that the server certificate
|
with keeps supplying the `Host` header, the SNI, and the name that the server
|
||||||
is verified against, so this is a connection-level override only, not a way to
|
certificate is verified against, so this is a connection-level override only,
|
||||||
talk to a different origin.
|
not a way to talk to a different origin.
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
httplib::Client cli("https://example.com");
|
httplib::Client cli("https://example.com");
|
||||||
|
|
||||||
// Connect to this IP address instead of resolving "example.com"
|
// Connect to this IP address instead of resolving "example.com"
|
||||||
cli.set_hostname_addr_map({{"example.com", "192.168.1.10"}});
|
cli.set_direct_hostname_addr_map({{"example.com", "192.168.1.10"}});
|
||||||
```
|
```
|
||||||
|
|
||||||
A mapped value may be an IP literal or another hostname. An IP literal is used
|
A mapped value may be an IP literal or another hostname. An IP literal is used
|
||||||
@@ -1492,13 +1512,37 @@ as-is; anything else is resolved as a name, so a host that is only reachable
|
|||||||
under a different name works too:
|
under a different name works too:
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
cli.set_hostname_addr_map({{"example.com", "internal.example.lan"}});
|
cli.set_direct_hostname_addr_map({{"example.com", "internal.example.lan"}});
|
||||||
```
|
```
|
||||||
|
|
||||||
An empty value is ignored, leaving the original hostname as the connection
|
An empty value is ignored, leaving the original hostname as the connection
|
||||||
target.
|
target.
|
||||||
|
|
||||||
The same method is available on `httplib::ws::WebSocketClient`.
|
The direct map applies only when the client connects to the origin itself.
|
||||||
|
Through a proxy, use `set_tunnel_hostname_addr_map`: its value becomes the
|
||||||
|
`CONNECT` target (and that request's `Host`) that the proxy is asked to connect
|
||||||
|
to (HTTPS and WebSocket). The `Host` header, the SNI and the certificate check
|
||||||
|
inside the tunnel keep the original hostname:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
httplib::Client cli("https://sni.example");
|
||||||
|
|
||||||
|
cli.set_tunnel_hostname_addr_map({{"sni.example", "server.example"}});
|
||||||
|
cli.set_proxy("proxy.example", 3128);
|
||||||
|
// CONNECT server.example:443, SNI sni.example
|
||||||
|
```
|
||||||
|
|
||||||
|
The two maps are independent; to use the same target on both paths, set both.
|
||||||
|
They correspond to curl's `--resolve` and `--connect-to`.
|
||||||
|
|
||||||
|
Neither map applies to the proxy's own address ("direct" means not through a
|
||||||
|
proxy, as in a PAC file's `DIRECT`). To connect to the proxy at a fixed
|
||||||
|
address, pass an IP literal to `set_proxy()`.
|
||||||
|
|
||||||
|
`set_hostname_addr_map` is deprecated and works the same as
|
||||||
|
`set_direct_hostname_addr_map`.
|
||||||
|
|
||||||
|
These methods are also available on `httplib::ws::WebSocketClient`.
|
||||||
|
|
||||||
### Automatic Path Encoding
|
### Automatic Path Encoding
|
||||||
|
|
||||||
@@ -1689,6 +1733,8 @@ cli.set_address_family(AF_UNIX);
|
|||||||
|
|
||||||
This library automatically sets the Host header to "localhost" for Unix socket connections, similar to curl's behavior.
|
This library automatically sets the Host header to "localhost" for Unix socket connections, similar to curl's behavior.
|
||||||
|
|
||||||
|
With `set_follow_location(true)`, a redirect is followed over the same socket, whatever host the `Location` names, as curl's `--unix-socket` does. A host mapped with `set_direct_hostname_addr_map` goes to its mapped socket instead. Only a `Location` naming `localhost` counts as the same host; any other host is treated as cross-host, so `Authorization` and `Cookie` are not sent to it.
|
||||||
|
|
||||||
|
|
||||||
URI Encoding/Decoding Utilities
|
URI Encoding/Decoding Utilities
|
||||||
-------------------------------
|
-------------------------------
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ langs = ["en", "ja"]
|
|||||||
|
|
||||||
[site]
|
[site]
|
||||||
title = "cpp-httplib"
|
title = "cpp-httplib"
|
||||||
version = "0.63.0"
|
version = "0.67.0"
|
||||||
hostname = "https://yhirose.github.io"
|
hostname = "https://yhirose.github.io"
|
||||||
base_path = "/cpp-httplib"
|
base_path = "/cpp-httplib"
|
||||||
footer_message = "© 2026 Yuji Hirose. All rights reserved."
|
footer_message = "© 2026 Yuji Hirose. All rights reserved."
|
||||||
|
|||||||
@@ -56,4 +56,4 @@ When you call `stop()`, new connections are refused, but handlers that are alrea
|
|||||||
|
|
||||||
> **Warning:** There's a wait between calling `stop()` and `listen()` returning — it's the time in-flight requests take to finish. To enforce a timeout, you'll need to add your own shutdown timer in application code.
|
> **Warning:** There's a wait between calling `stop()` and `listen()` returning — it's the time in-flight requests take to finish. To enforce a timeout, you'll need to add your own shutdown timer in application code.
|
||||||
|
|
||||||
> **Note:** `stop()` doesn't interrupt WebSocket handlers. A handler looping on `read()` keeps running as long as its peer stays connected, and `listen()` doesn't return until it ends. Keep track of open WebSocket connections and `close()` them on shutdown, e.g. with `CloseStatus::GoingAway`.
|
> **Note:** `stop()` doesn't interrupt WebSocket handlers. A handler looping on `read()` keeps running as long as its peer stays connected, and `listen()` doesn't return until it ends. Keep track of open WebSocket connections and `close()` them on shutdown, e.g. with `CloseStatus::GoingAway`. The handler's `read()` then waits for the peer's Close reply up to its read timeout (300 seconds by default). To shorten that, call `set_read_timeout()` in the handler before the loop; `read()` then also returns `Timeout` whenever the peer stays idle that long, so the loop has to handle it.
|
||||||
@@ -34,7 +34,7 @@ svr.set_keep_alive_timeout(10s);
|
|||||||
## Tuning ideas
|
## Tuning ideas
|
||||||
|
|
||||||
**Too many idle connections eating resources**
|
**Too many idle connections eating resources**
|
||||||
Shorten the timeout so idle connections drop and release their worker threads.
|
Shorten the timeout so idle connections drop and release their worker threads. If you want to keep them open, turn on keep-alive parking instead (see below).
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
svr.set_keep_alive_timeout(2s);
|
svr.set_keep_alive_timeout(2s);
|
||||||
@@ -52,6 +52,22 @@ Set `set_keep_alive_max_count(1)` and every request gets its own connection. Mos
|
|||||||
|
|
||||||
## Relationship with the thread pool
|
## Relationship with the thread pool
|
||||||
|
|
||||||
A Keep-Alive connection holds a worker thread for its entire lifetime. The pool adds threads under load, up to its maximum thread count (4× the base count by default). If open connections outnumber that maximum, new connections wait in the queue until one closes. For thread counts, see [S21. Configure the thread pool](../s21-thread-pool).
|
By default, a Keep-Alive connection holds a worker thread for its entire lifetime. The pool adds threads under load, up to its maximum thread count (4× the base count by default). If open connections outnumber that maximum, new connections wait in the queue until one closes. For thread counts, see [S21. Configure the thread pool](../s21-thread-pool).
|
||||||
|
|
||||||
|
## Keep idle connections off the threads
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
svr.set_keep_alive_parking(true); // call it before listen()
|
||||||
|
```
|
||||||
|
|
||||||
|
With keep-alive parking, a connection waiting for its next request is handed to an event loop instead of keeping its worker thread, and goes back to a worker when the request arrives. An idle connection then costs a socket and some memory (a few kilobytes, more with TLS) instead of a thread, and connections that outnumber the threads take turns instead of waiting for one to close. `SSLServer` supports it too.
|
||||||
|
|
||||||
|
What changes with it:
|
||||||
|
|
||||||
|
- Consecutive requests on one connection may run on different threads, so don't keep per-connection state in `thread_local` variables.
|
||||||
|
- Handing the connection over to a worker adds roughly 10-20 µs to each request. It only shows with very light handlers and a client as near as loopback.
|
||||||
|
- A handler, a content provider and a WebSocket still hold their thread while they run.
|
||||||
|
|
||||||
|
The full list is in the [README](https://github.com/yhirose/cpp-httplib/blob/master/README.md#keep-alive-parking).
|
||||||
|
|
||||||
> **Note:** For the client side, see [C14. Understand connection reuse and Keep-Alive behavior](../c14-keep-alive). Even when the server closes the connection on timeout, the client reconnects automatically.
|
> **Note:** For the client side, see [C14. Understand connection reuse and Keep-Alive behavior](../c14-keep-alive). Even when the server closes the connection on timeout, the client reconnects automatically.
|
||||||
@@ -42,7 +42,7 @@ ws.set_write_timeout(10s);
|
|||||||
|
|
||||||
## What the read timeout means
|
## What the read timeout means
|
||||||
|
|
||||||
`set_read_timeout()` applies to a single `read()` call. If no message arrives within that time, `read()` returns `ReadResult::Timeout`: **the connection is still open** and nothing was consumed, so you can send on it and read again. That is what separates it from `ReadResult::Fail`, which means the connection is gone.
|
`set_read_timeout()` applies to a single `read()` call. If no whole message arrives within that time, `read()` returns `ReadResult::Timeout`: **the connection is still open**, and what was read of a message is kept for the next `read()`, so you can send on it and read again. That is what separates it from `ReadResult::Fail`, which means the connection is gone.
|
||||||
|
|
||||||
This is what lets one thread own a connection in both directions:
|
This is what lets one thread own a connection in both directions:
|
||||||
|
|
||||||
@@ -67,7 +67,7 @@ Without a read timeout, `read()` blocks until a message arrives, so the thread h
|
|||||||
Two things to know about `Timeout`:
|
Two things to know about `Timeout`:
|
||||||
|
|
||||||
- It leaves `msg` untouched, and it is non-zero. So `while (ws.read(msg))` is not usable once a read timeout is set — the loop would keep running with the *previous* message still in `msg`.
|
- It leaves `msg` untouched, and it is non-zero. So `while (ws.read(msg))` is not usable once a read timeout is set — the loop would keep running with the *previous* message still in `msg`.
|
||||||
- It is only reported on a message boundary. If the timeout elapses partway through a fragmented message, that message cannot be resumed and `read()` returns `Fail`.
|
- It can land anywhere, partway through a message too. What was read of the message is kept, and the next `read()` goes on from there, so a message that arrives slowly or in fragments still comes out whole.
|
||||||
|
|
||||||
For connections where long idle periods are normal — waiting on notifications, for example — either leave the read timeout unset, or treat `Timeout` as the no-op it is and keep looping.
|
For connections where long idle periods are normal — waiting on notifications, for example — either leave the read timeout unset, or treat `Timeout` as the no-op it is and keep looping.
|
||||||
|
|
||||||
|
|||||||
@@ -214,7 +214,7 @@ svr.listen("/tmp/httplib.sock", 80); // The port is unused, but must not be 0
|
|||||||
// Client
|
// Client
|
||||||
httplib::Client cli("http://localhost");
|
httplib::Client cli("http://localhost");
|
||||||
cli.set_address_family(AF_UNIX);
|
cli.set_address_family(AF_UNIX);
|
||||||
cli.set_hostname_addr_map({{"localhost", "/tmp/httplib.sock"}});
|
cli.set_direct_hostname_addr_map({{"localhost", "/tmp/httplib.sock"}});
|
||||||
|
|
||||||
auto res = cli.Get("/");
|
auto res = cli.Get("/");
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -56,4 +56,4 @@ int main() {
|
|||||||
|
|
||||||
> **Warning:** `stop()`を呼んでから`listen()`が戻るまでには、処理中のリクエストが終わるのを待つ時間がかかります。タイムアウトを強制したい場合は、シャットダウン用のタイマーを別途用意するなど、アプリケーション側の工夫が必要です。
|
> **Warning:** `stop()`を呼んでから`listen()`が戻るまでには、処理中のリクエストが終わるのを待つ時間がかかります。タイムアウトを強制したい場合は、シャットダウン用のタイマーを別途用意するなど、アプリケーション側の工夫が必要です。
|
||||||
|
|
||||||
> **Note:** `stop()`はWebSocketハンドラを中断しません。`read()`をループで回しているハンドラは相手がつながっている限り動き続け、それが終わるまで`listen()`も戻りません。開いているWebSocket接続を記録しておき、シャットダウン時に`close()`(`CloseStatus::GoingAway`など)で閉じてください。
|
> **Note:** `stop()`はWebSocketハンドラを中断しません。`read()`をループで回しているハンドラは相手がつながっている限り動き続け、それが終わるまで`listen()`も戻りません。開いているWebSocket接続を記録しておき、シャットダウン時に`close()`(`CloseStatus::GoingAway`など)で閉じてください。そのあとハンドラの`read()`は、相手のClose応答を読み込みタイムアウト(既定は300秒)まで待ちます。短くしたい場合は、ループの前に`set_read_timeout()`を呼んでください。その場合、相手が黙っている間は`read()`が`Timeout`を返すので、ループ側で扱う必要があります。
|
||||||
@@ -34,7 +34,7 @@ svr.set_keep_alive_timeout(10s);
|
|||||||
## チューニングの目安
|
## チューニングの目安
|
||||||
|
|
||||||
**アイドル接続が多くてリソースを食う**
|
**アイドル接続が多くてリソースを食う**
|
||||||
タイムアウトを短めに設定すると、遊んでいる接続がすぐに切れてスレッドが解放されます。
|
タイムアウトを短めに設定すると、遊んでいる接続がすぐに切れてスレッドが解放されます。接続は残したままにしたい場合は、代わりにKeep-Aliveのパーキング(後述)を有効にします。
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
svr.set_keep_alive_timeout(2s);
|
svr.set_keep_alive_timeout(2s);
|
||||||
@@ -52,6 +52,22 @@ svr.set_keep_alive_max_count(1000);
|
|||||||
|
|
||||||
## スレッドプールとの関係
|
## スレッドプールとの関係
|
||||||
|
|
||||||
Keep-Aliveでつながりっぱなしの接続は、その間ずっとワーカースレッドを1つ占有します。スレッドプールは負荷に応じて最大スレッド数(デフォルトはベースの4倍)までスレッドを増やしますが、開いている接続数がそれを超えると、新しい接続はどれかが閉じるまでキューで待たされます。スレッド数の調整は[S21. マルチスレッド数を設定する](../s21-thread-pool)を参照してください。
|
デフォルトでは、Keep-Aliveでつながりっぱなしの接続は、その間ずっとワーカースレッドを1つ占有します。スレッドプールは負荷に応じて最大スレッド数(デフォルトはベースの4倍)までスレッドを増やしますが、開いている接続数がそれを超えると、新しい接続はどれかが閉じるまでキューで待たされます。スレッド数の調整は[S21. マルチスレッド数を設定する](../s21-thread-pool)を参照してください。
|
||||||
|
|
||||||
|
## アイドル接続にスレッドを持たせない
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
svr.set_keep_alive_parking(true); // listen()の前に呼ぶ
|
||||||
|
```
|
||||||
|
|
||||||
|
Keep-Aliveのパーキングを有効にすると、次のリクエストを待っている接続はワーカースレッドを手放してイベントループに預けられ、リクエストが届くとまたワーカーに戻ります。アイドル接続のコストはスレッド1つではなく、ソケットと少しのメモリ(数KB、TLSではもう少し多い)になり、スレッド数より多い接続も、どれかが閉じるのを待たずに順番に処理されます。`SSLServer`でも使えます。
|
||||||
|
|
||||||
|
有効にすると変わること:
|
||||||
|
|
||||||
|
- 同じ接続の連続するリクエストが、別々のスレッドで処理されることがあります。接続ごとの状態を`thread_local`変数に持たせないでください。
|
||||||
|
- ワーカーへの接続の受け渡しで、1リクエストあたり10〜20µsほど時間が増えます。目立つのは、ハンドラがとても軽く、クライアントがループバック並みに近いときだけです。
|
||||||
|
- ハンドラ、コンテンツプロバイダ、WebSocketは、実行中は今までどおりスレッドを占有します。
|
||||||
|
|
||||||
|
すべての変更点は[README](https://github.com/yhirose/cpp-httplib/blob/master/README.md#keep-alive-parking)にあります。
|
||||||
|
|
||||||
> **Note:** クライアント側の挙動は[C14. 接続の再利用とKeep-Aliveの挙動を理解する](../c14-keep-alive)を参照してください。サーバーがタイムアウトで接続を切っても、クライアントは自動で再接続します。
|
> **Note:** クライアント側の挙動は[C14. 接続の再利用とKeep-Aliveの挙動を理解する](../c14-keep-alive)を参照してください。サーバーがタイムアウトで接続を切っても、クライアントは自動で再接続します。
|
||||||
@@ -42,7 +42,7 @@ ws.set_write_timeout(10s);
|
|||||||
|
|
||||||
## 読み取りタイムアウトの意味
|
## 読み取りタイムアウトの意味
|
||||||
|
|
||||||
`set_read_timeout()`は「1回の`read()`呼び出し」に対するタイムアウトです。メッセージが届かないまま指定時間が経過すると`read()`は`ReadResult::Timeout`を返します。このとき**接続は開いたまま**で、1バイトも読み進めていないので、そのまま送信して読み直せます。接続が失われたことを意味する`ReadResult::Fail`とはここが違います。
|
`set_read_timeout()`は「1回の`read()`呼び出し」に対するタイムアウトです。メッセージがそろわないまま指定時間が経過すると`read()`は`ReadResult::Timeout`を返します。このとき**接続は開いたまま**で、途中まで読んだ分は次の`read()`に引き継がれるので、そのまま送信して読み直せます。接続が失われたことを意味する`ReadResult::Fail`とはここが違います。
|
||||||
|
|
||||||
1本の接続を1つのスレッドで双方向に扱えるのはこのためです。
|
1本の接続を1つのスレッドで双方向に扱えるのはこのためです。
|
||||||
|
|
||||||
@@ -67,7 +67,7 @@ while (ws.is_open()) {
|
|||||||
`Timeout`について2点あります。
|
`Timeout`について2点あります。
|
||||||
|
|
||||||
- `msg`は書き換えられません。値も0以外なので、読み取りタイムアウトを設定した状態で`while (ws.read(msg))`と書くと、**前回のメッセージ**が`msg`に残ったままループが回り続けます。
|
- `msg`は書き換えられません。値も0以外なので、読み取りタイムアウトを設定した状態で`while (ws.read(msg))`と書くと、**前回のメッセージ**が`msg`に残ったままループが回り続けます。
|
||||||
- 報告されるのはメッセージの境界だけです。分割されたメッセージの途中でタイムアウトした場合、そのメッセージは再開できないので`read()`は`Fail`を返します。
|
- メッセージの途中でも報告されます。それまでに読んだ分は残り、次の`read()`はその続きから読むので、ゆっくり届くメッセージや分割されたメッセージも、そろった形で受け取れます。
|
||||||
|
|
||||||
通知の待受のように長時間メッセージが来ないことが正常な接続では、読み取りタイムアウトを設定しないままにするか、`Timeout`を「まだ何も来ていない」印として扱ってループを続けてください。
|
通知の待受のように長時間メッセージが来ないことが正常な接続では、読み取りタイムアウトを設定しないままにするか、`Timeout`を「まだ何も来ていない」印として扱ってループを続けてください。
|
||||||
|
|
||||||
|
|||||||
@@ -214,7 +214,7 @@ svr.listen("/tmp/httplib.sock", 80); // ポート番号は使われない(0
|
|||||||
// クライアント
|
// クライアント
|
||||||
httplib::Client cli("http://localhost");
|
httplib::Client cli("http://localhost");
|
||||||
cli.set_address_family(AF_UNIX);
|
cli.set_address_family(AF_UNIX);
|
||||||
cli.set_hostname_addr_map({{"localhost", "/tmp/httplib.sock"}});
|
cli.set_direct_hostname_addr_map({{"localhost", "/tmp/httplib.sock"}});
|
||||||
|
|
||||||
auto res = cli.Get("/");
|
auto res = cli.Get("/");
|
||||||
```
|
```
|
||||||
|
|||||||
+10
-7
@@ -37,11 +37,6 @@ file(
|
|||||||
)
|
)
|
||||||
|
|
||||||
if(HTTPLIB_IS_USING_OPENSSL)
|
if(HTTPLIB_IS_USING_OPENSSL)
|
||||||
if (OPENSSL_VERSION VERSION_LESS "3.2.0")
|
|
||||||
set(OPENSSL_X509_FLAG "-x509")
|
|
||||||
else()
|
|
||||||
set(OPENSSL_X509_FLAG "-x509v1")
|
|
||||||
endif()
|
|
||||||
find_program(OPENSSL_COMMAND
|
find_program(OPENSSL_COMMAND
|
||||||
NAMES openssl
|
NAMES openssl
|
||||||
PATHS ${OPENSSL_INCLUDE_DIR}/../bin
|
PATHS ${OPENSSL_INCLUDE_DIR}/../bin
|
||||||
@@ -61,7 +56,7 @@ if(HTTPLIB_IS_USING_OPENSSL)
|
|||||||
COMMAND_ERROR_IS_FATAL ANY
|
COMMAND_ERROR_IS_FATAL ANY
|
||||||
)
|
)
|
||||||
execute_process(
|
execute_process(
|
||||||
COMMAND ${OPENSSL_COMMAND} req ${OPENSSL_X509_FLAG} -new -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key key.pem -sha256 -days 3650 -nodes -out cert2.pem -extensions SAN
|
COMMAND ${OPENSSL_COMMAND} req -x509 -new -config ${CMAKE_CURRENT_LIST_DIR}/test.conf -key key.pem -sha256 -days 3650 -nodes -out cert2.pem -extensions SAN
|
||||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||||
COMMAND_ERROR_IS_FATAL ANY
|
COMMAND_ERROR_IS_FATAL ANY
|
||||||
)
|
)
|
||||||
@@ -72,7 +67,7 @@ if(HTTPLIB_IS_USING_OPENSSL)
|
|||||||
COMMAND_ERROR_IS_FATAL ANY
|
COMMAND_ERROR_IS_FATAL ANY
|
||||||
)
|
)
|
||||||
execute_process(
|
execute_process(
|
||||||
COMMAND ${OPENSSL_COMMAND} req ${OPENSSL_X509_FLAG} -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.rootCA.conf -key rootCA.key.pem -days 1024
|
COMMAND ${OPENSSL_COMMAND} req -x509 -new -batch -config ${CMAKE_CURRENT_LIST_DIR}/test.rootCA.conf -key rootCA.key.pem -days 1024
|
||||||
OUTPUT_FILE rootCA.cert.pem
|
OUTPUT_FILE rootCA.cert.pem
|
||||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||||
COMMAND_ERROR_IS_FATAL ANY
|
COMMAND_ERROR_IS_FATAL ANY
|
||||||
@@ -153,6 +148,14 @@ if(HTTPLIB_IS_USING_OPENSSL)
|
|||||||
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||||
COMMAND_ERROR_IS_FATAL ANY
|
COMMAND_ERROR_IS_FATAL ANY
|
||||||
)
|
)
|
||||||
|
# cert_dns_cn.pem: CN is the DNS name the tests connect to, with NO
|
||||||
|
# subjectAltName. A host is named by a SAN only (RFC 9525),
|
||||||
|
# so verifying it against this cert must fail.
|
||||||
|
execute_process(
|
||||||
|
COMMAND ${OPENSSL_COMMAND} req -x509 -key key.pem -sha256 -days 3650 -nodes -subj /CN=localhost -out cert_dns_cn.pem
|
||||||
|
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
|
||||||
|
COMMAND_ERROR_IS_FATAL ANY
|
||||||
|
)
|
||||||
# cert_wildcard_san.pem: a leftmost wildcard next to one that is not
|
# cert_wildcard_san.pem: a leftmost wildcard next to one that is not
|
||||||
# leftmost. "*.leftmost.example.test" matches a single
|
# leftmost. "*.leftmost.example.test" matches a single
|
||||||
# label; the wildcard in "www.*.example.test" must not be
|
# label; the wildcard in "www.*.example.test" must not be
|
||||||
|
|||||||
+1
-1
@@ -260,7 +260,7 @@ endif
|
|||||||
test_benchmark : test_benchmark.cc ../httplib.h Makefile
|
test_benchmark : test_benchmark.cc ../httplib.h Makefile
|
||||||
$(CXX) -o $@ -I.. $(CXXFLAGS) test_benchmark.cc gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(BENCHMARK_LIBS)
|
$(CXX) -o $@ -I.. $(CXXFLAGS) test_benchmark.cc gtest/src/gtest-all.cc gtest/src/gtest_main.cc -Igtest -Igtest/include $(BENCHMARK_LIBS)
|
||||||
|
|
||||||
test_websocket_heartbeat : test_websocket_heartbeat.cc ../httplib.h Makefile
|
test_websocket_heartbeat : test_websocket_heartbeat.cc ../httplib.h Makefile cert.pem
|
||||||
$(CXX) -o $@ -I.. $(CXXFLAGS) test_websocket_heartbeat.cc $(TEST_ARGS)
|
$(CXX) -o $@ -I.. $(CXXFLAGS) test_websocket_heartbeat.cc $(TEST_ARGS)
|
||||||
@file $@
|
@file $@
|
||||||
|
|
||||||
|
|||||||
+13
-2
@@ -13,14 +13,15 @@ ZLIB_SUPPORT = -DCPPHTTPLIB_ZLIB_SUPPORT -lz
|
|||||||
BROTLI_DIR = /usr/local/opt/brotli
|
BROTLI_DIR = /usr/local/opt/brotli
|
||||||
# BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec
|
# BROTLI_SUPPORT = -DCPPHTTPLIB_BROTLI_SUPPORT -I$(BROTLI_DIR)/include -L$(BROTLI_DIR)/lib -lbrotlicommon -lbrotlienc -lbrotlidec
|
||||||
|
|
||||||
FUZZERS = server_fuzzer url_parser_fuzzer header_parser_fuzzer client_fuzzer multipart_parser_fuzzer
|
FUZZERS = server_fuzzer url_parser_fuzzer header_parser_fuzzer client_fuzzer multipart_parser_fuzzer websocket_fuzzer sse_parser_fuzzer server_mount_fuzzer
|
||||||
|
|
||||||
# Runs all the tests and also fuzz tests against seed corpus.
|
# Runs all the tests and also fuzz tests against seed corpus.
|
||||||
all : $(FUZZERS)
|
all : $(FUZZERS)
|
||||||
./server_fuzzer corpus/*
|
./server_fuzzer corpus/*
|
||||||
|
./server_mount_fuzzer corpus_mount/*
|
||||||
|
|
||||||
# Fuzz target, so that you can choose which $(LIB_FUZZING_ENGINE) to use.
|
# Fuzz target, so that you can choose which $(LIB_FUZZING_ENGINE) to use.
|
||||||
server_fuzzer : server_fuzzer.cc ../../httplib.h
|
server_fuzzer : server_fuzzer.cc fuzzed_stream.h ../../httplib.h
|
||||||
# $(CXX) $(CXXFLAGS) -o $@ $< -Wl,-Bstatic $(OPENSSL_SUPPORT) -Wl,-Bdynamic -ldl $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread
|
# $(CXX) $(CXXFLAGS) -o $@ $< -Wl,-Bstatic $(OPENSSL_SUPPORT) -Wl,-Bdynamic -ldl $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread
|
||||||
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
||||||
zip -q -r server_fuzzer_seed_corpus.zip corpus
|
zip -q -r server_fuzzer_seed_corpus.zip corpus
|
||||||
@@ -38,5 +39,15 @@ url_parser_fuzzer : url_parser_fuzzer.cc ../../httplib.h
|
|||||||
multipart_parser_fuzzer : multipart_parser_fuzzer.cc ../../httplib.h
|
multipart_parser_fuzzer : multipart_parser_fuzzer.cc ../../httplib.h
|
||||||
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
||||||
|
|
||||||
|
websocket_fuzzer : websocket_fuzzer.cc ../../httplib.h
|
||||||
|
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
||||||
|
|
||||||
|
sse_parser_fuzzer : sse_parser_fuzzer.cc ../../httplib.h
|
||||||
|
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
||||||
|
|
||||||
|
server_mount_fuzzer : server_mount_fuzzer.cc fuzzed_stream.h ../../httplib.h
|
||||||
|
$(CXX) $(CXXFLAGS) -o $@ $< $(ZLIB_SUPPORT) $(LIB_FUZZING_ENGINE) -pthread -lanl
|
||||||
|
zip -q -r server_mount_fuzzer_seed_corpus.zip corpus_mount
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f server_fuzzer pem *.0 *.o *.1 *.srl *.zip
|
rm -f server_fuzzer pem *.0 *.o *.1 *.srl *.zip
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
HEAD /mnt/a.txt HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
GET /mnt/a.txt HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
If-Modified-Since: Mon, 01 Jan 2024 00:00:00 GMT
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
GET /mnt/index.html HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
GET /mnt/index.html HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
If-None-Match: W/"bogusetag"
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
GET /mnt/link-in.txt HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
GET /mnt/link-out.txt HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
GET /mnt/link-sibling.txt HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
GET /mnt/missing.txt HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
GET /mnt/a.txt HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
Range: bytes=0-3
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
GET /mnt/sub/ HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
GET /mnt/../../../../etc/passwd HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
GET /mnt/%2e%2e/secret.txt HTTP/1.1
|
||||||
|
Host: x
|
||||||
|
|
||||||
@@ -0,0 +1,84 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include <httplib.h>
|
||||||
|
|
||||||
|
class FuzzedStream : public httplib::Stream {
|
||||||
|
public:
|
||||||
|
FuzzedStream(const uint8_t *data, size_t size)
|
||||||
|
: data_(data), size_(size), read_pos_(0) {}
|
||||||
|
|
||||||
|
ssize_t read(char *ptr, size_t size) override {
|
||||||
|
if (size + read_pos_ > size_) { size = size_ - read_pos_; }
|
||||||
|
memcpy(ptr, data_ + read_pos_, size);
|
||||||
|
read_pos_ += size;
|
||||||
|
// Like SocketStream, pull in more than was asked for, which the line
|
||||||
|
// reader then takes through buffered_data(), so both of its paths run.
|
||||||
|
buffered_end_ =
|
||||||
|
(std::max)(buffered_end_, (std::min)(size_, read_pos_ + 4096));
|
||||||
|
return static_cast<ssize_t>(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *buffered_data(size_t &size) const override {
|
||||||
|
size = buffered_end_ > read_pos_ ? buffered_end_ - read_pos_ : 0;
|
||||||
|
return size ? reinterpret_cast<const char *>(data_ + read_pos_) : nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void consume_buffered(size_t size) override { read_pos_ += size; }
|
||||||
|
|
||||||
|
ssize_t write(const char *ptr, size_t size) override {
|
||||||
|
response_.append(ptr, size);
|
||||||
|
return static_cast<int>(size);
|
||||||
|
}
|
||||||
|
|
||||||
|
ssize_t write(const char *ptr) { return write(ptr, strlen(ptr)); }
|
||||||
|
|
||||||
|
ssize_t write(const std::string &s) { return write(s.data(), s.size()); }
|
||||||
|
|
||||||
|
bool is_readable() const override { return true; }
|
||||||
|
|
||||||
|
bool wait_readable() const override { return true; }
|
||||||
|
|
||||||
|
bool wait_writable() const override { return true; }
|
||||||
|
|
||||||
|
void get_remote_ip_and_port(std::string &ip, int &port) const override {
|
||||||
|
ip = "127.0.0.1";
|
||||||
|
port = 8080;
|
||||||
|
}
|
||||||
|
|
||||||
|
void get_local_ip_and_port(std::string &ip, int &port) const override {
|
||||||
|
ip = "127.0.0.1";
|
||||||
|
port = 8080;
|
||||||
|
}
|
||||||
|
|
||||||
|
socket_t socket() const override { return 0; }
|
||||||
|
|
||||||
|
time_t duration() const override { return 0; };
|
||||||
|
|
||||||
|
const std::string &response() const { return response_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
const uint8_t *data_;
|
||||||
|
size_t size_;
|
||||||
|
size_t read_pos_;
|
||||||
|
size_t buffered_end_ = 0;
|
||||||
|
std::string response_;
|
||||||
|
};
|
||||||
|
|
||||||
|
class FuzzableServer : public httplib::Server {
|
||||||
|
public:
|
||||||
|
// A server that is not listening counts as shutting down, and then writes
|
||||||
|
// no file. The socket is never used.
|
||||||
|
void MarkAsRunning() { svr_sock_ = 0; }
|
||||||
|
|
||||||
|
void ProcessFuzzedRequest(FuzzedStream &stream) {
|
||||||
|
bool connection_close = false;
|
||||||
|
process_request(stream,
|
||||||
|
/*remote_addr=*/"",
|
||||||
|
/*remote_port =*/0,
|
||||||
|
/*local_addr=*/"",
|
||||||
|
/*local_port =*/0,
|
||||||
|
/*last_connection=*/false, connection_close, nullptr);
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1,79 +1,4 @@
|
|||||||
#include <cstdint>
|
#include "fuzzed_stream.h"
|
||||||
|
|
||||||
#include <httplib.h>
|
|
||||||
|
|
||||||
class FuzzedStream : public httplib::Stream {
|
|
||||||
public:
|
|
||||||
FuzzedStream(const uint8_t *data, size_t size)
|
|
||||||
: data_(data), size_(size), read_pos_(0) {}
|
|
||||||
|
|
||||||
ssize_t read(char *ptr, size_t size) override {
|
|
||||||
if (size + read_pos_ > size_) { size = size_ - read_pos_; }
|
|
||||||
memcpy(ptr, data_ + read_pos_, size);
|
|
||||||
read_pos_ += size;
|
|
||||||
// Like SocketStream, pull in more than was asked for, which the line
|
|
||||||
// reader then takes through buffered_data(), so both of its paths run.
|
|
||||||
buffered_end_ =
|
|
||||||
(std::max)(buffered_end_, (std::min)(size_, read_pos_ + 4096));
|
|
||||||
return static_cast<ssize_t>(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *buffered_data(size_t &size) const override {
|
|
||||||
size = buffered_end_ > read_pos_ ? buffered_end_ - read_pos_ : 0;
|
|
||||||
return size ? reinterpret_cast<const char *>(data_ + read_pos_) : nullptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void consume_buffered(size_t size) override { read_pos_ += size; }
|
|
||||||
|
|
||||||
ssize_t write(const char *ptr, size_t size) override {
|
|
||||||
response_.append(ptr, size);
|
|
||||||
return static_cast<int>(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
ssize_t write(const char *ptr) { return write(ptr, strlen(ptr)); }
|
|
||||||
|
|
||||||
ssize_t write(const std::string &s) { return write(s.data(), s.size()); }
|
|
||||||
|
|
||||||
bool is_readable() const override { return true; }
|
|
||||||
|
|
||||||
bool wait_readable() const override { return true; }
|
|
||||||
|
|
||||||
bool wait_writable() const override { return true; }
|
|
||||||
|
|
||||||
void get_remote_ip_and_port(std::string &ip, int &port) const override {
|
|
||||||
ip = "127.0.0.1";
|
|
||||||
port = 8080;
|
|
||||||
}
|
|
||||||
|
|
||||||
void get_local_ip_and_port(std::string &ip, int &port) const override {
|
|
||||||
ip = "127.0.0.1";
|
|
||||||
port = 8080;
|
|
||||||
}
|
|
||||||
|
|
||||||
socket_t socket() const override { return 0; }
|
|
||||||
|
|
||||||
time_t duration() const override { return 0; };
|
|
||||||
|
|
||||||
private:
|
|
||||||
const uint8_t *data_;
|
|
||||||
size_t size_;
|
|
||||||
size_t read_pos_;
|
|
||||||
size_t buffered_end_ = 0;
|
|
||||||
std::string response_;
|
|
||||||
};
|
|
||||||
|
|
||||||
class FuzzableServer : public httplib::Server {
|
|
||||||
public:
|
|
||||||
void ProcessFuzzedRequest(FuzzedStream &stream) {
|
|
||||||
bool connection_close = false;
|
|
||||||
process_request(stream,
|
|
||||||
/*remote_addr=*/"",
|
|
||||||
/*remote_port =*/0,
|
|
||||||
/*local_addr=*/"",
|
|
||||||
/*local_port =*/0,
|
|
||||||
/*last_connection=*/false, connection_close, nullptr);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
static FuzzableServer g_server;
|
static FuzzableServer g_server;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,117 @@
|
|||||||
|
#include <cstdio>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <fstream>
|
||||||
|
#include <random>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <utime.h>
|
||||||
|
|
||||||
|
#include "fuzzed_stream.h"
|
||||||
|
|
||||||
|
// Fixture layout, with <token> unknown to the fuzzer:
|
||||||
|
// root/mnt/{index.html,a.txt,data.bin,sub/index.html} served at /mnt
|
||||||
|
// root/mnt/link-in.txt -> a.txt (stays inside, may be served)
|
||||||
|
// root/mnt/link-out.txt -> ../outside.txt (escapes, must be refused)
|
||||||
|
// root/mnt/link-sibling.txt -> ../mnt_secret/secret.txt (shares the "mnt"
|
||||||
|
// prefix, must be refused)
|
||||||
|
// The response must never contain <token>.
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
FuzzableServer g_server;
|
||||||
|
std::string g_root;
|
||||||
|
std::string g_token;
|
||||||
|
std::vector<std::string> g_paths; // In creation order
|
||||||
|
|
||||||
|
void check(bool ok, const char *what) {
|
||||||
|
if (!ok) {
|
||||||
|
fprintf(stderr, "fixture setup failed: %s\n", what);
|
||||||
|
abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void make_dir(const std::string &path) {
|
||||||
|
check(mkdir(path.c_str(), 0755) == 0, path.c_str());
|
||||||
|
g_paths.push_back(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
void make_file(const std::string &path, const std::string &content) {
|
||||||
|
{
|
||||||
|
std::ofstream out(path, std::ios::binary);
|
||||||
|
out << content;
|
||||||
|
check(out.good(), path.c_str());
|
||||||
|
}
|
||||||
|
// Fixed, so that conditional requests behave the same in every run
|
||||||
|
utimbuf times = {1700000000, 1700000000};
|
||||||
|
check(utime(path.c_str(), ×) == 0, path.c_str());
|
||||||
|
g_paths.push_back(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
void make_link(const std::string &target, const std::string &path) {
|
||||||
|
check(symlink(target.c_str(), path.c_str()) == 0, path.c_str());
|
||||||
|
g_paths.push_back(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string serve(const uint8_t *data, size_t size) {
|
||||||
|
FuzzedStream stream{data, size};
|
||||||
|
g_server.ProcessFuzzedRequest(stream);
|
||||||
|
return stream.response();
|
||||||
|
}
|
||||||
|
|
||||||
|
void check_served(const char *path) {
|
||||||
|
auto req = std::string("GET ") + path + " HTTP/1.1\r\nHost: x\r\n\r\n";
|
||||||
|
auto res = serve(reinterpret_cast<const uint8_t *>(req.data()), req.size());
|
||||||
|
// Without this, a fuzzer that serves nothing would pass its check forever
|
||||||
|
check(res.find("abcdef") != std::string::npos, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove_fixture() {
|
||||||
|
for (auto it = g_paths.rbegin(); it != g_paths.rend(); ++it) {
|
||||||
|
if (unlink(it->c_str()) != 0) { rmdir(it->c_str()); }
|
||||||
|
}
|
||||||
|
rmdir(g_root.c_str());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
extern "C" int LLVMFuzzerInitialize(int * /*argc*/, char *** /*argv*/) {
|
||||||
|
std::random_device rd;
|
||||||
|
char token[33];
|
||||||
|
snprintf(token, sizeof(token), "%08x%08x%08x%08x", rd(), rd(), rd(), rd());
|
||||||
|
g_token = token;
|
||||||
|
|
||||||
|
auto tmpdir = getenv("TMPDIR");
|
||||||
|
auto tmpl = std::string(tmpdir && *tmpdir ? tmpdir : "/tmp") +
|
||||||
|
"/httplib-mount-XXXXXX";
|
||||||
|
check(mkdtemp(&tmpl[0]) != nullptr, tmpl.c_str());
|
||||||
|
g_root = tmpl;
|
||||||
|
atexit(remove_fixture);
|
||||||
|
|
||||||
|
auto mnt = g_root + "/mnt";
|
||||||
|
make_dir(mnt);
|
||||||
|
make_dir(mnt + "/sub");
|
||||||
|
make_dir(g_root + "/mnt_secret");
|
||||||
|
make_file(mnt + "/index.html", "<html>index</html>");
|
||||||
|
make_file(mnt + "/a.txt", "abcdef");
|
||||||
|
make_file(mnt + "/data.bin", std::string("\x01\x02\x03\x04\x05\x06", 6));
|
||||||
|
make_file(mnt + "/sub/index.html", "<html>sub</html>");
|
||||||
|
make_file(g_root + "/outside.txt", g_token);
|
||||||
|
make_file(g_root + "/mnt_secret/secret.txt", g_token);
|
||||||
|
make_link("a.txt", mnt + "/link-in.txt");
|
||||||
|
make_link("../outside.txt", mnt + "/link-out.txt");
|
||||||
|
make_link("../mnt_secret/secret.txt", mnt + "/link-sibling.txt");
|
||||||
|
|
||||||
|
g_server.MarkAsRunning();
|
||||||
|
check(g_server.set_mount_point("/mnt", mnt), "set_mount_point");
|
||||||
|
check_served("/mnt/a.txt");
|
||||||
|
check_served("/mnt/link-in.txt");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||||
|
if (serve(data, size).find(g_token) != std::string::npos) { abort(); }
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
"GET"
|
||||||
|
"HEAD"
|
||||||
|
"HTTP/1.1"
|
||||||
|
|
||||||
|
"/mnt/"
|
||||||
|
"/mnt/index.html"
|
||||||
|
"/mnt/a.txt"
|
||||||
|
"/mnt/data.bin"
|
||||||
|
"/mnt/sub/"
|
||||||
|
"/mnt/link-in.txt"
|
||||||
|
"/mnt/link-out.txt"
|
||||||
|
"/mnt/link-sibling.txt"
|
||||||
|
".."
|
||||||
|
"%2e%2e"
|
||||||
|
"%2f"
|
||||||
|
"//"
|
||||||
|
|
||||||
|
"Range: bytes=0-"
|
||||||
|
"Range: bytes=0-3,5-"
|
||||||
|
"If-None-Match: "
|
||||||
|
"W/"
|
||||||
|
"If-Modified-Since: "
|
||||||
|
"If-Range: "
|
||||||
|
"Accept-Encoding: gzip"
|
||||||
@@ -0,0 +1,49 @@
|
|||||||
|
#include <algorithm>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <httplib.h>
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
// Feeds the stream in chunks and records every handler call in order
|
||||||
|
std::string parse_in_chunks(const std::string &stream, size_t chunk,
|
||||||
|
size_t max_event_length) {
|
||||||
|
std::string trace;
|
||||||
|
httplib::detail::SSEParser parser(
|
||||||
|
[&](const httplib::sse::SSEMessage &msg) {
|
||||||
|
trace += "event[" + msg.event + "|" + msg.data + "|" + msg.id + "]";
|
||||||
|
},
|
||||||
|
[&](const std::string &id) { trace += "id[" + id + "]"; },
|
||||||
|
[&](int retry_ms) { trace += "retry[" + std::to_string(retry_ms) + "]"; },
|
||||||
|
max_event_length);
|
||||||
|
|
||||||
|
for (size_t off = 0; off < stream.size(); off += chunk) {
|
||||||
|
if (!parser.parse(stream.data() + off,
|
||||||
|
std::min(chunk, stream.size() - off))) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return trace;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||||
|
if (size < 2 || size > 65536) { return 0; }
|
||||||
|
|
||||||
|
// First byte selects the chunk size, the rest is the event stream
|
||||||
|
auto chunk = static_cast<size_t>(data[0]) + 1;
|
||||||
|
std::string stream(reinterpret_cast<const char *>(data + 1), size - 1);
|
||||||
|
|
||||||
|
// Without a length limit, chunking must not change what is reported
|
||||||
|
auto whole = parse_in_chunks(stream, stream.size(), 0);
|
||||||
|
auto chunked = parse_in_chunks(stream, chunk, 0);
|
||||||
|
if (whole != chunked) { std::abort(); }
|
||||||
|
|
||||||
|
// With a limit, only check that the parser stops cleanly
|
||||||
|
parse_in_chunks(stream, chunk, 64);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -13,7 +13,7 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
|||||||
size_t payload_size = size - 1;
|
size_t payload_size = size - 1;
|
||||||
std::string input(payload, payload_size);
|
std::string input(payload, payload_size);
|
||||||
|
|
||||||
switch (selector % 6) {
|
switch (selector % 13) {
|
||||||
case 0: {
|
case 0: {
|
||||||
// parse_query_text
|
// parse_query_text
|
||||||
httplib::Params params;
|
httplib::Params params;
|
||||||
@@ -46,6 +46,48 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
|||||||
httplib::detail::is_valid_path(input);
|
httplib::detail::is_valid_path(input);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 6: {
|
||||||
|
// parse_url
|
||||||
|
httplib::detail::UrlComponents uc;
|
||||||
|
httplib::detail::parse_url(input, uc);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 7: {
|
||||||
|
// remove_dot_segments
|
||||||
|
httplib::detail::remove_dot_segments(input);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 8: {
|
||||||
|
// resolve_relative_location, with the input as "<location>\n<base>"
|
||||||
|
auto nl = input.find('\n');
|
||||||
|
auto location = input.substr(0, nl);
|
||||||
|
auto base = nl == std::string::npos ? std::string("/index.html")
|
||||||
|
: input.substr(nl + 1);
|
||||||
|
httplib::detail::resolve_relative_location(location, base);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 9: {
|
||||||
|
// encode_path
|
||||||
|
httplib::detail::encode_path(input);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 10: {
|
||||||
|
// encode_uri_component, encode_uri
|
||||||
|
httplib::encode_uri_component(input);
|
||||||
|
httplib::encode_uri(input);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 11: {
|
||||||
|
// decode_uri_component, decode_uri
|
||||||
|
httplib::decode_uri_component(input);
|
||||||
|
httplib::decode_uri(input);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 12: {
|
||||||
|
// sanitize_filename
|
||||||
|
httplib::sanitize_filename(input);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
@@ -0,0 +1,54 @@
|
|||||||
|
#include <cstdint>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
#include <httplib.h>
|
||||||
|
|
||||||
|
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
|
||||||
|
if (size < 2) { return 0; }
|
||||||
|
|
||||||
|
auto selector = data[0];
|
||||||
|
std::string input(reinterpret_cast<const char *>(data + 1), size - 1);
|
||||||
|
|
||||||
|
switch (selector % 4) {
|
||||||
|
case 0: {
|
||||||
|
// read_websocket_frame. WebSocket::read passes the macro, close() passes
|
||||||
|
// 125, and 0 means no limit
|
||||||
|
static const size_t max_lens[] = {0, 125,
|
||||||
|
CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH};
|
||||||
|
auto expect_masked = (selector / 4) % 2 != 0;
|
||||||
|
auto max_len = max_lens[(selector / 8) % 3];
|
||||||
|
httplib::detail::BufferStream strm;
|
||||||
|
strm.write(input.data(), input.size());
|
||||||
|
auto opcode = httplib::ws::Opcode::Text;
|
||||||
|
std::string payload;
|
||||||
|
auto fin = false;
|
||||||
|
httplib::ws::impl::read_websocket_frame(strm, opcode, payload, fin,
|
||||||
|
expect_masked, max_len);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 1: {
|
||||||
|
// is_valid_utf8
|
||||||
|
httplib::ws::impl::is_valid_utf8(input);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 2: {
|
||||||
|
// is_valid_close_payload
|
||||||
|
httplib::ws::impl::is_valid_close_payload(input);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 3: {
|
||||||
|
// read_websocket_upgrade_response, with a fixed key since
|
||||||
|
// perform_websocket_handshake draws a random one
|
||||||
|
static const auto expected_accept =
|
||||||
|
httplib::detail::websocket_accept_key("dGhlIHNhbXBsZSBub25jZQ==");
|
||||||
|
httplib::detail::BufferStream strm;
|
||||||
|
strm.write(input.data(), input.size());
|
||||||
|
httplib::detail::WebSocketUpgradeResponse upgrade;
|
||||||
|
httplib::detail::read_websocket_upgrade_response(strm, expected_accept,
|
||||||
|
"chat, stream", upgrade);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
[libfuzzer]
|
||||||
|
max_len = 70000
|
||||||
+10
-7
@@ -1,15 +1,9 @@
|
|||||||
#!/usr/bin/env bash
|
#!/usr/bin/env bash
|
||||||
if [[ $(openssl version) =~ 3\.[2-9]\.[0-9]+ ]]; then
|
|
||||||
OPENSSL_X509_FLAG='-x509v1'
|
|
||||||
else
|
|
||||||
OPENSSL_X509_FLAG='-x509'
|
|
||||||
fi
|
|
||||||
|
|
||||||
openssl genrsa 2048 > key.pem
|
openssl genrsa 2048 > key.pem
|
||||||
openssl req -new -batch -config test.conf -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem
|
openssl req -new -batch -config test.conf -key key.pem | openssl x509 -days 3650 -req -signkey key.pem > cert.pem
|
||||||
openssl req -x509 -config test.conf -key key.pem -sha256 -days 3650 -nodes -out cert2.pem -extensions SAN
|
openssl req -x509 -config test.conf -key key.pem -sha256 -days 3650 -nodes -out cert2.pem -extensions SAN
|
||||||
openssl genrsa 2048 > rootCA.key.pem
|
openssl genrsa 2048 > rootCA.key.pem
|
||||||
openssl req $OPENSSL_X509_FLAG -new -batch -config test.rootCA.conf -key rootCA.key.pem -days 1024 > rootCA.cert.pem
|
openssl req -x509 -new -batch -config test.rootCA.conf -key rootCA.key.pem -days 1024 > rootCA.cert.pem
|
||||||
openssl genrsa 2048 > client.key.pem
|
openssl genrsa 2048 > client.key.pem
|
||||||
openssl req -new -batch -config test.conf -key client.key.pem | openssl x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial > client.cert.pem
|
openssl req -new -batch -config test.conf -key client.key.pem | openssl x509 -days 370 -req -CA rootCA.cert.pem -CAkey rootCA.key.pem -CAcreateserial > client.cert.pem
|
||||||
openssl genrsa -passout pass:test123! 2048 > key_encrypted.pem
|
openssl genrsa -passout pass:test123! 2048 > key_encrypted.pem
|
||||||
@@ -38,7 +32,16 @@ openssl req -x509 -key key.pem -sha256 -days 3650 -nodes -subj "/CN=::1" -addext
|
|||||||
# DNS:a.zz is 97.46.122.122, IP:42.46.122.122 is "*.zz".
|
# DNS:a.zz is 97.46.122.122, IP:42.46.122.122 is "*.zz".
|
||||||
openssl req -x509 -key key.pem -sha256 -days 3650 -nodes -subj "/CN=san-types" -addext "subjectAltName=DNS:a.zz,IP:42.46.122.122" -out cert_san_types.pem
|
openssl req -x509 -key key.pem -sha256 -days 3650 -nodes -subj "/CN=san-types" -addext "subjectAltName=DNS:a.zz,IP:42.46.122.122" -out cert_san_types.pem
|
||||||
|
|
||||||
|
# cert_dns_cn.pem: CN is the DNS name the tests connect to, with NO
|
||||||
|
# subjectAltName. A host is named by a SAN only (RFC 9525), so
|
||||||
|
# verifying it against this cert must fail.
|
||||||
|
openssl req -x509 -key key.pem -sha256 -days 3650 -nodes -subj "/CN=localhost" -out cert_dns_cn.pem
|
||||||
|
|
||||||
# cert_wildcard_san.pem: a leftmost wildcard next to one that is not leftmost.
|
# cert_wildcard_san.pem: a leftmost wildcard next to one that is not leftmost.
|
||||||
# "*.leftmost.example.test" matches a single label; the
|
# "*.leftmost.example.test" matches a single label; the
|
||||||
# wildcard in "www.*.example.test" must not be honoured.
|
# wildcard in "www.*.example.test" must not be honoured.
|
||||||
openssl req -x509 -key key.pem -sha256 -days 3650 -nodes -subj "/CN=wildcard-san" -addext "subjectAltName=DNS:*.leftmost.example.test,DNS:www.*.example.test" -out cert_wildcard_san.pem
|
openssl req -x509 -key key.pem -sha256 -days 3650 -nodes -subj "/CN=wildcard-san" -addext "subjectAltName=DNS:*.leftmost.example.test,DNS:www.*.example.test" -out cert_wildcard_san.pem
|
||||||
|
|
||||||
|
# The client certs must chain to a root that openssl accepts as a CA; a root
|
||||||
|
# without basicConstraints CA:TRUE makes every client-cert test fail.
|
||||||
|
openssl verify -CAfile rootCA.cert.pem client.cert.pem client_encrypted.cert.pem
|
||||||
@@ -146,6 +146,16 @@ cert_san_types_pem = custom_target(
|
|||||||
command: [openssl, 'req', '-x509', '-key', '@INPUT@', '-sha256', '-days', '3650', '-nodes', '-subj', '/CN=san-types', '-addext', 'subjectAltName=DNS:a.zz,IP:42.46.122.122', '-out', '@OUTPUT@']
|
command: [openssl, 'req', '-x509', '-key', '@INPUT@', '-sha256', '-days', '3650', '-nodes', '-subj', '/CN=san-types', '-addext', 'subjectAltName=DNS:a.zz,IP:42.46.122.122', '-out', '@OUTPUT@']
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# cert_dns_cn.pem: CN is the DNS name the tests connect to, with NO
|
||||||
|
# subjectAltName. A host is named by a SAN only (RFC 9525), so verifying it
|
||||||
|
# against this cert must fail.
|
||||||
|
cert_dns_cn_pem = custom_target(
|
||||||
|
'cert_dns_cn_pem',
|
||||||
|
input: key_pem,
|
||||||
|
output: 'cert_dns_cn.pem',
|
||||||
|
command: [openssl, 'req', '-x509', '-key', '@INPUT@', '-sha256', '-days', '3650', '-nodes', '-subj', '/CN=localhost', '-out', '@OUTPUT@']
|
||||||
|
)
|
||||||
|
|
||||||
# cert_wildcard_san.pem: a leftmost wildcard next to one that is not leftmost.
|
# cert_wildcard_san.pem: a leftmost wildcard next to one that is not leftmost.
|
||||||
# "*.leftmost.example.test" matches a single label; the wildcard in
|
# "*.leftmost.example.test" matches a single label; the wildcard in
|
||||||
# "www.*.example.test" must not be honoured.
|
# "www.*.example.test" must not be honoured.
|
||||||
@@ -197,6 +207,7 @@ test(
|
|||||||
client_encrypted_pbes1_key_pem,
|
client_encrypted_pbes1_key_pem,
|
||||||
client_encrypted_cert_pem,
|
client_encrypted_cert_pem,
|
||||||
cert_ip_cn_pem,
|
cert_ip_cn_pem,
|
||||||
|
cert_dns_cn_pem,
|
||||||
cert_ipv6_pem,
|
cert_ipv6_pem,
|
||||||
cert_san_types_pem,
|
cert_san_types_pem,
|
||||||
cert_wildcard_san_pem
|
cert_wildcard_san_pem
|
||||||
|
|||||||
+3344
-273
File diff suppressed because it is too large.
Load diff
@@ -2,6 +2,7 @@
|
|||||||
default_bits = 2048
|
default_bits = 2048
|
||||||
distinguished_name = req_distinguished_name
|
distinguished_name = req_distinguished_name
|
||||||
attributes = req_attributes
|
attributes = req_attributes
|
||||||
|
x509_extensions = v3_ca
|
||||||
prompt = no
|
prompt = no
|
||||||
output_password = mypass
|
output_password = mypass
|
||||||
|
|
||||||
@@ -16,3 +17,8 @@ emailAddress = test@email.address
|
|||||||
|
|
||||||
[req_attributes]
|
[req_attributes]
|
||||||
challengePassword = 1234
|
challengePassword = 1234
|
||||||
|
|
||||||
|
# Mark the root as a CA explicitly. Relying on the openssl version to emit a
|
||||||
|
# v1 certificate (or CA:TRUE by default) breaks when the default changes.
|
||||||
|
[v3_ca]
|
||||||
|
basicConstraints = critical,CA:TRUE
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
// Standalone test for WebSocket automatic heartbeat.
|
// Standalone test for WebSocket heartbeat and close() timing, compiled with
|
||||||
// Compiled with a 1-second ping interval so we can verify heartbeat behavior
|
// short intervals and timeouts so the tests don't wait for the defaults.
|
||||||
// without waiting 30 seconds.
|
|
||||||
|
|
||||||
#define CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND 1
|
#define CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND 1
|
||||||
#define CPPHTTPLIB_WEBSOCKET_CLIENT_READ_TIMEOUT_SECOND 3
|
#define CPPHTTPLIB_WEBSOCKET_CLIENT_READ_TIMEOUT_SECOND 3
|
||||||
#define CPPHTTPLIB_WEBSOCKET_SERVER_READ_TIMEOUT_SECOND 3
|
#define CPPHTTPLIB_WEBSOCKET_SERVER_READ_TIMEOUT_SECOND 3
|
||||||
|
#define CPPHTTPLIB_WEBSOCKET_CLOSE_TIMEOUT_SECOND 1
|
||||||
#include <httplib.h>
|
#include <httplib.h>
|
||||||
|
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
@@ -289,3 +289,139 @@ TEST_F(WebSocketHeartbeatTest, MultipleHeartbeatCycles) {
|
|||||||
|
|
||||||
client.close();
|
client.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A raw client never answers the server's close() and sends `bytes`, `chunk`
|
||||||
|
// bytes at a time `gap` apart; close() must still return at its 1 s timeout.
|
||||||
|
static void expect_close_bounded(bool use_tls, const std::string &bytes,
|
||||||
|
size_t chunk, std::chrono::milliseconds gap) {
|
||||||
|
std::unique_ptr<Server> svr;
|
||||||
|
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||||
|
if (use_tls) { svr.reset(new SSLServer("cert.pem", "key.pem")); }
|
||||||
|
#endif
|
||||||
|
if (!svr) { svr.reset(new Server); }
|
||||||
|
svr->set_websocket_ping_interval(0);
|
||||||
|
|
||||||
|
std::atomic<bool> upgraded{false};
|
||||||
|
std::atomic<bool> closed{false};
|
||||||
|
std::chrono::steady_clock::duration close_took{};
|
||||||
|
svr->WebSocket("/ws", [&](const Request &, ws::WebSocket &ws) {
|
||||||
|
while (!upgraded) {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
||||||
|
}
|
||||||
|
auto start = std::chrono::steady_clock::now();
|
||||||
|
ws.close();
|
||||||
|
close_took = std::chrono::steady_clock::now() - start;
|
||||||
|
closed = true;
|
||||||
|
});
|
||||||
|
auto port = svr->bind_to_any_port("127.0.0.1");
|
||||||
|
ASSERT_GT(port, 0);
|
||||||
|
std::thread t([&] { svr->listen_after_bind(); });
|
||||||
|
auto se = detail::scope_exit([&] {
|
||||||
|
upgraded = true; // Releases the handler when an assertion bails out early
|
||||||
|
svr->stop();
|
||||||
|
t.join();
|
||||||
|
});
|
||||||
|
svr->wait_until_ready();
|
||||||
|
|
||||||
|
auto sock = ::socket(AF_INET, SOCK_STREAM, 0);
|
||||||
|
ASSERT_NE(INVALID_SOCKET, sock);
|
||||||
|
auto sock_se = detail::scope_exit([&] { detail::close_socket(sock); });
|
||||||
|
sockaddr_in addr{};
|
||||||
|
addr.sin_family = AF_INET;
|
||||||
|
addr.sin_port = htons(static_cast<uint16_t>(port));
|
||||||
|
::inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr);
|
||||||
|
ASSERT_EQ(0,
|
||||||
|
::connect(sock, reinterpret_cast<sockaddr *>(&addr), sizeof(addr)));
|
||||||
|
|
||||||
|
std::unique_ptr<Stream> strm;
|
||||||
|
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||||
|
tls::ctx_t ctx = nullptr;
|
||||||
|
tls::session_t session = nullptr;
|
||||||
|
auto tls_se = detail::scope_exit([&] {
|
||||||
|
if (session) { tls::free_session(session); }
|
||||||
|
if (ctx) { tls::free_context(ctx); }
|
||||||
|
});
|
||||||
|
if (use_tls) {
|
||||||
|
ctx = tls::create_client_context();
|
||||||
|
tls::set_verify_client(ctx, false); // The test certificate is self-signed
|
||||||
|
session = tls::create_session(ctx, sock);
|
||||||
|
ASSERT_TRUE(tls::connect_nonblocking(session, sock, 5, 0, nullptr));
|
||||||
|
strm.reset(new detail::SSLSocketStream(sock, session, 5, 0, 5, 0));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if (!strm) { strm.reset(new detail::SocketStream(sock, 5, 0, 5, 0)); }
|
||||||
|
|
||||||
|
const std::string upgrade = "GET /ws HTTP/1.1\r\n"
|
||||||
|
"Host: 127.0.0.1\r\n"
|
||||||
|
"Upgrade: websocket\r\n"
|
||||||
|
"Connection: Upgrade\r\n"
|
||||||
|
"Sec-WebSocket-Key: AAAAAAAAAAAAAAAAAAAAAA==\r\n"
|
||||||
|
"Sec-WebSocket-Version: 13\r\n"
|
||||||
|
"\r\n";
|
||||||
|
ASSERT_EQ(static_cast<ssize_t>(upgrade.size()),
|
||||||
|
strm->write(upgrade.data(), upgrade.size()));
|
||||||
|
std::string response;
|
||||||
|
char c;
|
||||||
|
while (response.find("\r\n\r\n") == std::string::npos &&
|
||||||
|
strm->read(&c, 1) == 1) {
|
||||||
|
response += c;
|
||||||
|
}
|
||||||
|
ASSERT_NE(std::string::npos, response.find(" 101 "));
|
||||||
|
upgraded = true;
|
||||||
|
|
||||||
|
// Without the bound, close() would wait as long as the client sends
|
||||||
|
auto give_up = std::chrono::steady_clock::now() + std::chrono::seconds(4);
|
||||||
|
while (!closed && std::chrono::steady_clock::now() < give_up) {
|
||||||
|
for (size_t off = 0; off < bytes.size() && !closed;) {
|
||||||
|
auto n = strm->write(bytes.data() + off,
|
||||||
|
(std::min)(chunk, bytes.size() - off));
|
||||||
|
if (n <= 0) { break; }
|
||||||
|
off += static_cast<size_t>(n);
|
||||||
|
auto next = std::chrono::steady_clock::now() + gap;
|
||||||
|
while (!closed && std::chrono::steady_clock::now() < next) {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (!closed) {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||||
|
}
|
||||||
|
EXPECT_LT(close_took, std::chrono::milliseconds(2500));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Empty masked Ping frames, enough to keep the socket full
|
||||||
|
static std::string ping_flood() {
|
||||||
|
const std::string ping("\x89\x80\0\0\0\0", 6);
|
||||||
|
std::string bytes;
|
||||||
|
while (bytes.size() < 64 * 1024) {
|
||||||
|
bytes += ping;
|
||||||
|
}
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
// A masked 20-byte Ping; sent a byte at a time, it outlasts the close timeout
|
||||||
|
static std::string ping_with_payload() {
|
||||||
|
return std::string("\x89\x8e\0\0\0\0", 6) + std::string(14, 'x');
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(WebSocketCloseTest, BoundedAgainstPeerThatKeepsSending) {
|
||||||
|
expect_close_bounded(/*use_tls=*/false, ping_flood(), ping_flood().size(),
|
||||||
|
std::chrono::milliseconds(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(WebSocketCloseTest, BoundedAgainstPeerThatTricklesAFrame) {
|
||||||
|
expect_close_bounded(/*use_tls=*/false, ping_with_payload(), 1,
|
||||||
|
std::chrono::milliseconds(300));
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef CPPHTTPLIB_SSL_ENABLED
|
||||||
|
TEST(WebSocketCloseTest, SSLBoundedAgainstPeerThatKeepsSending) {
|
||||||
|
expect_close_bounded(/*use_tls=*/true, ping_flood(), ping_flood().size(),
|
||||||
|
std::chrono::milliseconds(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(WebSocketCloseTest, SSLBoundedAgainstPeerThatTricklesAFrame) {
|
||||||
|
expect_close_bounded(/*use_tls=*/true, ping_with_payload(), 1,
|
||||||
|
std::chrono::milliseconds(300));
|
||||||
|
}
|
||||||
|
#endif
|
||||||
Reference in new issue
Block a user