@@ -225,6 +225,11 @@ bool HTTPClient::begin(String url, String httpsFingerprint)
225
225
return false ;
226
226
}
227
227
_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
+
228
233
DEBUG_HTTPCLIENT (" [HTTP-Client][begin] httpsFingerprint: %s\n " , httpsFingerprint.c_str ());
229
234
return true ;
230
235
}
@@ -242,6 +247,11 @@ bool HTTPClient::begin(String url, const uint8_t httpsFingerprint[20])
242
247
return false ;
243
248
}
244
249
_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
+
245
255
DEBUG_HTTPCLIENT (" [HTTP-Client][begin] BearSSL-httpsFingerprint:" );
246
256
for (size_t i=0 ; i < 20 ; i++) {
247
257
DEBUG_HTTPCLIENT (" %02x" , httpsFingerprint[i]);
@@ -409,7 +419,7 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t htt
409
419
*/
410
420
void HTTPClient::end (void )
411
421
{
412
- disconnect ();
422
+ disconnect (false );
413
423
clear ();
414
424
_redirectCount = 0 ;
415
425
}
@@ -563,6 +573,7 @@ void HTTPClient::setRedirectLimit(uint16_t limit)
563
573
void HTTPClient::useHTTP10 (bool useHTTP10)
564
574
{
565
575
_useHTTP10 = useHTTP10;
576
+ _reuse = !useHTTP10;
566
577
}
567
578
568
579
/* *
@@ -980,7 +991,7 @@ int HTTPClient::writeToStream(Stream * stream)
980
991
return returnError (HTTPC_ERROR_ENCODING);
981
992
}
982
993
983
- disconnect ();
994
+ disconnect (true );
984
995
return ret;
985
996
}
986
997
@@ -1139,7 +1150,11 @@ bool HTTPClient::hasHeader(const char* name)
1139
1150
bool HTTPClient::connect (void )
1140
1151
{
1141
1152
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
+ }
1143
1158
while (_client->available () > 0 ) {
1144
1159
_client->read ();
1145
1160
}
@@ -1149,6 +1164,10 @@ bool HTTPClient::connect(void)
1149
1164
#if HTTPCLIENT_1_1_COMPATIBLE
1150
1165
if (!_client && _transportTraits) {
1151
1166
_tcpDeprecated = _transportTraits->create ();
1167
+ if (!_tcpDeprecated) {
1168
+ DEBUG_HTTPCLIENT (" [HTTP-Client] connect: could not create tcp\n " );
1169
+ return false ;
1170
+ }
1152
1171
_client = _tcpDeprecated.get ();
1153
1172
}
1154
1173
#endif
@@ -1246,9 +1265,12 @@ int HTTPClient::handleHeaderResponse()
1246
1265
return HTTPC_ERROR_NOT_CONNECTED;
1247
1266
}
1248
1267
1268
+ clear ();
1269
+
1270
+ _canReuse = _reuse;
1271
+
1249
1272
String transferEncoding;
1250
- _returnCode = -1 ;
1251
- _size = -1 ;
1273
+
1252
1274
_transferEncoding = HTTPC_TE_IDENTITY;
1253
1275
unsigned long lastDataTime = millis ();
1254
1276
@@ -1263,6 +1285,9 @@ int HTTPClient::handleHeaderResponse()
1263
1285
DEBUG_HTTPCLIENT (" [HTTP-Client][handleHeaderResponse] RX: '%s'\n " , headerLine.c_str ());
1264
1286
1265
1287
if (headerLine.startsWith (" HTTP/1." )) {
1288
+ if (_canReuse) {
1289
+ _canReuse = (headerLine[sizeof " HTTP/1." - 1 ] != ' 0' );
1290
+ }
1266
1291
_returnCode = headerLine.substring (9 , headerLine.indexOf (' ' , 9 )).toInt ();
1267
1292
} else if (headerLine.indexOf (' :' )) {
1268
1293
String headerName = headerLine.substring (0 , headerLine.indexOf (' :' ));
@@ -1273,8 +1298,10 @@ int HTTPClient::handleHeaderResponse()
1273
1298
_size = headerValue.toInt ();
1274
1299
}
1275
1300
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
+ }
1278
1305
}
1279
1306
1280
1307
if (headerName.equalsIgnoreCase (" Transfer-Encoding" )) {
0 commit comments