Skip to content

Commit 8c150e2

Browse files
authored
feat: adding possibility to manually set MD5 checksum for HTTP update (#7629)
1 parent 789ae4c commit 8c150e2

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

Diff for: libraries/HTTPUpdate/src/HTTPUpdate.cpp

+22-5
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@
3333
// To do extern "C" uint32_t _SPIFFS_end;
3434

3535
HTTPUpdate::HTTPUpdate(void)
36-
: _httpClientTimeout(8000), _ledPin(-1)
36+
: HTTPUpdate(8000)
3737
{
38-
_followRedirects = HTTPC_DISABLE_FOLLOW_REDIRECTS;
3938
}
4039

4140
HTTPUpdate::HTTPUpdate(int httpClientTimeout)
4241
: _httpClientTimeout(httpClientTimeout), _ledPin(-1)
4342
{
4443
_followRedirects = HTTPC_DISABLE_FOLLOW_REDIRECTS;
44+
_md5Sum = String();
45+
_user = String();
46+
_password = String();
47+
_auth = String();
4548
}
4649

4750
HTTPUpdate::~HTTPUpdate(void)
@@ -220,6 +223,14 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
220223
requestCB(&http);
221224
}
222225

226+
if (!_user.isEmpty() && !_password.isEmpty()) {
227+
http.setAuthorization(_user.c_str(), _password.c_str());
228+
}
229+
230+
if (!_auth.isEmpty()) {
231+
http.setAuthorization(_auth.c_str());
232+
}
233+
223234
const char * headerkeys[] = { "x-MD5" };
224235
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
225236

@@ -243,8 +254,14 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
243254
log_d(" - code: %d\n", code);
244255
log_d(" - len: %d\n", len);
245256

246-
if(http.hasHeader("x-MD5")) {
247-
log_d(" - MD5: %s\n", http.header("x-MD5").c_str());
257+
String md5;
258+
if (_md5Sum.length()) {
259+
md5 = _md5Sum;
260+
} else if(http.hasHeader("x-MD5")) {
261+
md5 = http.header("x-MD5");
262+
}
263+
if(md5.length()) {
264+
log_d(" - MD5: %s\n",md5.c_str());
248265
}
249266

250267
log_d("ESP32 info:\n");
@@ -341,7 +358,7 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
341358
}
342359
*/
343360
}
344-
if(runUpdate(*tcp, len, http.header("x-MD5"), command)) {
361+
if(runUpdate(*tcp, len, md5, command)) {
345362
ret = HTTP_UPDATE_OK;
346363
log_d("Update ok\n");
347364
http.end();

Diff for: libraries/HTTPUpdate/src/HTTPUpdate.h

+20
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ class HTTPUpdate
8585
_ledOn = ledOn;
8686
}
8787

88+
void setMD5sum(const String &md5Sum)
89+
{
90+
_md5Sum = md5Sum;
91+
}
92+
93+
void setAuthorization(const String& user, const String& password)
94+
{
95+
_user = user;
96+
_password = password;
97+
}
98+
99+
void setAuthorization(const String& auth)
100+
{
101+
_auth = auth;
102+
}
103+
88104
t_httpUpdate_return update(WiFiClient& client, const String& url, const String& currentVersion = "", HTTPUpdateRequestCB requestCB = NULL);
89105

90106
t_httpUpdate_return update(WiFiClient& client, const String& host, uint16_t port, const String& uri = "/",
@@ -123,6 +139,10 @@ class HTTPUpdate
123139
private:
124140
int _httpClientTimeout;
125141
followRedirects_t _followRedirects;
142+
String _user;
143+
String _password;
144+
String _auth;
145+
String _md5Sum;
126146

127147
// Callbacks
128148
HTTPUpdateStartCB _cbStart;

0 commit comments

Comments
 (0)