From 6f23cd5988aad7f18807e15a691690e6b03ef1cd Mon Sep 17 00:00:00 2001 From: me-no-dev Date: Thu, 4 Feb 2021 02:42:44 +0200 Subject: [PATCH] Make sure that HTTPClient disconnects from the old server if redirecting to new one Fixes: https://github.com/espressif/arduino-esp32/issues/4763 --- libraries/HTTPClient/src/HTTPClient.cpp | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/libraries/HTTPClient/src/HTTPClient.cpp b/libraries/HTTPClient/src/HTTPClient.cpp index 6d02120d..2d95446e 100644 --- a/libraries/HTTPClient/src/HTTPClient.cpp +++ b/libraries/HTTPClient/src/HTTPClient.cpp @@ -269,13 +269,20 @@ bool HTTPClient::beginInternal(String url, const char* expectedProtocol) // get port index = host.indexOf(':'); + String the_host; if(index >= 0) { - _host = host.substring(0, index); // hostname + the_host = host.substring(0, index); // hostname host.remove(0, (index + 1)); // remove hostname + : _port = host.toInt(); // get port } 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; log_d("host: %s port: %d url: %s", _host.c_str(), _port, _uri.c_str()); return true; @@ -1318,9 +1325,9 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size) readBytes = buff_size; } - // stop if no more reading - if (readBytes == 0) - break; + // stop if no more reading + if (readBytes == 0) + break; // read data int bytesRead = _client->readBytes(buff, readBytes);