Skip to content

feat: adding possibility to manually set MD5 checksum and authorization for HTTP update #7629

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

Merged
merged 2 commits into from
Nov 29, 2023
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
27 changes: 22 additions & 5 deletions libraries/HTTPUpdate/src/HTTPUpdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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*);

Expand All @@ -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");
Expand Down Expand Up @@ -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();
Expand Down
20 changes: 20 additions & 0 deletions libraries/HTTPUpdate/src/HTTPUpdate.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "/",
Expand Down Expand Up @@ -123,6 +139,10 @@ class HTTPUpdate
private:
int _httpClientTimeout;
followRedirects_t _followRedirects;
String _user;
String _password;
String _auth;
String _md5Sum;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_md5Sum = String(); should be added in the class constructor after _followRedirects is set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But String _md5Sum; means it is automatically initialized empty String instance, isn't it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the compiler will complain. The String is not necessarily initialized. You can initialize it in two general ways:

HTTPUpdate::HTTPUpdate(int httpClientTimeout): _httpClientTimeout(httpClientTimeout), _ledPin(-1), _md5Sum(){
  _followRedirects = HTTPC_DISABLE_FOLLOW_REDIRECTS;
}

Or

HTTPUpdate::HTTPUpdate(int httpClientTimeout): _httpClientTimeout(httpClientTimeout), _ledPin(-1){
  _followRedirects = HTTPC_DISABLE_FOLLOW_REDIRECTS;
  _md5Sum = String();
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, understood. Thanks for the correction 👍. Fixed.


// Callbacks
HTTPUpdateStartCB _cbStart;
Expand Down