7
7
8
8
9
9
const 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'>
11
11
<input type='file' name='update'>
12
12
<input type='submit' value='Update'>
13
13
</form>
14
14
</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..." ;
15
17
16
18
ESP8266HTTPUpdateServer::ESP8266HTTPUpdateServer (bool serial_debug)
17
19
{
18
20
_serial_output = serial_debug;
19
21
_server = NULL ;
22
+ _username = NULL ;
23
+ _password = NULL ;
24
+ _authenticated = false ;
20
25
}
21
26
22
- void ESP8266HTTPUpdateServer::setup (ESP8266WebServer *server)
27
+ void ESP8266HTTPUpdateServer::setup (ESP8266WebServer *server, const char * path, const char * username, const char * password )
23
28
{
24
29
_server = server;
30
+ _username = (char *)username;
31
+ _password = (char *)password;
25
32
26
33
// 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 ( );
30
37
_server->send (200 , " text/html" , _serverIndex);
31
38
});
32
39
33
40
// 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 );
38
45
ESP.restart ();
39
46
},[&](){
40
47
// handler for the file upload, get's the sketch bytes, and writes
@@ -43,27 +50,35 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server)
43
50
if (upload.status == UPLOAD_FILE_START){
44
51
if (_serial_output)
45
52
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
+
46
61
WiFiUDP::stopAll ();
47
62
if (_serial_output)
48
63
Serial.printf (" Update: %s\n " , upload.filename .c_str ());
49
64
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace () - 0x1000 ) & 0xFFFFF000 ;
50
65
if (!Update.begin (maxSketchSpace)){// start with max available size
51
66
if (_serial_output) Update.printError (Serial);
52
67
}
53
- } else if (upload.status == UPLOAD_FILE_WRITE){
68
+ } else if (_authenticated && upload.status == UPLOAD_FILE_WRITE){
54
69
if (_serial_output) Serial.printf (" ." );
55
70
if (Update.write (upload.buf , upload.currentSize ) != upload.currentSize ){
56
71
if (_serial_output) Update.printError (Serial);
57
72
58
73
}
59
- } else if (upload.status == UPLOAD_FILE_END){
74
+ } else if (_authenticated && upload.status == UPLOAD_FILE_END){
60
75
if (Update.end (true )){ // true to set the size to the current progress
61
76
if (_serial_output) Serial.printf (" Update Success: %u\n Rebooting...\n " , upload.totalSize );
62
77
} else {
63
78
if (_serial_output) Update.printError (Serial);
64
79
}
65
80
if (_serial_output) Serial.setDebugOutput (false );
66
- } else if (upload.status == UPLOAD_FILE_ABORTED){
81
+ } else if (_authenticated && upload.status == UPLOAD_FILE_ABORTED){
67
82
Update.end ();
68
83
if (_serial_output) Serial.println (" Update was aborted" );
69
84
}
0 commit comments