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 1/4] 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 875ca305753..eb468e4a05b 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; From 71396de822b6c8734839462ed87bd3c6a5d43143 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Fri, 13 Dec 2024 09:09:09 +0100 Subject: [PATCH 2/4] fix(sntp): Lock / Unlock LWIP if CONFIG_LWIP_TCPIP_CORE_LOCKING is set - Fixes: #10526 - Completes old PR #10529 --- cores/esp32/esp32-hal-time.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/cores/esp32/esp32-hal-time.c b/cores/esp32/esp32-hal-time.c index 25c060eabdd..074e999be71 100644 --- a/cores/esp32/esp32-hal-time.c +++ b/cores/esp32/esp32-hal-time.c @@ -51,9 +51,6 @@ static void setTimeZone(long offset, int daylight) { void configTime(long gmtOffset_sec, int daylightOffset_sec, const char *server1, const char *server2, const char *server3) { //tcpip_adapter_init(); // Should not hurt anything if already inited esp_netif_init(); - if (sntp_enabled()) { - sntp_stop(); - } #ifdef CONFIG_LWIP_TCPIP_CORE_LOCKING if (!sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)) { @@ -61,6 +58,10 @@ void configTime(long gmtOffset_sec, int daylightOffset_sec, const char *server1, } #endif + if (sntp_enabled()) { + sntp_stop(); + } + sntp_setoperatingmode(SNTP_OPMODE_POLL); sntp_setservername(0, (char *)server1); sntp_setservername(1, (char *)server2); @@ -83,9 +84,6 @@ void configTime(long gmtOffset_sec, int daylightOffset_sec, const char *server1, void configTzTime(const char *tz, const char *server1, const char *server2, const char *server3) { //tcpip_adapter_init(); // Should not hurt anything if already inited esp_netif_init(); - if (sntp_enabled()) { - sntp_stop(); - } #ifdef CONFIG_LWIP_TCPIP_CORE_LOCKING if (!sys_thread_tcpip(LWIP_CORE_LOCK_QUERY_HOLDER)) { @@ -93,6 +91,10 @@ void configTzTime(const char *tz, const char *server1, const char *server2, cons } #endif + if (sntp_enabled()) { + sntp_stop(); + } + sntp_setoperatingmode(SNTP_OPMODE_POLL); sntp_setservername(0, (char *)server1); sntp_setservername(1, (char *)server2); From c562aeceda7457924fd255ebdbec90c005d19e1a Mon Sep 17 00:00:00 2001 From: Marcos Del Sol Vives Date: Fri, 13 Dec 2024 14:32:32 +0100 Subject: [PATCH 3/4] Update libraries/WebServer/src/Parsing.cpp Co-authored-by: Lucas Saavedra Vaz <32426024+lucasssvaz@users.noreply.github.com> --- libraries/WebServer/src/Parsing.cpp | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/libraries/WebServer/src/Parsing.cpp b/libraries/WebServer/src/Parsing.cpp index eb468e4a05b..aae89e3ec42 100644 --- a/libraries/WebServer/src/Parsing.cpp +++ b/libraries/WebServer/src/Parsing.cpp @@ -186,11 +186,8 @@ bool WebServer::_parseRequest(NetworkClient &client) { _currentHandler->raw(*this, _currentUri, *_currentRaw); _currentRaw->status = RAW_WRITE; - while (1) { + while (_currentRaw->totalSize < _clientContentLength) { 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) { From 8b77d91b69f5ea5648c9b9307eba72393cf49f8b Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Fri, 13 Dec 2024 13:45:07 +0000 Subject: [PATCH 4/4] ci(pre-commit): Apply automatic fixes --- libraries/WebServer/src/Parsing.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/WebServer/src/Parsing.cpp b/libraries/WebServer/src/Parsing.cpp index aae89e3ec42..040338bb749 100644 --- a/libraries/WebServer/src/Parsing.cpp +++ b/libraries/WebServer/src/Parsing.cpp @@ -187,7 +187,7 @@ bool WebServer::_parseRequest(NetworkClient &client) { _currentRaw->status = RAW_WRITE; while (_currentRaw->totalSize < _clientContentLength) { - size_t read_len = std::min(_clientContentLength - _currentRaw->totalSize, (size_t) HTTP_RAW_BUFLEN); + size_t read_len = std::min(_clientContentLength - _currentRaw->totalSize, (size_t)HTTP_RAW_BUFLEN); _currentRaw->currentSize = client.readBytes(_currentRaw->buf, read_len); _currentRaw->totalSize += _currentRaw->currentSize; if (_currentRaw->currentSize == 0) {