Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6f23cd5

Browse files
committed
Make sure that HTTPClient disconnects from the old server if redirecting to new one
Fixes: #4763
1 parent ad4cf14 commit 6f23cd5

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

libraries/HTTPClient/src/HTTPClient.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,20 @@ bool HTTPClient::beginInternal(String url, const char* expectedProtocol)
269269

270270
// get port
271271
index = host.indexOf(':');
272+
String the_host;
272273
if(index >= 0) {
273-
_host = host.substring(0, index); // hostname
274+
the_host = host.substring(0, index); // hostname
274275
host.remove(0, (index + 1)); // remove hostname + :
275276
_port = host.toInt(); // get port
276277
} else {
277-
_host = host;
278+
the_host = host;
278279
}
280+
if(_host != the_host && connected()){
281+
log_d("switching host from '%s' to '%s'. disconnecting first", _host.c_str(), the_host.c_str());
282+
_canReuse = false;
283+
disconnect(true);
284+
}
285+
_host = the_host;
279286
_uri = url;
280287
log_d("host: %s port: %d url: %s", _host.c_str(), _port, _uri.c_str());
281288
return true;
@@ -1318,9 +1325,9 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size)
13181325
readBytes = buff_size;
13191326
}
13201327

1321-
// stop if no more reading
1322-
if (readBytes == 0)
1323-
break;
1328+
// stop if no more reading
1329+
if (readBytes == 0)
1330+
break;
13241331

13251332
// read data
13261333
int bytesRead = _client->readBytes(buff, readBytes);

0 commit comments

Comments
 (0)