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
@@ -31096,6 +31096,86 @@ TEST(WebSocketTest, CloseEndsAReadOfEndlessFragments) {
|
||||
EXPECT_TRUE(read_ended);
|
||||
}
|
||||
|
||||
TEST(WebSocketTest, ServerMaxMessageLength) {
|
||||
Server svr;
|
||||
svr.set_websocket_ping_interval(0);
|
||||
svr.set_websocket_max_message_length(100);
|
||||
svr.WebSocket("/ws", [&](const Request &, ws::WebSocket &ws) {
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {
|
||||
ws.send(msg);
|
||||
}
|
||||
});
|
||||
|
||||
auto port = svr.bind_to_any_port("127.0.0.1");
|
||||
std::thread t([&]() { svr.listen_after_bind(); });
|
||||
auto se = detail::scope_exit([&] {
|
||||
svr.stop();
|
||||
t.join();
|
||||
});
|
||||
svr.wait_until_ready();
|
||||
|
||||
const std::string ping(110, 'p');
|
||||
const std::string half(50, 'a');
|
||||
const std::string over(60, 'b');
|
||||
const std::string too_big("\x88\x02\x03\xf1"); // Close 1009
|
||||
|
||||
// A Ping longer than the limit is answered, since the limit is for
|
||||
// messages. A message at the limit is echoed, and one over it is refused
|
||||
// although each of its frames is within it.
|
||||
EXPECT_EQ(std::string("\x8a\x6e") + ping + "\x81\x64" + half + half + too_big,
|
||||
raw_websocket_reply(
|
||||
port,
|
||||
masked_frame(0x89, ping) + masked_frame(0x01, half) +
|
||||
masked_frame(0x80, half) + masked_frame(0x01, over) +
|
||||
masked_frame(0x80, over),
|
||||
2 + ping.size() + 2 + 2 * half.size() + too_big.size()));
|
||||
|
||||
// A header that announces more than the limit is refused before its payload
|
||||
EXPECT_EQ(too_big, raw_websocket_reply(port,
|
||||
std::string("\x82\xfe\x03\xe8", 4) +
|
||||
std::string(4, '\0'),
|
||||
too_big.size()));
|
||||
}
|
||||
|
||||
TEST(WebSocketTest, ClientMaxMessageLength) {
|
||||
Server svr;
|
||||
svr.set_websocket_ping_interval(0);
|
||||
svr.WebSocket("/ws", [&](const Request &, ws::WebSocket &ws) {
|
||||
ws.send(std::string(1024, 'a'));
|
||||
ws.send(std::string(2048, 'b'));
|
||||
std::string msg;
|
||||
while (ws.read(msg)) {}
|
||||
});
|
||||
|
||||
auto port = svr.bind_to_any_port(HOST);
|
||||
std::thread t([&]() { svr.listen_after_bind(); });
|
||||
auto se = detail::scope_exit([&] {
|
||||
svr.stop();
|
||||
t.join();
|
||||
});
|
||||
svr.wait_until_ready();
|
||||
|
||||
ws::WebSocketClient client("ws://localhost:" + std::to_string(port) + "/ws");
|
||||
client.set_read_timeout(5, 0);
|
||||
client.set_websocket_max_message_length(1024);
|
||||
ASSERT_TRUE(client.connect());
|
||||
|
||||
std::string msg;
|
||||
ASSERT_EQ(ws::Text, client.read(msg));
|
||||
EXPECT_EQ(1024u, msg.size());
|
||||
|
||||
EXPECT_EQ(ws::Fail, client.read(msg));
|
||||
EXPECT_FALSE(client.is_open());
|
||||
|
||||
// 0 lifts the limit
|
||||
client.set_websocket_max_message_length(0);
|
||||
ASSERT_TRUE(client.connect());
|
||||
EXPECT_EQ(ws::Text, client.read(msg));
|
||||
EXPECT_EQ(ws::Text, client.read(msg));
|
||||
EXPECT_EQ(2048u, msg.size());
|
||||
}
|
||||
|
||||
TEST(WebSocketTest, NegativePingIntervalSendsNoPings) {
|
||||
Server svr;
|
||||
svr.set_websocket_ping_interval(-1);
|
||||
|
||||
Reference in new issue
Block a user