Files
cpp-httplib/test
bb00fece7c Send a Close frame when a WebSocket read fails on the peer's error (#2694)
* Send a Close frame when a WebSocket read fails on the peer's error

WebSocket::read() answered a malformed Close with 1002 and invalid UTF-8
with 1007, but every other protocol error, and a message over
CPPHTTPLIB_WEBSOCKET_MAX_PAYLOAD_LENGTH, only marked the connection
closed, so the peer saw the TCP connection drop without a reason. That
covers set RSV bits, a mask bit the wrong way, a fragmented or oversized
control frame, a 64-bit length with its top bit set, a reserved opcode,
a continuation without a message to continue or a new message inside a
fragmented one, and a frame or message over the limit. RFC 6455 Section
7.1.7 says an endpoint that fails the connection should send a Close
frame with the status first.

FrameReader::read() now tells these apart from a failed read, with
FrameRead::ProtocolError and FrameRead::TooBig, and read() answers them,
and the opcode and fragmentation errors it finds itself, with a Close
frame of 1002 or 1009. As for the malformed Close, it does not wait for
the peer's reply: the stream may be in the middle of the bad frame, and
Section 7.1.7 says to read nothing more. A read that fails or times out
still closes without a frame. This applies to Server's WebSocket
handlers and to WebSocketClient alike.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Send every Close frame through one helper

WebSocket::close(), the Close echo and the new failure path each encoded
a status and wrote a Close frame under write_mutex_. They now share
send_close() and ws::impl::close_payload(), so every path that ends the
connection also wakes the heartbeat thread. The tests share a helper for
the raw-socket exchange and decode the client's Close with
read_websocket_frame().

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: yhirose <yuji.hirose.bug@gmail.com>
2026-11-26 17:55:50 -05:00
..
2021-09-11 14:26:48 -04:00
2021-09-11 14:26:48 -04:00
2017-12-29 22:34:59 -05:00
2013-07-04 18:18:52 -04:00
2024-11-16 11:14:13 -05:00