1+ #include < HardwareSerial.h>
12/* *
23 * HTTPClient.cpp
34 *
@@ -195,6 +196,11 @@ bool HTTPClient::begin(String url, const char* CAcert)
195196 }
196197 _secure = true ;
197198 _transportTraits = TransportTraitsPtr (new TLSTraits (CAcert));
199+ if (!_transportTraits) {
200+ log_e (" could not create transport traits" );
201+ return false ;
202+ }
203+
198204 return true ;
199205}
200206
@@ -215,6 +221,11 @@ bool HTTPClient::begin(String url)
215221 return begin (url, (const char *)NULL );
216222 }
217223 _transportTraits = TransportTraitsPtr (new TransportTraits ());
224+ if (!_transportTraits) {
225+ log_e (" could not create transport traits" );
226+ return false ;
227+ }
228+
218229 return true ;
219230}
220231#endif // HTTPCLIENT_1_1_COMPATIBLE
@@ -333,7 +344,8 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const char* CAcer
333344 */
334345void HTTPClient::end (void )
335346{
336- disconnect ();
347+ disconnect (false );
348+ clear ();
337349}
338350
339351
@@ -342,7 +354,7 @@ void HTTPClient::end(void)
342354 * disconnect
343355 * close the TCP socket
344356 */
345- void HTTPClient::disconnect ()
357+ void HTTPClient::disconnect (bool preserveClient )
346358{
347359 if (connected ()) {
348360 if (_client->available () > 0 ) {
@@ -357,7 +369,9 @@ void HTTPClient::disconnect()
357369 } else {
358370 log_d (" tcp stop\n " );
359371 _client->stop ();
360- _client = nullptr ;
372+ if (!preserveClient) {
373+ _client = nullptr ;
374+ }
361375#ifdef HTTPCLIENT_1_1_COMPATIBLE
362376 if (_tcpDeprecated) {
363377 _transportTraits.reset (nullptr );
@@ -456,6 +470,7 @@ void HTTPClient::setTimeout(uint16_t timeout)
456470void HTTPClient::useHTTP10 (bool useHTTP10)
457471{
458472 _useHTTP10 = useHTTP10;
473+ _reuse = !useHTTP10;
459474}
460475
461476/* *
@@ -816,7 +831,8 @@ int HTTPClient::writeToStream(Stream * stream)
816831 return returnError (HTTPC_ERROR_ENCODING);
817832 }
818833
819- end ();
834+ // end();
835+ disconnect (true );
820836 return ret;
821837}
822838
@@ -970,18 +986,25 @@ bool HTTPClient::hasHeader(const char* name)
970986 */
971987bool HTTPClient::connect (void )
972988{
973-
974989 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+ }
976995 while (_client->available () > 0 ) {
977996 _client->read ();
978997 }
979998 return true ;
980999 }
9811000
9821001#ifdef HTTPCLIENT_1_1_COMPATIBLE
983- if (!_client) {
1002+ if (_transportTraits && !_client) {
9841003 _tcpDeprecated = _transportTraits->create ();
1004+ if (!_tcpDeprecated) {
1005+ log_e (" failed to create client" );
1006+ return false ;
1007+ }
9851008 _client = _tcpDeprecated.get ();
9861009 }
9871010#endif
@@ -1080,11 +1103,12 @@ int HTTPClient::handleHeaderResponse()
10801103 return HTTPC_ERROR_NOT_CONNECTED;
10811104 }
10821105
1083- _canReuse = !_useHTTP10;
1106+ clear ();
1107+
1108+ _canReuse = _reuse;
10841109
10851110 String transferEncoding;
1086- _returnCode = -1 ;
1087- _size = -1 ;
1111+
10881112 _transferEncoding = HTTPC_TE_IDENTITY;
10891113 unsigned long lastDataTime = millis ();
10901114
@@ -1099,8 +1123,10 @@ int HTTPClient::handleHeaderResponse()
10991123 log_v (" RX: '%s'" , headerLine.c_str ());
11001124
11011125 if (headerLine.startsWith (" HTTP/1." )) {
1126+ if (_canReuse) {
1127+ _canReuse = (headerLine[sizeof " HTTP/1." - 1 ] != ' 0' );
1128+ }
11021129 _returnCode = headerLine.substring (9 , headerLine.indexOf (' ' , 9 )).toInt ();
1103- _canReuse = (_returnCode != ' 0' );
11041130 } else if (headerLine.indexOf (' :' )) {
11051131 String headerName = headerLine.substring (0 , headerLine.indexOf (' :' ));
11061132 String headerValue = headerLine.substring (headerLine.indexOf (' :' ) + 1 );
@@ -1110,8 +1136,10 @@ int HTTPClient::handleHeaderResponse()
11101136 _size = headerValue.toInt ();
11111137 }
11121138
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+ }
11151143 }
11161144
11171145 if (headerName.equalsIgnoreCase (" Transfer-Encoding" )) {
0 commit comments