1
+ #include < HardwareSerial.h>
1
2
/* *
2
3
* HTTPClient.cpp
3
4
*
@@ -195,6 +196,11 @@ bool HTTPClient::begin(String url, const char* CAcert)
195
196
}
196
197
_secure = true ;
197
198
_transportTraits = TransportTraitsPtr (new TLSTraits (CAcert));
199
+ if (!_transportTraits) {
200
+ log_e (" could not create transport traits" );
201
+ return false ;
202
+ }
203
+
198
204
return true ;
199
205
}
200
206
@@ -215,6 +221,11 @@ bool HTTPClient::begin(String url)
215
221
return begin (url, (const char *)NULL );
216
222
}
217
223
_transportTraits = TransportTraitsPtr (new TransportTraits ());
224
+ if (!_transportTraits) {
225
+ log_e (" could not create transport traits" );
226
+ return false ;
227
+ }
228
+
218
229
return true ;
219
230
}
220
231
#endif // HTTPCLIENT_1_1_COMPATIBLE
@@ -333,7 +344,8 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const char* CAcer
333
344
*/
334
345
void HTTPClient::end (void )
335
346
{
336
- disconnect ();
347
+ disconnect (false );
348
+ clear ();
337
349
}
338
350
339
351
@@ -342,7 +354,7 @@ void HTTPClient::end(void)
342
354
* disconnect
343
355
* close the TCP socket
344
356
*/
345
- void HTTPClient::disconnect ()
357
+ void HTTPClient::disconnect (bool preserveClient )
346
358
{
347
359
if (connected ()) {
348
360
if (_client->available () > 0 ) {
@@ -357,7 +369,9 @@ void HTTPClient::disconnect()
357
369
} else {
358
370
log_d (" tcp stop\n " );
359
371
_client->stop ();
360
- _client = nullptr ;
372
+ if (!preserveClient) {
373
+ _client = nullptr ;
374
+ }
361
375
#ifdef HTTPCLIENT_1_1_COMPATIBLE
362
376
if (_tcpDeprecated) {
363
377
_transportTraits.reset (nullptr );
@@ -456,6 +470,7 @@ void HTTPClient::setTimeout(uint16_t timeout)
456
470
void HTTPClient::useHTTP10 (bool useHTTP10)
457
471
{
458
472
_useHTTP10 = useHTTP10;
473
+ _reuse = !useHTTP10;
459
474
}
460
475
461
476
/* *
@@ -816,7 +831,8 @@ int HTTPClient::writeToStream(Stream * stream)
816
831
return returnError (HTTPC_ERROR_ENCODING);
817
832
}
818
833
819
- end ();
834
+ // end();
835
+ disconnect (true );
820
836
return ret;
821
837
}
822
838
@@ -970,18 +986,25 @@ bool HTTPClient::hasHeader(const char* name)
970
986
*/
971
987
bool HTTPClient::connect (void )
972
988
{
973
-
974
989
if (connected ()) {
975
- log_d (" already connected, try reuse!" );
990
+ if (_reuse) {
991
+ log_d (" already connected, reusing connection" );
992
+ } else {
993
+ log_d (" already connected, try reuse!" );
994
+ }
976
995
while (_client->available () > 0 ) {
977
996
_client->read ();
978
997
}
979
998
return true ;
980
999
}
981
1000
982
1001
#ifdef HTTPCLIENT_1_1_COMPATIBLE
983
- if (!_client) {
1002
+ if (_transportTraits && !_client) {
984
1003
_tcpDeprecated = _transportTraits->create ();
1004
+ if (!_tcpDeprecated) {
1005
+ log_e (" failed to create client" );
1006
+ return false ;
1007
+ }
985
1008
_client = _tcpDeprecated.get ();
986
1009
}
987
1010
#endif
@@ -1080,11 +1103,12 @@ int HTTPClient::handleHeaderResponse()
1080
1103
return HTTPC_ERROR_NOT_CONNECTED;
1081
1104
}
1082
1105
1083
- _canReuse = !_useHTTP10;
1106
+ clear ();
1107
+
1108
+ _canReuse = _reuse;
1084
1109
1085
1110
String transferEncoding;
1086
- _returnCode = -1 ;
1087
- _size = -1 ;
1111
+
1088
1112
_transferEncoding = HTTPC_TE_IDENTITY;
1089
1113
unsigned long lastDataTime = millis ();
1090
1114
@@ -1099,8 +1123,10 @@ int HTTPClient::handleHeaderResponse()
1099
1123
log_v (" RX: '%s'" , headerLine.c_str ());
1100
1124
1101
1125
if (headerLine.startsWith (" HTTP/1." )) {
1126
+ if (_canReuse) {
1127
+ _canReuse = (headerLine[sizeof " HTTP/1." - 1 ] != ' 0' );
1128
+ }
1102
1129
_returnCode = headerLine.substring (9 , headerLine.indexOf (' ' , 9 )).toInt ();
1103
- _canReuse = (_returnCode != ' 0' );
1104
1130
} else if (headerLine.indexOf (' :' )) {
1105
1131
String headerName = headerLine.substring (0 , headerLine.indexOf (' :' ));
1106
1132
String headerValue = headerLine.substring (headerLine.indexOf (' :' ) + 1 );
@@ -1110,8 +1136,10 @@ int HTTPClient::handleHeaderResponse()
1110
1136
_size = headerValue.toInt ();
1111
1137
}
1112
1138
1113
- if (headerName.equalsIgnoreCase (" Connection" )) {
1114
- _canReuse = headerValue.equalsIgnoreCase (" keep-alive" );
1139
+ if (_canReuse && headerName.equalsIgnoreCase (" Connection" )) {
1140
+ if (headerValue.indexOf (" close" ) >= 0 && headerValue.indexOf (" keep-alive" ) < 0 ) {
1141
+ _canReuse = false ;
1142
+ }
1115
1143
}
1116
1144
1117
1145
if (headerName.equalsIgnoreCase (" Transfer-Encoding" )) {
0 commit comments