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:
authored and GitHub committed 2026-11-26 19:31:41 -05:00
1 parent f57e25ec92
commit 0bdc860f2c
5 files changed
+139 -26

No files matched your search

+41 -23
View File
@@ -2415,6 +2415,8 @@ public:
Server &set_websocket_max_missed_pongs(int count);
Server &set_websocket_max_message_length(size_t length);
bool bind_to_port(const std::string &host, int port, int socket_flags = 0);
int bind_to_any_port(const std::string &host, int socket_flags = 0);
bool listen_after_bind();
@@ -2497,6 +2499,8 @@ protected:
time_t websocket_ping_interval_sec_ =
CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND;
int websocket_max_missed_pongs_ = CPPHTTPLIB_WEBSOCKET_MAX_MISSED_PONGS;
size_t websocket_max_message_length_ =
CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH;
private:
using Handlers =
@@ -4944,27 +4948,27 @@ private:
friend class httplib::Server;
friend class httplib::ws::WebSocketClient;
WebSocket(
Stream &strm, const Request &req, bool is_server,
time_t ping_interval_sec = CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND,
int max_missed_pongs = CPPHTTPLIB_WEBSOCKET_MAX_MISSED_PONGS,
std::function<bool()> server_stopping = nullptr)
WebSocket(Stream &strm, const Request &req, bool is_server,
time_t ping_interval_sec, int max_missed_pongs,
size_t max_message_length,
std::function<bool()> server_stopping = nullptr)
: strm_(strm), req_(req), is_server_(is_server),
ping_interval_sec_(ping_interval_sec),
max_missed_pongs_(max_missed_pongs),
max_message_length_(max_message_length),
server_stopping_(std::move(server_stopping)) {
interrupt_reads();
start_heartbeat();
}
WebSocket(
std::unique_ptr<Stream> &&owned_strm, const Request &req, bool is_server,
time_t ping_interval_sec = CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND,
int max_missed_pongs = CPPHTTPLIB_WEBSOCKET_MAX_MISSED_PONGS,
std::function<bool()> server_stopping = nullptr)
WebSocket(std::unique_ptr<Stream> &&owned_strm, const Request &req,
bool is_server, time_t ping_interval_sec, int max_missed_pongs,
size_t max_message_length,
std::function<bool()> server_stopping = nullptr)
: strm_(*owned_strm), owned_strm_(std::move(owned_strm)), req_(req),
is_server_(is_server), ping_interval_sec_(ping_interval_sec),
max_missed_pongs_(max_missed_pongs),
max_message_length_(max_message_length),
server_stopping_(std::move(server_stopping)) {
interrupt_reads();
start_heartbeat();
@@ -4985,6 +4989,8 @@ private:
bool is_server_;
time_t ping_interval_sec_;
int max_missed_pongs_;
// The largest frame or message read() accepts; 0 means no limit
size_t max_message_length_;
int unacked_pings_ = 0;
std::atomic<bool> closed_{false};
// Set when the peer stopped answering pings, to end a read() waiting on it
@@ -5047,6 +5053,7 @@ public:
void set_websocket_ping_interval(time_t sec);
void set_websocket_max_missed_pongs(int count);
void set_websocket_max_message_length(size_t length);
void set_tcp_nodelay(bool on);
void set_address_family(int family);
void set_ipv6_v6only(bool on);
@@ -5129,6 +5136,8 @@ private:
time_t websocket_ping_interval_sec_ =
CPPHTTPLIB_WEBSOCKET_PING_INTERVAL_SECOND;
int websocket_max_missed_pongs_ = CPPHTTPLIB_WEBSOCKET_MAX_MISSED_PONGS;
size_t websocket_max_message_length_ =
CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH;
int address_family_ = AF_UNSPEC;
bool tcp_nodelay_ = CPPHTTPLIB_WEBSOCKET_TCP_NODELAY;
bool ipv6_v6only_ = CPPHTTPLIB_IPV6_V6ONLY;
@@ -14981,6 +14990,11 @@ inline Server &Server::set_websocket_max_missed_pongs(int count) {
return *this;
}
inline Server &Server::set_websocket_max_message_length(size_t length) {
websocket_max_message_length_ = length;
return *this;
}
inline Server &Server::set_websocket_ping_interval(time_t sec) {
websocket_ping_interval_sec_ = sec;
return *this;
@@ -16403,7 +16417,8 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
write_timeout_sec_, write_timeout_usec_));
ws::WebSocket ws(std::move(ws_strm), req, true,
websocket_ping_interval_sec_,
websocket_max_missed_pongs_, stopping);
websocket_max_missed_pongs_,
websocket_max_message_length_, stopping);
ws_route->handler(req, ws);
return true;
}
@@ -16411,7 +16426,8 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
// Use WebSocket-specific read timeout instead of HTTP timeout
strm.set_read_timeout(CPPHTTPLIB_WEBSOCKET_SERVER_READ_TIMEOUT_SECOND, 0);
ws::WebSocket ws(strm, req, true, websocket_ping_interval_sec_,
websocket_max_missed_pongs_, stopping);
websocket_max_missed_pongs_,
websocket_max_message_length_, stopping);
ws_route->handler(req, ws);
return true;
}
@@ -24957,13 +24973,12 @@ inline ReadResult WebSocket::read(std::string &msg) {
std::string payload;
bool fin;
// While a message is assembled, only what is left of the limit, but room
// for a control frame, which may come between its fragments.
// What is left of the limit (all of it between messages), but always room
// for a control frame, which the limit does not cover.
auto max_len =
CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH == 0 || message_kind_ == Fail
? size_t(CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH)
: (std::max)(size_t(125), CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH -
message_.size());
max_message_length_ == 0
? size_t(0)
: (std::max)(size_t(125), max_message_length_ - message_.size());
auto r = frames_.read(strm_, opcode, payload, fin, is_server_, max_len);
if (r != impl::FrameRead::Ok && server_stopping()) { return going_away(); }
// A timeout leaves what was read of the frame and of the message for the
@@ -25003,8 +25018,7 @@ inline ReadResult WebSocket::read(std::string &msg) {
message_ = std::move(payload);
message_kind_ = opcode == Opcode::Text ? Text : Binary;
}
if (CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH > 0 &&
message_.size() > CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH) {
if (max_message_length_ > 0 && message_.size() > max_message_length_) {
return fail_with(CloseStatus::MessageTooBig);
}
if (!fin) { continue; }
@@ -25377,9 +25391,9 @@ inline Result WebSocketClient::connect() {
}
subprotocol_ = std::move(upgrade.selected_subprotocol);
ws_ = std::unique_ptr<WebSocket>(new WebSocket(std::move(strm), req, false,
websocket_ping_interval_sec_,
websocket_max_missed_pongs_));
ws_ = std::unique_ptr<WebSocket>(new WebSocket(
std::move(strm), req, false, websocket_ping_interval_sec_,
websocket_max_missed_pongs_, websocket_max_message_length_));
// The stream was created with the timeout already; tell the WebSocket
// whether it came from the caller, so read() knows to report it as Timeout.
ws_->read_timeout_set_ = read_timeout_set_;
@@ -25442,6 +25456,10 @@ inline void WebSocketClient::set_websocket_max_missed_pongs(int count) {
websocket_max_missed_pongs_ = count;
}
inline void WebSocketClient::set_websocket_max_message_length(size_t length) {
websocket_max_message_length_ = length;
}
inline void WebSocketClient::set_tcp_nodelay(bool on) { tcp_nodelay_ = on; }
inline void WebSocketClient::set_address_family(int family) {