From 8c3edfe2099c1535610682d591140506fca0ff8d Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Mon, 18 Jun 2018 17:49:00 +0200 Subject: [PATCH 01/13] Make HTTPClient suitabe for every Client --- .../src/ESP8266HTTPClient.cpp | 116 ++++++++++++++++-- .../ESP8266HTTPClient/src/ESP8266HTTPClient.h | 38 ++++-- 2 files changed, 135 insertions(+), 19 deletions(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 28a3503484..215f9ada42 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_DEPRECIATED_API #include #include +#endif + #include #include -#include "ESP8266HTTPClient.h" - +#ifdef KEEP_DEPRECIATED_API class TransportTraits { public: @@ -100,6 +104,7 @@ class BearSSLTraits : public TransportTraits protected: uint8_t _fingerprint[20]; }; +#endif /** * constructor @@ -129,9 +134,70 @@ void HTTPClient::clear() } +/** + * parsing the url for all needed parameters + * @param client Client& + * @param url String + * @param https bool + * @return success bool + */ +bool HTTPClient::begin(Client &client, String url) { +#ifdef KEEP_DEPRECIATED_API + _tcpDepreciated.reset(nullptr); + _transportTraits.reset(nullptr); +#endif + _tcp = &client; + + // check for : (http: or https:) + int index = url.indexOf(':'); + if(index < 0) { + DEBUG_HTTPCLIENT("[HTTP-Client][begin] failed to parse protocol\n"); + return false; + } + + String protocol = url.substring(0, index); + if(protocol != "http" && protocol != "https") { + DEBUG_HTTPCLIENT("[HTTP-Client][begin] unknown protocol '%s'\n", protocol.c_str()); + return false; + } + + _port = (protocol == "https" ? 443 : 80); + return beginInternal(url, protocol.c_str()); +} + + +/** + * directly supply all needed parameters + * @param client Client& + * @param host String + * @param port uint16_t + * @param uri String + * @param https bool + * @return success bool + */ +bool HTTPClient::begin(Client &client, String host, uint16_t port, String uri, bool https) +{ +#ifdef KEEP_DEPRECIATED_API + _tcpDepreciated.reset(nullptr); + _transportTraits.reset(nullptr); +#endif + _tcp = &client; + + clear(); + _host = host; + _port = port; + _uri = uri; + _protocol = (https ? "https" : "http"); + return true; +} + + +#ifdef KEEP_DEPRECIATED_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_DEPRECIATED_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 @@ -624,6 +702,7 @@ int HTTPClient::getSize(void) return _size; } +#ifdef KEEP_DEPRECIATED_API /** * returns the stream of the tcp connection * @return WiFiClient @@ -631,7 +710,7 @@ int HTTPClient::getSize(void) WiFiClient& HTTPClient::getStream(void) { if(connected()) { - return *_tcp; + return *_tcpDepreciated; } DEBUG_HTTPCLIENT("[HTTP-Client] getStream: not connected\n"); @@ -646,12 +725,13 @@ WiFiClient& HTTPClient::getStream(void) WiFiClient* HTTPClient::getStreamPtr(void) { if(connected()) { - return _tcp.get(); + return (WiFiClient*)_tcpDepreciated.get(); } DEBUG_HTTPCLIENT("[HTTP-Client] getStreamPtr: not connected\n"); return nullptr; } +#endif /** * write all message body / payload to Stream @@ -897,12 +977,21 @@ bool HTTPClient::connect(void) return true; } - if (!_transportTraits) { +#ifdef KEEP_DEPRECIATED_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_DEPRECIATED_API + if(!_tcp) { + _tcpDepreciated = _transportTraits->create(); + _tcp = _tcpDepreciated.get(); + } +#endif _tcp->setTimeout(_tcpTimeout); if(!_tcp->connect(_host.c_str(), _port)) { @@ -912,15 +1001,22 @@ bool HTTPClient::connect(void) DEBUG_HTTPCLIENT("[HTTP-Client] connected to %s:%u\n", _host.c_str(), _port); - if (!_transportTraits->verify(*_tcp, _host.c_str())) { +#ifdef KEEP_DEPRECIATED_API + if (_tcpDepreciated && _transportTraits && !_transportTraits->verify(*_tcpDepreciated, _host.c_str())) { DEBUG_HTTPCLIENT("[HTTP-Client] transport level verify failed\n"); _tcp->stop(); return false; } +#endif #ifdef ESP8266 - _tcp->setNoDelay(true); +#ifdef KEEP_DEPRECIATED_API + if(_tcpDepreciated) + _tcpDepreciated->setNoDelay(true); +#else +// Client has no setNoDelay(), is this important??????????????? +#endif #endif return connected(); } diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h index 72b42853f7..03cd53db03 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h @@ -20,14 +20,22 @@ * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * + * Modified by Jeroen Döll, June 2018 */ #ifndef ESP8266HTTPClient_H_ #define ESP8266HTTPClient_H_ +#define KEEP_DEPRECIATED_API + #include #include + +#ifdef KEEP_DEPRECIATED_API #include +#else +#include +#endif #ifdef DEBUG_ESP_HTTP_CLIENT #ifdef DEBUG_ESP_PORT @@ -124,8 +132,10 @@ typedef enum { HTTPC_TE_CHUNKED } transferEncoding_t; +#ifdef KEEP_DEPRECIATED_API class TransportTraits; typedef std::unique_ptr TransportTraitsPtr; +#endif class HTTPClient { @@ -133,17 +143,22 @@ class HTTPClient HTTPClient(); ~HTTPClient(); + bool begin(Client &client, String url); + bool begin(Client &client, String host, uint16_t port, String uri = "/", bool https = false); + +#ifdef KEEP_DEPRECIATED_API // 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 void end(void); @@ -182,8 +197,10 @@ class HTTPClient int getSize(void); - WiFiClient& getStream(void); - WiFiClient* getStreamPtr(void); +#ifdef KEEP_DEPRECIATED_API + WiFiClient& getStream(void) __attribute__ ((deprecated)); + WiFiClient* getStreamPtr(void) __attribute__ ((deprecated)); +#endif int writeToStream(Stream* stream); String getString(void); @@ -204,8 +221,11 @@ class HTTPClient int writeToStreamDataBlock(Stream * stream, int len); +#ifdef KEEP_DEPRECIATED_API TransportTraitsPtr _transportTraits; - std::unique_ptr _tcp; + std::unique_ptr _tcpDepreciated; +#endif + Client* _tcp; /// request handling String _host; From 1bf21cdba97876adfd0315c262736b9ecd40ceb9 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Mon, 18 Jun 2018 23:21:17 +0200 Subject: [PATCH 02/13] Removed deprecated marking of the current api --- .../src/ESP8266HTTPClient.cpp | 22 ++++++++--------- .../ESP8266HTTPClient/src/ESP8266HTTPClient.h | 24 +++++++++---------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 215f9ada42..50f53adff0 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -25,7 +25,7 @@ #include "ESP8266HTTPClient.h" -#ifdef KEEP_DEPRECIATED_API +#ifdef KEEP_PRESENT_API #include #include #endif @@ -33,7 +33,7 @@ #include #include -#ifdef KEEP_DEPRECIATED_API +#ifdef KEEP_PRESENT_API class TransportTraits { public: @@ -142,7 +142,7 @@ void HTTPClient::clear() * @return success bool */ bool HTTPClient::begin(Client &client, String url) { -#ifdef KEEP_DEPRECIATED_API +#ifdef KEEP_PRESENT_API _tcpDepreciated.reset(nullptr); _transportTraits.reset(nullptr); #endif @@ -177,7 +177,7 @@ bool HTTPClient::begin(Client &client, String url) { */ bool HTTPClient::begin(Client &client, String host, uint16_t port, String uri, bool https) { -#ifdef KEEP_DEPRECIATED_API +#ifdef KEEP_PRESENT_API _tcpDepreciated.reset(nullptr); _transportTraits.reset(nullptr); #endif @@ -192,7 +192,7 @@ bool HTTPClient::begin(Client &client, String host, uint16_t port, String uri, b } -#ifdef KEEP_DEPRECIATED_API +#ifdef KEEP_PRESENT_API bool HTTPClient::begin(String url, String httpsFingerprint) { _tcp = nullptr; @@ -295,7 +295,7 @@ bool HTTPClient::beginInternal(String url, const char* expectedProtocol) return true; } -#ifdef KEEP_DEPRECIATED_API +#ifdef KEEP_PRESENT_API bool HTTPClient::begin(String host, uint16_t port, String uri) { _tcp = nullptr; @@ -702,7 +702,7 @@ int HTTPClient::getSize(void) return _size; } -#ifdef KEEP_DEPRECIATED_API +#ifdef KEEP_PRESENT_API /** * returns the stream of the tcp connection * @return WiFiClient @@ -977,7 +977,7 @@ bool HTTPClient::connect(void) return true; } -#ifdef KEEP_DEPRECIATED_API +#ifdef KEEP_PRESENT_API if (!_tcp && !_transportTraits) { #else if(!_tcp) { @@ -986,7 +986,7 @@ bool HTTPClient::connect(void) return false; } -#ifdef KEEP_DEPRECIATED_API +#ifdef KEEP_PRESENT_API if(!_tcp) { _tcpDepreciated = _transportTraits->create(); _tcp = _tcpDepreciated.get(); @@ -1001,7 +1001,7 @@ bool HTTPClient::connect(void) DEBUG_HTTPCLIENT("[HTTP-Client] connected to %s:%u\n", _host.c_str(), _port); -#ifdef KEEP_DEPRECIATED_API +#ifdef KEEP_PRESENT_API if (_tcpDepreciated && _transportTraits && !_transportTraits->verify(*_tcpDepreciated, _host.c_str())) { DEBUG_HTTPCLIENT("[HTTP-Client] transport level verify failed\n"); _tcp->stop(); @@ -1011,7 +1011,7 @@ bool HTTPClient::connect(void) #ifdef ESP8266 -#ifdef KEEP_DEPRECIATED_API +#ifdef KEEP_PRESENT_API if(_tcpDepreciated) _tcpDepreciated->setNoDelay(true); #else diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h index 03cd53db03..37b7c6fc3b 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h @@ -26,12 +26,12 @@ #ifndef ESP8266HTTPClient_H_ #define ESP8266HTTPClient_H_ -#define KEEP_DEPRECIATED_API +#define KEEP_PRESENT_API #include #include -#ifdef KEEP_DEPRECIATED_API +#ifdef KEEP_PRESENT_API #include #else #include @@ -132,7 +132,7 @@ typedef enum { HTTPC_TE_CHUNKED } transferEncoding_t; -#ifdef KEEP_DEPRECIATED_API +#ifdef KEEP_PRESENT_API class TransportTraits; typedef std::unique_ptr TransportTraitsPtr; #endif @@ -146,16 +146,16 @@ class HTTPClient bool begin(Client &client, String url); bool begin(Client &client, String host, uint16_t port, String uri = "/", bool https = false); -#ifdef KEEP_DEPRECIATED_API +#ifdef KEEP_PRESENT_API // Plain HTTP connection, unencrypted - bool begin(String url) __attribute__ ((deprecated)); - bool begin(String host, uint16_t port, String uri = "/") __attribute__ ((deprecated)); + bool begin(String url); + bool begin(String host, uint16_t port, String uri = "/"); // Use axTLS for secure HTTPS connection - bool begin(String url, String httpsFingerprint) __attribute__ ((deprecated)); - bool begin(String host, uint16_t port, String uri, String httpsFingerprint) __attribute__ ((deprecated)); + bool begin(String url, String httpsFingerprint); + bool begin(String host, uint16_t port, String uri, String httpsFingerprint); // Use BearSSL for secure HTTPS connection - 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)); + bool begin(String url, const uint8_t httpsFingerprint[20]); + 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 @@ -197,7 +197,7 @@ class HTTPClient int getSize(void); -#ifdef KEEP_DEPRECIATED_API +#ifdef KEEP_PRESENT_API WiFiClient& getStream(void) __attribute__ ((deprecated)); WiFiClient* getStreamPtr(void) __attribute__ ((deprecated)); #endif @@ -221,7 +221,7 @@ class HTTPClient int writeToStreamDataBlock(Stream * stream, int len); -#ifdef KEEP_DEPRECIATED_API +#ifdef KEEP_PRESENT_API TransportTraitsPtr _transportTraits; std::unique_ptr _tcpDepreciated; #endif From 7535a401ed3ebae12696b7e0601435a8450f7f28 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Mon, 18 Jun 2018 23:55:29 +0200 Subject: [PATCH 03/13] Removed deprecated marking of the current api --- libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h index 37b7c6fc3b..9ff448a86f 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h @@ -198,8 +198,8 @@ class HTTPClient int getSize(void); #ifdef KEEP_PRESENT_API - WiFiClient& getStream(void) __attribute__ ((deprecated)); - WiFiClient* getStreamPtr(void) __attribute__ ((deprecated)); + WiFiClient& getStream(void); + WiFiClient* getStreamPtr(void); #endif int writeToStream(Stream* stream); String getString(void); From 0a644f28ce736b2e510472f0702b127633518f54 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 20 Jun 2018 13:39:51 +0200 Subject: [PATCH 04/13] Updated examples to work with new API and added a https example --- .../examples/Authorization/Authorization.ino | 15 ++- .../BasicHttpClient/BasicHttpClient.ino | 45 ++++--- .../DigestAuthorization.ino | 10 +- .../ReuseConnection/ReuseConnection.ino | 9 +- .../StreamHttpClient/StreamHttpClient.ino | 9 +- .../StreamHttpsClient/StreamHttpsClient.ino | 116 ++++++++++++++++++ .../src/ESP8266HTTPClient.cpp | 18 +-- .../ESP8266HTTPClient/src/ESP8266HTTPClient.h | 2 +- 8 files changed, 176 insertions(+), 48 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..83d0294735 100644 --- a/libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino +++ b/libraries/ESP8266HTTPClient/examples/Authorization/Authorization.ino @@ -12,6 +12,8 @@ #include +#include + #define USE_SERIAL Serial ESP8266WiFiMulti WiFiMulti; @@ -40,22 +42,24 @@ void loop() { // wait for WiFi connection if ((WiFiMulti.run() == WL_CONNECTED)) { + WiFiClient client; + HTTPClient http; USE_SERIAL.print("[HTTP] begin...\n"); // configure traged server and url - http.begin("http://user:password@192.168.1.12/test.html"); + http.begin((Client&)client, "http://guest:guest@jigsaw.w3.org/HTTP/Basic/"); /* // or - http.begin("http://192.168.1.12/test.html"); - http.setAuthorization("user", "password"); + http.begin("http://jigsaw.w3.org/HTTP/Basic/"); + http.setAuthorization("guest", "guest"); // or - http.begin("http://192.168.1.12/test.html"); - http.setAuthorization("dXNlcjpwYXN3b3Jk"); + http.begin("http://jigsaw.w3.org/HTTP/Basic/"); + http.setAuthorization("Z3Vlc3Q6Z3Vlc3Q="); */ @@ -82,4 +86,3 @@ void loop() { delay(10000); } - diff --git a/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino b/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino index 766e85b2a4..38f412c440 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 - - 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); - - // file found at server - if (httpCode == HTTP_CODE_OK) { - String payload = http.getString(); - USE_SERIAL.println(payload); + if(http.begin((Client &)client, "http://tls.mbed.org/")) { // HTTP + + + USE_SERIAL.print("[HTTP] GET...\n"); + // start connection and send HTTP header + int httpCode = http.GET(); + + // 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 || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { + String payload = http.getString(); + USE_SERIAL.println(payload); + } + } else { + USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); } + + http.end(); } else { - USE_SERIAL.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); + USE_SERIAL.printf("Unable to connect\n"); } - - http.end(); } delay(10000); } - diff --git a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino index bbf07c1b89..8924061f69 100644 --- a/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino +++ b/libraries/ESP8266HTTPClient/examples/DigestAuthorization/DigestAuthorization.ino @@ -11,8 +11,8 @@ #include -const char* ssid = "........"; -const char* ssidPassword = "........"; +const char* ssid = "SSID"; +const char* ssidPassword = "PASSWORD"; const char *username = "admin"; const char *password = "admin"; @@ -76,7 +76,7 @@ String getDigestAuth(String& authReq, const String& username, const String& pass } void setup() { - Serial.begin(9600); + Serial.begin(115200); WiFi.mode(WIFI_STA); WiFi.begin(ssid, ssidPassword); @@ -95,10 +95,12 @@ void setup() { void loop() { HTTPClient http; + WiFiClient client; + Serial.print("[HTTP] begin...\n"); // configure traged server and url - http.begin(String(server) + String(uri)); + http.begin((Client&) client, String(server) + String(uri)); const char *keys[] = {"WWW-Authenticate"}; diff --git a/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino b/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino index 420549d8e7..259981d37c 100644 --- a/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino +++ b/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino @@ -45,8 +45,10 @@ void loop() { // wait for WiFi connection if ((WiFiMulti.run() == WL_CONNECTED)) { - http.begin("http://192.168.1.12/test.html"); - //http.begin("192.168.1.12", 80, "/test.html"); + WiFiClient client; + + http.begin((Client&) client, "http://192.168.1.12/test.html"); + //http.begin((Client&) client, "192.168.1.12", 80, "/test.html"); int httpCode = http.GET(); if (httpCode > 0) { @@ -65,6 +67,3 @@ void loop() { delay(1000); } - - - diff --git a/libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino b/libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino index 57db978882..7ad246cebb 100644 --- a/libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino +++ b/libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino @@ -42,11 +42,13 @@ void loop() { HTTPClient http; + WiFiClient client; + USE_SERIAL.print("[HTTP] begin...\n"); // configure server and url - http.begin("http://192.168.1.12/test.html"); - //http.begin("192.168.1.12", 80, "/test.html"); + http.begin((Client&) client, "http://jigsaw.w3.org/HTTP/connection.html"); + //http.begin("jigsaw.w3.org", 80, "/HTTP/connection.html"); USE_SERIAL.print("[HTTP] GET...\n"); // start connection and send HTTP header @@ -65,7 +67,7 @@ void loop() { uint8_t buff[128] = { 0 }; // get tcp stream - WiFiClient * stream = http.getStreamPtr(); + WiFiClient * stream = &client; // read all data from server while (http.connected() && (len > 0 || len == -1)) { @@ -99,4 +101,3 @@ void loop() { delay(10000); } - diff --git a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino new file mode 100644 index 0000000000..25b59fef1c --- /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&) client, "https://tls.mbed.org/")) { + //if(http.begin("jigsaw.w3.org", 443, "/HTTP/connection.html", true)) { + + 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 50f53adff0..a927c3b6e9 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -143,7 +143,7 @@ void HTTPClient::clear() */ bool HTTPClient::begin(Client &client, String url) { #ifdef KEEP_PRESENT_API - _tcpDepreciated.reset(nullptr); + _tcpDeprecated.reset(nullptr); _transportTraits.reset(nullptr); #endif _tcp = &client; @@ -178,7 +178,7 @@ bool HTTPClient::begin(Client &client, String url) { bool HTTPClient::begin(Client &client, String host, uint16_t port, String uri, bool https) { #ifdef KEEP_PRESENT_API - _tcpDepreciated.reset(nullptr); + _tcpDeprecated.reset(nullptr); _transportTraits.reset(nullptr); #endif _tcp = &client; @@ -710,7 +710,7 @@ int HTTPClient::getSize(void) WiFiClient& HTTPClient::getStream(void) { if(connected()) { - return *_tcpDepreciated; + return *_tcpDeprecated; } DEBUG_HTTPCLIENT("[HTTP-Client] getStream: not connected\n"); @@ -725,7 +725,7 @@ WiFiClient& HTTPClient::getStream(void) WiFiClient* HTTPClient::getStreamPtr(void) { if(connected()) { - return (WiFiClient*)_tcpDepreciated.get(); + return (WiFiClient*)_tcpDeprecated.get(); } DEBUG_HTTPCLIENT("[HTTP-Client] getStreamPtr: not connected\n"); @@ -988,8 +988,8 @@ bool HTTPClient::connect(void) #ifdef KEEP_PRESENT_API if(!_tcp) { - _tcpDepreciated = _transportTraits->create(); - _tcp = _tcpDepreciated.get(); + _tcpDeprecated = _transportTraits->create(); + _tcp = _tcpDeprecated.get(); } #endif _tcp->setTimeout(_tcpTimeout); @@ -1002,7 +1002,7 @@ bool HTTPClient::connect(void) DEBUG_HTTPCLIENT("[HTTP-Client] connected to %s:%u\n", _host.c_str(), _port); #ifdef KEEP_PRESENT_API - if (_tcpDepreciated && _transportTraits && !_transportTraits->verify(*_tcpDepreciated, _host.c_str())) { + if (_tcpDeprecated && _transportTraits && !_transportTraits->verify(*_tcpDeprecated, _host.c_str())) { DEBUG_HTTPCLIENT("[HTTP-Client] transport level verify failed\n"); _tcp->stop(); return false; @@ -1012,8 +1012,8 @@ bool HTTPClient::connect(void) #ifdef ESP8266 #ifdef KEEP_PRESENT_API - if(_tcpDepreciated) - _tcpDepreciated->setNoDelay(true); + if(_tcpDeprecated) + _tcpDeprecated->setNoDelay(true); #else // Client has no setNoDelay(), is this important??????????????? #endif diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h index 9ff448a86f..471003f53b 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h @@ -223,7 +223,7 @@ class HTTPClient #ifdef KEEP_PRESENT_API TransportTraitsPtr _transportTraits; - std::unique_ptr _tcpDepreciated; + std::unique_ptr _tcpDeprecated; #endif Client* _tcp; From 3ad2b07ee5314ce879da07f5e27bd57eef96d52c Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 20 Jun 2018 14:21:14 +0200 Subject: [PATCH 05/13] Corrected minor formatting issues --- .../BasicHttpClient/BasicHttpClient.ino | 9 +++--- .../ReuseConnection/ReuseConnection.ino | 2 +- .../StreamHttpsClient/StreamHttpsClient.ino | 32 +++++++++---------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino b/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino index 38f412c440..901111ac80 100644 --- a/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino +++ b/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino @@ -49,18 +49,17 @@ void loop() { 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 - if(http.begin((Client &)client, "http://tls.mbed.org/")) { // HTTP - + if (http.begin((Client &)client, "http://tls.mbed.org/")) { // HTTP USE_SERIAL.print("[HTTP] GET...\n"); // start connection and send HTTP header int httpCode = http.GET(); - + // 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 || httpCode == HTTP_CODE_MOVED_PERMANENTLY) { String payload = http.getString(); @@ -69,7 +68,7 @@ void loop() { } 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"); diff --git a/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino b/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino index 259981d37c..f115e36bf0 100644 --- a/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino +++ b/libraries/ESP8266HTTPClient/examples/ReuseConnection/ReuseConnection.ino @@ -46,7 +46,7 @@ void loop() { if ((WiFiMulti.run() == WL_CONNECTED)) { WiFiClient client; - + http.begin((Client&) client, "http://192.168.1.12/test.html"); //http.begin((Client&) client, "192.168.1.12", 80, "/test.html"); diff --git a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino index 25b59fef1c..d700733188 100644 --- a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino +++ b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino @@ -50,15 +50,15 @@ void loop() { 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}; + 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&) client, "https://tls.mbed.org/")) { - //if(http.begin("jigsaw.w3.org", 443, "/HTTP/connection.html", true)) { + if (http.begin((Client&) client, "https://tls.mbed.org/")) { + //if (http.begin("jigsaw.w3.org", 443, "/HTTP/connection.html", true)) { USE_SERIAL.print("[HTTP] GET...\n"); // start connection and send HTTP header @@ -66,49 +66,49 @@ void loop() { 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"); + USE_SERIAL.printf("Unable to connect\n"); } } From a068dcab41489addc76e3c8751ed1a694e4c1466 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 20 Jun 2018 14:48:53 +0200 Subject: [PATCH 06/13] Corrected minor formatting issues --- .../examples/BasicHttpClient/BasicHttpClient.ino | 3 ++- .../examples/StreamHttpsClient/StreamHttpsClient.ino | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino b/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino index 901111ac80..725dde9a76 100644 --- a/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino +++ b/libraries/ESP8266HTTPClient/examples/BasicHttpClient/BasicHttpClient.ino @@ -43,7 +43,7 @@ void loop() { if ((WiFiMulti.run() == WL_CONNECTED)) { WiFiClient client; - + HTTPClient http; USE_SERIAL.print("[HTTP] begin...\n"); @@ -51,6 +51,7 @@ void loop() { //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 if (http.begin((Client &)client, "http://tls.mbed.org/")) { // HTTP + USE_SERIAL.print("[HTTP] GET...\n"); // start connection and send HTTP header int httpCode = http.GET(); diff --git a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino index d700733188..14a37ab9f2 100644 --- a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino +++ b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino @@ -50,7 +50,7 @@ void loop() { if (mfln) { client.setBufferSizes(1024, 1024); } - + USE_SERIAL.print("[HTTP] begin...\n"); // configure server and url @@ -58,7 +58,7 @@ void loop() { client.setFingerprint(fingerprint); if (http.begin((Client&) client, "https://tls.mbed.org/")) { - //if (http.begin("jigsaw.w3.org", 443, "/HTTP/connection.html", true)) { + //if(http.begin("jigsaw.w3.org", 443, "/HTTP/connection.html", true)) { USE_SERIAL.print("[HTTP] GET...\n"); // start connection and send HTTP header From bf1188e38bc60c6d3c2b40a4c769cc3bda1a88a1 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 20 Jun 2018 16:18:37 +0200 Subject: [PATCH 07/13] Corrected minor formatting issues --- .../examples/StreamHttpsClient/StreamHttpsClient.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino index 14a37ab9f2..15d56571c2 100644 --- a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino +++ b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino @@ -57,8 +57,8 @@ void loop() { const uint8_t fingerprint[20] = {0xEB, 0xD9, 0xDF, 0x37, 0xC2, 0xCC, 0x84, 0x89, 0x00, 0xA0, 0x58, 0x52, 0x24, 0x04, 0xE4, 0x37, 0x3E, 0x2B, 0xF1, 0x41}; client.setFingerprint(fingerprint); + //if (http.begin("jigsaw.w3.org", 443, "/HTTP/connection.html", true)) { if (http.begin((Client&) client, "https://tls.mbed.org/")) { - //if(http.begin("jigsaw.w3.org", 443, "/HTTP/connection.html", true)) { USE_SERIAL.print("[HTTP] GET...\n"); // start connection and send HTTP header From deb4a5b3e57697909592a32285624b70de05b4e9 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 20 Jun 2018 17:47:36 +0200 Subject: [PATCH 08/13] Corrected minor formatting issues --- .../examples/StreamHttpsClient/StreamHttpsClient.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino index 15d56571c2..7f302ec642 100644 --- a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino +++ b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino @@ -50,7 +50,7 @@ void loop() { if (mfln) { client.setBufferSizes(1024, 1024); } - + USE_SERIAL.print("[HTTP] begin...\n"); // configure server and url From b34228f79899da553f33881c7b75c85decfc979b Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 20 Jun 2018 20:21:17 +0200 Subject: [PATCH 09/13] Include support for getStreamPtr --- .../ESP8266HTTPClient/src/ESP8266HTTPClient.cpp | 16 +++++++++++++++- .../ESP8266HTTPClient/src/ESP8266HTTPClient.h | 2 ++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index a927c3b6e9..ee6e368231 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -725,7 +725,21 @@ WiFiClient& HTTPClient::getStream(void) WiFiClient* HTTPClient::getStreamPtr(void) { if(connected()) { - return (WiFiClient*)_tcpDeprecated.get(); + return _tcpDeprecated ? (WiFiClient*)_tcpDeprecated.get() : (WiFiClient*)_tcp; + } + + DEBUG_HTTPCLIENT("[HTTP-Client] getStreamPtr: not connected\n"); + return nullptr; +} +#else +/** + * returns the stream of the tcp connection + * @return Client * + */ +Client* HTTPClient::getStreamPtr(void) +{ + if(connected()) { + return _tcp; } DEBUG_HTTPCLIENT("[HTTP-Client] getStreamPtr: not connected\n"); diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h index 471003f53b..3317d011a1 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h @@ -200,6 +200,8 @@ class HTTPClient #ifdef KEEP_PRESENT_API WiFiClient& getStream(void); WiFiClient* getStreamPtr(void); +#else + Client* getStreamPtr(void); #endif int writeToStream(Stream* stream); String getString(void); From 6088be9e471097dfbf52dc6f54a07cb6b3920eac Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 20 Jun 2018 20:49:56 +0200 Subject: [PATCH 10/13] TLS/SSL httpUpdate passing Client& parameter and with WiFiClientSecureBearSSL example using MFLN --- .../httpUpdateSecure/httpUpdateSecure.ino | 143 ++++++++++++++++++ .../src/ESP8266httpUpdate.cpp | 30 +++- .../ESP8266httpUpdate/src/ESP8266httpUpdate.h | 13 ++ 3 files changed, 185 insertions(+), 1 deletion(-) create mode 100644 libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino b/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino new file mode 100644 index 0000000000..99a053d729 --- /dev/null +++ b/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino @@ -0,0 +1,143 @@ +/** + httpUpdateSecure.ino + + Created on: 20.06.2018 as an adaptation of httpUpdate.ino + +*/ + +#include + +#include +#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); + } + + setTime(now); + + USE_SERIAL.println(F("")); + struct tm timeinfo; + gmtime_r(&now, &timeinfo); + USE_SERIAL.print(F("Current time: ")); + USE_SERIAL.print(asctime(&timeinfo)); +} + +void setup() { + + USE_SERIAL.begin(115200); + // USE_SERIAL.setDebugOutput(true); + + USE_SERIAL.println(); + USE_SERIAL.println(); + USE_SERIAL.println(); + + for (uint8_t t = 4; t > 0; t--) { + USE_SERIAL.printf("[SETUP] WAIT %d...\n", t); + USE_SERIAL.flush(); + delay(1000); + } + + WiFi.mode(WIFI_STA); + WiFiMulti.addAP("SSID", "PASSWORD"); + + SPIFFS.begin(); + + int numCerts = certStore.initCertStore(&certs_idx, &certs_ar); + USE_SERIAL.print(F("Number of CA certs read: ")); USE_SERIAL.println(numCerts); + if (numCerts == 0) { + USE_SERIAL.println(F("No certs found. Did you run certs-from-mozill.py and upload the SPIFFS directory before running?")); + return; // Can't connect to anything w/o certs! + } +} + +void loop() { + // wait for WiFi connection + if ((WiFiMulti.run() == WL_CONNECTED)) { + + setClock(); + + BearSSL::WiFiClientSecure client; + bool mfln = client.probeMaxFragmentLength("server", 443, 1024); // server must be the same as in ESPhttpUpdate.update() + USE_SERIAL.printf("MFLN supported: %s\n", mfln ? "yes" : "no"); + if (mfln) { + client.setBufferSizes(1024, 1024); + } + client.setCertStore(&certStore); + + t_httpUpdate_return ret = ESPhttpUpdate.update((Client&) client, "https://server/file.bin"); + + switch (ret) { + case HTTP_UPDATE_FAILED: + USE_SERIAL.printf("HTTP_UPDATE_FAILED Error (%d): %s\n", ESPhttpUpdate.getLastError(), ESPhttpUpdate.getLastErrorString().c_str()); + break; + + case HTTP_UPDATE_NO_UPDATES: + USE_SERIAL.println("HTTP_UPDATE_NO_UPDATES"); + break; + + case HTTP_UPDATE_OK: + USE_SERIAL.println("HTTP_UPDATE_OK"); + break; + } + } +} diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp index 5cc4b6a09e..93574636bb 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.cpp @@ -43,6 +43,7 @@ ESP8266HTTPUpdate::~ESP8266HTTPUpdate(void) { } +#ifdef ESP8266HTTPUPDATE_KEEP_CURRENT_API HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& currentVersion, const String& httpsFingerprint, bool reboot) { @@ -72,7 +73,16 @@ HTTPUpdateResult ESP8266HTTPUpdate::update(const String& url, const String& curr http.begin(url, httpsFingerprint); return handleUpdate(http, currentVersion, false); } +#endif + +HTTPUpdateResult ESP8266HTTPUpdate::update(Client& client, const String& url, const String& currentVersion) +{ + HTTPClient http; + http.begin(client, url); + return handleUpdate(http, currentVersion, false); +} +#ifdef ESP8266HTTPUPDATE_KEEP_CURRENT_API HTTPUpdateResult ESP8266HTTPUpdate::updateSpiffs(const String& url, const String& currentVersion, const String& httpsFingerprint) { HTTPClient http; @@ -93,7 +103,16 @@ HTTPUpdateResult ESP8266HTTPUpdate::updateSpiffs(const String& url, const String http.begin(url); return handleUpdate(http, currentVersion, true); } +#endif +HTTPUpdateResult ESP8266HTTPUpdate::updateSpiffs(Client& client, const String& url, const String& currentVersion) +{ + HTTPClient http; + http.begin(client, url); + return handleUpdate(http, currentVersion, true); +} + +#ifdef ESP8266HTTPUPDATE_KEEP_CURRENT_API HTTPUpdateResult ESP8266HTTPUpdate::update(const String& host, uint16_t port, const String& uri, const String& currentVersion, bool https, const String& httpsFingerprint, bool reboot) { @@ -129,6 +148,15 @@ HTTPUpdateResult ESP8266HTTPUpdate::update(const String& host, uint16_t port, co http.begin(host, port, url, httpsFingerprint); return handleUpdate(http, currentVersion, false); } +#endif + +HTTPUpdateResult ESP8266HTTPUpdate::update(Client& client, const String& host, uint16_t port, const String& uri, + const String& currentVersion) +{ + HTTPClient http; + http.begin(client, host, port, uri); + return handleUpdate(http, currentVersion, false); +} /** * return error code as int @@ -276,7 +304,7 @@ HTTPUpdateResult ESP8266HTTPUpdate::handleUpdate(HTTPClient& http, const String& ret = HTTP_UPDATE_FAILED; } else { - WiFiClient * tcp = http.getStreamPtr(); + WiFiClient * tcp = (WiFiClient *) http.getStreamPtr(); WiFiUDP::stopAll(); WiFiClient::stopAllExcept(tcp); diff --git a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h index 1676d51dfc..da0063e000 100644 --- a/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h +++ b/libraries/ESP8266httpUpdate/src/ESP8266httpUpdate.h @@ -26,11 +26,14 @@ #ifndef ESP8266HTTPUPDATE_H_ #define ESP8266HTTPUPDATE_H_ +#define ESP8266HTTPUPDATE_KEEP_CURRENT_API + #include #include #include #include #include +#include #ifdef DEBUG_ESP_HTTP_UPDATE #ifdef DEBUG_ESP_PORT @@ -72,6 +75,7 @@ class ESP8266HTTPUpdate _rebootOnUpdate = reboot; } +#ifdef ESP8266HTTPUPDATE_KEEP_CURRENT_API // This function is deprecated, use rebootOnUpdate and the next one instead t_httpUpdate_return update(const String& url, const String& currentVersion, const String& httpsFingerprint, bool reboot) __attribute__((deprecated)); @@ -80,7 +84,10 @@ class ESP8266HTTPUpdate const String& httpsFingerprint); t_httpUpdate_return update(const String& url, const String& currentVersion, const uint8_t httpsFingerprint[20]); // BearSSL +#endif + t_httpUpdate_return update(Client& client, const String& url, const String& currentVersion = ""); +#ifdef ESP8266HTTPUPDATE_KEEP_CURRENT_API // This function is deprecated, use one of the overloads below along with rebootOnUpdate t_httpUpdate_return update(const String& host, uint16_t port, const String& uri, const String& currentVersion, bool https, const String& httpsFingerprint, bool reboot) __attribute__((deprecated)); @@ -91,13 +98,19 @@ class ESP8266HTTPUpdate const String& currentVersion, const String& httpsFingerprint); t_httpUpdate_return update(const String& host, uint16_t port, const String& url, const String& currentVersion, const uint8_t httpsFingerprint[20]); // BearSSL +#endif + t_httpUpdate_return update(Client& client, const String& host, uint16_t port, const String& uri = "/", + const String& currentVersion = ""); +#ifdef ESP8266HTTPUPDATE_KEEP_CURRENT_API // This function is deprecated, use rebootOnUpdate and the next one instead t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion, const String& httpsFingerprint, bool reboot) __attribute__((deprecated)); t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion = ""); t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion, const String& httpsFingerprint); t_httpUpdate_return updateSpiffs(const String& url, const String& currentVersion, const uint8_t httpsFingerprint[20]); // BearSSL +#endif + t_httpUpdate_return updateSpiffs(Client& client, const String& url, const String& currentVersion = ""); int getLastError(void); From 86a2d12085871af1b7df9068184b4f8d1a0e4b92 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 20 Jun 2018 21:20:44 +0200 Subject: [PATCH 11/13] Remove #include --- .../examples/httpUpdateSecure/httpUpdateSecure.ino | 3 --- 1 file changed, 3 deletions(-) diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino b/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino index 99a053d729..d5017ae5c8 100644 --- a/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino +++ b/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino @@ -13,7 +13,6 @@ #include #include -#include #include #define USE_SERIAL Serial @@ -73,8 +72,6 @@ void setClock() { now = time(nullptr); } - setTime(now); - USE_SERIAL.println(F("")); struct tm timeinfo; gmtime_r(&now, &timeinfo); From 1ed6f4a0ce0c2360b46902f688e60b46ed6dd7e8 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Thu, 21 Jun 2018 07:50:58 +0200 Subject: [PATCH 12/13] Remove #include --- .../examples/httpUpdateSecure/httpUpdateSecure.ino | 2 -- 1 file changed, 2 deletions(-) diff --git a/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino b/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino index d5017ae5c8..47ec7b5a78 100644 --- a/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino +++ b/libraries/ESP8266httpUpdate/examples/httpUpdateSecure/httpUpdateSecure.ino @@ -5,8 +5,6 @@ */ -#include - #include #include From d4fb1d606603e234aa4dd74dc4922ab79bcdb365 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Tue, 17 Jul 2018 22:53:33 +0200 Subject: [PATCH 13/13] Bug fix NULL pointer reference --- libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index ee6e368231..18ed6bc2cc 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -372,6 +372,7 @@ void HTTPClient::end(void) } else { DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp stop\n"); _tcp->stop(); + _tcp = nullptr; } } else { DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp is closed\n");