77
88
99const char * ESP8266HTTPUpdateServer::_serverIndex =
10- R"( <html><body><form method='POST' action='/update ' enctype='multipart/form-data'>
10+ R"( <html><body><form method='POST' action='' enctype='multipart/form-data'>
1111 <input type='file' name='update'>
1212 <input type='submit' value='Update'>
1313 </form>
1414 </body></html>)" ;
15+ const char * ESP8266HTTPUpdateServer::_failedResponse = R"( Update Failed!)" ;
16+ const char * ESP8266HTTPUpdateServer::_successResponse = " <META http-equiv=\" refresh\" content=\" 15;URL=\" >Update Success! Rebooting..." ;
1517
1618ESP8266HTTPUpdateServer::ESP8266HTTPUpdateServer (bool serial_debug)
1719{
1820 _serial_output = serial_debug;
1921 _server = NULL ;
22+ _username = NULL ;
23+ _password = NULL ;
24+ _authenticated = false ;
2025}
2126
22- void ESP8266HTTPUpdateServer::setup (ESP8266WebServer *server)
27+ void ESP8266HTTPUpdateServer::setup (ESP8266WebServer *server, const char * path, const char * username, const char * password )
2328{
2429 _server = server;
30+ _username = (char *)username;
31+ _password = (char *)password;
2532
2633 // handler for the /update form page
27- _server->on (" /update " , HTTP_GET, [&](){
28- _server->sendHeader ( " Connection " , " close " );
29- _server->sendHeader ( " Access-Control-Allow-Origin " , " * " );
34+ _server->on (path , HTTP_GET, [&](){
35+ if (_username != NULL && _password != NULL && ! _server->authenticate (_username, _password))
36+ return _server->requestAuthentication ( );
3037 _server->send (200 , " text/html" , _serverIndex);
3138 });
3239
3340 // handler for the /update form POST (once file upload finishes)
34- _server->on (" /update " , HTTP_POST, [&](){
35- _server-> sendHeader ( " Connection " , " close " );
36- _server->sendHeader ( " Access-Control-Allow-Origin " , " * " );
37- _server->send (200 , " text/html" , ( Update.hasError ())? " FAIL " : " <META http-equiv= \" refresh \" content= \" 15;URL=/update \" >OK " );
41+ _server->on (path , HTTP_POST, [&](){
42+ if (!_authenticated)
43+ return _server->requestAuthentication ( );
44+ _server->send (200 , " text/html" , Update.hasError () ? _failedResponse : _successResponse );
3845 ESP.restart ();
3946 },[&](){
4047 // handler for the file upload, get's the sketch bytes, and writes
@@ -43,27 +50,35 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server)
4350 if (upload.status == UPLOAD_FILE_START){
4451 if (_serial_output)
4552 Serial.setDebugOutput (true );
53+
54+ _authenticated = (_username == NULL || _password == NULL || _server->authenticate (_username, _password));
55+ if (!_authenticated){
56+ if (_serial_output)
57+ Serial.printf (" Unauthenticated Update\n " );
58+ return ;
59+ }
60+
4661 WiFiUDP::stopAll ();
4762 if (_serial_output)
4863 Serial.printf (" Update: %s\n " , upload.filename .c_str ());
4964 uint32_t maxSketchSpace = (ESP.getFreeSketchSpace () - 0x1000 ) & 0xFFFFF000 ;
5065 if (!Update.begin (maxSketchSpace)){// start with max available size
5166 if (_serial_output) Update.printError (Serial);
5267 }
53- } else if (upload.status == UPLOAD_FILE_WRITE){
68+ } else if (_authenticated && upload.status == UPLOAD_FILE_WRITE){
5469 if (_serial_output) Serial.printf (" ." );
5570 if (Update.write (upload.buf , upload.currentSize ) != upload.currentSize ){
5671 if (_serial_output) Update.printError (Serial);
5772
5873 }
59- } else if (upload.status == UPLOAD_FILE_END){
74+ } else if (_authenticated && upload.status == UPLOAD_FILE_END){
6075 if (Update.end (true )){ // true to set the size to the current progress
6176 if (_serial_output) Serial.printf (" Update Success: %u\n Rebooting...\n " , upload.totalSize );
6277 } else {
6378 if (_serial_output) Update.printError (Serial);
6479 }
6580 if (_serial_output) Serial.setDebugOutput (false );
66- } else if (upload.status == UPLOAD_FILE_ABORTED){
81+ } else if (_authenticated && upload.status == UPLOAD_FILE_ABORTED){
6782 Update.end ();
6883 if (_serial_output) Serial.println (" Update was aborted" );
6984 }
0 commit comments