From 3a10f93a981a67a990b9142bcfd924d417af3eba Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 10 Oct 2018 18:29:27 +0200 Subject: [PATCH 1/7] Removed _client->stop() from destructor; some minor changes --- .../ESP8266HTTPClient/src/ESP8266HTTPClient.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 034a815b97..1872ed08ca 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -123,7 +123,7 @@ HTTPClient::HTTPClient() HTTPClient::~HTTPClient() { if(_client) { - _client->stop(); + DEBUG_HTTPCLIENT("[HTTP-Client][~HTTPClient] end() not called before destruction of HTTPClient\n"); } if(_currentHeaders) { delete[] _currentHeaders; @@ -196,7 +196,7 @@ bool HTTPClient::begin(WiFiClient &client, String host, uint16_t port, String ur #ifdef HTTPCLIENT_1_1_COMPATIBLE bool HTTPClient::begin(String url, String httpsFingerprint) { - if(_client) _canReuse = false; + _canReuse = false; end(); _port = 443; @@ -214,7 +214,7 @@ bool HTTPClient::begin(String url, String httpsFingerprint) bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20]) { - if(_client) _canReuse = false; + _canReuse = false; end(); _port = 443; @@ -237,7 +237,7 @@ bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20]) */ bool HTTPClient::begin(String url) { - if(_client) _canReuse = false; + _canReuse = false; end(); _port = 80; @@ -299,7 +299,7 @@ bool HTTPClient::beginInternal(String url, const char* expectedProtocol) #ifdef HTTPCLIENT_1_1_COMPATIBLE bool HTTPClient::begin(String host, uint16_t port, String uri) { - if(_client) _canReuse = false; + _canReuse = false; end(); clear(); @@ -325,7 +325,7 @@ 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) { - if(_client) _canReuse = false; + _canReuse = false; end(); clear(); @@ -343,7 +343,7 @@ 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]) { - if(_client) _canReuse = false; + _canReuse = false; end(); clear(); @@ -367,6 +367,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t htt */ void HTTPClient::end(void) { + _canReuse = false; disconnect(); clear(); } From 18654ce532e8a424613b5a37401ecd1a8ad8dc3a Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Wed, 10 Oct 2018 18:36:10 +0200 Subject: [PATCH 2/7] Changed BasicHttpsClient.ino to allocate BearSSL::WiFiClientSecure object on the heap in stead of stack --- .../examples/BasicHttpsClient/BasicHttpsClient.ino | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino index 3c5c0ed5b7..e0eb2879b2 100644 --- a/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino +++ b/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino @@ -41,13 +41,14 @@ void loop() { // wait for WiFi connection if ((WiFiMulti.run() == WL_CONNECTED)) { - BearSSL::WiFiClientSecure client; - client.setFingerprint(fingerprint); + BearSSL::WiFiClientSecure *client = new BearSSL::WiFiClientSecure; + + client->setFingerprint(fingerprint); HTTPClient https; Serial.print("[HTTPS] begin...\n"); - if (https.begin(client, "https://jigsaw.w3.org/HTTP/connection.html")) { // HTTPS + if (https.begin(*client, "https://jigsaw.w3.org/HTTP/connection.html")) { // HTTPS Serial.print("[HTTPS] GET...\n"); @@ -72,6 +73,8 @@ void loop() { } else { Serial.printf("[HTTPS] Unable to connect\n"); } + + delete client; } delay(10000); From c7829595042ba98792cb1d6cce72488cbcd6c707 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Fri, 12 Oct 2018 08:28:19 +0200 Subject: [PATCH 3/7] Removed unnecessary code --- libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 1872ed08ca..c80d3229cd 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -196,7 +196,6 @@ bool HTTPClient::begin(WiFiClient &client, String host, uint16_t port, String ur #ifdef HTTPCLIENT_1_1_COMPATIBLE bool HTTPClient::begin(String url, String httpsFingerprint) { - _canReuse = false; end(); _port = 443; @@ -214,7 +213,6 @@ bool HTTPClient::begin(String url, String httpsFingerprint) bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20]) { - _canReuse = false; end(); _port = 443; @@ -237,7 +235,6 @@ bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20]) */ bool HTTPClient::begin(String url) { - _canReuse = false; end(); _port = 80; @@ -299,7 +296,6 @@ bool HTTPClient::beginInternal(String url, const char* expectedProtocol) #ifdef HTTPCLIENT_1_1_COMPATIBLE bool HTTPClient::begin(String host, uint16_t port, String uri) { - _canReuse = false; end(); clear(); @@ -325,7 +321,6 @@ 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) { - _canReuse = false; end(); clear(); @@ -343,7 +338,6 @@ 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]) { - _canReuse = false; end(); clear(); From 3c23315cb87dab604b78244359fb936eefabb47f Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Tue, 16 Oct 2018 07:57:49 +0200 Subject: [PATCH 4/7] Correcting bad fix for #5216 --- .../BasicHttpsClient/BasicHttpsClient.ino | 53 +++++++------ .../StreamHttpsClient/StreamHttpsClient.ino | 79 ++++++++++--------- .../src/ESP8266HTTPClient.cpp | 67 ++++++++++++---- 3 files changed, 118 insertions(+), 81 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino index e0eb2879b2..8a97f5ac76 100644 --- a/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino +++ b/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino @@ -45,37 +45,38 @@ void loop() { 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); + { // Create a block arround HTTPClient, so it is destroyed before WiFiClientSecure *client is deleted + 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] GET... failed, error: %s\n", https.errorToString(httpCode).c_str()); + Serial.printf("[HTTPS] Unable to connect\n"); } - - https.end(); - } else { - Serial.printf("[HTTPS] Unable to connect\n"); - } - + } // End block around HTTPClient delete client; } + Serial.println("Wait 10s before next round..."); delay(10000); } diff --git a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino index b802e2dc5e..4b8f08d6de 100644 --- a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino +++ b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino @@ -38,8 +38,6 @@ void loop() { // wait for WiFi connection if ((WiFiMulti.run() == WL_CONNECTED)) { - HTTPClient http; - BearSSL::WiFiClientSecure *client = new BearSSL::WiFiClientSecure ; bool mfln = client->probeMaxFragmentLength("tls.mbed.org", 443, 1024); @@ -55,61 +53,66 @@ 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(*client, "https://tls.mbed.org/")) { + { // Create a block arround HTTPClient, so it is destroyed before WiFiClientSecure *client is deleted + HTTPClient https; + + if (https.begin(*client, "https://tls.mbed.org/")) { - 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 - Serial.printf("[HTTPS] GET... code: %d\n", httpCode); + Serial.print("[HTTPS] GET...\n"); + // start connection and send HTTP header + int httpCode = https.GET(); + 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) { + // 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(); + // get lenght of document (is -1 when Server sends no Content-Length header) + int len = https.getSize(); - // create buffer for read - static uint8_t buff[128] = { 0 }; + // create buffer for read + static uint8_t buff[128] = { 0 }; - // get tcp stream - WiFiClient * stream = client; + // 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(); + // read all data from server + while (https.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)); + if (size) { + // read up to 128 byte + int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); - // write it to Serial - Serial.write(buff, c); + // write it to Serial + Serial.write(buff, c); - if (len > 0) { - len -= c; + if (len > 0) { + len -= c; + } } + delay(1); } - delay(1); - } - Serial.println(); - Serial.print("[HTTPS] connection closed or file end.\n"); + Serial.println(); + Serial.print("[HTTPS] connection closed or file end.\n"); + } + } else { + Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str()); } + + https.end(); } else { - Serial.printf("[HTTPS] GET... failed, error: %s\n", http.errorToString(httpCode).c_str()); + Serial.printf("Unable to connect\n"); } - - http.end(); - } else { - Serial.printf("Unable to connect\n"); - } + } // End block around HTTPCLient delete client; } + Serial.println("Wait 10s before the next round..."); delay(10000); } diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index c80d3229cd..62031fb50a 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -123,7 +123,7 @@ HTTPClient::HTTPClient() HTTPClient::~HTTPClient() { if(_client) { - DEBUG_HTTPCLIENT("[HTTP-Client][~HTTPClient] end() not called before destruction of HTTPClient\n"); + _client->stop(); } if(_currentHeaders) { delete[] _currentHeaders; @@ -147,7 +147,13 @@ void HTTPClient::clear() * @return success bool */ bool HTTPClient::begin(WiFiClient &client, String url) { - end(); +#ifdef HTTPCLIENT_1_1_COMPATIBLE + if(_tcpDeprecated) { + DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n"); + _canReuse = false; + end(); + } +#endif _client = &client; @@ -180,7 +186,13 @@ bool HTTPClient::begin(WiFiClient &client, String url) { */ bool HTTPClient::begin(WiFiClient &client, String host, uint16_t port, String uri, bool https) { - end(); +#ifdef HTTPCLIENT_1_1_COMPATIBLE + if(_tcpDeprecated) { + DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n"); + _canReuse = false; + end(); + } +#endif _client = &client; @@ -196,7 +208,11 @@ bool HTTPClient::begin(WiFiClient &client, String host, uint16_t port, String ur #ifdef HTTPCLIENT_1_1_COMPATIBLE bool HTTPClient::begin(String url, String httpsFingerprint) { - end(); + if(_client && !_tcpDeprecated) { + DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n"); + _canReuse = false; + end(); + } _port = 443; if (httpsFingerprint.length() == 0) { @@ -213,7 +229,11 @@ bool HTTPClient::begin(String url, String httpsFingerprint) bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20]) { - end(); + if(_client && !_tcpDeprecated) { + DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n"); + _canReuse = false; + end(); + } _port = 443; if (!beginInternal(url, "https")) { @@ -235,7 +255,11 @@ bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20]) */ bool HTTPClient::begin(String url) { - end(); + if(_client && !_tcpDeprecated) { + DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n"); + _canReuse = false; + end(); + } _port = 80; if (!beginInternal(url, "http")) { @@ -296,7 +320,11 @@ bool HTTPClient::beginInternal(String url, const char* expectedProtocol) #ifdef HTTPCLIENT_1_1_COMPATIBLE bool HTTPClient::begin(String host, uint16_t port, String uri) { - end(); + if(_client && !_tcpDeprecated) { + DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n"); + _canReuse = false; + end(); + } clear(); _host = host; @@ -321,7 +349,11 @@ 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) { - end(); + if(_client && !_tcpDeprecated) { + DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n"); + _canReuse = false; + end(); + } clear(); _host = host; @@ -338,7 +370,11 @@ 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]) { - end(); + if(_client && !_tcpDeprecated) { + DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n"); + _canReuse = false; + end(); + } clear(); _host = host; @@ -361,7 +397,6 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t htt */ void HTTPClient::end(void) { - _canReuse = false; disconnect(); clear(); } @@ -373,15 +408,13 @@ void HTTPClient::end(void) void HTTPClient::disconnect() { if(connected()) { - 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(_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 { From 29d97b0e2a742217b184db89b101448a98c1bc89 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Tue, 16 Oct 2018 16:00:22 +0200 Subject: [PATCH 5/7] Minor formatting to pass Travis tests --- .../examples/BasicHttpsClient/BasicHttpsClient.ino | 7 +++++-- .../examples/StreamHttpsClient/StreamHttpsClient.ino | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino index 8a97f5ac76..dbd5299000 100644 --- a/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino +++ b/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino @@ -45,7 +45,8 @@ void loop() { client->setFingerprint(fingerprint); - { // Create a block arround HTTPClient, so it is destroyed before WiFiClientSecure *client is deleted + { + // Create a block arround HTTPClient, so it is destroyed before WiFiClientSecure *client is deleted HTTPClient https; Serial.print("[HTTPS] begin...\n"); @@ -73,7 +74,9 @@ void loop() { } else { Serial.printf("[HTTPS] Unable to connect\n"); } - } // End block around HTTPClient + + // End block around HTTPClient + } delete client; } diff --git a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino index 4b8f08d6de..a8103688cc 100644 --- a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino +++ b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino @@ -53,7 +53,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); - { // Create a block arround HTTPClient, so it is destroyed before WiFiClientSecure *client is deleted + { + // Create a block arround HTTPClient, so it is destroyed before WiFiClientSecure *client is deleted HTTPClient https; if (https.begin(*client, "https://tls.mbed.org/")) { @@ -108,7 +109,9 @@ void loop() { } else { Serial.printf("Unable to connect\n"); } - } // End block around HTTPCLient + + // End block around HTTPCLient + } delete client; } From 20184a8d8160ec2b14a5b29c5312506a0f485092 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Thu, 18 Oct 2018 11:11:24 +0200 Subject: [PATCH 6/7] Changed client * to std::unique_ptr<> client --- .../BasicHttpsClient/BasicHttpsClient.ino | 52 +++++------- .../StreamHttpsClient/StreamHttpsClient.ino | 83 +++++++++---------- 2 files changed, 60 insertions(+), 75 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino index dbd5299000..5e767ccd57 100644 --- a/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino +++ b/libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino @@ -41,43 +41,37 @@ void loop() { // wait for WiFi connection if ((WiFiMulti.run() == WL_CONNECTED)) { - BearSSL::WiFiClientSecure *client = new BearSSL::WiFiClientSecure; + std::unique_ptrclient(new BearSSL::WiFiClientSecure); client->setFingerprint(fingerprint); - { - // Create a block arround HTTPClient, so it is destroyed before WiFiClientSecure *client is deleted - 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()); - } + 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(); - https.end(); + // 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] Unable to connect\n"); + Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str()); } - // End block around HTTPClient + https.end(); + } else { + Serial.printf("[HTTPS] Unable to connect\n"); } - delete client; } Serial.println("Wait 10s before next round..."); diff --git a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino index a8103688cc..2bb87802a4 100644 --- a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino +++ b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino @@ -38,9 +38,9 @@ void loop() { // wait for WiFi connection if ((WiFiMulti.run() == WL_CONNECTED)) { - BearSSL::WiFiClientSecure *client = new BearSSL::WiFiClientSecure ; + std::unique_ptr client(new BearSSL::WiFiClientSecure); - bool mfln = client->probeMaxFragmentLength("tls.mbed.org", 443, 1024); + bool mfln = client->probeMaxFragmentLength("jigsaw.w3.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) { @@ -50,70 +50,61 @@ void loop() { 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); + 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}; - { - // Create a block arround HTTPClient, so it is destroyed before WiFiClientSecure *client is deleted - HTTPClient https; + client->setFingerprint(fingerprint); - if (https.begin(*client, "https://tls.mbed.org/")) { + HTTPClient https; - Serial.print("[HTTPS] GET...\n"); - // start connection and send HTTP header - int httpCode = https.GET(); - if (httpCode > 0) { - // HTTP header has been send and Server response header has been handled - Serial.printf("[HTTPS] GET... code: %d\n", httpCode); + if (https.begin(*client, "https://jigsaw.w3.org/HTTP/connection.html")) { - // file found at server - if (httpCode == HTTP_CODE_OK) { + Serial.print("[HTTPS] GET...\n"); + // start connection and send HTTP header + int httpCode = https.GET(); + if (httpCode > 0) { + // HTTP header has been send and Server response header has been handled + Serial.printf("[HTTPS] GET... code: %d\n", httpCode); - // get lenght of document (is -1 when Server sends no Content-Length header) - int len = https.getSize(); + // file found at server + if (httpCode == HTTP_CODE_OK) { - // create buffer for read - static uint8_t buff[128] = { 0 }; + // get lenght of document (is -1 when Server sends no Content-Length header) + int len = https.getSize(); - // get tcp stream - WiFiClient * stream = client; + // create buffer for read + static uint8_t buff[128] = { 0 }; - // read all data from server - while (https.connected() && (len > 0 || len == -1)) { - // get available data size - size_t size = stream->available(); + // read all data from server + while (https.connected() && (len > 0 || len == -1)) { + // get available data size + size_t size = client->available(); - if (size) { - // read up to 128 byte - int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); + if (size) { + // read up to 128 byte + int c = client->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size)); - // write it to Serial - Serial.write(buff, c); + // write it to Serial + Serial.write(buff, c); - if (len > 0) { - len -= c; - } + if (len > 0) { + len -= c; } - delay(1); } + delay(1); + } - Serial.println(); - Serial.print("[HTTPS] connection closed or file end.\n"); + Serial.println(); + Serial.print("[HTTPS] connection closed or file end.\n"); - } - } else { - Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str()); } - - https.end(); } else { - Serial.printf("Unable to connect\n"); + Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str()); } - // End block around HTTPCLient + https.end(); + } else { + Serial.printf("Unable to connect\n"); } - - delete client; } Serial.println("Wait 10s before the next round..."); From ec90b3adb5175e222a3d402aa9928dc6370762e5 Mon Sep 17 00:00:00 2001 From: Jeroen88 Date: Thu, 18 Oct 2018 12:31:13 +0200 Subject: [PATCH 7/7] Updated example --- .../examples/StreamHttpsClient/StreamHttpsClient.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino index 2bb87802a4..55009349fa 100644 --- a/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino +++ b/libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino @@ -40,7 +40,7 @@ void loop() { std::unique_ptr client(new BearSSL::WiFiClientSecure); - bool mfln = client->probeMaxFragmentLength("jigsaw.w3.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) { @@ -50,13 +50,13 @@ void loop() { Serial.print("[HTTPS] begin...\n"); // configure server and url - 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}; + 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); HTTPClient https; - if (https.begin(*client, "https://jigsaw.w3.org/HTTP/connection.html")) { + if (https.begin(*client, "https://tls.mbed.org/")) { Serial.print("[HTTPS] GET...\n"); // start connection and send HTTP header