Skip to content

Commit d2fd7ad

Browse files
committed
feat: adding possibility to manually set MD5 checksum for HTTP update
1 parent 0d84018 commit d2fd7ad

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

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)
@@ -217,6 +220,14 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
217220
http.addHeader("x-ESP32-version", currentVersion);
218221
}
219222

223+
if (!_user.isEmpty() && !_password.isEmpty()) {
224+
http.setAuthorization(_user.c_str(), _password.c_str());
225+
}
226+
227+
if (!_auth.isEmpty()) {
228+
http.setAuthorization(_auth.c_str());
229+
}
230+
220231
const char * headerkeys[] = { "x-MD5" };
221232
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
222233

@@ -240,8 +251,14 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
240251
log_d(" - code: %d\n", code);
241252
log_d(" - len: %d\n", len);
242253

243-
if(http.hasHeader("x-MD5")) {
244-
log_d(" - MD5: %s\n", http.header("x-MD5").c_str());
254+
String md5;
255+
if (_md5Sum.length()) {
256+
md5 = _md5Sum;
257+
} else if(http.hasHeader("x-MD5")) {
258+
md5 = http.header("x-MD5");
259+
}
260+
if(md5.length()) {
261+
log_d(" - MD5: %s\n",md5.c_str());
245262
}
246263

247264
log_d("ESP32 info:\n");
@@ -338,7 +355,7 @@ HTTPUpdateResult HTTPUpdate::handleUpdate(HTTPClient& http, const String& curren
338355
}
339356
*/
340357
}
341-
if(runUpdate(*tcp, len, http.header("x-MD5"), command)) {
358+
if(runUpdate(*tcp, len, md5, command)) {
342359
ret = HTTP_UPDATE_OK;
343360
log_d("Update ok\n");
344361
http.end();

libraries/HTTPUpdate/src/HTTPUpdate.h

+19
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,21 @@ class HTTPUpdate
8484
_ledOn = ledOn;
8585
}
8686

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

89104
t_httpUpdate_return update(WiFiClient& client, const String& host, uint16_t port, const String& uri = "/",
@@ -121,6 +136,10 @@ class HTTPUpdate
121136
private:
122137
int _httpClientTimeout;
123138
followRedirects_t _followRedirects;
139+
String _user;
140+
String _password;
141+
String _auth;
142+
String _md5Sum;
124143

125144
// Callbacks
126145
HTTPUpdateStartCB _cbStart;

0 commit comments

Comments
 (0)