mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-27 20:58:28 +00:00
Add set_websocket_max_message_length() to Server and WebSocketClient (#2696)
* Add set_websocket_max_message_length() to Server and WebSocketClient The largest WebSocket frame and message read() accepts was fixed at compile time by CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH, so a program running several servers, or one build of httplib.h shared by many applications, could not pick a limit per server. Checking the size after read() does not help, since by then the message has already been assembled in memory. Add set_websocket_max_message_length() to Server and WebSocketClient, next to the heartbeat setters. It defaults to the macro, is handed to ws::WebSocket like the ping settings, and replaces the macro in both checks in read(): the declared frame length and the size of a message assembled from fragments. 0 still means no limit, and a message over the limit still makes read() fail and close the connection. On the client, the value applies from the next connect(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Keep control frames out of the message limit and trim the limit tests With a limit under 125 bytes, a frame read between messages was held to the limit itself, so a Ping or a Close with a reason was answered with 1009. The frame limit is now what is left of the message limit, but never less than 125, so control frames always fit; a small data frame over the limit is still refused by the message check. The test that sent two 16 MiB messages is replaced by cheap checks in the remaining tests: a header that announces more than the limit is refused before its payload, a Ping over the limit is answered, and 0 lifts the limit on the client. * Fold the server limit tests and drop unused constructor defaults The two server-side tests become one on an echo server, checked with raw_websocket_reply(): a Ping over the limit is answered, a message at the limit is echoed, one over it gets 1009, and so does a header that announces more than the limit. Every caller of the WebSocket constructors passes the settings, so their defaults are dropped. --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: yhirose <yuji.hirose.bug@gmail.com>
This commit is contained in:
5 files changed
+139
-26
No files matched your search
+16
-1
@@ -242,6 +242,9 @@ void set_connection_timeout(const std::chrono::duration<Rep, Period> &duration);
|
||||
void set_websocket_ping_interval(time_t sec);
|
||||
void set_websocket_max_missed_pongs(int count);
|
||||
|
||||
// Largest message read() accepts (see Maximum Message Length below)
|
||||
void set_websocket_max_message_length(size_t length);
|
||||
|
||||
// Socket - the same setters Client has (TCP_NODELAY is on by default here)
|
||||
void set_tcp_nodelay(bool on);
|
||||
void set_address_family(int family);
|
||||
@@ -525,7 +528,7 @@ if (ws.connect()) {
|
||||
|
||||
| Macro | Default | Description |
|
||||
|---------------------------------------------|-------------------|----------------------------------------------------------|
|
||||
| `CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH` | `16777216` (16MB) | Maximum payload size per message (`0` means no limit) |
|
||||
| `CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH` | `16777216` (16MB) | Default maximum message size (`0` means no limit) |
|
||||
| `CPPHTTPLIB_WEBSOCKET_CLIENT_READ_TIMEOUT_SECOND` | `0` | Client read timeout (seconds); `0` waits forever |
|
||||
| `CPPHTTPLIB_WEBSOCKET_SERVER_READ_TIMEOUT_SECOND` | `300` | Server read timeout (seconds) |
|
||||
| `CPPHTTPLIB_WEBSOCKET_CLOSE_TIMEOUT_SECOND` | `5` | Timeout for waiting peer's Close response (seconds) |
|
||||
@@ -567,6 +570,18 @@ With the default ping interval of 30 seconds, `max_missed_pongs = 2` detects a d
|
||||
|
||||
**The default is `0`**, which means "never close the connection because of missing pongs." Pings are still sent on the heartbeat interval, but their responses are not checked. On the server side a dead connection still does not linger: while a handler is inside `read()`, `CPPHTTPLIB_WEBSOCKET_SERVER_READ_TIMEOUT_SECOND` (default **300 seconds = 5 minutes**) acts as a backstop. A client has no such backstop — it waits forever unless you set a read timeout — so there `max_missed_pongs` is what notices an unresponsive peer at all. On either side it is also the knob for noticing one *faster* than the 5-minute fallback.
|
||||
|
||||
### Maximum Message Length
|
||||
|
||||
`read()` refuses a message larger than `CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH` (16MB by default): it returns `Fail` and closes the connection with `MessageTooBig` (1009). A frame that announces a larger length is refused before its payload is read, and a fragmented message is refused as soon as its fragments add up to more, so the limit bounds the memory a peer can make `read()` hold. You can set it per server or per client at runtime instead of changing the compile-time macro. `0` means no limit.
|
||||
|
||||
```cpp
|
||||
// Server side
|
||||
svr.set_websocket_max_message_length(64 * 1024); // 64KB
|
||||
|
||||
// Client side, applied by the next connect()
|
||||
ws.set_websocket_max_message_length(64 * 1024);
|
||||
```
|
||||
|
||||
## Threading Model
|
||||
|
||||
WebSocket connections share the same thread pool as HTTP requests. Each WebSocket connection occupies one thread for its entire lifetime, so a client opening many of them can leave none for HTTP requests. Reject the upgrades you do not want in `set_pre_request_handler()`, before the connection takes a thread for good.
|
||||
|
||||
Reference in new issue
Block a user