Skip to content

Commit 7e0ee46

Browse files
committed
fix(websockets): Discard torn close reasons
1 parent d1fefc9 commit 7e0ee46

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

src/AsyncWebSocket.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -701,11 +701,12 @@ bool AsyncWebSocketClient::_handleClientFrame(uint8_t *data, size_t datalen, boo
701701
if (_pinfo.opcode == WS_DISCONNECT) {
702702
async_ws_log_v("[%s][%" PRIu32 "] DATA WS_DISCONNECT", _server->url(), _clientId);
703703

704-
if (datalen) {
704+
// Pass up the close frame error code if we can. If the frame was torn, the data was discarded.
705+
if ((_pinfo.index == 0) && (datalen >= 2)) {
705706
uint16_t reasonCode = (uint16_t)(data[0] << 8) + data[1];
706707
char *reasonString = (char *)(data + 2);
707708
if (reasonCode > 1001) {
708-
_server->_handleEvent(this, WS_EVT_ERROR, (void *)&reasonCode, (uint8_t *)reasonString, strlen(reasonString));
709+
_server->_handleEvent(this, WS_EVT_ERROR, (void *)&reasonCode, (uint8_t *)reasonString, strnlen(reasonString, datalen - 2));
709710
}
710711
}
711712
if (_status == WS_DISCONNECTING) {

0 commit comments

Comments
 (0)