Skip to content

Commit e954022

Browse files
Jeroen88devyte
Jeroen88
authored andcommitted
Bugfix/esp8266 http client (#5250)
* Removed _client->stop() from destructor; some minor changes * Changed BasicHttpsClient.ino to allocate BearSSL::WiFiClientSecure object on the heap in stead of stack * Removed unnecessary code * Correcting bad fix for #5216 * Minor formatting to pass Travis tests * Changed client * to std::unique_ptr<> client * Updated example
1 parent 6218c40 commit e954022

File tree

3 files changed

+65
-43
lines changed

3 files changed

+65
-43
lines changed

libraries/ESP8266HTTPClient/examples/BasicHttpsClient/BasicHttpsClient.ino

+2-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void loop() {
4141
// wait for WiFi connection
4242
if ((WiFiMulti.run() == WL_CONNECTED)) {
4343

44-
BearSSL::WiFiClientSecure *client = new BearSSL::WiFiClientSecure;
44+
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
4545

4646
client->setFingerprint(fingerprint);
4747

@@ -50,7 +50,6 @@ void loop() {
5050
Serial.print("[HTTPS] begin...\n");
5151
if (https.begin(*client, "https://jigsaw.w3.org/HTTP/connection.html")) { // HTTPS
5252

53-
5453
Serial.print("[HTTPS] GET...\n");
5554
// start connection and send HTTP header
5655
int httpCode = https.GET();
@@ -73,9 +72,8 @@ void loop() {
7372
} else {
7473
Serial.printf("[HTTPS] Unable to connect\n");
7574
}
76-
77-
delete client;
7875
}
7976

77+
Serial.println("Wait 10s before next round...");
8078
delay(10000);
8179
}

libraries/ESP8266HTTPClient/examples/StreamHttpsClient/StreamHttpsClient.ino

+13-16
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ void loop() {
3838
// wait for WiFi connection
3939
if ((WiFiMulti.run() == WL_CONNECTED)) {
4040

41-
HTTPClient http;
42-
43-
BearSSL::WiFiClientSecure *client = new BearSSL::WiFiClientSecure ;
41+
std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure);
4442

4543
bool mfln = client->probeMaxFragmentLength("tls.mbed.org", 443, 1024);
4644
Serial.printf("\nConnecting to https://tls.mbed.org\n");
@@ -53,13 +51,16 @@ void loop() {
5351

5452
// configure server and url
5553
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};
54+
5655
client->setFingerprint(fingerprint);
5756

58-
if (http.begin(*client, "https://tls.mbed.org/")) {
57+
HTTPClient https;
58+
59+
if (https.begin(*client, "https://tls.mbed.org/")) {
5960

6061
Serial.print("[HTTPS] GET...\n");
6162
// start connection and send HTTP header
62-
int httpCode = http.GET();
63+
int httpCode = https.GET();
6364
if (httpCode > 0) {
6465
// HTTP header has been send and Server response header has been handled
6566
Serial.printf("[HTTPS] GET... code: %d\n", httpCode);
@@ -68,22 +69,19 @@ void loop() {
6869
if (httpCode == HTTP_CODE_OK) {
6970

7071
// get lenght of document (is -1 when Server sends no Content-Length header)
71-
int len = http.getSize();
72+
int len = https.getSize();
7273

7374
// create buffer for read
7475
static uint8_t buff[128] = { 0 };
7576

76-
// get tcp stream
77-
WiFiClient * stream = client;
78-
7977
// read all data from server
80-
while (http.connected() && (len > 0 || len == -1)) {
78+
while (https.connected() && (len > 0 || len == -1)) {
8179
// get available data size
82-
size_t size = stream->available();
80+
size_t size = client->available();
8381

8482
if (size) {
8583
// read up to 128 byte
86-
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
84+
int c = client->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
8785

8886
// write it to Serial
8987
Serial.write(buff, c);
@@ -100,16 +98,15 @@ void loop() {
10098

10199
}
102100
} else {
103-
Serial.printf("[HTTPS] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
101+
Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
104102
}
105103

106-
http.end();
104+
https.end();
107105
} else {
108106
Serial.printf("Unable to connect\n");
109107
}
110-
111-
delete client;
112108
}
113109

110+
Serial.println("Wait 10s before the next round...");
114111
delay(10000);
115112
}

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

+50-23
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ HTTPClient::HTTPClient()
123123
HTTPClient::~HTTPClient()
124124
{
125125
if(_client) {
126-
DEBUG_HTTPCLIENT("[HTTP-Client][~HTTPClient] end() not called before destruction of HTTPClient\n");
126+
_client->stop();
127127
}
128128
if(_currentHeaders) {
129129
delete[] _currentHeaders;
@@ -147,7 +147,13 @@ void HTTPClient::clear()
147147
* @return success bool
148148
*/
149149
bool HTTPClient::begin(WiFiClient &client, String url) {
150-
end();
150+
#ifdef HTTPCLIENT_1_1_COMPATIBLE
151+
if(_tcpDeprecated) {
152+
DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n");
153+
_canReuse = false;
154+
end();
155+
}
156+
#endif
151157

152158
_client = &client;
153159

@@ -180,7 +186,13 @@ bool HTTPClient::begin(WiFiClient &client, String url) {
180186
*/
181187
bool HTTPClient::begin(WiFiClient &client, String host, uint16_t port, String uri, bool https)
182188
{
183-
end();
189+
#ifdef HTTPCLIENT_1_1_COMPATIBLE
190+
if(_tcpDeprecated) {
191+
DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n");
192+
_canReuse = false;
193+
end();
194+
}
195+
#endif
184196

185197
_client = &client;
186198

@@ -196,8 +208,11 @@ bool HTTPClient::begin(WiFiClient &client, String host, uint16_t port, String ur
196208
#ifdef HTTPCLIENT_1_1_COMPATIBLE
197209
bool HTTPClient::begin(String url, String httpsFingerprint)
198210
{
199-
_canReuse = false;
200-
end();
211+
if(_client && !_tcpDeprecated) {
212+
DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n");
213+
_canReuse = false;
214+
end();
215+
}
201216

202217
_port = 443;
203218
if (httpsFingerprint.length() == 0) {
@@ -214,8 +229,11 @@ bool HTTPClient::begin(String url, String httpsFingerprint)
214229

215230
bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20])
216231
{
217-
_canReuse = false;
218-
end();
232+
if(_client && !_tcpDeprecated) {
233+
DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n");
234+
_canReuse = false;
235+
end();
236+
}
219237

220238
_port = 443;
221239
if (!beginInternal(url, "https")) {
@@ -237,8 +255,11 @@ bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20])
237255
*/
238256
bool HTTPClient::begin(String url)
239257
{
240-
_canReuse = false;
241-
end();
258+
if(_client && !_tcpDeprecated) {
259+
DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n");
260+
_canReuse = false;
261+
end();
262+
}
242263

243264
_port = 80;
244265
if (!beginInternal(url, "http")) {
@@ -299,8 +320,11 @@ bool HTTPClient::beginInternal(String url, const char* expectedProtocol)
299320
#ifdef HTTPCLIENT_1_1_COMPATIBLE
300321
bool HTTPClient::begin(String host, uint16_t port, String uri)
301322
{
302-
_canReuse = false;
303-
end();
323+
if(_client && !_tcpDeprecated) {
324+
DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n");
325+
_canReuse = false;
326+
end();
327+
}
304328

305329
clear();
306330
_host = host;
@@ -325,8 +349,11 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, bool https, Strin
325349

326350
bool HTTPClient::begin(String host, uint16_t port, String uri, String httpsFingerprint)
327351
{
328-
_canReuse = false;
329-
end();
352+
if(_client && !_tcpDeprecated) {
353+
DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n");
354+
_canReuse = false;
355+
end();
356+
}
330357

331358
clear();
332359
_host = host;
@@ -343,8 +370,11 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, String httpsFinge
343370

344371
bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t httpsFingerprint[20])
345372
{
346-
_canReuse = false;
347-
end();
373+
if(_client && !_tcpDeprecated) {
374+
DEBUG_HTTPCLIENT("[HTTP-Client][begin] mix up of new and deprecated api\n");
375+
_canReuse = false;
376+
end();
377+
}
348378

349379
clear();
350380
_host = host;
@@ -367,7 +397,6 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t htt
367397
*/
368398
void HTTPClient::end(void)
369399
{
370-
_canReuse = false;
371400
disconnect();
372401
clear();
373402
}
@@ -379,15 +408,13 @@ void HTTPClient::end(void)
379408
void HTTPClient::disconnect()
380409
{
381410
if(connected()) {
382-
if(_client) {
383-
if(_client->available() > 0) {
384-
DEBUG_HTTPCLIENT("[HTTP-Client][end] still data in buffer (%d), clean up.\n", _client->available());
385-
while(_client->available() > 0) {
386-
_client->read();
387-
}
411+
if(_client->available() > 0) {
412+
DEBUG_HTTPCLIENT("[HTTP-Client][end] still data in buffer (%d), clean up.\n", _client->available());
413+
while(_client->available() > 0) {
414+
_client->read();
388415
}
389-
390416
}
417+
391418
if(_reuse && _canReuse) {
392419
DEBUG_HTTPCLIENT("[HTTP-Client][end] tcp keep open for reuse\n");
393420
} else {

0 commit comments

Comments
 (0)