From 6f41eeba375794ffd68d17094d9338039a476b5c Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Tue, 10 Nov 2026 10:45:02 +0100 Subject: [PATCH] A couple of code cleanups --- src/AsyncWebServerRequest.cpp | 18 ++++++++---------- src/WebAuthentication.cpp | 11 ++++++++++- src/WebResponses.cpp | 9 ++++----- src/literals.h | 1 + 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/AsyncWebServerRequest.cpp b/src/AsyncWebServerRequest.cpp index 09fcee866..698963d88 100644 --- a/src/AsyncWebServerRequest.cpp +++ b/src/AsyncWebServerRequest.cpp @@ -71,8 +71,6 @@ void AsyncWebServerRequest::send(FS &fs, const String &path, const char *content * @return true if the ETag was successfully generated, false otherwise (e.g., file too short or seek failed). */ bool AsyncWebServerRequest::_getEtag(File gzFile, char *etag) { - static constexpr char hexChars[] = "0123456789ABCDEF"; - if (!gzFile.seek(gzFile.size() - 8)) { return false; } @@ -81,14 +79,14 @@ bool AsyncWebServerRequest::_getEtag(File gzFile, char *etag) { gzFile.read(reinterpret_cast(&crc), sizeof(crc)); etag[0] = '"'; - etag[1] = hexChars[(crc >> 4) & 0x0F]; - etag[2] = hexChars[crc & 0x0F]; - etag[3] = hexChars[(crc >> 12) & 0x0F]; - etag[4] = hexChars[(crc >> 8) & 0x0F]; - etag[5] = hexChars[(crc >> 20) & 0x0F]; - etag[6] = hexChars[(crc >> 16) & 0x0F]; - etag[7] = hexChars[(crc >> 28)]; - etag[8] = hexChars[(crc >> 24) & 0x0F]; + etag[1] = asyncsrv::T_hex_chars[(crc >> 4) & 0x0F]; + etag[2] = asyncsrv::T_hex_chars[crc & 0x0F]; + etag[3] = asyncsrv::T_hex_chars[(crc >> 12) & 0x0F]; + etag[4] = asyncsrv::T_hex_chars[(crc >> 8) & 0x0F]; + etag[5] = asyncsrv::T_hex_chars[(crc >> 20) & 0x0F]; + etag[6] = asyncsrv::T_hex_chars[(crc >> 16) & 0x0F]; + etag[7] = asyncsrv::T_hex_chars[(crc >> 28)]; + etag[8] = asyncsrv::T_hex_chars[(crc >> 24) & 0x0F]; etag[9] = '"'; etag[10] = '\0'; diff --git a/src/WebAuthentication.cpp b/src/WebAuthentication.cpp index 50a119540..58096e037 100644 --- a/src/WebAuthentication.cpp +++ b/src/WebAuthentication.cpp @@ -8,7 +8,16 @@ #include +// platform classification for the MD5 backend used in this file: +// - HAS_MD5_BUILDER: the MD5Builder wrapper class exists (ESP32 arduino, RP2040, HOST) +// - everything else (ESP8266, LibreTiny): low-level md5_context_t C API from #if defined(ESP32) || defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350) || defined(HOST) +#define HAS_MD5_BUILDER 1 +#else +#define HAS_MD5_BUILDER 0 +#endif + +#if HAS_MD5_BUILDER #include #else #include @@ -71,7 +80,7 @@ String generateBasicHash(const char *username, const char *password) { } static bool getMD5(uint8_t *data, uint16_t len, char *output) { // 33 bytes or more -#if defined(ESP32) || defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350) || defined(HOST) +#if HAS_MD5_BUILDER MD5Builder md5; md5.begin(); md5.add(data, len); diff --git a/src/WebResponses.cpp b/src/WebResponses.cpp index 0eb437a80..f1789a431 100644 --- a/src/WebResponses.cpp +++ b/src/WebResponses.cpp @@ -476,11 +476,10 @@ size_t AsyncAbstractResponse::write_send_buffs(AsyncWebServerRequest *request, s _fillBufferAndProcessTemplates(_send_buffer->data() + 6, std::min(_send_buffer->size(), tcp_win) - 8); // reserve 8 bytes for chunk size data if (readLen != RESPONSE_TRY_AGAIN) { // Write 4 hex digits directly without null terminator - static constexpr char hexChars[] = "0123456789abcdef"; - _send_buffer->data()[0] = hexChars[(readLen >> 12) & 0xF]; - _send_buffer->data()[1] = hexChars[(readLen >> 8) & 0xF]; - _send_buffer->data()[2] = hexChars[(readLen >> 4) & 0xF]; - _send_buffer->data()[3] = hexChars[readLen & 0xF]; + _send_buffer->data()[0] = asyncsrv::T_hex_chars[(readLen >> 12) & 0xF]; + _send_buffer->data()[1] = asyncsrv::T_hex_chars[(readLen >> 8) & 0xF]; + _send_buffer->data()[2] = asyncsrv::T_hex_chars[(readLen >> 4) & 0xF]; + _send_buffer->data()[3] = asyncsrv::T_hex_chars[readLen & 0xF]; _send_buffer->data()[4] = '\r'; _send_buffer->data()[5] = '\n'; // data (readLen bytes) is already there diff --git a/src/literals.h b/src/literals.h index 8871a6cbf..4be495ea7 100644 --- a/src/literals.h +++ b/src/literals.h @@ -112,6 +112,7 @@ static constexpr const char T_TRUE[] = "true"; static constexpr const char T_UPGRADE[] = "Upgrade"; static constexpr const char T_uri[] = "uri"; static constexpr const char T_username[] = "username"; +static constexpr const char T_hex_chars[] = "0123456789abcdef"; static constexpr const char T_WS[] = "websocket"; static constexpr const char T_WWW_AUTH[] = "WWW-Authenticate"; static constexpr const char T_X_Expected_Entity_Length[] = "X-Expected-Entity-Length";