diff --git a/libraries/HTTPUpdate/src/HTTPUpdate.cpp b/libraries/HTTPUpdate/src/HTTPUpdate.cpp index ceaf021c698..f42bd14ae86 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) @@ -220,6 +223,14 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren requestCB(&http); } + 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*); @@ -243,8 +254,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"); @@ -341,7 +358,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 0847deb837a..2e9f3c1b1bc 100644 --- a/libraries/HTTPUpdate/src/HTTPUpdate.h +++ b/libraries/HTTPUpdate/src/HTTPUpdate.h @@ -85,6 +85,22 @@ 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 = "", HTTPUpdateRequestCB requestCB = NULL); t_httpUpdate_return update(WiFiClient& client, const String& host, uint16_t port, const String& uri = "/", @@ -123,6 +139,10 @@ class HTTPUpdate private: int _httpClientTimeout; followRedirects_t _followRedirects; + String _user; + String _password; + String _auth; + String _md5Sum; // Callbacks HTTPUpdateStartCB _cbStart;