1
0
mirror of https://github.com/arduino-libraries/ArduinoHttpClient.git synced 2026-10-23 10:09:02 +03:00

Merge pull request #203 from pennam/websocket-parse-fix

WebSocketClient: check extended length/mask bytes are available
This commit is contained in:
Mattia Pennasilico
2026-07-28 10:36:46 +02:00
committed by GitHub

View File

@@ -206,25 +206,54 @@ int WebSocketClient::parseMessage()
iRxMasked = (length & 0x80);
length &= 0x7f;
// work out how many extended length and mask bytes follow the
// initial opcode + length bytes
int extendedLength = 0;
if (length == 126)
{
extendedLength = 2;
}
else if (length == 127)
{
extendedLength = 8;
}
int maskLength = iRxMasked ? (int)sizeof(iRxMaskKey) : 0;
// make sure the remaining header bytes are available before reading
// them, otherwise the frame header is incomplete
if (HttpClient::available() < (extendedLength + maskLength))
{
return 0;
}
// read the RX size
if (length < 126)
iRxSize = length;
if (length == 126)
{
iRxSize = length;
// read each byte in a well-defined order (the evaluation order of
// multiple read() calls in a single expression is unspecified)
uint8_t b1 = HttpClient::read();
uint8_t b0 = HttpClient::read();
iRxSize = ((uint64_t)b1 << 8) | b0;
}
else if (length == 126)
else if (length == 127)
{
iRxSize = (HttpClient::read() << 8) | HttpClient::read();
}
else
{
iRxSize = ((uint64_t)HttpClient::read() << 56) |
((uint64_t)HttpClient::read() << 48) |
((uint64_t)HttpClient::read() << 40) |
((uint64_t)HttpClient::read() << 32) |
((uint64_t)HttpClient::read() << 24) |
((uint64_t)HttpClient::read() << 16) |
((uint64_t)HttpClient::read() << 8) |
(uint64_t)HttpClient::read();
uint8_t b7 = HttpClient::read();
uint8_t b6 = HttpClient::read();
uint8_t b5 = HttpClient::read();
uint8_t b4 = HttpClient::read();
uint8_t b3 = HttpClient::read();
uint8_t b2 = HttpClient::read();
uint8_t b1 = HttpClient::read();
uint8_t b0 = HttpClient::read();
iRxSize = ((uint64_t)b7 << 56) |
((uint64_t)b6 << 48) |
((uint64_t)b5 << 40) |
((uint64_t)b4 << 32) |
((uint64_t)b3 << 24) |
((uint64_t)b2 << 16) |
((uint64_t)b1 << 8) |
(uint64_t)b0;
}
// read in the mask, if present