@@ -43,6 +43,26 @@ ESP8266HTTPUpdate::~ESP8266HTTPUpdate(void)
4343{
4444}
4545
46+ /* *
47+ * set the Authorization for the http request
48+ * @param user const String&
49+ * @param password const String&
50+ */
51+ void ESP8266HTTPUpdate::setAuthorization (const String &user, const String &password)
52+ {
53+ _user = user;
54+ _password = password;
55+ }
56+
57+ /* *
58+ * set the Authorization for the http request
59+ * @param auth const String& base64
60+ */
61+ void ESP8266HTTPUpdate::setAuthorization (const String &auth)
62+ {
63+ _auth = auth;
64+ }
65+
4666#if HTTPUPDATE_1_2_COMPATIBLE
4767HTTPUpdateResult ESP8266HTTPUpdate::update (const String& url, const String& currentVersion,
4868 const String& httpsFingerprint, bool reboot)
@@ -241,6 +261,8 @@ String ESP8266HTTPUpdate::getLastErrorString(void)
241261 return F (" Verify Bin Header Failed" );
242262 case HTTP_UE_BIN_FOR_WRONG_FLASH:
243263 return F (" New Binary Does Not Fit Flash Size" );
264+ case HTTP_UE_SERVER_UNAUTHORIZED:
265+ return F (" Unauthorized (401)" );
244266 }
245267
246268 return String ();
@@ -282,6 +304,16 @@ HTTPUpdateResult ESP8266HTTPUpdate::handleUpdate(HTTPClient& http, const String&
282304 http.addHeader (F (" x-ESP8266-version" ), currentVersion);
283305 }
284306
307+ if (!_user.isEmpty () && !_password.isEmpty ())
308+ {
309+ http.setAuthorization (_user.c_str (), _password.c_str ());
310+ }
311+
312+ if (!_auth.isEmpty ())
313+ {
314+ http.setAuthorization (_auth.c_str ());
315+ }
316+
285317 const char * headerkeys[] = { " x-MD5" };
286318 size_t headerkeyssize = sizeof (headerkeys) / sizeof (char *);
287319
@@ -432,6 +464,10 @@ HTTPUpdateResult ESP8266HTTPUpdate::handleUpdate(HTTPClient& http, const String&
432464 _setLastError (HTTP_UE_SERVER_FORBIDDEN);
433465 ret = HTTP_UPDATE_FAILED;
434466 break ;
467+ case HTTP_CODE_UNAUTHORIZED:
468+ _setLastError (HTTP_UE_SERVER_UNAUTHORIZED);
469+ ret = HTTP_UPDATE_FAILED;
470+ break ;
435471 default :
436472 _setLastError (HTTP_UE_SERVER_WRONG_HTTP_CODE);
437473 ret = HTTP_UPDATE_FAILED;
0 commit comments