1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-21 10:26:06 +03:00

ESP8266httpUpdate: decouple HTTPS overloads

This commit is contained in:
Ivan Grokhotkov 2016-04-05 01:56:29 +03:00
parent cae4039225
commit c450023a32
2 changed files with 489 additions and 449 deletions

View File

@ -35,51 +35,69 @@ ESP8266HTTPUpdate::ESP8266HTTPUpdate(void) {
ESP8266HTTPUpdate::~ESP8266HTTPUpdate(void) { ESP8266HTTPUpdate::~ESP8266HTTPUpdate(void) {
} }
/** t_httpUpdate_return ESP8266HTTPUpdate::update(const String& url, const String& currentVersion,
* const String& httpsFingerprint, bool reboot)
* @param url const char * {
* @param current_version const char * rebootOnUpdate(reboot);
* @param httpsFingerprint const char * return update(url, currentVersion, httpsFingerprint);
* @return t_httpUpdate_return }
*/
t_httpUpdate_return ESP8266HTTPUpdate::update(const char * url, const char * current_version, const char * httpsFingerprint, bool reboot) { t_httpUpdate_return ESP8266HTTPUpdate::update(const String& url, const String& currentVersion)
{
HTTPClient http;
http.begin(url);
return handleUpdate(http, currentVersion, false);
}
t_httpUpdate_return ESP8266HTTPUpdate::update(const String& url, const String& currentVersion,
const String& httpsFingerprint)
{
HTTPClient http; HTTPClient http;
http.begin(url, httpsFingerprint); http.begin(url, httpsFingerprint);
return handleUpdate(&http, current_version, reboot, false); return handleUpdate(http, currentVersion, false);
} }
/**
* t_httpUpdate_return ESP8266HTTPUpdate::updateSpiffs(const String& url, const String& currentVersion, const String& httpsFingerprint)
* @param url const char * {
* @param current_version const char *
* @param httpsFingerprint const char *
* @return t_httpUpdate_return
*/
t_httpUpdate_return ESP8266HTTPUpdate::updateSpiffs(const char * url, const char * current_version, const char * httpsFingerprint, bool reboot) {
HTTPClient http; HTTPClient http;
http.begin(url, httpsFingerprint); http.begin(url, httpsFingerprint);
return handleUpdate(&http, current_version, reboot, true); return handleUpdate(http, currentVersion, true);
} }
/** t_httpUpdate_return ESP8266HTTPUpdate::updateSpiffs(const String& url, const String& currentVersion)
* {
* @param host const char *
* @param port uint16_t
* @param url const char *
* @param current_version const char *
* @param httpsFingerprint const char *
* @return
*/
t_httpUpdate_return ESP8266HTTPUpdate::update(const char * host, uint16_t port, const char * url, const char * current_version, bool https, const char * httpsFingerprint, bool reboot) {
HTTPClient http; HTTPClient http;
http.begin(host, port, url, https, httpsFingerprint); http.begin(url);
return handleUpdate(&http, current_version, reboot, false); return handleUpdate(http, currentVersion, true);
} }
t_httpUpdate_return ESP8266HTTPUpdate::update(String host, uint16_t port, String url, String current_version, bool https, String httpsFingerprint, bool reboot) { t_httpUpdate_return ESP8266HTTPUpdate::update(const String& host, uint16_t port, const String& uri, const String& currentVersion,
bool https, const String& httpsFingerprint, bool reboot)
{
rebootOnUpdate(reboot);
if (httpsFingerprint.length() == 0) {
return update(host, port, uri, currentVersion);
}
else {
return update(host, port, uri, currentVersion, httpsFingerprint);
}
}
t_httpUpdate_return ESP8266HTTPUpdate::update(const String& host, uint16_t port, const String& uri,
const String& currentVersion)
{
HTTPClient http; HTTPClient http;
http.begin(host, port, url, https, httpsFingerprint); http.begin(host, port, uri);
return handleUpdate(&http, current_version.c_str(), reboot, false); return handleUpdate(http, currentVersion, false);
}
t_httpUpdate_return ESP8266HTTPUpdate::update(const String& host, uint16_t port, const String& url,
const String& currentVersion, const String& httpsFingerprint)
{
HTTPClient http;
http.begin(host, port, url, httpsFingerprint);
return handleUpdate(http, currentVersion, false);
} }
/** /**
@ -87,7 +105,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::update(String host, uint16_t port, String
* @return int error code * @return int error code
*/ */
int ESP8266HTTPUpdate::getLastError(void){ int ESP8266HTTPUpdate::getLastError(void){
return lastError; return _lastError;
} }
/** /**
@ -96,12 +114,12 @@ int ESP8266HTTPUpdate::getLastError(void){
*/ */
String ESP8266HTTPUpdate::getLastErrorString(void) { String ESP8266HTTPUpdate::getLastErrorString(void) {
if(lastError == 0) { if(_lastError == 0) {
return String(); // no error return String(); // no error
} }
// error from Update class // error from Update class
if(lastError > 0) { if(_lastError > 0) {
StreamString error; StreamString error;
Update.printError(error); Update.printError(error);
error.trim(); // remove line ending error.trim(); // remove line ending
@ -109,11 +127,11 @@ String ESP8266HTTPUpdate::getLastErrorString(void) {
} }
// error from http client // error from http client
if(lastError > -100) { if(_lastError > -100) {
return "HTTP error: " + HTTPClient::errorToString(lastError); return "HTTP error: " + HTTPClient::errorToString(_lastError);
} }
switch(lastError) { switch(_lastError) {
case HTTP_UE_TOO_LESS_SPACE: case HTTP_UE_TOO_LESS_SPACE:
return String("To less space"); return String("To less space");
case HTTP_UE_SERVER_NOT_REPORT_SIZE: case HTTP_UE_SERVER_NOT_REPORT_SIZE:
@ -139,48 +157,48 @@ String ESP8266HTTPUpdate::getLastErrorString(void) {
/** /**
* *
* @param http HTTPClient * * @param http HTTPClient *
* @param current_version const char * * @param currentVersion const char *
* @return t_httpUpdate_return * @return t_httpUpdate_return
*/ */
t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const char * current_version, bool reboot, bool spiffs) { t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs) {
t_httpUpdate_return ret = HTTP_UPDATE_FAILED; t_httpUpdate_return ret = HTTP_UPDATE_FAILED;
// use HTTP/1.0 for update since the update handler not support any transfer Encoding // use HTTP/1.0 for update since the update handler not support any transfer Encoding
http->useHTTP10(true); http.useHTTP10(true);
http->setTimeout(8000); http.setTimeout(8000);
http->setUserAgent("ESP8266-http-Update"); http.setUserAgent("ESP8266-http-Update");
http->addHeader("x-ESP8266-STA-MAC", WiFi.macAddress()); http.addHeader("x-ESP8266-STA-MAC", WiFi.macAddress());
http->addHeader("x-ESP8266-AP-MAC", WiFi.softAPmacAddress()); http.addHeader("x-ESP8266-AP-MAC", WiFi.softAPmacAddress());
http->addHeader("x-ESP8266-free-space", String(ESP.getFreeSketchSpace())); http.addHeader("x-ESP8266-free-space", String(ESP.getFreeSketchSpace()));
http->addHeader("x-ESP8266-sketch-size", String(ESP.getSketchSize())); http.addHeader("x-ESP8266-sketch-size", String(ESP.getSketchSize()));
http->addHeader("x-ESP8266-chip-size", String(ESP.getFlashChipRealSize())); http.addHeader("x-ESP8266-chip-size", String(ESP.getFlashChipRealSize()));
http->addHeader("x-ESP8266-sdk-version", ESP.getSdkVersion()); http.addHeader("x-ESP8266-sdk-version", ESP.getSdkVersion());
if(spiffs) { if(spiffs) {
http->addHeader("x-ESP8266-mode", "spiffs"); http.addHeader("x-ESP8266-mode", "spiffs");
} else { } else {
http->addHeader("x-ESP8266-mode", "sketch"); http.addHeader("x-ESP8266-mode", "sketch");
} }
if(current_version && current_version[0] != 0x00) { if(currentVersion && currentVersion[0] != 0x00) {
http->addHeader("x-ESP8266-version", current_version); http.addHeader("x-ESP8266-version", currentVersion);
} }
const char * headerkeys[] = { "x-MD5" }; const char * headerkeys[] = { "x-MD5" };
size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*); size_t headerkeyssize = sizeof(headerkeys) / sizeof(char*);
// track these headers // track these headers
http->collectHeaders(headerkeys, headerkeyssize); http.collectHeaders(headerkeys, headerkeyssize);
int code = http->GET(); int code = http.GET();
int len = http->getSize(); int len = http.getSize();
if(code <= 0) { if(code <= 0) {
DEBUG_HTTP_UPDATE("[httpUpdate] HTTP error: %s\n", http->errorToString(code).c_str()); DEBUG_HTTP_UPDATE("[httpUpdate] HTTP error: %s\n", http.errorToString(code).c_str());
lastError = code; _lastError = code;
http->end(); http.end();
return HTTP_UPDATE_FAILED; return HTTP_UPDATE_FAILED;
} }
@ -190,16 +208,16 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
DEBUG_HTTP_UPDATE("[httpUpdate] - code: %d\n", code); DEBUG_HTTP_UPDATE("[httpUpdate] - code: %d\n", code);
DEBUG_HTTP_UPDATE("[httpUpdate] - len: %d\n", len); DEBUG_HTTP_UPDATE("[httpUpdate] - len: %d\n", len);
if(http->hasHeader("x-MD5")) { if(http.hasHeader("x-MD5")) {
DEBUG_HTTP_UPDATE("[httpUpdate] - MD5: %s\n", http->header("x-MD5").c_str()); DEBUG_HTTP_UPDATE("[httpUpdate] - MD5: %s\n", http.header("x-MD5").c_str());
} }
DEBUG_HTTP_UPDATE("[httpUpdate] ESP8266 info:\n"); DEBUG_HTTP_UPDATE("[httpUpdate] ESP8266 info:\n");
DEBUG_HTTP_UPDATE("[httpUpdate] - free Space: %d\n", ESP.getFreeSketchSpace()); DEBUG_HTTP_UPDATE("[httpUpdate] - free Space: %d\n", ESP.getFreeSketchSpace());
DEBUG_HTTP_UPDATE("[httpUpdate] - current Sketch Size: %d\n", ESP.getSketchSize()); DEBUG_HTTP_UPDATE("[httpUpdate] - current Sketch Size: %d\n", ESP.getSketchSize());
if(current_version && current_version[0] != 0x00) { if(currentVersion && currentVersion[0] != 0x00) {
DEBUG_HTTP_UPDATE("[httpUpdate] - current version: %s\n", current_version); DEBUG_HTTP_UPDATE("[httpUpdate] - current version: %s\n", currentVersion);
} }
switch(code) { switch(code) {
@ -220,11 +238,11 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
} }
if(!startUpdate) { if(!startUpdate) {
lastError = HTTP_UE_TOO_LESS_SPACE; _lastError = HTTP_UE_TOO_LESS_SPACE;
ret = HTTP_UPDATE_FAILED; ret = HTTP_UPDATE_FAILED;
} else { } else {
WiFiClient * tcp = http->getStreamPtr(); WiFiClient * tcp = http.getStreamPtr();
WiFiUDP::stopAll(); WiFiUDP::stopAll();
WiFiClient::stopAllExcept(tcp); WiFiClient::stopAllExcept(tcp);
@ -245,16 +263,16 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
uint8_t buf[4]; uint8_t buf[4];
if(tcp->peekBytes(&buf[0], 4) != 4) { if(tcp->peekBytes(&buf[0], 4) != 4) {
DEBUG_HTTP_UPDATE("[httpUpdate] peekBytes magic header failed\n"); DEBUG_HTTP_UPDATE("[httpUpdate] peekBytes magic header failed\n");
lastError = HTTP_UE_BIN_VERIFY_HEADER_FAILED; _lastError = HTTP_UE_BIN_VERIFY_HEADER_FAILED;
http->end(); http.end();
return HTTP_UPDATE_FAILED; return HTTP_UPDATE_FAILED;
} }
// check for valid first magic byte // check for valid first magic byte
if(buf[0] != 0xE9) { if(buf[0] != 0xE9) {
DEBUG_HTTP_UPDATE("[httpUpdate] magic header not starts with 0xE9\n"); DEBUG_HTTP_UPDATE("[httpUpdate] magic header not starts with 0xE9\n");
lastError = HTTP_UE_BIN_VERIFY_HEADER_FAILED; _lastError = HTTP_UE_BIN_VERIFY_HEADER_FAILED;
http->end(); http.end();
return HTTP_UPDATE_FAILED; return HTTP_UPDATE_FAILED;
} }
@ -264,18 +282,18 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
// check if new bin fits to SPI flash // check if new bin fits to SPI flash
if(bin_flash_size > ESP.getFlashChipRealSize()) { if(bin_flash_size > ESP.getFlashChipRealSize()) {
DEBUG_HTTP_UPDATE("[httpUpdate] magic header, new bin not fits SPI Flash\n"); DEBUG_HTTP_UPDATE("[httpUpdate] magic header, new bin not fits SPI Flash\n");
lastError = HTTP_UE_BIN_FOR_WRONG_FLASH; _lastError = HTTP_UE_BIN_FOR_WRONG_FLASH;
http->end(); http.end();
return HTTP_UPDATE_FAILED; return HTTP_UPDATE_FAILED;
} }
} }
if(runUpdate(*tcp, len, http->header("x-MD5"), command)) { if(runUpdate(*tcp, len, http.header("x-MD5"), command)) {
ret = HTTP_UPDATE_OK; ret = HTTP_UPDATE_OK;
DEBUG_HTTP_UPDATE("[httpUpdate] Update ok\n"); DEBUG_HTTP_UPDATE("[httpUpdate] Update ok\n");
http->end(); http.end();
if(reboot) { if(_rebootOnUpdate) {
ESP.restart(); ESP.restart();
} }
@ -285,7 +303,7 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
} }
} }
} else { } else {
lastError = HTTP_UE_SERVER_NOT_REPORT_SIZE; _lastError = HTTP_UE_SERVER_NOT_REPORT_SIZE;
ret = HTTP_UPDATE_FAILED; ret = HTTP_UPDATE_FAILED;
DEBUG_HTTP_UPDATE("[httpUpdate] Content-Length is 0 or not set by Server?!\n"); DEBUG_HTTP_UPDATE("[httpUpdate] Content-Length is 0 or not set by Server?!\n");
} }
@ -295,22 +313,22 @@ t_httpUpdate_return ESP8266HTTPUpdate::handleUpdate(HTTPClient * http, const cha
ret = HTTP_UPDATE_NO_UPDATES; ret = HTTP_UPDATE_NO_UPDATES;
break; break;
case HTTP_CODE_NOT_FOUND: case HTTP_CODE_NOT_FOUND:
lastError = HTTP_UE_SERVER_FILE_NOT_FOUND; _lastError = HTTP_UE_SERVER_FILE_NOT_FOUND;
ret = HTTP_UPDATE_FAILED; ret = HTTP_UPDATE_FAILED;
break; break;
case HTTP_CODE_FORBIDDEN: case HTTP_CODE_FORBIDDEN:
lastError = HTTP_UE_SERVER_FORBIDDEN; _lastError = HTTP_UE_SERVER_FORBIDDEN;
ret = HTTP_UPDATE_FAILED; ret = HTTP_UPDATE_FAILED;
break; break;
default: default:
lastError = HTTP_UE_SERVER_WRONG_HTTP_CODE; _lastError = HTTP_UE_SERVER_WRONG_HTTP_CODE;
ret = HTTP_UPDATE_FAILED; ret = HTTP_UPDATE_FAILED;
DEBUG_HTTP_UPDATE("[httpUpdate] HTTP Code is (%d)\n", code); DEBUG_HTTP_UPDATE("[httpUpdate] HTTP Code is (%d)\n", code);
//http->writeToStream(&Serial1); //http.writeToStream(&Serial1);
break; break;
} }
http->end(); http.end();
return ret; return ret;
} }
@ -326,7 +344,7 @@ bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int com
StreamString error; StreamString error;
if(!Update.begin(size, command)) { if(!Update.begin(size, command)) {
lastError = Update.getError(); _lastError = Update.getError();
Update.printError(error); Update.printError(error);
error.trim(); // remove line ending error.trim(); // remove line ending
DEBUG_HTTP_UPDATE("[httpUpdate] Update.begin failed! (%s)\n", error.c_str()); DEBUG_HTTP_UPDATE("[httpUpdate] Update.begin failed! (%s)\n", error.c_str());
@ -335,14 +353,14 @@ bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int com
if(md5.length()) { if(md5.length()) {
if(!Update.setMD5(md5.c_str())) { if(!Update.setMD5(md5.c_str())) {
lastError = HTTP_UE_SERVER_FAULTY_MD5; _lastError = HTTP_UE_SERVER_FAULTY_MD5;
DEBUG_HTTP_UPDATE("[httpUpdate] Update.setMD5 failed! (%s)\n", md5.c_str()); DEBUG_HTTP_UPDATE("[httpUpdate] Update.setMD5 failed! (%s)\n", md5.c_str());
return false; return false;
} }
} }
if(Update.writeStream(in) != size) { if(Update.writeStream(in) != size) {
lastError = Update.getError(); _lastError = Update.getError();
Update.printError(error); Update.printError(error);
error.trim(); // remove line ending error.trim(); // remove line ending
DEBUG_HTTP_UPDATE("[httpUpdate] Update.writeStream failed! (%s)\n", error.c_str()); DEBUG_HTTP_UPDATE("[httpUpdate] Update.writeStream failed! (%s)\n", error.c_str());
@ -350,7 +368,7 @@ bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int com
} }
if(!Update.end()) { if(!Update.end()) {
lastError = Update.getError(); _lastError = Update.getError();
Update.printError(error); Update.printError(error);
error.trim(); // remove line ending error.trim(); // remove line ending
DEBUG_HTTP_UPDATE("[httpUpdate] Update.end failed! (%s)\n", error.c_str()); DEBUG_HTTP_UPDATE("[httpUpdate] Update.end failed! (%s)\n", error.c_str());

View File

@ -52,31 +52,53 @@
#define HTTP_UE_BIN_VERIFY_HEADER_FAILED (-106) #define HTTP_UE_BIN_VERIFY_HEADER_FAILED (-106)
#define HTTP_UE_BIN_FOR_WRONG_FLASH (-107) #define HTTP_UE_BIN_FOR_WRONG_FLASH (-107)
typedef enum { enum HTTPUpdateResult {
HTTP_UPDATE_FAILED, HTTP_UPDATE_FAILED,
HTTP_UPDATE_NO_UPDATES, HTTP_UPDATE_NO_UPDATES,
HTTP_UPDATE_OK HTTP_UPDATE_OK
} t_httpUpdate_return; };
typedef HTTPUpdateResult t_httpUpdate_return; // backward compatibility
class ESP8266HTTPUpdate { class ESP8266HTTPUpdate {
public: public:
ESP8266HTTPUpdate(void); ESP8266HTTPUpdate(void);
~ESP8266HTTPUpdate(void); ~ESP8266HTTPUpdate(void);
t_httpUpdate_return update(const char * url, const char * current_version = "", const char * httpsFingerprint = "", bool reboot = true); void rebootOnUpdate(bool reboot) { _rebootOnUpdate = reboot; }
t_httpUpdate_return update(const char * host, uint16_t port, const char * url = "/", const char * current_version = "", bool https = false, const char * httpsFingerprint = "", bool reboot = true);
t_httpUpdate_return update(String host, uint16_t port, String url = "/", String current_version = "", bool https = false, String httpsFingerprint = "", bool reboot = true); // This function is deprecated, use rebootOnUpdate and the next one instead
t_httpUpdate_return update(const String& url, const String& currentVersion,
const String& httpsFingerprint, bool reboot) __attribute__((deprecated));
t_httpUpdate_return update(const String& url, const String& currentVersion = "");
t_httpUpdate_return update(const String& url, const String& currentVersion,
const String& httpsFingerprint);
// This function is deprecated, use one of the overloads below along with rebootOnUpdate
t_httpUpdate_return update(const String& host, uint16_t port, const String& uri, const String& currentVersion,
bool https, const String& httpsFingerprint, bool reboot) __attribute__((deprecated));
t_httpUpdate_return update(const String& host, uint16_t port, const String& uri = "/",
const String& currentVersion = "");
t_httpUpdate_return update(const String& host, uint16_t port, const String& url,
const String& currentVersion, const String& httpsFingerprint);
// This function is deprecated, use rebootOnUpdate and the next one instead
t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion,
const String& httpsFingerprint, bool reboot) __attribute__((deprecated));
t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion = "");
t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion, const String& httpsFingerprint);
t_httpUpdate_return updateSpiffs(const char * url, const char * current_version = "", const char * httpsFingerprint = "", bool reboot = false);
int getLastError(void); int getLastError(void);
String getLastErrorString(void); String getLastErrorString(void);
protected: protected:
t_httpUpdate_return handleUpdate(HTTPClient * http, const char * current_version, bool reboot = true, bool spiffs = false); t_httpUpdate_return handleUpdate(HTTPClient& http, const String& currentVersion, bool spiffs = false);
bool runUpdate(Stream& in, uint32_t size, String md5, int command = U_FLASH); bool runUpdate(Stream& in, uint32_t size, String md5, int command = U_FLASH);
int lastError; int _lastError;
bool _rebootOnUpdate = true;
}; };
extern ESP8266HTTPUpdate ESPhttpUpdate; extern ESP8266HTTPUpdate ESPhttpUpdate;