-
Notifications
You must be signed in to change notification settings - Fork 1
/
HTTP_update_server.cpp
127 lines (115 loc) · 4.21 KB
/
HTTP_update_server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <Arduino.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include "StreamString.h"
#include "HTTP_update_server.h"
static const char serverIndex[]
PROGMEM =
R"(<html><body><form method='POST' action='/update/flash' enctype='multipart/form-data'>
<input type='file' name='update'>
<input type='submit' value='Update Flash'>
</form>
<form method='POST' action='/update/spiffs' enctype='multipart/form-data'>
<input type='file' name='update'>
<input type='submit' value='Update SPIFFS'>
</form>
</body></html>)";
static const char successResponse[]
PROGMEM =
"<META http-equiv=\"refresh\" content=\"15;URL=/\">Update Success! Rebooting...\n";
HTTPUpdateServer::HTTPUpdateServer(bool serial_debug) {
_serial_output = serial_debug;
_server = NULL;
_username = emptyString;
_password = emptyString;
_authenticated = false;
}
void HTTPUpdateServer::setup(ESP8266WebServer *server, const String &username,
const String &password) {
_server = server;
_username = username;
_password = password;
// handler for the /update form page
_server->on("/update", 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("/update/flash", HTTP_POST,
[&]() {
handlePOSTFinish(U_FLASH);
},
[&]() {
handlePOSTData(U_FLASH);
});
_server->on("/update/spiffs", HTTP_POST,
[&]() {
handlePOSTFinish(U_FS);
},
[&]() {
handlePOSTData(U_FS);
});
}
void HTTPUpdateServer::_setUpdaterError() {
if (_serial_output) Update.printError(Serial);
StreamString str;
Update.printError(str);
_updaterError = str.c_str();
}
void HTTPUpdateServer::handlePOSTFinish(int target) {
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();
}
}
void HTTPUpdateServer::handlePOSTData(int target) {
// 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);
_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, target)) { //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_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);
} else if (_authenticated && upload.status == UPLOAD_FILE_ABORTED) {
Update.end();
if (_serial_output) Serial.println("Update was aborted");
}
delay(0);
}