From ff2858a906fc65e2b3c5c251e3488ffa6d53ff60 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Sun, 29 Jul 2018 12:41:26 +0200 Subject: [PATCH 01/31] Function added to detect baudrate --- cores/esp8266/uart.c | 37 +++++++++++++++++++++++++++++++++++++ cores/esp8266/uart.h | 2 ++ 2 files changed, 39 insertions(+) diff --git a/cores/esp8266/uart.c b/cores/esp8266/uart.c index 47814f7a9d..2c9f56bcb9 100644 --- a/cores/esp8266/uart.c +++ b/cores/esp8266/uart.c @@ -44,6 +44,7 @@ #include "uart.h" #include "esp8266_peri.h" #include "user_interface.h" +#include "uart_register.h" const char overrun_str [] ICACHE_RODATA_ATTR STORE_ATTR = "uart input full!\r\n"; static int s_uart_debug_nr = UART0; @@ -739,3 +740,39 @@ uart_get_debug() { return s_uart_debug_nr; } + +/* +To start detection of baud rate with the UART the UART_AUTOBAUD_EN bit needs to be cleared and set. The ROM function uart_baudrate_detect() does this only once, so on a next call the UartDev.rcv_state is not equal to BAUD_RATE_DET. Instead of poking around in the UartDev struct with unknown effect, the UART_AUTOBAUD_EN bit is directly triggered by the function uart_detect_baudrate(). +*/ +int +uart_detect_baudrate(int uart_nr) +{ + static bool doTrigger = true; + + if(doTrigger) + { + USA(uart_nr) &= ~(UART_GLITCH_FILT << UART_GLITCH_FILT_S | UART_AUTOBAUD_EN); + USA(uart_nr) = 0x08 << UART_GLITCH_FILT_S | UART_AUTOBAUD_EN; + doTrigger = false; + } + + int32_t divisor = uart_baudrate_detect(uart_nr, 1); + if(!divisor) return 0; + + doTrigger = true; // Initialize for a next round + int32_t baudrate = UART_CLK_FREQ / divisor; + + static const int default_rates[] = {300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 256000, 460800, 921600, 1843200, 3686400}; + + size_t i; + for(i = 1; i < sizeof(default_rates) / sizeof(default_rates[0]) - 1; i++) // find the nearest real baudrate + { + if(baudrate <= default_rates[i]) + { + if(baudrate - default_rates[i - 1] < default_rates[i] - baudrate) i--; + break; + } + } + + return default_rates[i]; +} diff --git a/cores/esp8266/uart.h b/cores/esp8266/uart.h index 127c5d0ebb..9b050645e7 100644 --- a/cores/esp8266/uart.h +++ b/cores/esp8266/uart.h @@ -141,6 +141,8 @@ bool uart_has_overrun (uart_t* uart); // returns then clear overrun flag void uart_set_debug(int uart_nr); int uart_get_debug(); +int uart_detect_baudrate(int uart_nr); + #if defined (__cplusplus) } // extern "C" From 588d03b3c6f8175150083bfdcb8b7e7af3006a61 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Sun, 29 Jul 2018 14:16:56 +0200 Subject: [PATCH 02/31] Two begin() methods added to pass WiFiClient --- .../examples/Authorization/Authorization.ino | 15 ++- .../BasicHttpClient/BasicHttpClient.ino | 39 +++--- .../DigestAuthorization.ino | 10 +- .../ReuseConnection/ReuseConnection.ino | 9 +- .../StreamHttpClient/StreamHttpClient.ino | 9 +- .../StreamHttpsClient/StreamHttpsClient.ino | 116 +++++++++++++++++ .../src/ESP8266HTTPClient.cpp | 121 ++++++++++++++++-- .../ESP8266HTTPClient/src/ESP8266HTTPClient.h | 16 ++- 8 files changed, 290 insertions(+), 45 deletions(-) create mode 100644 libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino diff --git a/libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino b/libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino index ebd30b7259..c3dbf18d23 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, "http://guest:guest@jigsaw.w3.org/HTTP/Basic/"); /* // or - http.begin("http://192.168.1.12/test.html"); - http.setAuthorization("user", "password"); + http.begin(client, "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(client, "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..adb4fc1b37 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, "http://jigsaw.w3.org/HTTP/connection.html")) { // 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..56d55d8f61 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, 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..c9463787b2 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, "http://192.168.1.12/test.html"); + //http.begin(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..5dc4bad33c 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, "http://jigsaw.w3.org/HTTP/connection.html"); + //http.begin(client, "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..df6689c16c --- /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(client, "jigsaw.w3.org", 443, "/HTTP/connection.html", true)) { + if (http.begin(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..141708dbab 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(WiFiClient &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(WiFiClient &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 @@ -631,7 +709,11 @@ int HTTPClient::getSize(void) WiFiClient& HTTPClient::getStream(void) { if(connected()) { +#ifdef KEEP_PRESENT_API + return *_tcpDeprecated; +#else return *_tcp; +#endif } DEBUG_HTTPCLIENT("[HTTP-Client] getStream: not connected\n"); @@ -646,7 +728,11 @@ WiFiClient& HTTPClient::getStream(void) WiFiClient* HTTPClient::getStreamPtr(void) { if(connected()) { - return _tcp.get(); +#ifdef KEEP_PRESENT_API + return _tcpDeprecated.get(); +#else + return _tcp; +#endif } DEBUG_HTTPCLIENT("[HTTP-Client] getStreamPtr: not connected\n"); @@ -897,12 +983,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 +1007,23 @@ 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 + if(_tcp) + _tcp->setNoDelay(true); +#endif #endif return connected(); } diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h index 72b42853f7..3c44370236 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h @@ -20,13 +20,17 @@ * 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 + #include #ifdef DEBUG_ESP_HTTP_CLIENT @@ -124,8 +128,10 @@ typedef enum { HTTPC_TE_CHUNKED } transferEncoding_t; +#ifdef KEEP_PRESENT_API class TransportTraits; typedef std::unique_ptr TransportTraitsPtr; +#endif class HTTPClient { @@ -133,6 +139,10 @@ class HTTPClient HTTPClient(); ~HTTPClient(); + bool begin(WiFiClient &client, String url); + bool begin(WiFiClient &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 +154,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); @@ -204,8 +215,11 @@ class HTTPClient int writeToStreamDataBlock(Stream * stream, int len); +#ifdef KEEP_PRESENT_API TransportTraitsPtr _transportTraits; - std::unique_ptr _tcp; + std::unique_ptr _tcpDeprecated; +#endif + WiFiClient* _tcp; /// request handling String _host; From 5d958714ab000c94ca455bf3c434b3f098f2c889 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Sun, 29 Jul 2018 14:16:56 +0200 Subject: [PATCH 03/31] Two begin() methods added to pass WiFiClient, updated examples --- .../examples/Authorization/Authorization.ino | 15 ++- .../BasicHttpClient/BasicHttpClient.ino | 39 +++--- .../DigestAuthorization.ino | 10 +- .../ReuseConnection/ReuseConnection.ino | 9 +- .../StreamHttpClient/StreamHttpClient.ino | 9 +- .../StreamHttpsClient/StreamHttpsClient.ino | 116 +++++++++++++++++ .../src/ESP8266HTTPClient.cpp | 121 ++++++++++++++++-- .../ESP8266HTTPClient/src/ESP8266HTTPClient.h | 16 ++- 8 files changed, 290 insertions(+), 45 deletions(-) create mode 100644 libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino diff --git a/libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino b/libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino index ebd30b7259..c3dbf18d23 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, "http://guest:guest@jigsaw.w3.org/HTTP/Basic/"); /* // or - http.begin("http://192.168.1.12/test.html"); - http.setAuthorization("user", "password"); + http.begin(client, "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(client, "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..adb4fc1b37 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, "http://jigsaw.w3.org/HTTP/connection.html")) { // 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..56d55d8f61 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, 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..c9463787b2 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, "http://192.168.1.12/test.html"); + //http.begin(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..5dc4bad33c 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, "http://jigsaw.w3.org/HTTP/connection.html"); + //http.begin(client, "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..df6689c16c --- /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(client, "jigsaw.w3.org", 443, "/HTTP/connection.html", true)) { + if (http.begin(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..141708dbab 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(WiFiClient &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(WiFiClient &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 @@ -631,7 +709,11 @@ int HTTPClient::getSize(void) WiFiClient& HTTPClient::getStream(void) { if(connected()) { +#ifdef KEEP_PRESENT_API + return *_tcpDeprecated; +#else return *_tcp; +#endif } DEBUG_HTTPCLIENT("[HTTP-Client] getStream: not connected\n"); @@ -646,7 +728,11 @@ WiFiClient& HTTPClient::getStream(void) WiFiClient* HTTPClient::getStreamPtr(void) { if(connected()) { - return _tcp.get(); +#ifdef KEEP_PRESENT_API + return _tcpDeprecated.get(); +#else + return _tcp; +#endif } DEBUG_HTTPCLIENT("[HTTP-Client] getStreamPtr: not connected\n"); @@ -897,12 +983,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 +1007,23 @@ 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 + if(_tcp) + _tcp->setNoDelay(true); +#endif #endif return connected(); } diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h index 72b42853f7..3c44370236 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h @@ -20,13 +20,17 @@ * 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 + #include #ifdef DEBUG_ESP_HTTP_CLIENT @@ -124,8 +128,10 @@ typedef enum { HTTPC_TE_CHUNKED } transferEncoding_t; +#ifdef KEEP_PRESENT_API class TransportTraits; typedef std::unique_ptr TransportTraitsPtr; +#endif class HTTPClient { @@ -133,6 +139,10 @@ class HTTPClient HTTPClient(); ~HTTPClient(); + bool begin(WiFiClient &client, String url); + bool begin(WiFiClient &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 +154,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); @@ -204,8 +215,11 @@ class HTTPClient int writeToStreamDataBlock(Stream * stream, int len); +#ifdef KEEP_PRESENT_API TransportTraitsPtr _transportTraits; - std::unique_ptr _tcp; + std::unique_ptr _tcpDeprecated; +#endif + WiFiClient* _tcp; /// request handling String _host; From f9ae2c4a75be1e7d7f410569c1777710d051a107 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Sun, 29 Jul 2018 14:16:56 +0200 Subject: [PATCH 04/31] Two begin() methods added to pass WiFiClient, updated examples --- cores/esp8266/uart.c | 37 ------ cores/esp8266/uart.h | 2 - .../examples/Authorization/Authorization.ino | 15 ++- .../BasicHttpClient/BasicHttpClient.ino | 39 +++--- .../DigestAuthorization.ino | 10 +- .../ReuseConnection/ReuseConnection.ino | 9 +- .../StreamHttpClient/StreamHttpClient.ino | 9 +- .../StreamHttpsClient/StreamHttpsClient.ino | 116 +++++++++++++++++ .../src/ESP8266HTTPClient.cpp | 121 ++++++++++++++++-- .../ESP8266HTTPClient/src/ESP8266HTTPClient.h | 16 ++- 10 files changed, 290 insertions(+), 84 deletions(-) create mode 100644 libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino diff --git a/cores/esp8266/uart.c b/cores/esp8266/uart.c index 2c9f56bcb9..47814f7a9d 100644 --- a/cores/esp8266/uart.c +++ b/cores/esp8266/uart.c @@ -44,7 +44,6 @@ #include "uart.h" #include "esp8266_peri.h" #include "user_interface.h" -#include "uart_register.h" const char overrun_str [] ICACHE_RODATA_ATTR STORE_ATTR = "uart input full!\r\n"; static int s_uart_debug_nr = UART0; @@ -740,39 +739,3 @@ uart_get_debug() { return s_uart_debug_nr; } - -/* -To start detection of baud rate with the UART the UART_AUTOBAUD_EN bit needs to be cleared and set. The ROM function uart_baudrate_detect() does this only once, so on a next call the UartDev.rcv_state is not equal to BAUD_RATE_DET. Instead of poking around in the UartDev struct with unknown effect, the UART_AUTOBAUD_EN bit is directly triggered by the function uart_detect_baudrate(). -*/ -int -uart_detect_baudrate(int uart_nr) -{ - static bool doTrigger = true; - - if(doTrigger) - { - USA(uart_nr) &= ~(UART_GLITCH_FILT << UART_GLITCH_FILT_S | UART_AUTOBAUD_EN); - USA(uart_nr) = 0x08 << UART_GLITCH_FILT_S | UART_AUTOBAUD_EN; - doTrigger = false; - } - - int32_t divisor = uart_baudrate_detect(uart_nr, 1); - if(!divisor) return 0; - - doTrigger = true; // Initialize for a next round - int32_t baudrate = UART_CLK_FREQ / divisor; - - static const int default_rates[] = {300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 256000, 460800, 921600, 1843200, 3686400}; - - size_t i; - for(i = 1; i < sizeof(default_rates) / sizeof(default_rates[0]) - 1; i++) // find the nearest real baudrate - { - if(baudrate <= default_rates[i]) - { - if(baudrate - default_rates[i - 1] < default_rates[i] - baudrate) i--; - break; - } - } - - return default_rates[i]; -} diff --git a/cores/esp8266/uart.h b/cores/esp8266/uart.h index 9b050645e7..127c5d0ebb 100644 --- a/cores/esp8266/uart.h +++ b/cores/esp8266/uart.h @@ -141,8 +141,6 @@ bool uart_has_overrun (uart_t* uart); // returns then clear overrun flag void uart_set_debug(int uart_nr); int uart_get_debug(); -int uart_detect_baudrate(int uart_nr); - #if defined (__cplusplus) } // extern "C" diff --git a/libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino b/libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino index ebd30b7259..c3dbf18d23 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, "http://guest:guest@jigsaw.w3.org/HTTP/Basic/"); /* // or - http.begin("http://192.168.1.12/test.html"); - http.setAuthorization("user", "password"); + http.begin(client, "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(client, "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..adb4fc1b37 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, "http://jigsaw.w3.org/HTTP/connection.html")) { // 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..56d55d8f61 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, 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..c9463787b2 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, "http://192.168.1.12/test.html"); + //http.begin(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..5dc4bad33c 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, "http://jigsaw.w3.org/HTTP/connection.html"); + //http.begin(client, "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..df6689c16c --- /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(client, "jigsaw.w3.org", 443, "/HTTP/connection.html", true)) { + if (http.begin(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..141708dbab 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(WiFiClient &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(WiFiClient &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 @@ -631,7 +709,11 @@ int HTTPClient::getSize(void) WiFiClient& HTTPClient::getStream(void) { if(connected()) { +#ifdef KEEP_PRESENT_API + return *_tcpDeprecated; +#else return *_tcp; +#endif } DEBUG_HTTPCLIENT("[HTTP-Client] getStream: not connected\n"); @@ -646,7 +728,11 @@ WiFiClient& HTTPClient::getStream(void) WiFiClient* HTTPClient::getStreamPtr(void) { if(connected()) { - return _tcp.get(); +#ifdef KEEP_PRESENT_API + return _tcpDeprecated.get(); +#else + return _tcp; +#endif } DEBUG_HTTPCLIENT("[HTTP-Client] getStreamPtr: not connected\n"); @@ -897,12 +983,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 +1007,23 @@ 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 + if(_tcp) + _tcp->setNoDelay(true); +#endif #endif return connected(); } diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h index 72b42853f7..3c44370236 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h @@ -20,13 +20,17 @@ * 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 + #include #ifdef DEBUG_ESP_HTTP_CLIENT @@ -124,8 +128,10 @@ typedef enum { HTTPC_TE_CHUNKED } transferEncoding_t; +#ifdef KEEP_PRESENT_API class TransportTraits; typedef std::unique_ptr TransportTraitsPtr; +#endif class HTTPClient { @@ -133,6 +139,10 @@ class HTTPClient HTTPClient(); ~HTTPClient(); + bool begin(WiFiClient &client, String url); + bool begin(WiFiClient &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 +154,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); @@ -204,8 +215,11 @@ class HTTPClient int writeToStreamDataBlock(Stream * stream, int len); +#ifdef KEEP_PRESENT_API TransportTraitsPtr _transportTraits; - std::unique_ptr _tcp; + std::unique_ptr _tcpDeprecated; +#endif + WiFiClient* _tcp; /// request handling String _host; From 7b134cb22756ee4c5b30639ff58293d6d4891453 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Sun, 29 Jul 2018 14:40:44 +0200 Subject: [PATCH 05/31] update() functions and updateSpiffs() function added to pass a WiFiClient parameter and added secure update example --- cores/esp8266/uart.c | 37 ----- cores/esp8266/uart.h | 2 - .../httpUpdateSecure/httpUpdateSecure.ino | 138 ++++++++++++++++++ .../src/ESP8266httpUpdate.cpp | 28 ++++ .../ESP8266httpUpdate/src/ESP8266httpUpdate.h | 12 ++ 5 files changed, 178 insertions(+), 39 deletions(-) create mode 100644 libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino diff --git a/cores/esp8266/uart.c b/cores/esp8266/uart.c index 2c9f56bcb9..47814f7a9d 100644 --- a/cores/esp8266/uart.c +++ b/cores/esp8266/uart.c @@ -44,7 +44,6 @@ #include "uart.h" #include "esp8266_peri.h" #include "user_interface.h" -#include "uart_register.h" const char overrun_str [] ICACHE_RODATA_ATTR STORE_ATTR = "uart input full!\r\n"; static int s_uart_debug_nr = UART0; @@ -740,39 +739,3 @@ uart_get_debug() { return s_uart_debug_nr; } - -/* -To start detection of baud rate with the UART the UART_AUTOBAUD_EN bit needs to be cleared and set. The ROM function uart_baudrate_detect() does this only once, so on a next call the UartDev.rcv_state is not equal to BAUD_RATE_DET. Instead of poking around in the UartDev struct with unknown effect, the UART_AUTOBAUD_EN bit is directly triggered by the function uart_detect_baudrate(). -*/ -int -uart_detect_baudrate(int uart_nr) -{ - static bool doTrigger = true; - - if(doTrigger) - { - USA(uart_nr) &= ~(UART_GLITCH_FILT << UART_GLITCH_FILT_S | UART_AUTOBAUD_EN); - USA(uart_nr) = 0x08 << UART_GLITCH_FILT_S | UART_AUTOBAUD_EN; - doTrigger = false; - } - - int32_t divisor = uart_baudrate_detect(uart_nr, 1); - if(!divisor) return 0; - - doTrigger = true; // Initialize for a next round - int32_t baudrate = UART_CLK_FREQ / divisor; - - static const int default_rates[] = {300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 256000, 460800, 921600, 1843200, 3686400}; - - size_t i; - for(i = 1; i < sizeof(default_rates) / sizeof(default_rates[0]) - 1; i++) // find the nearest real baudrate - { - if(baudrate <= default_rates[i]) - { - if(baudrate - default_rates[i - 1] < default_rates[i] - baudrate) i--; - break; - } - } - - return default_rates[i]; -} diff --git a/cores/esp8266/uart.h b/cores/esp8266/uart.h index 9b050645e7..127c5d0ebb 100644 --- a/cores/esp8266/uart.h +++ b/cores/esp8266/uart.h @@ -141,8 +141,6 @@ bool uart_has_overrun (uart_t* uart); // returns then clear overrun flag void uart_set_debug(int uart_nr); int uart_get_debug(); -int uart_detect_baudrate(int uart_nr); - #if defined (__cplusplus) } // extern "C" diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino b/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino new file mode 100644 index 0000000000..2cc665b77d --- /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, "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..1e4c7cd517 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(WiFiClient& 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(WiFiClient& 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(WiFiClient& 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 diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h index 1676d51dfc..53f5f0bb19 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h @@ -26,6 +26,8 @@ #ifndef ESP8266HTTPUPDATE_H_ #define ESP8266HTTPUPDATE_H_ +#define ESP8266HTTPUPDATE_KEEP_CURRENT_API + #include #include #include @@ -72,6 +74,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 +83,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(WiFiClient& 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 +97,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(WiFiClient& 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(WiFiClient& client, const String& url, const String& currentVersion = ""); int getLastError(void); From d4db72d0ced94165958b921486fba05052d89bfd Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Mon, 20 Aug 2018 16:10:06 +0200 Subject: [PATCH 06/31] add basicHttpsClient example, ensured in the backward compatible function that only one connection is made, updated version to 1.2 deprecating present begin() functions, several minor updates --- .../examples/Authorization/Authorization.ino | 26 ++- .../BasicHttpClient/BasicHttpClient.ino | 30 ++- .../BasicHttpsClient/BasicHttpsClient.ino | 78 +++++++ .../ReuseConnection/ReuseConnection.ino | 26 ++- .../StreamHttpClient/StreamHttpClient.ino | 30 ++- .../StreamHttpsClient/StreamHttpsClient.ino | 39 ++-- .../ESP8266HTTPClient/library.properties | 2 +- .../src/ESP8266HTTPClient.cpp | 200 +++++++++++------- .../ESP8266HTTPClient/src/ESP8266HTTPClient.h | 26 ++- 9 files changed, 290 insertions(+), 167 deletions(-) create mode 100644 libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino diff --git a/libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino b/libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino index c3dbf18d23..9de3077ff4 100644 --- a/libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino +++ b/libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino @@ -14,22 +14,20 @@ #include -#define USE_SERIAL Serial - ESP8266WiFiMulti WiFiMulti; void setup() { - USE_SERIAL.begin(115200); - // USE_SERIAL.setDebugOutput(true); + Serial.begin(115200); + // Serial.setDebugOutput(true); - USE_SERIAL.println(); - USE_SERIAL.println(); - USE_SERIAL.println(); + Serial.println(); + Serial.println(); + Serial.println(); for (uint8_t t = 4; t > 0; t--) { - USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); - USE_SERIAL.flush(); + Serial.printf("[SETUP] WAIT %d...\n", t); + Serial.flush(); delay(1000); } @@ -46,7 +44,7 @@ void loop() { HTTPClient http; - USE_SERIAL.print("[HTTP] begin...\n"); + Serial.print("[HTTP] begin...\n"); // configure traged server and url @@ -63,22 +61,22 @@ void loop() { */ - USE_SERIAL.print("[HTTP] GET...\n"); + 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); + 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); + Serial.println(payload); } } else { - USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); + Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); diff --git a/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino b/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino index adb4fc1b37..21a97172ef 100644 --- a/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino +++ b/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino @@ -14,22 +14,20 @@ #include -#define USE_SERIAL Serial - ESP8266WiFiMulti WiFiMulti; void setup() { - USE_SERIAL.begin(115200); - // USE_SERIAL.setDebugOutput(true); + Serial.begin(115200); + // Serial.setDebugOutput(true); - USE_SERIAL.println(); - USE_SERIAL.println(); - USE_SERIAL.println(); + Serial.println(); + Serial.println(); + Serial.println(); for (uint8_t t = 4; t > 0; t--) { - USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); - USE_SERIAL.flush(); + Serial.printf("[SETUP] WAIT %d...\n", t); + Serial.flush(); delay(1000); } @@ -46,33 +44,31 @@ void loop() { 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 + Serial.print("[HTTP] begin...\n"); if (http.begin(client, "http://jigsaw.w3.org/HTTP/connection.html")) { // HTTP - USE_SERIAL.print("[HTTP] GET...\n"); + 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); + Serial.printf("[HTTP] GET... code: %d\n", httpCode); // file found at server if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { String payload = http.getString(); - USE_SERIAL.println(payload); + Serial.println(payload); } } else { - USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); + Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); } else { - USE_SERIAL.printf("Unable to connect\n"); + Serial.printf("[HTTP} Unable to connect\n"); } } diff --git a/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino new file mode 100644 index 0000000000..3c5c0ed5b7 --- /dev/null +++ b/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino @@ -0,0 +1,78 @@ +/** + BasicHTTPSClient.ino + + Created on: 20.08.2018 + +*/ + +#include + +#include +#include + +#include + +#include +// Fingerprint for demo URL, expires on June 2, 2019, needs to be updated well before this date +const uint8_t fingerprint[20] = {0x5A, 0xCF, 0xFE, 0xF0, 0xF1, 0xA6, 0xF4, 0x5F, 0xD2, 0x11, 0x11, 0xC6, 0x1D, 0x2F, 0x0E, 0xBC, 0x39, 0x8D, 0x50, 0xE0}; + +ESP8266WiFiMulti WiFiMulti; + +void setup() { + + Serial.begin(115200); + // Serial.setDebugOutput(true); + + Serial.println(); + Serial.println(); + Serial.println(); + + for (uint8_t t = 4; t > 0; t--) { + Serial.printf("[SETUP] WAIT %d...\n", t); + Serial.flush(); + delay(1000); + } + + WiFi.mode(WIFI_STA); + WiFiMulti.addAP("SSID", "PASSWORD"); +} + +void loop() { + // wait for WiFi connection + if ((WiFiMulti.run() == WL_CONNECTED)) { + + BearSSL::WiFiClientSecure client; + client.setFingerprint(fingerprint); + + HTTPClient https; + + Serial.print("[HTTPS] begin...\n"); + if (https.begin(client, "https://jigsaw.w3.org/HTTP/connection.html")) { // HTTPS + + + Serial.print("[HTTPS] GET...\n"); + // start connection and send HTTP header + int httpCode = https.GET(); + + // httpCode will be negative on error + if (httpCode > 0) { + // HTTP header has been send and Server response header has been handled + Serial.printf("[HTTPS] GET... code: %d\n", httpCode); + + // file found at server + if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { + String payload = https.getString(); + Serial.println(payload); + } + } else { + Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str()); + } + + https.end(); + } else { + Serial.printf("[HTTPS] Unable to connect\n"); + } + } + + delay(10000); +} diff --git a/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino b/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino index c9463787b2..2823a2dbb9 100644 --- a/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino +++ b/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino @@ -13,24 +13,22 @@ #include -#define USE_SERIAL Serial - ESP8266WiFiMulti WiFiMulti; HTTPClient http; void setup() { - USE_SERIAL.begin(115200); - // USE_SERIAL.setDebugOutput(true); + Serial.begin(115200); + // Serial.setDebugOutput(true); - USE_SERIAL.println(); - USE_SERIAL.println(); - USE_SERIAL.println(); + Serial.println(); + Serial.println(); + Serial.println(); for (uint8_t t = 4; t > 0; t--) { - USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); - USE_SERIAL.flush(); + Serial.printf("[SETUP] WAIT %d...\n", t); + Serial.flush(); delay(1000); } @@ -47,19 +45,19 @@ void loop() { WiFiClient client; - http.begin(client, "http://192.168.1.12/test.html"); - //http.begin(client, "192.168.1.12", 80, "/test.html"); + http.begin(client, "http://jigsaw.w3.org/HTTP/connection.html"); + //http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html"); int httpCode = http.GET(); if (httpCode > 0) { - USE_SERIAL.printf("[HTTP] GET... code: %d\n", httpCode); + Serial.printf("[HTTP] GET... code: %d\n", httpCode); // file found at server if (httpCode == HTTP_CODE_OK) { - http.writeToStream(&USE_SERIAL); + http.writeToStream(&Serial); } } else { - USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); + Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); diff --git a/libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino b/libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino index 5dc4bad33c..10faa68b5d 100644 --- a/libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino +++ b/libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino @@ -12,22 +12,20 @@ #include -#define USE_SERIAL Serial - ESP8266WiFiMulti WiFiMulti; void setup() { - USE_SERIAL.begin(115200); - // USE_SERIAL.setDebugOutput(true); + Serial.begin(115200); + // Serial.setDebugOutput(true); - USE_SERIAL.println(); - USE_SERIAL.println(); - USE_SERIAL.println(); + Serial.println(); + Serial.println(); + Serial.println(); for (uint8_t t = 4; t > 0; t--) { - USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); - USE_SERIAL.flush(); + Serial.printf("[SETUP] WAIT %d...\n", t); + Serial.flush(); delay(1000); } @@ -44,18 +42,18 @@ void loop() { WiFiClient client; - USE_SERIAL.print("[HTTP] begin...\n"); + Serial.print("[HTTP] begin...\n"); // configure server and url http.begin(client, "http://jigsaw.w3.org/HTTP/connection.html"); //http.begin(client, "jigsaw.w3.org", 80, "/HTTP/connection.html"); - USE_SERIAL.print("[HTTP] GET...\n"); + 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); + Serial.printf("[HTTP] GET... code: %d\n", httpCode); // file found at server if (httpCode == HTTP_CODE_OK) { @@ -79,7 +77,7 @@ void loop() { int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); // write it to Serial - USE_SERIAL.write(buff, c); + Serial.write(buff, c); if (len > 0) { len -= c; @@ -88,12 +86,12 @@ void loop() { delay(1); } - USE_SERIAL.println(); - USE_SERIAL.print("[HTTP] connection closed or file end.\n"); + Serial.println(); + Serial.print("[HTTP] connection closed or file end.\n"); } } else { - USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); + Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); diff --git a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino index df6689c16c..156fa17dd9 100644 --- a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino +++ b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino @@ -12,22 +12,20 @@ #include -#define USE_SERIAL Serial - ESP8266WiFiMulti WiFiMulti; void setup() { - USE_SERIAL.begin(115200); - // USE_SERIAL.setDebugOutput(true); + Serial.begin(115200); + // Serial.setDebugOutput(true); - USE_SERIAL.println(); - USE_SERIAL.println(); - USE_SERIAL.println(); + Serial.println(); + Serial.println(); + Serial.println(); for (uint8_t t = 4; t > 0; t--) { - USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); - USE_SERIAL.flush(); + Serial.printf("[SETUP] WAIT %d...\n", t); + Serial.flush(); delay(1000); } @@ -45,27 +43,26 @@ void loop() { 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"); + Serial.printf("\nConnecting to https://tls.mbed.org\n"); + Serial.printf("Maximum fragment Length negotiation supported: %s\n", mfln ? "yes" : "no"); if (mfln) { client.setBufferSizes(1024, 1024); } - USE_SERIAL.print("[HTTP] begin...\n"); + Serial.print("[HTTPS] 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(client, "jigsaw.w3.org", 443, "/HTTP/connection.html", true)) { if (http.begin(client, "https://tls.mbed.org/")) { - USE_SERIAL.print("[HTTP] GET...\n"); + Serial.print("[HTTPS] 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); + Serial.printf("[HTTPS] GET... code: %d\n", httpCode); // file found at server if (httpCode == HTTP_CODE_OK) { @@ -74,7 +71,7 @@ void loop() { int len = http.getSize(); // create buffer for read - uint8_t buff[128] = { 0 }; + static uint8_t buff[128] = { 0 }; // get tcp stream WiFiClient * stream = &client; @@ -89,7 +86,7 @@ void loop() { int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); // write it to Serial - USE_SERIAL.write(buff, c); + Serial.write(buff, c); if (len > 0) { len -= c; @@ -98,17 +95,17 @@ void loop() { delay(1); } - USE_SERIAL.println(); - USE_SERIAL.print("[HTTP] connection closed or file end.\n"); + Serial.println(); + Serial.print("[HTTPS] connection closed or file end.\n"); } } else { - USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); + Serial.printf("[HTTPS] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } http.end(); } else { - USE_SERIAL.printf("Unable to connect\n"); + Serial.printf("Unable to connect\n"); } } diff --git a/libraries/ESP8266HTTPClient/library.properties b/libraries/ESP8266HTTPClient/library.properties index a63eb5717b..01cedadb66 100644 --- a/libraries/ESP8266HTTPClient/library.properties +++ b/libraries/ESP8266HTTPClient/library.properties @@ -1,5 +1,5 @@ name=ESP8266HTTPClient -version=1.1 +version=1.2 author=Markus Sattler maintainer=Markus Sattler sentence=http Client for ESP8266 diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 141708dbab..25d75cfbd9 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -25,7 +25,7 @@ #include "ESP8266HTTPClient.h" -#ifdef KEEP_PRESENT_API +#ifdef HTTPCLIENT_1_1_COMPATIBLE #include #include #endif @@ -33,7 +33,7 @@ #include #include -#ifdef KEEP_PRESENT_API +#ifdef HTTPCLIENT_1_1_COMPATIBLE class TransportTraits { public: @@ -104,13 +104,17 @@ class BearSSLTraits : public TransportTraits protected: uint8_t _fingerprint[20]; }; -#endif +#endif // HTTPCLIENT_1_1_COMPATIBLE /** * constructor */ HTTPClient::HTTPClient() { + _client = nullptr; +#ifdef HTTPCLIENT_1_1_COMPATIBLE + _tcpDeprecated.reset(nullptr); +#endif } /** @@ -118,9 +122,14 @@ HTTPClient::HTTPClient() */ HTTPClient::~HTTPClient() { - if(_tcp) { - _tcp->stop(); + if(_client) { + _client->stop(); } +#ifdef HTTPCLIENT_1_1_COMPATIBLE + if(_tcpDeprecated) { + _tcpDeprecated->stop(); + } +#endif if(_currentHeaders) { delete[] _currentHeaders; } @@ -142,11 +151,9 @@ void HTTPClient::clear() * @return success bool */ bool HTTPClient::begin(WiFiClient &client, String url) { -#ifdef KEEP_PRESENT_API - _tcpDeprecated.reset(nullptr); - _transportTraits.reset(nullptr); -#endif - _tcp = &client; + end(); + + _client = &client; // check for : (http: or https:) int index = url.indexOf(':'); @@ -177,11 +184,9 @@ bool HTTPClient::begin(WiFiClient &client, String url) { */ bool HTTPClient::begin(WiFiClient &client, String host, uint16_t port, String uri, bool https) { -#ifdef KEEP_PRESENT_API - _tcpDeprecated.reset(nullptr); - _transportTraits.reset(nullptr); -#endif - _tcp = &client; + end(); + + _client = &client; clear(); _host = host; @@ -192,11 +197,11 @@ bool HTTPClient::begin(WiFiClient &client, String host, uint16_t port, String ur } -#ifdef KEEP_PRESENT_API +#ifdef HTTPCLIENT_1_1_COMPATIBLE bool HTTPClient::begin(String url, String httpsFingerprint) { - _tcp = nullptr; - _transportTraits.reset(nullptr); + if(_client) _canReuse = false; + end(); _port = 443; if (httpsFingerprint.length() == 0) { @@ -213,8 +218,8 @@ bool HTTPClient::begin(String url, String httpsFingerprint) bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20]) { - _tcp = nullptr; - _transportTraits.reset(nullptr); + if(_client) _canReuse = false; + end(); _port = 443; if (!beginInternal(url, "https")) { @@ -236,8 +241,8 @@ bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20]) */ bool HTTPClient::begin(String url) { - _tcp = nullptr; - _transportTraits.reset(nullptr); + if(_client) _canReuse = false; + end(); _port = 80; if (!beginInternal(url, "http")) { @@ -246,7 +251,7 @@ bool HTTPClient::begin(String url) _transportTraits = TransportTraitsPtr(new TransportTraits()); return true; } -#endif +#endif // HTTPCLIENT_1_1_COMPATIBLE bool HTTPClient::beginInternal(String url, const char* expectedProtocol) { @@ -295,10 +300,11 @@ bool HTTPClient::beginInternal(String url, const char* expectedProtocol) return true; } -#ifdef KEEP_PRESENT_API +#ifdef HTTPCLIENT_1_1_COMPATIBLE bool HTTPClient::begin(String host, uint16_t port, String uri) { - _tcp = nullptr; + if(_client) _canReuse = false; + end(); clear(); _host = host; @@ -320,7 +326,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; + if(_client) _canReuse = false; + end(); clear(); _host = host; @@ -337,7 +344,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; + if(_client) _canReuse = false; + end(); clear(); _host = host; @@ -352,41 +360,87 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t htt DEBUG_HTTPCLIENT("\n"); return true; } -#endif +#endif // HTTPCLIENT_1_1_COMPATIBLE /** * end * called after the payload is handled */ +#ifdef HTTPCLIENT_1_1_COMPATIBLE void HTTPClient::end(void) { if(connected()) { - if(_tcp->available() > 0) { - DEBUG_HTTPCLIENT("[HTTP-Client][end] still data in buffer (%d), clean up.\n", _tcp->available()); - while(_tcp->available() > 0) { - _tcp->read(); + if(_client) { + if(_client->available() > 0) { + DEBUG_HTTPCLIENT("[HTTP-Client][end] still data in buffer (%d), clean up.\n", _client->available()); + while(_client->available() > 0) { + _client->read(); + } + } + + } + if(_tcpDeprecated) { + if(_tcpDeprecated->available() > 0) { + DEBUG_HTTPCLIENT("[HTTP-Client][end] still data in buffer (%d), clean up.\n", _tcpDeprecated->available()); + while(_tcpDeprecated->available() > 0) { + _tcpDeprecated->read(); + } } } if(_reuse && _canReuse) { DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp keep open for reuse\n"); } else { DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp stop\n"); - _tcp->stop(); + if(_client) { + _client->stop(); + _client = nullptr; + } + if(_tcpDeprecated) { + _tcpDeprecated->stop(); + _transportTraits.reset(nullptr); + _tcpDeprecated.reset(nullptr); + } } } else { DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp is closed\n"); } } - +#else +void HTTPClient::end(void) +{ + if(connected()) { + if(_client->available() > 0) { + DEBUG_HTTPCLIENT("[HTTP-Client][end] still data in buffer (%d), clean up.\n", _client->available()); + while(_client->available() > 0) { + _client->read(); + } + } + if(_reuse && _canReuse) { + DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp keep open for reuse\n"); + } else { + DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp stop\n"); + _client->stop(); + _client = nullptr; + } + } else { + DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp is closed\n"); + } +} +#endif /** * connected * @return connected status */ bool HTTPClient::connected() { - if(_tcp) { - return (_tcp->connected() || (_tcp->available() > 0)); + if(_client) { + return (_client->connected() || (_client->available() > 0)); } +#ifdef HTTPCLIENT_1_1_COMPATIBLE + if(_tcpDeprecated) { + return (_tcpDeprecated->connected() || (_tcpDeprecated->available() > 0)); + } +#endif return false; } @@ -443,7 +497,7 @@ void HTTPClient::setTimeout(uint16_t timeout) { _tcpTimeout = timeout; if(connected()) { - _tcp->setTimeout(timeout); + _client->setTimeout(timeout); } } @@ -545,7 +599,7 @@ int HTTPClient::sendRequest(const char * type, uint8_t * payload, size_t size) // send Payload if needed if(payload && size > 0) { - if(_tcp->write(&payload[0], size) != size) { + if(_client->write(&payload[0], size) != size) { return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED); } } @@ -624,7 +678,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) int bytesRead = stream->readBytes(buff, readBytes); // write it to Stream - int bytesWrite = _tcp->write((const uint8_t *) buff, bytesRead); + int bytesWrite = _client->write((const uint8_t *) buff, bytesRead); bytesWritten += bytesWrite; // are all Bytes a writen to stream ? @@ -632,11 +686,11 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] short write, asked for %d but got %d retry...\n", bytesRead, bytesWrite); // check for write error - if(_tcp->getWriteError()) { - DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] stream write error %d\n", _tcp->getWriteError()); + if(_client->getWriteError()) { + DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] stream write error %d\n", _client->getWriteError()); //reset write error for retry - _tcp->clearWriteError(); + _client->clearWriteError(); } // some time for the stream @@ -645,7 +699,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) int leftBytes = (readBytes - bytesWrite); // retry to send the missed bytes - bytesWrite = _tcp->write((const uint8_t *) (buff + bytesWrite), leftBytes); + bytesWrite = _client->write((const uint8_t *) (buff + bytesWrite), leftBytes); bytesWritten += bytesWrite; if(bytesWrite != leftBytes) { @@ -657,8 +711,8 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) } // check for write error - if(_tcp->getWriteError()) { - DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] stream write error %d\n", _tcp->getWriteError()); + if(_client->getWriteError()) { + DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] stream write error %d\n", _client->getWriteError()); free(buff); return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED); } @@ -709,10 +763,10 @@ int HTTPClient::getSize(void) WiFiClient& HTTPClient::getStream(void) { if(connected()) { -#ifdef KEEP_PRESENT_API - return *_tcpDeprecated; +#ifdef HTTPCLIENT_1_1_COMPATIBLE + return _client ? *_client : *_tcpDeprecated; #else - return *_tcp; + return *_client; #endif } @@ -728,10 +782,10 @@ WiFiClient& HTTPClient::getStream(void) WiFiClient* HTTPClient::getStreamPtr(void) { if(connected()) { -#ifdef KEEP_PRESENT_API +#ifdef HTTPCLIENT_1_1_COMPATIBLE return _tcpDeprecated.get(); #else - return _tcp; + return _client; #endif } @@ -772,7 +826,7 @@ int HTTPClient::writeToStream(Stream * stream) if(!connected()) { return returnError(HTTPC_ERROR_CONNECTION_LOST); } - String chunkHeader = _tcp->readStringUntil('\n'); + String chunkHeader = _client->readStringUntil('\n'); if(chunkHeader.length() <= 0) { return returnError(HTTPC_ERROR_READ_TIMEOUT); @@ -809,7 +863,7 @@ int HTTPClient::writeToStream(Stream * stream) // read trailing \r\n at the end of the chunk char buf[2]; - auto trailing_seq_len = _tcp->readBytes((uint8_t*)buf, 2); + auto trailing_seq_len = _client->readBytes((uint8_t*)buf, 2); if (trailing_seq_len != 2 || buf[0] != '\r' || buf[1] != '\n') { return returnError(HTTPC_ERROR_READ_TIMEOUT); } @@ -977,52 +1031,52 @@ bool HTTPClient::connect(void) if(connected()) { DEBUG_HTTPCLIENT("[HTTP-Client] connect. already connected, try reuse!\n"); - while(_tcp->available() > 0) { - _tcp->read(); + while(_client->available() > 0) { + _client->read(); } return true; } -#ifdef KEEP_PRESENT_API - if (!_tcp && !_transportTraits) { +#ifdef HTTPCLIENT_1_1_COMPATIBLE + if (!_client && !_transportTraits) { #else - if(!_tcp) { + if(!_client) { #endif DEBUG_HTTPCLIENT("[HTTP-Client] connect: HTTPClient::begin was not called or returned error\n"); return false; } -#ifdef KEEP_PRESENT_API - if(!_tcp) { +#ifdef HTTPCLIENT_1_1_COMPATIBLE + if(!_client) { _tcpDeprecated = _transportTraits->create(); - _tcp = _tcpDeprecated.get(); + _client = _tcpDeprecated.get(); } #endif - _tcp->setTimeout(_tcpTimeout); + _client->setTimeout(_tcpTimeout); - if(!_tcp->connect(_host.c_str(), _port)) { + if(!_client->connect(_host.c_str(), _port)) { DEBUG_HTTPCLIENT("[HTTP-Client] failed connect to %s:%u\n", _host.c_str(), _port); return false; } DEBUG_HTTPCLIENT("[HTTP-Client] connected to %s:%u\n", _host.c_str(), _port); -#ifdef KEEP_PRESENT_API +#ifdef HTTPCLIENT_1_1_COMPATIBLE if (_tcpDeprecated && _transportTraits && !_transportTraits->verify(*_tcpDeprecated, _host.c_str())) { DEBUG_HTTPCLIENT("[HTTP-Client] transport level verify failed\n"); - _tcp->stop(); + _client->stop(); return false; } #endif #ifdef ESP8266 -#ifdef KEEP_PRESENT_API +#ifdef HTTPCLIENT_1_1_COMPATIBLE if(_tcpDeprecated) _tcpDeprecated->setNoDelay(true); #else - if(_tcp) - _tcp->setNoDelay(true); + if(_client) + _client->setNoDelay(true); #endif #endif return connected(); @@ -1078,7 +1132,7 @@ bool HTTPClient::sendHeader(const char * type) DEBUG_HTTPCLIENT("[HTTP-Client] sending request header\n-----\n%s-----\n", header.c_str()); - return (_tcp->write((const uint8_t *) header.c_str(), header.length()) == header.length()); + return (_client->write((const uint8_t *) header.c_str(), header.length()) == header.length()); } /** @@ -1099,9 +1153,9 @@ int HTTPClient::handleHeaderResponse() unsigned long lastDataTime = millis(); while(connected()) { - size_t len = _tcp->available(); + size_t len = _client->available(); if(len > 0) { - String headerLine = _tcp->readStringUntil('\n'); + String headerLine = _client->readStringUntil('\n'); headerLine.trim(); // remove \r lastDataTime = millis(); @@ -1197,7 +1251,7 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size) while(connected() && (len > 0 || len == -1)) { // get available data size - size_t sizeAvailable = _tcp->available(); + size_t sizeAvailable = _client->available(); if(sizeAvailable) { @@ -1214,7 +1268,7 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size) } // read data - int bytesRead = _tcp->readBytes(buff, readBytes); + int bytesRead = _client->readBytes(buff, readBytes); // write it to Stream int bytesWrite = stream->write(buff, bytesRead); @@ -1295,7 +1349,7 @@ int HTTPClient::returnError(int error) DEBUG_HTTPCLIENT("[HTTP-Client][returnError] error(%d): %s\n", error, errorToString(error).c_str()); if(connected()) { DEBUG_HTTPCLIENT("[HTTP-Client][returnError] tcp stop\n"); - _tcp->stop(); + _client->stop(); } } return error; diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h index 3c44370236..4a2577c4aa 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h @@ -26,7 +26,7 @@ #ifndef ESP8266HTTPClient_H_ #define ESP8266HTTPClient_H_ -#define KEEP_PRESENT_API +#define HTTPCLIENT_1_1_COMPATIBLE #include #include @@ -128,7 +128,7 @@ typedef enum { HTTPC_TE_CHUNKED } transferEncoding_t; -#ifdef KEEP_PRESENT_API +#ifdef HTTPCLIENT_1_1_COMPATIBLE class TransportTraits; typedef std::unique_ptr TransportTraitsPtr; #endif @@ -139,19 +139,23 @@ class HTTPClient HTTPClient(); ~HTTPClient(); +/* + * Since both begin() functions take a reference to client as a parameter, you need to + * ensure the client object lives the entire time of the HTTPClient + */ bool begin(WiFiClient &client, String url); bool begin(WiFiClient &client, String host, uint16_t port, String uri = "/", bool https = false); -#ifdef KEEP_PRESENT_API +#ifdef HTTPCLIENT_1_1_COMPATIBLE // Plain HTTP connection, unencrypted - bool begin(String url); - bool begin(String host, uint16_t port, String uri = "/"); + bool begin(String url) __attribute__ ((deprecated)); + bool begin(String host, uint16_t port, String uri = "/") __attribute__ ((deprecated)); // Use axTLS for secure HTTPS connection - bool begin(String url, String httpsFingerprint); - bool begin(String host, uint16_t port, String uri, String httpsFingerprint); + bool begin(String url, String httpsFingerprint) __attribute__ ((deprecated)); + bool begin(String host, uint16_t port, String uri, String httpsFingerprint) __attribute__ ((deprecated)); // Use BearSSL for secure HTTPS connection - bool begin(String url, const uint8_t httpsFingerprint[20]); - bool begin(String host, uint16_t port, String uri, const uint8_t httpsFingerprint[20]); + bool begin(String url, const uint8_t httpsFingerprint[20]) __attribute__ ((deprecated)); + bool begin(String host, uint16_t port, String uri, const uint8_t httpsFingerprint[20]) __attribute__ ((deprecated)); // deprecated, use the overload above instead bool begin(String host, uint16_t port, String uri, bool https, String httpsFingerprint) __attribute__ ((deprecated)); #endif @@ -215,11 +219,11 @@ class HTTPClient int writeToStreamDataBlock(Stream * stream, int len); -#ifdef KEEP_PRESENT_API +#ifdef HTTPCLIENT_1_1_COMPATIBLE TransportTraitsPtr _transportTraits; std::unique_ptr _tcpDeprecated; #endif - WiFiClient* _tcp; + WiFiClient* _client; /// request handling String _host; From 815fb0f71f4bf396faf84ab2b7e4ce1d5cd0ea11 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Tue, 21 Aug 2018 16:23:51 +0200 Subject: [PATCH 07/31] Adding #pragma's to suppress Travis Build warning for use of deprecated function --- libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 25d75cfbd9..595f026b96 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -315,6 +315,8 @@ bool HTTPClient::begin(String host, uint16_t port, String uri) return true; } +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wall" bool HTTPClient::begin(String host, uint16_t port, String uri, bool https, String httpsFingerprint) { if (https) { @@ -323,6 +325,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, bool https, Strin return begin(host, port, uri); } } +#pragma GCC diagnostic pop bool HTTPClient::begin(String host, uint16_t port, String uri, String httpsFingerprint) { From 2047402dc0b0a018c7031d513377e3df71e67fed Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Tue, 21 Aug 2018 16:35:03 +0200 Subject: [PATCH 08/31] Adding #pragma's to suppress Travis Build warning for use of deprecated function 2nd try --- libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 595f026b96..56a617a645 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -316,7 +316,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri) } #pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wall" +#pragma GCC diagnostic ignored "-Werror=deprecated-declarations" bool HTTPClient::begin(String host, uint16_t port, String uri, bool https, String httpsFingerprint) { if (https) { From 5ff9728ebd4ec21d80e6cf1bf45471c8849bf6db Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Tue, 21 Aug 2018 16:49:18 +0200 Subject: [PATCH 09/31] Adding #pragma's to suppress Travis Build warning for use of deprecated function 3rd try --- libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 56a617a645..2261b1a74b 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -316,7 +316,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri) } #pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Werror=deprecated-declarations" +#pragma GCC diagnostic ignored "-Wno-deprecated-declarations" bool HTTPClient::begin(String host, uint16_t port, String uri, bool https, String httpsFingerprint) { if (https) { From 11ea32a22f984f39ce092729e1dfe9a762b0ed07 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Tue, 21 Aug 2018 17:04:36 +0200 Subject: [PATCH 10/31] Adding #pragma's to suppress Travis Build warning for use of deprecated function 4th try --- libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 2261b1a74b..e48780ea05 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -316,7 +316,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri) } #pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wno-deprecated-declarations" +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" bool HTTPClient::begin(String host, uint16_t port, String uri, bool https, String httpsFingerprint) { if (https) { From cfd977ce6030d62506beb1f77c5a2fa383aa646e Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Tue, 21 Aug 2018 17:49:20 +0200 Subject: [PATCH 11/31] revert accidentally changed files ESP8266httpUpdate.h and .cpp to origin/master --- libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index e48780ea05..d43118ea4f 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -316,7 +316,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri) } #pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" bool HTTPClient::begin(String host, uint16_t port, String uri, bool https, String httpsFingerprint) { if (https) { From dc2069683eb405ee8ae35303cd29af9c33a82aea Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Tue, 21 Aug 2018 18:46:43 +0200 Subject: [PATCH 12/31] Adding #pragma's to ESP8266httpUpdate.cpp to ignore use of deprecated functions. Adaptations to work with new ESP8266hhtpClient are made in PR #4980 --- .../DigestAuthorization.ino | 2 +- .../src/ESP8266httpUpdate.cpp | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino index 56d55d8f61..8045620ae2 100644 --- a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino +++ b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino @@ -117,7 +117,7 @@ void loop() { String authorization = getDigestAuth(authReq, String(username), String(password), String(uri), 1); http.end(); - http.begin(String(server) + String(uri)); + http.begin(client, String(server) + String(uri)); http.addHeader("Authorization", authorization); diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp index 5cc4b6a09e..84d9907650 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp @@ -53,7 +53,10 @@ HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& curr HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& currentVersion) { HTTPClient http; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" http.begin(url); +#pragma GCC diagnostic pop return handleUpdate(http, currentVersion, false); } @@ -61,7 +64,10 @@ HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& curr const String& httpsFingerprint) { HTTPClient http; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" http.begin(url, httpsFingerprint); +#pragma GCC diagnostic pop return handleUpdate(http, currentVersion, false); } @@ -69,28 +75,40 @@ HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& curr const uint8_t httpsFingerprint[20]) { HTTPClient http; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" http.begin(url, httpsFingerprint); +#pragma GCC diagnostic pop return handleUpdate(http, currentVersion, false); } HTTPUpdateResult ESP8266HTTPUpdate::updateSpiffs(const String& url, const String& currentVersion, const String& httpsFingerprint) { HTTPClient http; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" http.begin(url, httpsFingerprint); +#pragma GCC diagnostic pop return handleUpdate(http, currentVersion, true); } HTTPUpdateResult ESP8266HTTPUpdate::updateSpiffs(const String& url, const String& currentVersion, const uint8_t httpsFingerprint[20]) { HTTPClient http; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" http.begin(url, httpsFingerprint); +#pragma GCC diagnostic pop return handleUpdate(http, currentVersion, true); } HTTPUpdateResult ESP8266HTTPUpdate::updateSpiffs(const String& url, const String& currentVersion) { HTTPClient http; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" http.begin(url); +#pragma GCC diagnostic pop return handleUpdate(http, currentVersion, true); } @@ -110,7 +128,10 @@ HTTPUpdateResult ESP8266HTTPUpdate::update(const String& host, uint16_t port, co const String& currentVersion) { HTTPClient http; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" http.begin(host, port, uri); +#pragma GCC diagnostic pop return handleUpdate(http, currentVersion, false); } @@ -118,7 +139,10 @@ HTTPUpdateResult ESP8266HTTPUpdate::update(const String& host, uint16_t port, co const String& currentVersion, const String& httpsFingerprint) { HTTPClient http; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" http.begin(host, port, url, httpsFingerprint); +#pragma GCC diagnostic pop return handleUpdate(http, currentVersion, false); } @@ -126,7 +150,10 @@ HTTPUpdateResult ESP8266HTTPUpdate::update(const String& host, uint16_t port, co const String& currentVersion, const uint8_t httpsFingerprint[20]) { HTTPClient http; +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" http.begin(host, port, url, httpsFingerprint); +#pragma GCC diagnostic pop return handleUpdate(http, currentVersion, false); } From 156b50664f704308eed189f7f100eea2c42a301b Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 22 Aug 2018 00:04:55 +0200 Subject: [PATCH 13/31] Work in progress --- .../ESP8266httpUpdate/library.properties | 2 +- .../src/ESP8266httpUpdate.cpp | 6 ++--- .../ESP8266httpUpdate/src/ESP8266httpUpdate.h | 26 +++++++++---------- 3 files changed, 17 insertions(+), 17 deletions(-) diff --git a/libraries/ESP8266httpUpdate/library.properties b/libraries/ESP8266httpUpdate/library.properties index 94a8499b89..766043b6ef 100644 --- a/libraries/ESP8266httpUpdate/library.properties +++ b/libraries/ESP8266httpUpdate/library.properties @@ -1,5 +1,5 @@ name=ESP8266httpUpdate -version=1.2 +version=1.3 author=Markus Sattler maintainer=Markus Sattler sentence=Http Update for ESP8266 diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp index 1e4c7cd517..c5200d1f14 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp @@ -43,7 +43,7 @@ ESP8266HTTPUpdate::~ESP8266HTTPUpdate(void) { } -#ifdef ESP8266HTTPUPDATE_KEEP_CURRENT_API +#ifdef HTTPUPDATE_1_2_COMPATIBLE HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& currentVersion, const String& httpsFingerprint, bool reboot) { @@ -82,7 +82,7 @@ HTTPUpdateResult ESP8266HTTPUpdate::update(WiFiClient& client, const String& url return handleUpdate(http, currentVersion, false); } -#ifdef ESP8266HTTPUPDATE_KEEP_CURRENT_API +#ifdef HTTPUPDATE_1_2_COMPATIBLE HTTPUpdateResult ESP8266HTTPUpdate::updateSpiffs(const String& url, const String& currentVersion, const String& httpsFingerprint) { HTTPClient http; @@ -112,7 +112,7 @@ HTTPUpdateResult ESP8266HTTPUpdate::updateSpiffs(WiFiClient& client, const Strin return handleUpdate(http, currentVersion, true); } -#ifdef ESP8266HTTPUPDATE_KEEP_CURRENT_API +#ifdef HTTPUPDATE_1_2_COMPATIBLE HTTPUpdateResult ESP8266HTTPUpdate::update(const String& host, uint16_t port, const String& uri, const String& currentVersion, bool https, const String& httpsFingerprint, bool reboot) { diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h index 53f5f0bb19..ec7359ba57 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h @@ -26,7 +26,7 @@ #ifndef ESP8266HTTPUPDATE_H_ #define ESP8266HTTPUPDATE_H_ -#define ESP8266HTTPUPDATE_KEEP_CURRENT_API +#define HTTPUPDATE_1_2_COMPATIBLE #include #include @@ -74,40 +74,40 @@ class ESP8266HTTPUpdate _rebootOnUpdate = reboot; } -#ifdef ESP8266HTTPUPDATE_KEEP_CURRENT_API +#ifdef HTTPUPDATE_1_2_COMPATIBLE // 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 = "") __attribute__((deprecated)); t_httpUpdate_return update(const String& url, const String& currentVersion, - const String& httpsFingerprint); + const String& httpsFingerprint) __attribute__((deprecated)); t_httpUpdate_return update(const String& url, const String& currentVersion, - const uint8_t httpsFingerprint[20]); // BearSSL + const uint8_t httpsFingerprint[20]) __attribute__((deprecated)); // BearSSL #endif t_httpUpdate_return update(WiFiClient& client, const String& url, const String& currentVersion = ""); -#ifdef ESP8266HTTPUPDATE_KEEP_CURRENT_API +#ifdef HTTPUPDATE_1_2_COMPATIBLE // 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 = ""); + const String& currentVersion = "") __attribute__((deprecated)); t_httpUpdate_return update(const String& host, uint16_t port, const String& url, - const String& currentVersion, const String& httpsFingerprint); + const String& currentVersion, const String& httpsFingerprint) __attribute__((deprecated)); t_httpUpdate_return update(const String& host, uint16_t port, const String& url, - const String& currentVersion, const uint8_t httpsFingerprint[20]); // BearSSL + const String& currentVersion, const uint8_t httpsFingerprint[20]) __attribute__((deprecated)); // BearSSL #endif t_httpUpdate_return update(WiFiClient& client, const String& host, uint16_t port, const String& uri = "/", const String& currentVersion = ""); -#ifdef ESP8266HTTPUPDATE_KEEP_CURRENT_API +#ifdef HTTPUPDATE_1_2_COMPATIBLE // 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 + t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion = "") __attribute__((deprecated)); + t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion, const String& httpsFingerprint) __attribute__((deprecated)); + t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion, const uint8_t httpsFingerprint[20]) __attribute__((deprecated)); // BearSSL #endif t_httpUpdate_return updateSpiffs(WiFiClient& client, const String& url, const String& currentVersion = ""); From 29e7754eed8ea893fb7ce38d3da85126598dde3d Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 22 Aug 2018 11:50:11 +0200 Subject: [PATCH 14/31] Allocate BearSSL::WiFiClientSecure dynamically in StreamHttpsClient.ino --- .../StreamHttpsClient/StreamHttpsClient.ino | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino index 156fa17dd9..b802e2dc5e 100644 --- a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino +++ b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino @@ -40,22 +40,22 @@ void loop() { HTTPClient http; - BearSSL::WiFiClientSecure client; + BearSSL::WiFiClientSecure *client = new BearSSL::WiFiClientSecure ; - bool mfln = client.probeMaxFragmentLength("tls.mbed.org", 443, 1024); + bool mfln = client->probeMaxFragmentLength("tls.mbed.org", 443, 1024); Serial.printf("\nConnecting to https://tls.mbed.org\n"); Serial.printf("Maximum fragment Length negotiation supported: %s\n", mfln ? "yes" : "no"); if (mfln) { - client.setBufferSizes(1024, 1024); + client->setBufferSizes(1024, 1024); } Serial.print("[HTTPS] 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); + client->setFingerprint(fingerprint); - if (http.begin(client, "https://tls.mbed.org/")) { + if (http.begin(*client, "https://tls.mbed.org/")) { Serial.print("[HTTPS] GET...\n"); // start connection and send HTTP header @@ -74,7 +74,7 @@ void loop() { static uint8_t buff[128] = { 0 }; // get tcp stream - WiFiClient * stream = &client; + WiFiClient * stream = client; // read all data from server while (http.connected() && (len > 0 || len == -1)) { @@ -107,6 +107,8 @@ void loop() { } else { Serial.printf("Unable to connect\n"); } + + delete client; } delay(10000); From 6fe03e3d2e869842f9ba6b2d6d99d6ffeb49c7e6 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 22 Aug 2018 12:24:49 +0200 Subject: [PATCH 15/31] Added one \n --- libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp index 2df7fadaa7..107335f9c5 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp @@ -376,7 +376,6 @@ HTTPUpdateResult ESP8266HTTPUpdate::handleUpdate(HTTPClient& http, const String& return HTTP_UPDATE_FAILED; } } - if(runUpdate(*tcp, len, http.header("x-MD5"), command)) { ret = HTTP_UPDATE_OK; DEBUG_HTTP_UPDATE("[httpUpdate] Update ok\n"); From fba638c1e526ad801c7dbd16eef5d26fa61eba4b Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 22 Aug 2018 12:29:24 +0200 Subject: [PATCH 16/31] Bugfix in getStream() and getStreamPtr() --- libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index d43118ea4f..21432e86fd 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -767,10 +767,9 @@ WiFiClient& HTTPClient::getStream(void) { if(connected()) { #ifdef HTTPCLIENT_1_1_COMPATIBLE - return _client ? *_client : *_tcpDeprecated; -#else - return *_client; + if(_tcpDeprecated) return *_tcpDeprecated; #endif + return *_client; } DEBUG_HTTPCLIENT("[HTTP-Client] getStream: not connected\n"); @@ -786,10 +785,9 @@ WiFiClient* HTTPClient::getStreamPtr(void) { if(connected()) { #ifdef HTTPCLIENT_1_1_COMPATIBLE - return _tcpDeprecated.get(); -#else - return _client; + if(_tcpDeprecated) return _tcpDeprecated.get(); #endif + return _client; } DEBUG_HTTPCLIENT("[HTTP-Client] getStreamPtr: not connected\n"); From 4fb623633aa4d893909db77cb3e5e7c71da0e844 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 22 Aug 2018 12:59:47 +0200 Subject: [PATCH 17/31] Updated examples httpUpdate.ino and httpUpdateSecure.ino --- .../ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino | 7 +++---- .../examples/httpUpdateSecure/httpUpdateSecure.ino | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino b/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino index b9fc58c588..146c7a5531 100644 --- a/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino +++ b/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino @@ -43,10 +43,9 @@ void loop() { if ((WiFiMulti.run() == WL_CONNECTED)) { WiFiClient client; -// t_httpUpdate_return ret = ESPhttpUpdate.update("http://server/file.bin"); - t_httpUpdate_return ret = ESPhttpUpdate.update("https://iot-hub.metriot.nl/devices/update/12345/", "IoT_Embedded_Software_018.ino.generic.bin"); - - //t_httpUpdate_return ret = ESPhttpUpdate.update("https://server/file.bin", "", "fingerprint"); + t_httpUpdate_return ret = ESPhttpUpdate.update("http://server/file.bin"); + // Or: +// t_httpUpdate_return ret = ESPhttpUpdate.update(client, "server", 80, "file.bin"); switch (ret) { case HTTP_UPDATE_FAILED: diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino b/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino index 70ca270080..48f7f680fc 100644 --- a/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino +++ b/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino @@ -93,8 +93,7 @@ void setup() { } WiFi.mode(WIFI_STA); -// WiFiMulti.addAP("SSID", "PASSWORD"); - WiFiMulti.addAP("H369A394602", "445396E996F9"); + WiFiMulti.addAP("SSID", "PASSWORD"); SPIFFS.begin(); @@ -121,8 +120,8 @@ void loop() { client.setCertStore(&certStore); // t_httpUpdate_return ret = ESPhttpUpdate.update(client, "https://server/file.bin"); -// t_httpUpdate_return ret = ESPhttpUpdate.update(client, "https://iot-hub.metriot.nl/devices/update/12345/IoT_Embedded_Software_018.ino.generic.bin"); - t_httpUpdate_return ret = ESPhttpUpdate.update(client, "iot-hub.metriot.nl", 443, "/devices/update/12345", "IoT_Embedded_Software_017.ino.generic.bin"); + // Or: +// t_httpUpdate_return ret = ESPhttpUpdate.update(client, "server", 443, "file.bin"); switch (ret) { From 7c19eb27ea07e503ec8a0e04a00c7e636c87b874 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 22 Aug 2018 13:37:01 +0200 Subject: [PATCH 18/31] Indentation to pass Travis and removed comment --- .../ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino | 4 ++-- .../examples/httpUpdateSecure/httpUpdateSecure.ino | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino b/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino index 146c7a5531..7c9780cf07 100644 --- a/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino +++ b/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino @@ -43,9 +43,9 @@ void loop() { if ((WiFiMulti.run() == WL_CONNECTED)) { WiFiClient client; - t_httpUpdate_return ret = ESPhttpUpdate.update("http://server/file.bin"); + t_httpUpdate_return ret = ESPhttpUpdate.update(client, "http://server/file.bin"); // Or: -// t_httpUpdate_return ret = ESPhttpUpdate.update(client, "server", 80, "file.bin"); + //t_httpUpdate_return ret = ESPhttpUpdate.update(client, "server", 80, "file.bin"); switch (ret) { case HTTP_UPDATE_FAILED: diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino b/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino index 48f7f680fc..836de09fce 100644 --- a/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino +++ b/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino @@ -119,9 +119,9 @@ void loop() { } client.setCertStore(&certStore); -// t_httpUpdate_return ret = ESPhttpUpdate.update(client, "https://server/file.bin"); + t_httpUpdate_return ret = ESPhttpUpdate.update(client, "https://server/file.bin"); // Or: -// t_httpUpdate_return ret = ESPhttpUpdate.update(client, "server", 443, "file.bin"); + //t_httpUpdate_return ret = ESPhttpUpdate.update(client, "server", 443, "file.bin"); switch (ret) { From af95fc0db7a065b93f188070a4590faf21998e4b Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Thu, 23 Aug 2018 09:31:09 +0200 Subject: [PATCH 19/31] Added #pragma's around calls to deprecated functions from deprecated functions to pass Travis test --- libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp index 107335f9c5..a4037b3f6d 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp @@ -48,7 +48,10 @@ HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& curr const String& httpsFingerprint, bool reboot) { rebootOnUpdate(reboot); +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" return update(url, currentVersion, httpsFingerprint); +#pragma GCC diagnostic pop } HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& currentVersion) @@ -137,9 +140,12 @@ HTTPUpdateResult ESP8266HTTPUpdate::update(const String& host, uint16_t port, co (void)https; rebootOnUpdate(reboot); if (httpsFingerprint.length() == 0) { +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" return update(host, port, uri, currentVersion); } else { return update(host, port, uri, currentVersion, httpsFingerprint); +#pragma GCC diagnostic pop } } From ae41ad74a05b0aa31a6d851fb2ba64bb283289e8 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Thu, 23 Aug 2018 10:02:53 +0200 Subject: [PATCH 20/31] Moving httpUpdateSPIFFS.ino to new API --- .../examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino b/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino index 2f75d6fff8..4b77c49c40 100644 --- a/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino +++ b/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino @@ -42,10 +42,13 @@ void loop() { if ((WiFiMulti.run() == WL_CONNECTED)) { USE_SERIAL.println("Update SPIFFS..."); - t_httpUpdate_return ret = ESPhttpUpdate.updateSpiffs("http://server/spiffs.bin"); + + WiFiClient client; + + t_httpUpdate_return ret = ESPhttpUpdate.updateSpiffs(client, "http://server/spiffs.bin"); if (ret == HTTP_UPDATE_OK) { USE_SERIAL.println("Update sketch..."); - ret = ESPhttpUpdate.update("http://server/file.bin"); + ret = ESPhttpUpdate.update(client, "http://server/file.bin"); switch (ret) { case HTTP_UPDATE_FAILED: From 1593266f0a7cdf66061783e0d7a93a9dd0927607 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Fri, 5 Oct 2018 08:07:20 +0200 Subject: [PATCH 21/31] Exit writeSteam() on received bytes in stead of timeout and add visual feedback while updating using LED on pin 2 --- cores/esp8266/Updater.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/Updater.cpp b/cores/esp8266/Updater.cpp index cd3e3f65b0..d057b5c4ae 100644 --- a/cores/esp8266/Updater.cpp +++ b/cores/esp8266/Updater.cpp @@ -360,18 +360,27 @@ size_t UpdaterClass::writeStream(Stream &data) { return 0; } + pinMode(2, OUTPUT); + while(remaining()) { - toRead = data.readBytes(_buffer + _bufferLen, (_bufferSize - _bufferLen)); + digitalWrite(2, LOW); // Switch LED on + size_t bytesToRead = _bufferSize - _bufferLen; + if(bytesToRead > remaining()) { + bytesToRead = remaining(); + } + toRead = data.readBytes(_buffer + _bufferLen, bytesToRead); if(toRead == 0) { //Timeout delay(100); - toRead = data.readBytes(_buffer + _bufferLen, (_bufferSize - _bufferLen)); + toRead = data.readBytes(_buffer + _bufferLen, bytesToRead); if(toRead == 0) { //Timeout + digitalWrite(2, HIGH); // Switch LED off _currentAddress = (_startAddress + _size); _setError(UPDATE_ERROR_STREAM); _reset(); return written; } } + digitalWrite(2, HIGH); // Switch LED off _bufferLen += toRead; if((_bufferLen == remaining() || _bufferLen == _bufferSize) && !_writeBuffer()) return written; From d1eab93d291a7ff95ef9f4d218de2448942292f8 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Fri, 5 Oct 2018 11:13:07 +0200 Subject: [PATCH 22/31] Removed unnecessary references to _tcpDeprecated and unnecessary #ifdefs --- .../src/ESP8266HTTPClient.cpp | 70 +++---------------- 1 file changed, 9 insertions(+), 61 deletions(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 3d6a85a0a0..049f54e915 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -122,14 +122,12 @@ HTTPClient::HTTPClient() */ HTTPClient::~HTTPClient() { +Serial.printf("Destructor client is %s\n", _client? "not null" : "null"); if(_client) { +Serial.println("Before stop"); _client->stop(); +Serial.println("After stop"); } -#ifdef HTTPCLIENT_1_1_COMPATIBLE - if(_tcpDeprecated) { - _tcpDeprecated->stop(); - } -#endif if(_currentHeaders) { delete[] _currentHeaders; } @@ -370,7 +368,6 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t htt * end * called after the payload is handled */ -#ifdef HTTPCLIENT_1_1_COMPATIBLE void HTTPClient::end(void) { disconnect(); @@ -393,14 +390,6 @@ void HTTPClient::disconnect() } } - if(_tcpDeprecated) { - if(_tcpDeprecated->available() > 0) { - DEBUG_HTTPCLIENT("[HTTP-Client][end] still data in buffer (%d), clean up.\n", _tcpDeprecated->available()); - while(_tcpDeprecated->available() > 0) { - _tcpDeprecated->read(); - } - } - } if(_reuse && _canReuse) { DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp keep open for reuse\n"); } else { @@ -409,38 +398,18 @@ void HTTPClient::disconnect() _client->stop(); _client = nullptr; } +#ifdef HTTPCLIENT_1_1_COMPATIBLE if(_tcpDeprecated) { - _tcpDeprecated->stop(); _transportTraits.reset(nullptr); _tcpDeprecated.reset(nullptr); } +#endif } } else { DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp is closed\n"); } } -#else -void HTTPClient::end(void) -{ - if(connected()) { - if(_client->available() > 0) { - DEBUG_HTTPCLIENT("[HTTP-Client][end] still data in buffer (%d), clean up.\n", _client->available()); - while(_client->available() > 0) { - _client->read(); - } - } - if(_reuse && _canReuse) { - DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp keep open for reuse\n"); - } else { - DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp stop\n"); - _client->stop(); - _client = nullptr; - } - } else { - DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp is closed\n"); - } -} -#endif + /** * connected * @return connected status @@ -450,11 +419,6 @@ bool HTTPClient::connected() if(_client) { return (_client->connected() || (_client->available() > 0)); } -#ifdef HTTPCLIENT_1_1_COMPATIBLE - if(_tcpDeprecated) { - return (_tcpDeprecated->connected() || (_tcpDeprecated->available() > 0)); - } -#endif return false; } @@ -777,9 +741,6 @@ int HTTPClient::getSize(void) WiFiClient& HTTPClient::getStream(void) { if(connected()) { -#ifdef HTTPCLIENT_1_1_COMPATIBLE - if(_tcpDeprecated) return *_tcpDeprecated; -#endif return *_client; } @@ -795,9 +756,6 @@ WiFiClient& HTTPClient::getStream(void) WiFiClient* HTTPClient::getStreamPtr(void) { if(connected()) { -#ifdef HTTPCLIENT_1_1_COMPATIBLE - if(_tcpDeprecated) return _tcpDeprecated.get(); -#endif return _client; } @@ -1044,7 +1002,6 @@ bool HTTPClient::hasHeader(const char* name) */ bool HTTPClient::connect(void) { - if(connected()) { DEBUG_HTTPCLIENT("[HTTP-Client] connect. already connected, try reuse!\n"); while(_client->available() > 0) { @@ -1053,11 +1010,7 @@ bool HTTPClient::connect(void) return true; } -#ifdef HTTPCLIENT_1_1_COMPATIBLE - if (!_client && !_transportTraits) { -#else if(!_client) { -#endif DEBUG_HTTPCLIENT("[HTTP-Client] connect: HTTPClient::begin was not called or returned error\n"); return false; } @@ -1068,6 +1021,7 @@ bool HTTPClient::connect(void) _client = _tcpDeprecated.get(); } #endif + _client->setTimeout(_tcpTimeout); if(!_client->connect(_host.c_str(), _port)) { @@ -1078,7 +1032,7 @@ bool HTTPClient::connect(void) DEBUG_HTTPCLIENT("[HTTP-Client] connected to %s:%u\n", _host.c_str(), _port); #ifdef HTTPCLIENT_1_1_COMPATIBLE - if (_tcpDeprecated && _transportTraits && !_transportTraits->verify(*_tcpDeprecated, _host.c_str())) { + if (_tcpDeprecated && !_transportTraits->verify(*_tcpDeprecated, _host.c_str())) { DEBUG_HTTPCLIENT("[HTTP-Client] transport level verify failed\n"); _client->stop(); return false; @@ -1087,13 +1041,7 @@ bool HTTPClient::connect(void) #ifdef ESP8266 -#ifdef HTTPCLIENT_1_1_COMPATIBLE - if(_tcpDeprecated) - _tcpDeprecated->setNoDelay(true); -#else - if(_client) - _client->setNoDelay(true); -#endif + _client->setNoDelay(true); #endif return connected(); } From fc3517ce37a76b85d205fb38cb5a96c62f66af65 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Fri, 5 Oct 2018 11:44:35 +0200 Subject: [PATCH 23/31] Make LED status optional --- cores/esp8266/Updater.cpp | 19 ++++++++++++++----- cores/esp8266/Updater.h | 6 +++++- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/cores/esp8266/Updater.cpp b/cores/esp8266/Updater.cpp index d057b5c4ae..bcd30c28af 100644 --- a/cores/esp8266/Updater.cpp +++ b/cores/esp8266/Updater.cpp @@ -35,9 +35,13 @@ void UpdaterClass::_reset() { _currentAddress = 0; _size = 0; _command = U_FLASH; + + if(_ledPin != -1) { + digitalWrite(_ledPin, _ledStateRestore); + } } -bool UpdaterClass::begin(size_t size, int command) { +bool UpdaterClass::begin(size_t size, int command, int ledPin, uint8_t ledOn) { if(_size > 0){ #ifdef DEBUG_UPDATER DEBUG_UPDATER.println(F("[begin] already running")); @@ -45,6 +49,12 @@ bool UpdaterClass::begin(size_t size, int command) { return false; } + _ledPin = ledPin; + _ledOn = ledOn; + if(_ledPin != -1) { + _ledStateRestore = digitalRead(_ledPin); + } + /* Check boot mode; if boot mode is 1 (UART download mode), we will not be able to reset into normal mode once update is done. Fail early to avoid frustration. @@ -360,10 +370,10 @@ size_t UpdaterClass::writeStream(Stream &data) { return 0; } - pinMode(2, OUTPUT); + pinMode(LED_BUILTIN, OUTPUT); while(remaining()) { - digitalWrite(2, LOW); // Switch LED on + digitalWrite(LED_BUILTIN, _ledOn); // Switch LED on size_t bytesToRead = _bufferSize - _bufferLen; if(bytesToRead > remaining()) { bytesToRead = remaining(); @@ -373,14 +383,13 @@ size_t UpdaterClass::writeStream(Stream &data) { delay(100); toRead = data.readBytes(_buffer + _bufferLen, bytesToRead); if(toRead == 0) { //Timeout - digitalWrite(2, HIGH); // Switch LED off _currentAddress = (_startAddress + _size); _setError(UPDATE_ERROR_STREAM); _reset(); return written; } } - digitalWrite(2, HIGH); // Switch LED off + digitalWrite(LED_BUILTIN, _ledOn == HIGH ? LOW : HIGH); // Switch LED off _bufferLen += toRead; if((_bufferLen == remaining() || _bufferLen == _bufferSize) && !_writeBuffer()) return written; diff --git a/cores/esp8266/Updater.h b/cores/esp8266/Updater.h index 2509988ca8..7fcbb56a5c 100644 --- a/cores/esp8266/Updater.h +++ b/cores/esp8266/Updater.h @@ -35,7 +35,7 @@ class UpdaterClass { Call this to check the space needed for the update Will return false if there is not enough space */ - bool begin(size_t size, int command = U_FLASH); + bool begin(size_t size, int command = U_FLASH, int ledPin = -1, uint8_t ledOn = LOW); /* Run Updater from asynchronous callbacs @@ -162,6 +162,10 @@ class UpdaterClass { String _target_md5; MD5Builder _md5; + + int _ledPin; + uint8_t _ledOn; + int _ledStateRestore; }; extern UpdaterClass Update; From 284e3c607bdafe39c7557eaff3998ba9f72258eb Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Fri, 5 Oct 2018 12:10:28 +0200 Subject: [PATCH 24/31] Make LED feedback optional --- cores/esp8266/Updater.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/cores/esp8266/Updater.cpp b/cores/esp8266/Updater.cpp index bcd30c28af..fb539cc3da 100644 --- a/cores/esp8266/Updater.cpp +++ b/cores/esp8266/Updater.cpp @@ -370,10 +370,14 @@ size_t UpdaterClass::writeStream(Stream &data) { return 0; } - pinMode(LED_BUILTIN, OUTPUT); + if(_ledPin != -1) { + pinMode(_ledPin, OUTPUT); + } while(remaining()) { - digitalWrite(LED_BUILTIN, _ledOn); // Switch LED on + if(_ledPin != -1) { + digitalWrite(LED_BUILTIN, _ledOn); // Switch LED on + } size_t bytesToRead = _bufferSize - _bufferLen; if(bytesToRead > remaining()) { bytesToRead = remaining(); @@ -389,7 +393,9 @@ size_t UpdaterClass::writeStream(Stream &data) { return written; } } - digitalWrite(LED_BUILTIN, _ledOn == HIGH ? LOW : HIGH); // Switch LED off + if(_ledPin != -1) { + digitalWrite(LED_BUILTIN, _ledOn == HIGH ? LOW : HIGH); // Switch LED off + } _bufferLen += toRead; if((_bufferLen == remaining() || _bufferLen == _bufferSize) && !_writeBuffer()) return written; From bc897d5ce811992535ba05259f18374897811bcb Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Fri, 5 Oct 2018 12:30:26 +0200 Subject: [PATCH 25/31] Give optional feedback (provided by additional feature on Update class) on update progress using a flashing LED --- libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp | 6 +++--- libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp index a4037b3f6d..b23ad4631c 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp @@ -30,12 +30,12 @@ extern "C" uint32_t _SPIFFS_start; extern "C" uint32_t _SPIFFS_end; ESP8266HTTPUpdate::ESP8266HTTPUpdate(void) - : _httpClientTimeout(8000) + : _httpClientTimeout(8000), _ledPin(-1) { } ESP8266HTTPUpdate::ESP8266HTTPUpdate(int httpClientTimeout) - : _httpClientTimeout(httpClientTimeout) + : _httpClientTimeout(httpClientTimeout), _ledPin(-1) { } @@ -438,7 +438,7 @@ bool ESP8266HTTPUpdate::runUpdate(Stream& in, uint32_t size, String md5, int com StreamString error; - if(!Update.begin(size, command)) { + if(!Update.begin(size, command, _ledPin, _ledOn)) { _lastError = Update.getError(); Update.printError(error); error.trim(); // remove line ending diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h index ec7359ba57..2e5b140f7f 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h @@ -74,6 +74,12 @@ class ESP8266HTTPUpdate _rebootOnUpdate = reboot; } + void setLedPin(int ledPin = -1, uint8_t ledOn = HIGH) + { + _ledPin = ledPin; + _ledOn = ledOn; + } + #ifdef HTTPUPDATE_1_2_COMPATIBLE // This function is deprecated, use rebootOnUpdate and the next one instead t_httpUpdate_return update(const String& url, const String& currentVersion, @@ -123,6 +129,9 @@ class ESP8266HTTPUpdate bool _rebootOnUpdate = true; private: int _httpClientTimeout; + + int _ledPin; + uint8_t _ledOn; }; #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_HTTPUPDATE) From 8eeaa012e4356a5c78ff6161059e1a049877ffe0 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Fri, 5 Oct 2018 13:27:51 +0200 Subject: [PATCH 26/31] Removing extraneous debug comment --- libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index f465391a22..df3be9ec4f 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -122,11 +122,8 @@ HTTPClient::HTTPClient() */ HTTPClient::~HTTPClient() { -Serial.printf("Destructor client is %s\n", _client? "not null" : "null"); if(_client) { -Serial.println("Before stop"); _client->stop(); -Serial.println("After stop"); } if(_currentHeaders) { delete[] _currentHeaders; From 2e7c7a99370479ea2045023c8d3f2f924dcb951d Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Fri, 5 Oct 2018 17:02:15 +0200 Subject: [PATCH 27/31] Updated the examples with setLedPin() including an explanation of the usage --- .../ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino | 9 +++++++++ .../examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino | 8 ++++++++ .../examples/httpUpdateSecure/httpUpdateSecure.ino | 8 ++++++++ 3 files changed, 25 insertions(+) diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino b/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino index 7c9780cf07..03285d6a5e 100644 --- a/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino +++ b/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino @@ -43,6 +43,15 @@ void loop() { if ((WiFiMulti.run() == WL_CONNECTED)) { WiFiClient client; + + // The line below is optional. It can be used to blink the LED on the board during flashing + // The LED will be on during download of one buffer of data from the network. The LED will + // be off during writing that buffer to flash + // On a good connection the LED should flash regularly. On a bad connection the LED will be + // on much longer than it will be off. Other pins than LED_BUILTIN may be used. The second + // value is used to put the LED on. If the LED is on with HIGH, that value should be passed + ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW); + t_httpUpdate_return ret = ESPhttpUpdate.update(client, "http://server/file.bin"); // Or: //t_httpUpdate_return ret = ESPhttpUpdate.update(client, "server", 80, "file.bin"); diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino b/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino index 4b77c49c40..fef85255c0 100644 --- a/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino +++ b/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino @@ -45,6 +45,14 @@ void loop() { WiFiClient client; + // The line below is optional. It can be used to blink the LED on the board during flashing + // The LED will be on during download of one buffer of data from the network. The LED will + // be off during writing that buffer to flash + // On a good connection the LED should flash regularly. On a bad connection the LED will be + // on much longer than it will be off. Other pins than LED_BUILTIN may be used. The second + // value is used to put the LED on. If the LED is on with HIGH, that value should be passed + ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW); + t_httpUpdate_return ret = ESPhttpUpdate.updateSpiffs(client, "http://server/spiffs.bin"); if (ret == HTTP_UPDATE_OK) { USE_SERIAL.println("Update sketch..."); diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino b/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino index 836de09fce..83ade78469 100644 --- a/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino +++ b/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino @@ -119,6 +119,14 @@ void loop() { } client.setCertStore(&certStore); + // The line below is optional. It can be used to blink the LED on the board during flashing + // The LED will be on during download of one buffer of data from the network. The LED will + // be off during writing that buffer to flash + // On a good connection the LED should flash regularly. On a bad connection the LED will be + // on much longer than it will be off. Other pins than LED_BUILTIN may be used. The second + // value is used to put the LED on. If the LED is on with HIGH, that value should be passed + ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW); + t_httpUpdate_return ret = ESPhttpUpdate.update(client, "https://server/file.bin"); // Or: //t_httpUpdate_return ret = ESPhttpUpdate.update(client, "server", 443, "file.bin"); From b3571bc1a68b98b3e6a4d3fd96029cac424c4b59 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 5 Oct 2018 10:36:47 -0700 Subject: [PATCH 28/31] Remove trailing space causing CI to fail. --- libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino b/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino index 03285d6a5e..3763bd1f79 100644 --- a/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino +++ b/libraries/ESP8266httpUpdate/examples/httpUpdate/httpUpdate.ino @@ -49,7 +49,7 @@ void loop() { // be off during writing that buffer to flash // On a good connection the LED should flash regularly. On a bad connection the LED will be // on much longer than it will be off. Other pins than LED_BUILTIN may be used. The second - // value is used to put the LED on. If the LED is on with HIGH, that value should be passed + // value is used to put the LED on. If the LED is on with HIGH, that value should be passed ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW); t_httpUpdate_return ret = ESPhttpUpdate.update(client, "http://server/file.bin"); From 5126cfa85c01760f880a2a4eda336db4ad064b80 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 5 Oct 2018 10:37:19 -0700 Subject: [PATCH 29/31] Remove trailing space causing CI to fail. --- .../examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino b/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino index fef85255c0..9253e8379f 100644 --- a/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino +++ b/libraries/ESP8266httpUpdate/examples/httpUpdateSPIFFS/httpUpdateSPIFFS.ino @@ -50,7 +50,7 @@ void loop() { // be off during writing that buffer to flash // On a good connection the LED should flash regularly. On a bad connection the LED will be // on much longer than it will be off. Other pins than LED_BUILTIN may be used. The second - // value is used to put the LED on. If the LED is on with HIGH, that value should be passed + // value is used to put the LED on. If the LED is on with HIGH, that value should be passed ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW); t_httpUpdate_return ret = ESPhttpUpdate.updateSpiffs(client, "http://server/spiffs.bin"); From 7ce28bd9e08cce4aa7afeb124bcb13f1b4ce407f Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 5 Oct 2018 10:37:46 -0700 Subject: [PATCH 30/31] Remove trailing space causing CI to fail. --- .../examples/httpUpdateSecure/httpUpdateSecure.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino b/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino index 83ade78469..546e20f886 100644 --- a/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino +++ b/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino @@ -124,7 +124,7 @@ void loop() { // be off during writing that buffer to flash // On a good connection the LED should flash regularly. On a bad connection the LED will be // on much longer than it will be off. Other pins than LED_BUILTIN may be used. The second - // value is used to put the LED on. If the LED is on with HIGH, that value should be passed + // value is used to put the LED on. If the LED is on with HIGH, that value should be passed ESPhttpUpdate.setLedPin(LED_BUILTIN, LOW); t_httpUpdate_return ret = ESPhttpUpdate.update(client, "https://server/file.bin"); From 5a20da5a6466a1c31622fe1a0d377060a28a95f6 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Fri, 5 Oct 2018 17:34:53 -0700 Subject: [PATCH 31/31] Fix ::connect() to work for legacy API The `_client==NULL` check was performed before any `_client` could be set by the shim code. Move the check past where the shim code actually sets the variable. Fixes any unmodified legacy code and the old samples. --- libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index df3be9ec4f..034a815b97 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -1007,11 +1007,6 @@ bool HTTPClient::connect(void) return true; } - if(!_client) { - DEBUG_HTTPCLIENT("[HTTP-Client] connect: HTTPClient::begin was not called or returned error\n"); - return false; - } - #ifdef HTTPCLIENT_1_1_COMPATIBLE if(!_client) { _tcpDeprecated = _transportTraits->create(); @@ -1019,6 +1014,11 @@ bool HTTPClient::connect(void) } #endif + if(!_client) { + DEBUG_HTTPCLIENT("[HTTP-Client] connect: HTTPClient::begin was not called or returned error\n"); + return false; + } + _client->setTimeout(_tcpTimeout); if(!_client->connect(_host.c_str(), _port)) {