mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-11-24 20:31:50 +00:00
mark a websocket closed when a frame write fails
WebSocket::send_frame returned false when write_websocket_frame failed but left the connection open, and the Pong reply in read() ignored a failed write. A write that fails partway (the write timeout expiring while the peer is not reading) has already sent a frame header that announces the whole payload, so anything written after it is read by the peer as the rest of that frame, and a later payload's bytes as a frame of their own. Mark the connection closed on a failed write, as the heartbeat already does for its Ping, and send the Pong through send_frame.
This commit is contained in:
1 parent
277b1d62a4
commit
77b035c892
3 files changed
+67
-8
No files matched your search
@@ -29615,6 +29615,62 @@ TEST(WebSocketPartialWriteTest, FrameIsWrittenWhole) {
|
||||
|
||||
client.close();
|
||||
}
|
||||
|
||||
// A send() that fails partway leaves a frame on the wire whose header announces
|
||||
// more than was sent. Nothing may follow it, or the peer reads the next frames
|
||||
// as the rest of it and a later payload's bytes as a frame of their own.
|
||||
TEST(WebSocketPartialWriteTest, FailedWriteClosesConnection) {
|
||||
signal(SIGPIPE, SIG_IGN);
|
||||
|
||||
const std::string large(4 * 1024 * 1024, 'A');
|
||||
std::atomic<bool> large_sent{true};
|
||||
std::atomic<bool> open_after_failure{true};
|
||||
std::atomic<bool> small_sent{true};
|
||||
std::atomic<bool> handler_done{false};
|
||||
|
||||
Server svr;
|
||||
svr.set_write_timeout(0, 200000);
|
||||
svr.set_socket_options([](socket_t sock) {
|
||||
int size = 8192;
|
||||
setsockopt(sock, SOL_SOCKET, SO_SNDBUF,
|
||||
reinterpret_cast<const char *>(&size), sizeof(size));
|
||||
});
|
||||
svr.WebSocket("/ws", [&](const Request &, ws::WebSocket &ws) {
|
||||
large_sent = ws.send(large.data(), large.size());
|
||||
open_after_failure = ws.is_open();
|
||||
small_sent = ws.send("after");
|
||||
handler_done = true;
|
||||
});
|
||||
|
||||
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://" + std::string(HOST) + ":" +
|
||||
std::to_string(port) + "/ws");
|
||||
client.set_socket_options([](socket_t sock) {
|
||||
int size = 8192;
|
||||
setsockopt(sock, SOL_SOCKET, SO_RCVBUF,
|
||||
reinterpret_cast<const char *>(&size), sizeof(size));
|
||||
});
|
||||
ASSERT_TRUE(client.connect());
|
||||
|
||||
// Read nothing, so the server's write times out partway through the frame.
|
||||
for (int i = 0; i < 100 && !handler_done; i++) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
}
|
||||
ASSERT_TRUE(handler_done);
|
||||
|
||||
EXPECT_FALSE(large_sent);
|
||||
EXPECT_FALSE(open_after_failure);
|
||||
EXPECT_FALSE(small_sent);
|
||||
|
||||
client.close();
|
||||
}
|
||||
#endif
|
||||
|
||||
TEST(WebSocketPreRoutingTest, RejectWithoutAuth) {
|
||||
|
||||
Reference in new issue
Block a user