Skip to content

Commit

Permalink
Merge pull request #192 from mathieucarbou/memory
Browse files Browse the repository at this point in the history
Lower memory usage
  • Loading branch information
ayushsharma82 authored Jun 13, 2024
2 parents 79c562e + 4417cc3 commit b451846
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
36 changes: 17 additions & 19 deletions src/ElegantOTA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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");
Expand All @@ -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();
}

Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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();
}

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
}
Expand All @@ -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{
Expand All @@ -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
Expand All @@ -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;
}
Expand All @@ -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());
}

Expand All @@ -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(){
Expand Down
4 changes: 2 additions & 2 deletions src/ElegantOTA.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit b451846

Please sign in to comment.