Skip to content

Commit 60d519e

Browse files
Jeroen88d-a-v
Jeroen88
authored andcommitted
Bugfix/esp8266 http client (#6176)
1 parent 55539ae commit 60d519e

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

Diff for: libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

+34-7
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,11 @@ bool HTTPClient::begin(String url, String httpsFingerprint)
225225
return false;
226226
}
227227
_transportTraits = TransportTraitsPtr(new TLSTraits(httpsFingerprint));
228+
if(!_transportTraits) {
229+
DEBUG_HTTPCLIENT("[HTTP-Client][begin] could not create transport traits\n");
230+
return false;
231+
}
232+
228233
DEBUG_HTTPCLIENT("[HTTP-Client][begin] httpsFingerprint: %s\n", httpsFingerprint.c_str());
229234
return true;
230235
}
@@ -242,6 +247,11 @@ bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20])
242247
return false;
243248
}
244249
_transportTraits = TransportTraitsPtr(new BearSSLTraits(httpsFingerprint));
250+
if(!_transportTraits) {
251+
DEBUG_HTTPCLIENT("[HTTP-Client][begin] could not create transport traits\n");
252+
return false;
253+
}
254+
245255
DEBUG_HTTPCLIENT("[HTTP-Client][begin] BearSSL-httpsFingerprint:");
246256
for (size_t i=0; i < 20; i++) {
247257
DEBUG_HTTPCLIENT(" %02x", httpsFingerprint[i]);
@@ -409,7 +419,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t htt
409419
*/
410420
void HTTPClient::end(void)
411421
{
412-
disconnect();
422+
disconnect(false);
413423
clear();
414424
_redirectCount = 0;
415425
}
@@ -563,6 +573,7 @@ void HTTPClient::setRedirectLimit(uint16_t limit)
563573
void HTTPClient::useHTTP10(bool useHTTP10)
564574
{
565575
_useHTTP10 = useHTTP10;
576+
_reuse = !useHTTP10;
566577
}
567578

568579
/**
@@ -980,7 +991,7 @@ int HTTPClient::writeToStream(Stream * stream)
980991
return returnError(HTTPC_ERROR_ENCODING);
981992
}
982993

983-
disconnect();
994+
disconnect(true);
984995
return ret;
985996
}
986997

@@ -1139,7 +1150,11 @@ bool HTTPClient::hasHeader(const char* name)
11391150
bool HTTPClient::connect(void)
11401151
{
11411152
if(connected()) {
1142-
DEBUG_HTTPCLIENT("[HTTP-Client] connect. already connected, try reuse!\n");
1153+
if(_reuse) {
1154+
DEBUG_HTTPCLIENT("[HTTP-Client] connect: already connected, reusing connection\n");
1155+
} else {
1156+
DEBUG_HTTPCLIENT("[HTTP-Client] connect: already connected, try reuse!\n");
1157+
}
11431158
while(_client->available() > 0) {
11441159
_client->read();
11451160
}
@@ -1149,6 +1164,10 @@ bool HTTPClient::connect(void)
11491164
#if HTTPCLIENT_1_1_COMPATIBLE
11501165
if(!_client && _transportTraits) {
11511166
_tcpDeprecated = _transportTraits->create();
1167+
if(!_tcpDeprecated) {
1168+
DEBUG_HTTPCLIENT("[HTTP-Client] connect: could not create tcp\n");
1169+
return false;
1170+
}
11521171
_client = _tcpDeprecated.get();
11531172
}
11541173
#endif
@@ -1246,9 +1265,12 @@ int HTTPClient::handleHeaderResponse()
12461265
return HTTPC_ERROR_NOT_CONNECTED;
12471266
}
12481267

1268+
clear();
1269+
1270+
_canReuse = _reuse;
1271+
12491272
String transferEncoding;
1250-
_returnCode = -1;
1251-
_size = -1;
1273+
12521274
_transferEncoding = HTTPC_TE_IDENTITY;
12531275
unsigned long lastDataTime = millis();
12541276

@@ -1263,6 +1285,9 @@ int HTTPClient::handleHeaderResponse()
12631285
DEBUG_HTTPCLIENT("[HTTP-Client][handleHeaderResponse] RX: '%s'\n", headerLine.c_str());
12641286

12651287
if(headerLine.startsWith("HTTP/1.")) {
1288+
if(_canReuse) {
1289+
_canReuse = (headerLine[sizeof "HTTP/1." - 1] != '0');
1290+
}
12661291
_returnCode = headerLine.substring(9, headerLine.indexOf(' ', 9)).toInt();
12671292
} else if(headerLine.indexOf(':')) {
12681293
String headerName = headerLine.substring(0, headerLine.indexOf(':'));
@@ -1273,8 +1298,10 @@ int HTTPClient::handleHeaderResponse()
12731298
_size = headerValue.toInt();
12741299
}
12751300

1276-
if(headerName.equalsIgnoreCase("Connection")) {
1277-
_canReuse = headerValue.equalsIgnoreCase("keep-alive");
1301+
if(_canReuse && headerName.equalsIgnoreCase("Connection")) {
1302+
if(headerValue.indexOf("close") >= 0 && headerValue.indexOf("keep-alive") < 0) {
1303+
_canReuse = false;
1304+
}
12781305
}
12791306

12801307
if(headerName.equalsIgnoreCase("Transfer-Encoding")) {

Diff for: libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ class HTTPClient
235235
/// request handling
236236
String _host;
237237
uint16_t _port = 0;
238-
bool _reuse = false;
238+
bool _reuse = true;
239239
uint16_t _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT;
240240
bool _useHTTP10 = false;
241241

0 commit comments

Comments
 (0)