mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-12 01:53:07 +03:00
Allman now (#6080)
* switch restyle script for CI * remove confirmation * restyle with allman
This commit is contained in:
committed by
david gauchard
parent
625c3a62c4
commit
98125f8860
@ -8,21 +8,21 @@
|
||||
|
||||
|
||||
static const char serverIndex[] PROGMEM =
|
||||
R"(<html><body><form method='POST' action='' enctype='multipart/form-data'>
|
||||
R"(<html><body><form method='POST' action='' enctype='multipart/form-data'>
|
||||
<input type='file' name='update'>
|
||||
<input type='submit' value='Update'>
|
||||
</form>
|
||||
</body></html>)";
|
||||
static const char successResponse[] PROGMEM =
|
||||
"<META http-equiv=\"refresh\" content=\"15;URL=/\">Update Success! Rebooting...\n";
|
||||
static const char successResponse[] PROGMEM =
|
||||
"<META http-equiv=\"refresh\" content=\"15;URL=/\">Update Success! Rebooting...\n";
|
||||
|
||||
ESP8266HTTPUpdateServer::ESP8266HTTPUpdateServer(bool serial_debug)
|
||||
{
|
||||
_serial_output = serial_debug;
|
||||
_server = NULL;
|
||||
_username = emptyString;
|
||||
_password = emptyString;
|
||||
_authenticated = false;
|
||||
_serial_output = serial_debug;
|
||||
_server = NULL;
|
||||
_username = emptyString;
|
||||
_password = emptyString;
|
||||
_authenticated = false;
|
||||
}
|
||||
|
||||
void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const String& path, const String& username, const String& password)
|
||||
@ -32,73 +32,117 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const String& path
|
||||
_password = password;
|
||||
|
||||
// handler for the /update form page
|
||||
_server->on(path.c_str(), HTTP_GET, [&](){
|
||||
if(_username != emptyString && _password != emptyString && !_server->authenticate(_username.c_str(), _password.c_str()))
|
||||
return _server->requestAuthentication();
|
||||
_server->send_P(200, PSTR("text/html"), serverIndex);
|
||||
_server->on(path.c_str(), HTTP_GET, [&]()
|
||||
{
|
||||
if (_username != emptyString && _password != emptyString && !_server->authenticate(_username.c_str(), _password.c_str()))
|
||||
{
|
||||
return _server->requestAuthentication();
|
||||
}
|
||||
_server->send_P(200, PSTR("text/html"), serverIndex);
|
||||
});
|
||||
|
||||
// handler for the /update form POST (once file upload finishes)
|
||||
_server->on(path.c_str(), HTTP_POST, [&](){
|
||||
if(!_authenticated)
|
||||
return _server->requestAuthentication();
|
||||
if (Update.hasError()) {
|
||||
_server->send(200, F("text/html"), String(F("Update error: ")) + _updaterError);
|
||||
} else {
|
||||
_server->client().setNoDelay(true);
|
||||
_server->send_P(200, PSTR("text/html"), successResponse);
|
||||
delay(100);
|
||||
_server->client().stop();
|
||||
ESP.restart();
|
||||
}
|
||||
},[&](){
|
||||
// handler for the file upload, get's the sketch bytes, and writes
|
||||
// them through the Update object
|
||||
HTTPUpload& upload = _server->upload();
|
||||
_server->on(path.c_str(), HTTP_POST, [&]()
|
||||
{
|
||||
if (!_authenticated)
|
||||
{
|
||||
return _server->requestAuthentication();
|
||||
}
|
||||
if (Update.hasError())
|
||||
{
|
||||
_server->send(200, F("text/html"), String(F("Update error: ")) + _updaterError);
|
||||
}
|
||||
else
|
||||
{
|
||||
_server->client().setNoDelay(true);
|
||||
_server->send_P(200, PSTR("text/html"), successResponse);
|
||||
delay(100);
|
||||
_server->client().stop();
|
||||
ESP.restart();
|
||||
}
|
||||
}, [&]()
|
||||
{
|
||||
// handler for the file upload, get's the sketch bytes, and writes
|
||||
// them through the Update object
|
||||
HTTPUpload& upload = _server->upload();
|
||||
|
||||
if(upload.status == UPLOAD_FILE_START){
|
||||
_updaterError = String();
|
||||
if (_serial_output)
|
||||
Serial.setDebugOutput(true);
|
||||
if (upload.status == UPLOAD_FILE_START)
|
||||
{
|
||||
_updaterError = String();
|
||||
if (_serial_output)
|
||||
{
|
||||
Serial.setDebugOutput(true);
|
||||
}
|
||||
|
||||
_authenticated = (_username == emptyString || _password == emptyString || _server->authenticate(_username.c_str(), _password.c_str()));
|
||||
if(!_authenticated){
|
||||
if (_serial_output)
|
||||
Serial.printf("Unauthenticated Update\n");
|
||||
return;
|
||||
}
|
||||
_authenticated = (_username == emptyString || _password == emptyString || _server->authenticate(_username.c_str(), _password.c_str()));
|
||||
if (!_authenticated)
|
||||
{
|
||||
if (_serial_output)
|
||||
{
|
||||
Serial.printf("Unauthenticated Update\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
WiFiUDP::stopAll();
|
||||
if (_serial_output)
|
||||
Serial.printf("Update: %s\n", upload.filename.c_str());
|
||||
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
|
||||
if(!Update.begin(maxSketchSpace)){//start with max available size
|
||||
_setUpdaterError();
|
||||
WiFiUDP::stopAll();
|
||||
if (_serial_output)
|
||||
{
|
||||
Serial.printf("Update: %s\n", upload.filename.c_str());
|
||||
}
|
||||
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
|
||||
if (!Update.begin(maxSketchSpace)) //start with max available size
|
||||
{
|
||||
_setUpdaterError();
|
||||
}
|
||||
}
|
||||
} else if(_authenticated && upload.status == UPLOAD_FILE_WRITE && !_updaterError.length()){
|
||||
if (_serial_output) Serial.printf(".");
|
||||
if(Update.write(upload.buf, upload.currentSize) != upload.currentSize){
|
||||
_setUpdaterError();
|
||||
else if (_authenticated && upload.status == UPLOAD_FILE_WRITE && !_updaterError.length())
|
||||
{
|
||||
if (_serial_output)
|
||||
{
|
||||
Serial.printf(".");
|
||||
}
|
||||
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize)
|
||||
{
|
||||
_setUpdaterError();
|
||||
}
|
||||
}
|
||||
} else if(_authenticated && upload.status == UPLOAD_FILE_END && !_updaterError.length()){
|
||||
if(Update.end(true)){ //true to set the size to the current progress
|
||||
if (_serial_output) Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
|
||||
} else {
|
||||
_setUpdaterError();
|
||||
else if (_authenticated && upload.status == UPLOAD_FILE_END && !_updaterError.length())
|
||||
{
|
||||
if (Update.end(true)) //true to set the size to the current progress
|
||||
{
|
||||
if (_serial_output)
|
||||
{
|
||||
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
_setUpdaterError();
|
||||
}
|
||||
if (_serial_output)
|
||||
{
|
||||
Serial.setDebugOutput(false);
|
||||
}
|
||||
}
|
||||
if (_serial_output) Serial.setDebugOutput(false);
|
||||
} else if(_authenticated && upload.status == UPLOAD_FILE_ABORTED){
|
||||
Update.end();
|
||||
if (_serial_output) Serial.println("Update was aborted");
|
||||
}
|
||||
delay(0);
|
||||
else if (_authenticated && upload.status == UPLOAD_FILE_ABORTED)
|
||||
{
|
||||
Update.end();
|
||||
if (_serial_output)
|
||||
{
|
||||
Serial.println("Update was aborted");
|
||||
}
|
||||
}
|
||||
delay(0);
|
||||
});
|
||||
}
|
||||
|
||||
void ESP8266HTTPUpdateServer::_setUpdaterError()
|
||||
{
|
||||
if (_serial_output) Update.printError(Serial);
|
||||
StreamString str;
|
||||
Update.printError(str);
|
||||
_updaterError = str.c_str();
|
||||
if (_serial_output)
|
||||
{
|
||||
Update.printError(Serial);
|
||||
}
|
||||
StreamString str;
|
||||
Update.printError(str);
|
||||
_updaterError = str.c_str();
|
||||
}
|
||||
|
@ -5,36 +5,36 @@ class ESP8266WebServer;
|
||||
|
||||
class ESP8266HTTPUpdateServer
|
||||
{
|
||||
public:
|
||||
ESP8266HTTPUpdateServer(bool serial_debug=false);
|
||||
public:
|
||||
ESP8266HTTPUpdateServer(bool serial_debug = false);
|
||||
|
||||
void setup(ESP8266WebServer *server)
|
||||
{
|
||||
setup(server, emptyString, emptyString);
|
||||
setup(server, emptyString, emptyString);
|
||||
}
|
||||
|
||||
void setup(ESP8266WebServer *server, const String& path)
|
||||
{
|
||||
setup(server, path, emptyString, emptyString);
|
||||
setup(server, path, emptyString, emptyString);
|
||||
}
|
||||
|
||||
void setup(ESP8266WebServer *server, const String& username, const String& password)
|
||||
{
|
||||
setup(server, "/update", username, password);
|
||||
setup(server, "/update", username, password);
|
||||
}
|
||||
|
||||
void setup(ESP8266WebServer *server, const String& path, const String& username, const String& password);
|
||||
|
||||
void updateCredentials(const String& username, const String& password)
|
||||
{
|
||||
_username = username;
|
||||
_password = password;
|
||||
_username = username;
|
||||
_password = password;
|
||||
}
|
||||
|
||||
protected:
|
||||
protected:
|
||||
void _setUpdaterError();
|
||||
|
||||
private:
|
||||
private:
|
||||
bool _serial_output;
|
||||
ESP8266WebServer *_server;
|
||||
String _username;
|
||||
|
Reference in New Issue
Block a user