Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lower memory usage #192

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading