Skip to content

Commit 9ba38e6

Browse files
committed
fix(websockets): Discard torn close reasons
1 parent 24da481 commit 9ba38e6

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
@@ -698,11 +698,12 @@ bool AsyncWebSocketClient::_handleClientFrame(uint8_t *data, size_t datalen, boo
698698
if (_pinfo.opcode == WS_DISCONNECT) {
699699
async_ws_log_v("[%s][%" PRIu32 "] DATA WS_DISCONNECT", _server->url(), _clientId);
700700

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

0 commit comments

Comments
 (0)