From 6743187f59c7c021860f609abcc5ff18a5ea2d80 Mon Sep 17 00:00:00 2001 From: trandi Date: Sat, 3 Oct 2020 11:33:12 +0100 Subject: [PATCH] Fix support for following redirects added by ee88c42c3b5e87a2fc09303f9e82dd8172e99990 (#4240) --- libraries/HTTPClient/src/HTTPClient.cpp | 26 ++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/libraries/HTTPClient/src/HTTPClient.cpp b/libraries/HTTPClient/src/HTTPClient.cpp index 0383c149597..0d84a6d61ff 100644 --- a/libraries/HTTPClient/src/HTTPClient.cpp +++ b/libraries/HTTPClient/src/HTTPClient.cpp @@ -919,21 +919,19 @@ int HTTPClient::writeToStream(Stream * stream) */ String HTTPClient::getString(void) { - StreamString sstring; - - if(_size > 0) { - // try to reserve needed memmory - if(!sstring.reserve((_size + 1))) { + // _size can be -1 when Server sends no Content-Length header + if(_size > 0 || _size == -1) { + StreamString sstring; + // try to reserve needed memory (noop if _size == -1) + if(sstring.reserve((_size + 1))) { + writeToStream(&sstring); + return sstring; + } else { log_d("not enough memory to reserve a string! need: %d", (_size + 1)); - return ""; } } - else { - return ""; - } - writeToStream(&sstring); - return sstring; + return ""; } /** @@ -1441,8 +1439,10 @@ bool HTTPClient::setURL(const String& url) _port = (_protocol == "https" ? 443 : 80); } - // disconnect but preserve _client (clear _canReuse so disconnect will close the connection) - _canReuse = false; + // disconnect but preserve _client. + // Also have to keep the connection otherwise it will free some of the memory used by _client + // and will blow up later when trying to do _client->available() or similar + _canReuse = true; disconnect(true); return beginInternal(url, _protocol.c_str()); }