From 4417cc36ea0927ebadd03d466528793a10e7b7d6 Mon Sep 17 00:00:00 2001 From: Mathieu Carbou Date: Mon, 10 Jun 2024 15:46:06 +0200 Subject: [PATCH] Lower memory usage --- src/ElegantOTA.cpp | 36 +++++++++++++++++------------------- src/ElegantOTA.h | 4 ++-- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/ElegantOTA.cpp b/src/ElegantOTA.cpp index 4bf7bfa..af979ed 100644 --- a/src/ElegantOTA.cpp +++ b/src/ElegantOTA.cpp @@ -16,7 +16,7 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username, #if ELEGANTOTA_USE_ASYNC_WEBSERVER == 1 _server->on("/update", HTTP_GET, [&](AsyncWebServerRequest *request){ - if(_authenticate && !request->authenticate(_username, _password)){ + if(_authenticate && !request->authenticate(_username.c_str(), _password.c_str())){ return request->requestAuthentication(); } AsyncWebServerResponse *response = request->beginResponse_P(200, "text/html", ELEGANT_HTML, sizeof(ELEGANT_HTML)); @@ -25,7 +25,7 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username, }); #else _server->on("/update", HTTP_GET, [&](){ - if (_authenticate && !_server->authenticate(_username, _password)) { + if (_authenticate && !_server->authenticate(_username.c_str(), _password.c_str())) { return _server->requestAuthentication(); } _server->sendHeader("Content-Encoding", "gzip"); @@ -35,7 +35,7 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username, #if ELEGANTOTA_USE_ASYNC_WEBSERVER == 1 _server->on("/ota/start", HTTP_GET, [&](AsyncWebServerRequest *request) { - if (_authenticate && !request->authenticate(_username, _password)) { + if (_authenticate && !request->authenticate(_username.c_str(), _password.c_str())) { return request->requestAuthentication(); } @@ -84,7 +84,7 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username, StreamString str; Update.printError(str); _update_error_str = str.c_str(); - _update_error_str += "\n"; + _update_error_str.concat("\n"); ELEGANTOTA_DEBUG_MSG(_update_error_str.c_str()); } #elif defined(ESP32) @@ -94,7 +94,7 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username, StreamString str; Update.printError(str); _update_error_str = str.c_str(); - _update_error_str += "\n"; + _update_error_str.concat("\n"); ELEGANTOTA_DEBUG_MSG(_update_error_str.c_str()); } #endif @@ -103,7 +103,7 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username, }); #else _server->on("/ota/start", HTTP_GET, [&]() { - if (_authenticate && !_server->authenticate(_username, _password)) { + if (_authenticate && !_server->authenticate(_username.c_str(), _password.c_str())) { return _server->requestAuthentication(); } @@ -152,7 +152,7 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username, StreamString str; Update.printError(str); _update_error_str = str.c_str(); - _update_error_str += "\n"; + _update_error_str.concat("\n"); ELEGANTOTA_DEBUG_MSG(_update_error_str.c_str()); } #elif defined(ESP32) @@ -162,7 +162,7 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username, StreamString str; Update.printError(str); _update_error_str = str.c_str(); - _update_error_str += "\n"; + _update_error_str.concat("\n"); ELEGANTOTA_DEBUG_MSG(_update_error_str.c_str()); } #elif defined(TARGET_RP2040) @@ -191,7 +191,7 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username, #if ELEGANTOTA_USE_ASYNC_WEBSERVER == 1 _server->on("/ota/upload", HTTP_POST, [&](AsyncWebServerRequest *request) { - if(_authenticate && !request->authenticate(_username, _password)){ + if(_authenticate && !request->authenticate(_username.c_str(), _password.c_str())){ return request->requestAuthentication(); } // Post-OTA update callback @@ -210,7 +210,7 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username, }, [&](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) { //Upload handler chunks in data if(_authenticate){ - if(!request->authenticate(_username, _password)){ + if(!request->authenticate(_username.c_str(), _password.c_str())){ return request->requestAuthentication(); } } @@ -236,7 +236,7 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username, StreamString str; Update.printError(str); _update_error_str = str.c_str(); - _update_error_str += "\n"; + _update_error_str.concat("\n"); ELEGANTOTA_DEBUG_MSG(_update_error_str.c_str()); } }else{ @@ -245,7 +245,7 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username, }); #else _server->on("/ota/upload", HTTP_POST, [&](){ - if (_authenticate && !_server->authenticate(_username, _password)) { + if (_authenticate && !_server->authenticate(_username.c_str(), _password.c_str())) { return _server->requestAuthentication(); } // Post-OTA update callback @@ -264,7 +264,7 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username, HTTPUpload& upload = _server->upload(); if (upload.status == UPLOAD_FILE_START) { // Check authentication - if (_authenticate && !_server->authenticate(_username, _password)) { + if (_authenticate && !_server->authenticate(_username.c_str(), _password.c_str())) { ELEGANTOTA_DEBUG_MSG("Authentication Failed on UPLOAD_FILE_START\n"); return; } @@ -289,7 +289,7 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username, StreamString str; Update.printError(str); _update_error_str = str.c_str(); - _update_error_str += "\n"; + _update_error_str.concat("\n"); ELEGANTOTA_DEBUG_MSG(_update_error_str.c_str()); } @@ -304,11 +304,9 @@ void ElegantOTAClass::begin(ELEGANTOTA_WEBSERVER *server, const char * username, } void ElegantOTAClass::setAuth(const char * username, const char * password){ - if (strlen(username) > 0 && strlen(password) > 0) { - strlcpy(_username, username, sizeof(_username)); - strlcpy(_password, password, sizeof(_password)); - _authenticate = true; - } + _username = username; + _password = password; + _authenticate = _username.length() && _password.length(); } void ElegantOTAClass::clearAuth(){ diff --git a/src/ElegantOTA.h b/src/ElegantOTA.h index eb961cb..ab50231 100644 --- a/src/ElegantOTA.h +++ b/src/ElegantOTA.h @@ -119,8 +119,8 @@ class ElegantOTAClass{ ELEGANTOTA_WEBSERVER *_server; bool _authenticate; - char _username[64]; - char _password[64]; + String _username; + String _password; bool _auto_reboot = true; bool _reboot = false;