From 784ef0f286f37b0c2e8a0a786885343a044dea38 Mon Sep 17 00:00:00 2001 From: Marcos Del Sol Vives Date: Fri, 13 Dec 2024 02:55:30 +0100 Subject: [PATCH] fix(webserver): Cap size of last chunk in raw read in WebServer Before, the raw read would time out if the content length was not a multiple of HTTP_RAW_BUFLEN, as it tried to read HTTP_RAW_BUFLEN bytes even if the last chunk should actually contain less. --- libraries/WebServer/src/Parsing.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/libraries/WebServer/src/Parsing.cpp b/libraries/WebServer/src/Parsing.cpp index 875ca3057..eb468e4a0 100644 --- a/libraries/WebServer/src/Parsing.cpp +++ b/libraries/WebServer/src/Parsing.cpp @@ -186,8 +186,12 @@ bool WebServer::_parseRequest(NetworkClient &client) { _currentHandler->raw(*this, _currentUri, *_currentRaw); _currentRaw->status = RAW_WRITE; - while (_currentRaw->totalSize < _clientContentLength) { - _currentRaw->currentSize = client.readBytes(_currentRaw->buf, HTTP_RAW_BUFLEN); + while (1) { + size_t read_len = std::min(_clientContentLength - _currentRaw->totalSize, (size_t) HTTP_RAW_BUFLEN); + if (read_len == 0) { + break; + } + _currentRaw->currentSize = client.readBytes(_currentRaw->buf, read_len); _currentRaw->totalSize += _currentRaw->currentSize; if (_currentRaw->currentSize == 0) { _currentRaw->status = RAW_ABORTED;