diff --git a/libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino b/libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino index ebd30b7259..83d0294735 100644 --- a/libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino +++ b/libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino @@ -12,6 +12,8 @@ #include +#include + #define USE_SERIAL Serial ESP8266WiFiMulti WiFiMulti; @@ -40,22 +42,24 @@ void loop() { // wait for WiFi connection if ((WiFiMulti.run() == WL_CONNECTED)) { + WiFiClient client; + HTTPClient http; USE_SERIAL.print("[HTTP] begin...\n"); // configure traged server and url - http.begin("http://user:password@192.168.1.12/test.html"); + http.begin((Client&)client, "http://guest:guest@jigsaw.w3.org/HTTP/Basic/"); /* // or - http.begin("http://192.168.1.12/test.html"); - http.setAuthorization("user", "password"); + http.begin("http://jigsaw.w3.org/HTTP/Basic/"); + http.setAuthorization("guest", "guest"); // or - http.begin("http://192.168.1.12/test.html"); - http.setAuthorization("dXNlcjpwYXN3b3Jk"); + http.begin("http://jigsaw.w3.org/HTTP/Basic/"); + http.setAuthorization("Z3Vlc3Q6Z3Vlc3Q="); */ @@ -82,4 +86,3 @@ void loop() { delay(10000); } - diff --git a/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino b/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino index 766e85b2a4..725dde9a76 100644 --- a/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino +++ b/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino @@ -12,6 +12,8 @@ #include +#include + #define USE_SERIAL Serial ESP8266WiFiMulti WiFiMulti; @@ -40,34 +42,39 @@ void loop() { // wait for WiFi connection if ((WiFiMulti.run() == WL_CONNECTED)) { + WiFiClient client; + HTTPClient http; USE_SERIAL.print("[HTTP] begin...\n"); // configure traged server and url //http.begin("https://192.168.1.12/test.html", "7a 9c f4 db 40 d3 62 5a 6e 21 bc 5c cc 66 c8 3e a1 45 59 38"); //HTTPS - http.begin("http://192.168.1.12/test.html"); //HTTP + if (http.begin((Client &)client, "http://tls.mbed.org/")) { // HTTP + - USE_SERIAL.print("[HTTP] GET...\n"); - // start connection and send HTTP header - int httpCode = http.GET(); + USE_SERIAL.print("[HTTP] GET...\n"); + // start connection and send HTTP header + int httpCode = http.GET(); - // httpCode will be negative on error - if (httpCode > 0) { - // HTTP header has been send and Server response header has been handled - USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); + // httpCode will be negative on error + if (httpCode > 0) { + // HTTP header has been send and Server response header has been handled + USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); - // file found at server - if (httpCode == HTTP_CODE_OK) { - String payload = http.getString(); - USE_SERIAL.println(payload); + // file found at server + if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { + String payload = http.getString(); + USE_SERIAL.println(payload); + } + } else { + USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } + + http.end(); } else { - USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); + USE_SERIAL.printf("Unable to connect\n"); } - - http.end(); } delay(10000); } - diff --git a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino index bbf07c1b89..8924061f69 100644 --- a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino +++ b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino @@ -11,8 +11,8 @@ #include -const char* ssid = "........"; -const char* ssidPassword = "........"; +const char* ssid = "SSID"; +const char* ssidPassword = "PASSWORD"; const char *username = "admin"; const char *password = "admin"; @@ -76,7 +76,7 @@ String getDigestAuth(String& authReq, const String& username, const String& pass } void setup() { - Serial.begin(9600); + Serial.begin(115200); WiFi.mode(WIFI_STA); WiFi.begin(ssid, ssidPassword); @@ -95,10 +95,12 @@ void setup() { void loop() { HTTPClient http; + WiFiClient client; + Serial.print("[HTTP] begin...\n"); // configure traged server and url - http.begin(String(server) + String(uri)); + http.begin((Client&) client, String(server) + String(uri)); const char *keys[] = {"WWW-Authenticate"}; diff --git a/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino b/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino index 420549d8e7..f115e36bf0 100644 --- a/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino +++ b/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino @@ -45,8 +45,10 @@ void loop() { // wait for WiFi connection if ((WiFiMulti.run() == WL_CONNECTED)) { - http.begin("http://192.168.1.12/test.html"); - //http.begin("192.168.1.12", 80, "/test.html"); + WiFiClient client; + + http.begin((Client&) client, "http://192.168.1.12/test.html"); + //http.begin((Client&) client, "192.168.1.12", 80, "/test.html"); int httpCode = http.GET(); if (httpCode > 0) { @@ -65,6 +67,3 @@ void loop() { delay(1000); } - - - diff --git a/libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino b/libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino index 57db978882..7ad246cebb 100644 --- a/libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino +++ b/libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino @@ -42,11 +42,13 @@ void loop() { HTTPClient http; + WiFiClient client; + USE_SERIAL.print("[HTTP] begin...\n"); // configure server and url - http.begin("http://192.168.1.12/test.html"); - //http.begin("192.168.1.12", 80, "/test.html"); + http.begin((Client&) client, "http://jigsaw.w3.org/HTTP/connection.html"); + //http.begin("jigsaw.w3.org", 80, "/HTTP/connection.html"); USE_SERIAL.print("[HTTP] GET...\n"); // start connection and send HTTP header @@ -65,7 +67,7 @@ void loop() { uint8_t buff[128] = { 0 }; // get tcp stream - WiFiClient * stream = http.getStreamPtr(); + WiFiClient * stream = &client; // read all data from server while (http.connected() && (len > 0 || len == -1)) { @@ -99,4 +101,3 @@ void loop() { delay(10000); } - diff --git a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino new file mode 100644 index 0000000000..7f302ec642 --- /dev/null +++ b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino @@ -0,0 +1,116 @@ +/** + StreamHTTPClient.ino + + Created on: 24.05.2015 + +*/ + +#include + +#include +#include + +#include + +#define USE_SERIAL Serial + +ESP8266WiFiMulti WiFiMulti; + +void setup() { + + USE_SERIAL.begin(115200); + // USE_SERIAL.setDebugOutput(true); + + USE_SERIAL.println(); + USE_SERIAL.println(); + USE_SERIAL.println(); + + for (uint8_t t = 4; t > 0; t--) { + USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); + USE_SERIAL.flush(); + delay(1000); + } + + WiFi.mode(WIFI_STA); + WiFiMulti.addAP("SSID", "PASSWORD"); + +} + +void loop() { + // wait for WiFi connection + if ((WiFiMulti.run() == WL_CONNECTED)) { + + HTTPClient http; + + BearSSL::WiFiClientSecure client; + + bool mfln = client.probeMaxFragmentLength("tls.mbed.org", 443, 1024); + USE_SERIAL.printf("\nConnecting to https://tls.mbed.org\n"); + USE_SERIAL.printf("MFLN supported: %s\n", mfln ? "yes" : "no"); + if (mfln) { + client.setBufferSizes(1024, 1024); + } + + USE_SERIAL.print("[HTTP] begin...\n"); + + // configure server and url + const uint8_t fingerprint[20] = {0xEB, 0xD9, 0xDF, 0x37, 0xC2, 0xCC, 0x84, 0x89, 0x00, 0xA0, 0x58, 0x52, 0x24, 0x04, 0xE4, 0x37, 0x3E, 0x2B, 0xF1, 0x41}; + client.setFingerprint(fingerprint); + + //if (http.begin("jigsaw.w3.org", 443, "/HTTP/connection.html", true)) { + if (http.begin((Client&) client, "https://tls.mbed.org/")) { + + USE_SERIAL.print("[HTTP] GET...\n"); + // start connection and send HTTP header + int httpCode = http.GET(); + if (httpCode > 0) { + // HTTP header has been send and Server response header has been handled + USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); + + // file found at server + if (httpCode == HTTP_CODE_OK) { + + // get lenght of document (is -1 when Server sends no Content-Length header) + int len = http.getSize(); + + // create buffer for read + uint8_t buff[128] = { 0 }; + + // get tcp stream + WiFiClient * stream = &client; + + // read all data from server + while (http.connected() && (len > 0 || len == -1)) { + // get available data size + size_t size = stream->available(); + + if (size) { + // read up to 128 byte + int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); + + // write it to Serial + USE_SERIAL.write(buff, c); + + if (len > 0) { + len -= c; + } + } + delay(1); + } + + USE_SERIAL.println(); + USE_SERIAL.print("[HTTP] connection closed or file end.\n"); + + } + } else { + USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); + } + + http.end(); + } else { + USE_SERIAL.printf("Unable to connect\n"); + } + } + + delay(10000); +} diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 28a3503484..18ed6bc2cc 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -21,15 +21,19 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * */ - #include + +#include "ESP8266HTTPClient.h" + +#ifdef KEEP_PRESENT_API #include #include +#endif + #include #include -#include "ESP8266HTTPClient.h" - +#ifdef KEEP_PRESENT_API class TransportTraits { public: @@ -100,6 +104,7 @@ class BearSSLTraits : public TransportTraits protected: uint8_t _fingerprint[20]; }; +#endif /** * constructor @@ -129,9 +134,70 @@ void HTTPClient::clear() } +/** + * parsing the url for all needed parameters + * @param client Client& + * @param url String + * @param https bool + * @return success bool + */ +bool HTTPClient::begin(Client &client, String url) { +#ifdef KEEP_PRESENT_API + _tcpDeprecated.reset(nullptr); + _transportTraits.reset(nullptr); +#endif + _tcp = &client; + + // check for : (http: or https:) + int index = url.indexOf(':'); + if(index < 0) { + DEBUG_HTTPCLIENT("[HTTP-Client][begin] failed to parse protocol\n"); + return false; + } + + String protocol = url.substring(0, index); + if(protocol != "http" && protocol != "https") { + DEBUG_HTTPCLIENT("[HTTP-Client][begin] unknown protocol '%s'\n", protocol.c_str()); + return false; + } + + _port = (protocol == "https" ? 443 : 80); + return beginInternal(url, protocol.c_str()); +} + + +/** + * directly supply all needed parameters + * @param client Client& + * @param host String + * @param port uint16_t + * @param uri String + * @param https bool + * @return success bool + */ +bool HTTPClient::begin(Client &client, String host, uint16_t port, String uri, bool https) +{ +#ifdef KEEP_PRESENT_API + _tcpDeprecated.reset(nullptr); + _transportTraits.reset(nullptr); +#endif + _tcp = &client; + + clear(); + _host = host; + _port = port; + _uri = uri; + _protocol = (https ? "https" : "http"); + return true; +} + + +#ifdef KEEP_PRESENT_API bool HTTPClient::begin(String url, String httpsFingerprint) { + _tcp = nullptr; _transportTraits.reset(nullptr); + _port = 443; if (httpsFingerprint.length() == 0) { return false; @@ -147,7 +213,9 @@ bool HTTPClient::begin(String url, String httpsFingerprint) bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20]) { + _tcp = nullptr; _transportTraits.reset(nullptr); + _port = 443; if (!beginInternal(url, "https")) { return false; @@ -168,7 +236,9 @@ bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20]) */ bool HTTPClient::begin(String url) { + _tcp = nullptr; _transportTraits.reset(nullptr); + _port = 80; if (!beginInternal(url, "http")) { return false; @@ -176,6 +246,7 @@ bool HTTPClient::begin(String url) _transportTraits = TransportTraitsPtr(new TransportTraits()); return true; } +#endif bool HTTPClient::beginInternal(String url, const char* expectedProtocol) { @@ -224,8 +295,11 @@ bool HTTPClient::beginInternal(String url, const char* expectedProtocol) return true; } +#ifdef KEEP_PRESENT_API bool HTTPClient::begin(String host, uint16_t port, String uri) { + _tcp = nullptr; + clear(); _host = host; _port = port; @@ -246,6 +320,8 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, bool https, Strin bool HTTPClient::begin(String host, uint16_t port, String uri, String httpsFingerprint) { + _tcp = nullptr; + clear(); _host = host; _port = port; @@ -261,6 +337,8 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, String httpsFinge bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t httpsFingerprint[20]) { + _tcp = nullptr; + clear(); _host = host; _port = port; @@ -274,7 +352,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t htt DEBUG_HTTPCLIENT("\n"); return true; } - +#endif /** * end @@ -294,6 +372,7 @@ void HTTPClient::end(void) } else { DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp stop\n"); _tcp->stop(); + _tcp = nullptr; } } else { DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp is closed\n"); @@ -624,6 +703,7 @@ int HTTPClient::getSize(void) return _size; } +#ifdef KEEP_PRESENT_API /** * returns the stream of the tcp connection * @return WiFiClient @@ -631,7 +711,7 @@ int HTTPClient::getSize(void) WiFiClient& HTTPClient::getStream(void) { if(connected()) { - return *_tcp; + return *_tcpDeprecated; } DEBUG_HTTPCLIENT("[HTTP-Client] getStream: not connected\n"); @@ -646,12 +726,27 @@ WiFiClient& HTTPClient::getStream(void) WiFiClient* HTTPClient::getStreamPtr(void) { if(connected()) { - return _tcp.get(); + return _tcpDeprecated ? (WiFiClient*)_tcpDeprecated.get() : (WiFiClient*)_tcp; } DEBUG_HTTPCLIENT("[HTTP-Client] getStreamPtr: not connected\n"); return nullptr; } +#else +/** + * returns the stream of the tcp connection + * @return Client * + */ +Client* HTTPClient::getStreamPtr(void) +{ + if(connected()) { + return _tcp; + } + + DEBUG_HTTPCLIENT("[HTTP-Client] getStreamPtr: not connected\n"); + return nullptr; +} +#endif /** * write all message body / payload to Stream @@ -897,12 +992,21 @@ bool HTTPClient::connect(void) return true; } - if (!_transportTraits) { +#ifdef KEEP_PRESENT_API + if (!_tcp && !_transportTraits) { +#else + if(!_tcp) { +#endif DEBUG_HTTPCLIENT("[HTTP-Client] connect: HTTPClient::begin was not called or returned error\n"); return false; } - _tcp = _transportTraits->create(); +#ifdef KEEP_PRESENT_API + if(!_tcp) { + _tcpDeprecated = _transportTraits->create(); + _tcp = _tcpDeprecated.get(); + } +#endif _tcp->setTimeout(_tcpTimeout); if(!_tcp->connect(_host.c_str(), _port)) { @@ -912,15 +1016,22 @@ bool HTTPClient::connect(void) DEBUG_HTTPCLIENT("[HTTP-Client] connected to %s:%u\n", _host.c_str(), _port); - if (!_transportTraits->verify(*_tcp, _host.c_str())) { +#ifdef KEEP_PRESENT_API + if (_tcpDeprecated && _transportTraits && !_transportTraits->verify(*_tcpDeprecated, _host.c_str())) { DEBUG_HTTPCLIENT("[HTTP-Client] transport level verify failed\n"); _tcp->stop(); return false; } +#endif #ifdef ESP8266 - _tcp->setNoDelay(true); +#ifdef KEEP_PRESENT_API + if(_tcpDeprecated) + _tcpDeprecated->setNoDelay(true); +#else +// Client has no setNoDelay(), is this important??????????????? +#endif #endif return connected(); } diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h index 72b42853f7..3317d011a1 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h @@ -20,14 +20,22 @@ * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * + * Modified by Jeroen Döll, June 2018 */ #ifndef ESP8266HTTPClient_H_ #define ESP8266HTTPClient_H_ +#define KEEP_PRESENT_API + #include #include + +#ifdef KEEP_PRESENT_API #include +#else +#include +#endif #ifdef DEBUG_ESP_HTTP_CLIENT #ifdef DEBUG_ESP_PORT @@ -124,8 +132,10 @@ typedef enum { HTTPC_TE_CHUNKED } transferEncoding_t; +#ifdef KEEP_PRESENT_API class TransportTraits; typedef std::unique_ptr TransportTraitsPtr; +#endif class HTTPClient { @@ -133,6 +143,10 @@ class HTTPClient HTTPClient(); ~HTTPClient(); + bool begin(Client &client, String url); + bool begin(Client &client, String host, uint16_t port, String uri = "/", bool https = false); + +#ifdef KEEP_PRESENT_API // Plain HTTP connection, unencrypted bool begin(String url); bool begin(String host, uint16_t port, String uri = "/"); @@ -144,6 +158,7 @@ class HTTPClient bool begin(String host, uint16_t port, String uri, const uint8_t httpsFingerprint[20]); // deprecated, use the overload above instead bool begin(String host, uint16_t port, String uri, bool https, String httpsFingerprint) __attribute__ ((deprecated)); +#endif void end(void); @@ -182,8 +197,12 @@ class HTTPClient int getSize(void); +#ifdef KEEP_PRESENT_API WiFiClient& getStream(void); WiFiClient* getStreamPtr(void); +#else + Client* getStreamPtr(void); +#endif int writeToStream(Stream* stream); String getString(void); @@ -204,8 +223,11 @@ class HTTPClient int writeToStreamDataBlock(Stream * stream, int len); +#ifdef KEEP_PRESENT_API TransportTraitsPtr _transportTraits; - std::unique_ptr _tcp; + std::unique_ptr _tcpDeprecated; +#endif + Client* _tcp; /// request handling String _host; diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino b/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino new file mode 100644 index 0000000000..47ec7b5a78 --- /dev/null +++ b/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino @@ -0,0 +1,138 @@ +/** + httpUpdateSecure.ino + + Created on: 20.06.2018 as an adaptation of httpUpdate.ino + +*/ + +#include +#include + +#include +#include + +#include + +#define USE_SERIAL Serial + +ESP8266WiFiMulti WiFiMulti; + +// A single, global CertStore which can be used by all +// connections. Needs to stay live the entire time any of +// the WiFiClientBearSSLs are present. +#include +BearSSL::CertStore certStore; + +#include +class SPIFFSCertStoreFile : public BearSSL::CertStoreFile { + public: + SPIFFSCertStoreFile(const char *name) { + _name = name; + }; + virtual ~SPIFFSCertStoreFile() override {}; + + // The main API + virtual bool open(bool write = false) override { + _file = SPIFFS.open(_name, write ? "w" : "r"); + return _file; + } + virtual bool seek(size_t absolute_pos) override { + return _file.seek(absolute_pos, SeekSet); + } + virtual ssize_t read(void *dest, size_t bytes) override { + return _file.readBytes((char*)dest, bytes); + } + virtual ssize_t write(void *dest, size_t bytes) override { + return _file.write((uint8_t*)dest, bytes); + } + virtual void close() override { + _file.close(); + } + + private: + File _file; + const char *_name; +}; + +SPIFFSCertStoreFile certs_idx("/certs.idx"); +SPIFFSCertStoreFile certs_ar("/certs.ar"); + +// Set time via NTP, as required for x.509 validation +void setClock() { + configTime(0, 0, "pool.ntp.org", "time.nist.gov"); // UTC + + USE_SERIAL.print(F("Waiting for NTP time sync: ")); + time_t now = time(nullptr); + while (now < 8 * 3600 * 2) { + yield(); + delay(500); + USE_SERIAL.print(F(".")); + now = time(nullptr); + } + + USE_SERIAL.println(F("")); + struct tm timeinfo; + gmtime_r(&now, &timeinfo); + USE_SERIAL.print(F("Current time: ")); + USE_SERIAL.print(asctime(&timeinfo)); +} + +void setup() { + + USE_SERIAL.begin(115200); + // USE_SERIAL.setDebugOutput(true); + + USE_SERIAL.println(); + USE_SERIAL.println(); + USE_SERIAL.println(); + + for (uint8_t t = 4; t > 0; t--) { + USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); + USE_SERIAL.flush(); + delay(1000); + } + + WiFi.mode(WIFI_STA); + WiFiMulti.addAP("SSID", "PASSWORD"); + + SPIFFS.begin(); + + int numCerts = certStore.initCertStore(&certs_idx, &certs_ar); + USE_SERIAL.print(F("Number of CA certs read: ")); USE_SERIAL.println(numCerts); + if (numCerts == 0) { + USE_SERIAL.println(F("No certs found. Did you run certs-from-mozill.py and upload the SPIFFS directory before running?")); + return; // Can't connect to anything w/o certs! + } +} + +void loop() { + // wait for WiFi connection + if ((WiFiMulti.run() == WL_CONNECTED)) { + + setClock(); + + BearSSL::WiFiClientSecure client; + bool mfln = client.probeMaxFragmentLength("server", 443, 1024); // server must be the same as in ESPhttpUpdate.update() + USE_SERIAL.printf("MFLN supported: %s\n", mfln ? "yes" : "no"); + if (mfln) { + client.setBufferSizes(1024, 1024); + } + client.setCertStore(&certStore); + + t_httpUpdate_return ret = ESPhttpUpdate.update((Client&) client, "https://server/file.bin"); + + switch (ret) { + case HTTP_UPDATE_FAILED: + USE_SERIAL.printf("HTTP_UPDATE_FAILED Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); + break; + + case HTTP_UPDATE_NO_UPDATES: + USE_SERIAL.println("HTTP_UPDATE_NO_UPDATES"); + break; + + case HTTP_UPDATE_OK: + USE_SERIAL.println("HTTP_UPDATE_OK"); + break; + } + } +} diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp index 5cc4b6a09e..93574636bb 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp @@ -43,6 +43,7 @@ ESP8266HTTPUpdate::~ESP8266HTTPUpdate(void) { } +#ifdef ESP8266HTTPUPDATE_KEEP_CURRENT_API HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& currentVersion, const String& httpsFingerprint, bool reboot) { @@ -72,7 +73,16 @@ HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& curr http.begin(url, httpsFingerprint); return handleUpdate(http, currentVersion, false); } +#endif + +HTTPUpdateResult ESP8266HTTPUpdate::update(Client& client, const String& url, const String& currentVersion) +{ + HTTPClient http; + http.begin(client, url); + return handleUpdate(http, currentVersion, false); +} +#ifdef ESP8266HTTPUPDATE_KEEP_CURRENT_API HTTPUpdateResult ESP8266HTTPUpdate::updateSpiffs(const String& url, const String& currentVersion, const String& httpsFingerprint) { HTTPClient http; @@ -93,7 +103,16 @@ HTTPUpdateResult ESP8266HTTPUpdate::updateSpiffs(const String& url, const String http.begin(url); return handleUpdate(http, currentVersion, true); } +#endif +HTTPUpdateResult ESP8266HTTPUpdate::updateSpiffs(Client& client, const String& url, const String& currentVersion) +{ + HTTPClient http; + http.begin(client, url); + return handleUpdate(http, currentVersion, true); +} + +#ifdef ESP8266HTTPUPDATE_KEEP_CURRENT_API HTTPUpdateResult ESP8266HTTPUpdate::update(const String& host, uint16_t port, const String& uri, const String& currentVersion, bool https, const String& httpsFingerprint, bool reboot) { @@ -129,6 +148,15 @@ HTTPUpdateResult ESP8266HTTPUpdate::update(const String& host, uint16_t port, co http.begin(host, port, url, httpsFingerprint); return handleUpdate(http, currentVersion, false); } +#endif + +HTTPUpdateResult ESP8266HTTPUpdate::update(Client& client, const String& host, uint16_t port, const String& uri, + const String& currentVersion) +{ + HTTPClient http; + http.begin(client, host, port, uri); + return handleUpdate(http, currentVersion, false); +} /** * return error code as int @@ -276,7 +304,7 @@ HTTPUpdateResult ESP8266HTTPUpdate::handleUpdate(HTTPClient& http, const String& ret = HTTP_UPDATE_FAILED; } else { - WiFiClient * tcp = http.getStreamPtr(); + WiFiClient * tcp = (WiFiClient *) http.getStreamPtr(); WiFiUDP::stopAll(); WiFiClient::stopAllExcept(tcp); diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h index 1676d51dfc..da0063e000 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h @@ -26,11 +26,14 @@ #ifndef ESP8266HTTPUPDATE_H_ #define ESP8266HTTPUPDATE_H_ +#define ESP8266HTTPUPDATE_KEEP_CURRENT_API + #include #include #include #include #include +#include #ifdef DEBUG_ESP_HTTP_UPDATE #ifdef DEBUG_ESP_PORT @@ -72,6 +75,7 @@ class ESP8266HTTPUpdate _rebootOnUpdate = reboot; } +#ifdef ESP8266HTTPUPDATE_KEEP_CURRENT_API // 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)); @@ -80,7 +84,10 @@ class ESP8266HTTPUpdate const String& httpsFingerprint); t_httpUpdate_return update(const String& url, const String& currentVersion, const uint8_t httpsFingerprint[20]); // BearSSL +#endif + t_httpUpdate_return update(Client& client, const String& url, const String& currentVersion = ""); +#ifdef ESP8266HTTPUPDATE_KEEP_CURRENT_API // 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)); @@ -91,13 +98,19 @@ class ESP8266HTTPUpdate const String& currentVersion, const String& httpsFingerprint); t_httpUpdate_return update(const String& host, uint16_t port, const String& url, const String& currentVersion, const uint8_t httpsFingerprint[20]); // BearSSL +#endif + t_httpUpdate_return update(Client& client, const String& host, uint16_t port, const String& uri = "/", + const String& currentVersion = ""); +#ifdef ESP8266HTTPUPDATE_KEEP_CURRENT_API // 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 String& url, const String& currentVersion, const uint8_t httpsFingerprint[20]); // BearSSL +#endif + t_httpUpdate_return updateSpiffs(Client& client, const String& url, const String& currentVersion = ""); int getLastError(void);