Skip to content

Commit ace0622

Browse files
committed
ESP8266HTTPUpdateServer: fix responses after uploading
- fix response not being delivered to the browser after upload is done (#2221) - if Update.begin fails, don’t attempt to write data - if update is not successful, send error message from Update to the client - move strings into PROGMEM
1 parent b623613 commit ace0622

File tree

2 files changed

+42
-23
lines changed

2 files changed

+42
-23
lines changed

libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer.cpp

+31-14
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@
33
#include <WiFiServer.h>
44
#include <ESP8266WebServer.h>
55
#include <WiFiUdp.h>
6+
#include "StreamString.h"
67
#include "ESP8266HTTPUpdateServer.h"
78

89

9-
const char* ESP8266HTTPUpdateServer::_serverIndex =
10-
R"(<html><body><form method='POST' action='' enctype='multipart/form-data'>
10+
static const char serverIndex[] PROGMEM =
11+
R"(<html><body><form method='POST' action='' enctype='multipart/form-data'>
1112
<input type='file' name='update'>
1213
<input type='submit' value='Update'>
1314
</form>
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+
</body></html>\n)";
16+
static const char successResponse[] PROGMEM =
17+
"<META http-equiv=\"refresh\" content=\"15;URL=\">Update Success! Rebooting...\n";
1718

1819
ESP8266HTTPUpdateServer::ESP8266HTTPUpdateServer(bool serial_debug)
1920
{
@@ -34,20 +35,29 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const char * path,
3435
_server->on(path, HTTP_GET, [&](){
3536
if(_username != NULL && _password != NULL && !_server->authenticate(_username, _password))
3637
return _server->requestAuthentication();
37-
_server->send(200, "text/html", _serverIndex);
38+
_server->send_P(200, PSTR("text/html"), serverIndex);
3839
});
3940

4041
// handler for the /update form POST (once file upload finishes)
4142
_server->on(path, HTTP_POST, [&](){
4243
if(!_authenticated)
4344
return _server->requestAuthentication();
44-
_server->send(200, "text/html", Update.hasError() ? _failedResponse : _successResponse);
45-
ESP.restart();
45+
if (Update.hasError()) {
46+
_server->send(200, F("text/html"), String(F("Update error: ")) + _updaterError);
47+
} else {
48+
_server->client().setNoDelay(true);
49+
_server->send_P(200, PSTR("text/html"), successResponse);
50+
delay(100);
51+
_server->client().stop();
52+
ESP.restart();
53+
}
4654
},[&](){
4755
// handler for the file upload, get's the sketch bytes, and writes
4856
// them through the Update object
4957
HTTPUpload& upload = _server->upload();
58+
5059
if(upload.status == UPLOAD_FILE_START){
60+
_updaterError = String();
5161
if (_serial_output)
5262
Serial.setDebugOutput(true);
5363

@@ -63,19 +73,18 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const char * path,
6373
Serial.printf("Update: %s\n", upload.filename.c_str());
6474
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
6575
if(!Update.begin(maxSketchSpace)){//start with max available size
66-
if (_serial_output) Update.printError(Serial);
76+
_setUpdaterError();
6777
}
68-
} else if(_authenticated && upload.status == UPLOAD_FILE_WRITE){
78+
} else if(_authenticated && upload.status == UPLOAD_FILE_WRITE && !_updaterError.length()){
6979
if (_serial_output) Serial.printf(".");
7080
if(Update.write(upload.buf, upload.currentSize) != upload.currentSize){
71-
if (_serial_output) Update.printError(Serial);
72-
81+
_setUpdaterError();
7382
}
74-
} else if(_authenticated && upload.status == UPLOAD_FILE_END){
83+
} else if(_authenticated && upload.status == UPLOAD_FILE_END && !_updaterError.length()){
7584
if(Update.end(true)){ //true to set the size to the current progress
7685
if (_serial_output) Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
7786
} else {
78-
if (_serial_output) Update.printError(Serial);
87+
_setUpdaterError();
7988
}
8089
if (_serial_output) Serial.setDebugOutput(false);
8190
} else if(_authenticated && upload.status == UPLOAD_FILE_ABORTED){
@@ -85,3 +94,11 @@ void ESP8266HTTPUpdateServer::setup(ESP8266WebServer *server, const char * path,
8594
delay(0);
8695
});
8796
}
97+
98+
void ESP8266HTTPUpdateServer::_setUpdaterError()
99+
{
100+
if (_serial_output) Update.printError(Serial);
101+
StreamString str;
102+
Update.printError(str);
103+
_updaterError = str.c_str();
104+
}

libraries/ESP8266HTTPUpdateServer/src/ESP8266HTTPUpdateServer.h

+11-9
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,6 @@ class ESP8266WebServer;
55

66
class ESP8266HTTPUpdateServer
77
{
8-
private:
9-
bool _serial_output;
10-
ESP8266WebServer *_server;
11-
static const char *_serverIndex;
12-
static const char *_failedResponse;
13-
static const char *_successResponse;
14-
char * _username;
15-
char * _password;
16-
bool _authenticated;
178
public:
189
ESP8266HTTPUpdateServer(bool serial_debug=false);
1910

@@ -33,6 +24,17 @@ class ESP8266HTTPUpdateServer
3324
}
3425

3526
void setup(ESP8266WebServer *server, const char * path, const char * username, const char * password);
27+
28+
protected:
29+
void _setUpdaterError();
30+
31+
private:
32+
bool _serial_output;
33+
ESP8266WebServer *_server;
34+
char * _username;
35+
char * _password;
36+
bool _authenticated;
37+
String _updaterError;
3638
};
3739

3840

0 commit comments

Comments
 (0)