From d2fd7ad17da303a7edbd646f72195e056b724ba1 Mon Sep 17 00:00:00 2001
From: vlastahajek <29980246+vlastahajek@users.noreply.github.com>
Date: Thu, 23 Feb 2023 09:11:57 +0100
Subject: [PATCH] feat: adding possibility to manually set MD5 checksum for
 HTTP update

---
 libraries/HTTPUpdate/src/HTTPUpdate.cpp | 27 ++++++++++++++++++++-----
 libraries/HTTPUpdate/src/HTTPUpdate.h   | 19 +++++++++++++++++
 2 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/libraries/HTTPUpdate/src/HTTPUpdate.cpp b/libraries/HTTPUpdate/src/HTTPUpdate.cpp
index 9d1c782225f..9a0c2af39cb 100644
--- a/libraries/HTTPUpdate/src/HTTPUpdate.cpp
+++ b/libraries/HTTPUpdate/src/HTTPUpdate.cpp
@@ -33,15 +33,18 @@
 // To do extern "C" uint32_t _SPIFFS_end;
 
 HTTPUpdate::HTTPUpdate(void)
-        : _httpClientTimeout(8000), _ledPin(-1)
+        : HTTPUpdate(8000)
 {
-    _followRedirects = HTTPC_DISABLE_FOLLOW_REDIRECTS;
 }
 
 HTTPUpdate::HTTPUpdate(int httpClientTimeout)
         : _httpClientTimeout(httpClientTimeout), _ledPin(-1)
 {
     _followRedirects = HTTPC_DISABLE_FOLLOW_REDIRECTS;
+    _md5Sum = String();
+    _user = String();
+    _password = String();
+    _auth = String();
 }
 
 HTTPUpdate::~HTTPUpdate(void)
@@ -217,6 +220,14 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
         http.addHeader("x-ESP32-version", currentVersion);
     }
 
+    if (!_user.isEmpty() && !_password.isEmpty()) {
+        http.setAuthorization(_user.c_str(), _password.c_str());
+    }
+
+    if (!_auth.isEmpty()) {
+        http.setAuthorization(_auth.c_str());
+    }
+
     const char * headerkeys[] = { "x-MD5" };
     size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
 
@@ -240,8 +251,14 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
     log_d(" - code: %d\n", code);
     log_d(" - len: %d\n", len);
 
-    if(http.hasHeader("x-MD5")) {
-        log_d(" - MD5: %s\n", http.header("x-MD5").c_str());
+    String md5;
+    if (_md5Sum.length()) {
+        md5 = _md5Sum; 
+    } else if(http.hasHeader("x-MD5")) {
+        md5 = http.header("x-MD5");
+    }
+    if(md5.length()) {
+        log_d(" - MD5: %s\n",md5.c_str());
     }
 
     log_d("ESP32 info:\n");
@@ -338,7 +355,7 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
                     }
 */
                 }
-                if(runUpdate(*tcp, len, http.header("x-MD5"), command)) {
+                if(runUpdate(*tcp, len, md5, command)) {
                     ret = HTTP_UPDATE_OK;
                     log_d("Update ok\n");
                     http.end();
diff --git a/libraries/HTTPUpdate/src/HTTPUpdate.h b/libraries/HTTPUpdate/src/HTTPUpdate.h
index ca287399e68..05300633727 100644
--- a/libraries/HTTPUpdate/src/HTTPUpdate.h
+++ b/libraries/HTTPUpdate/src/HTTPUpdate.h
@@ -84,6 +84,21 @@ class HTTPUpdate
         _ledOn = ledOn;
     }
 
+    void setMD5sum(const String &md5Sum) 
+    {
+        _md5Sum = md5Sum;
+    }
+
+ void setAuthorization(const String& user, const String& password) 
+    {
+        _user = user;
+        _password = password;
+    }
+    void setAuthorization(const String& auth)
+    {
+        _auth = auth;
+    }
+
     t_httpUpdate_return update(WiFiClient& client, const String& url, const String& currentVersion = "");
 
     t_httpUpdate_return update(WiFiClient& client, const String& host, uint16_t port, const String& uri = "/",
@@ -121,6 +136,10 @@ class HTTPUpdate
 private:
     int _httpClientTimeout;
     followRedirects_t _followRedirects;
+    String _user;
+    String _password;
+    String _auth;
+    String _md5Sum;
 
     // Callbacks
     HTTPUpdateStartCB    _cbStart;