Make sure that HTTPClient disconnects from the old server if redirecting to new one

Fixes: https://github.com/espressif/arduino-esp32/issues/4763
This commit is contained in:
me-no-dev 2021-02-04 02:42:44 +02:00
parent ad4cf1461b
commit 6f23cd5988

View File

@ -269,13 +269,20 @@ bool HTTPClient::beginInternal(String url, const char* expectedProtocol)
// get port // get port
index = host.indexOf(':'); index = host.indexOf(':');
String the_host;
if(index >= 0) { if(index >= 0) {
_host = host.substring(0, index); // hostname the_host = host.substring(0, index); // hostname
host.remove(0, (index + 1)); // remove hostname + : host.remove(0, (index + 1)); // remove hostname + :
_port = host.toInt(); // get port _port = host.toInt(); // get port
} else { } else {
_host = host; the_host = host;
} }
if(_host != the_host && connected()){
log_d("switching host from '%s' to '%s'. disconnecting first", _host.c_str(), the_host.c_str());
_canReuse = false;
disconnect(true);
}
_host = the_host;
_uri = url; _uri = url;
log_d("host: %s port: %d url: %s", _host.c_str(), _port, _uri.c_str()); log_d("host: %s port: %d url: %s", _host.c_str(), _port, _uri.c_str());
return true; return true;
@ -1318,9 +1325,9 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size)
readBytes = buff_size; readBytes = buff_size;
} }
// stop if no more reading // stop if no more reading
if (readBytes == 0) if (readBytes == 0)
break; break;
// read data // read data
int bytesRead = _client->readBytes(buff, readBytes); int bytesRead = _client->readBytes(buff, readBytes);