From 351ffad3161ed8ef968e0910ff7f3c8d773f1de9 Mon Sep 17 00:00:00 2001 From: Junxiao Shi Date: Sat, 8 Sep 2018 21:54:34 -0400 Subject: [PATCH] ESP8266HTTPClient: allow getString() more than once fixes #4951 --- .../src/ESP8266HTTPClient.cpp | 31 ++++++++++++++----- .../ESP8266HTTPClient/src/ESP8266HTTPClient.h | 6 +++- 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 28a3503484..f04a1d7043 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -33,7 +33,7 @@ class TransportTraits { public: - virtual ~TransportTraits() + virtual ~TransportTraits() { } @@ -126,6 +126,7 @@ void HTTPClient::clear() _returnCode = 0; _size = -1; _headers = ""; + _payload.reset(); } @@ -281,6 +282,16 @@ bool HTTPClient::begin(String host, uint16_t port, String uri, const uint8_t htt * called after the payload is handled */ void HTTPClient::end(void) +{ + disconnect(); + clear(); +} + +/** + * disconnect + * close the TCP socket + */ +void HTTPClient::disconnect() { if(connected()) { if(_tcp->available() > 0) { @@ -734,7 +745,7 @@ int HTTPClient::writeToStream(Stream * stream) return returnError(HTTPC_ERROR_ENCODING); } - end(); + disconnect(); return ret; } @@ -742,20 +753,24 @@ int HTTPClient::writeToStream(Stream * stream) * return all payload as String (may need lot of ram or trigger out of memory!) * @return String */ -String HTTPClient::getString(void) +const String& HTTPClient::getString(void) { - StreamString sstring; + if (_payload) { + return *_payload; + } + + _payload.reset(new StreamString()); if(_size) { // try to reserve needed memmory - if(!sstring.reserve((_size + 1))) { + if(!_payload->reserve((_size + 1))) { DEBUG_HTTPCLIENT("[HTTP-Client][getString] not enough memory to reserve a string! need: %d\n", (_size + 1)); - return ""; + return *_payload; } } - writeToStream(&sstring); - return sstring; + writeToStream(_payload.get()); + return *_payload; } /** diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h index 72b42853f7..c0332ffeda 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h @@ -127,6 +127,8 @@ typedef enum { class TransportTraits; typedef std::unique_ptr TransportTraitsPtr; +class StreamString; + class HTTPClient { public: @@ -185,7 +187,7 @@ class HTTPClient WiFiClient& getStream(void); WiFiClient* getStreamPtr(void); int writeToStream(Stream* stream); - String getString(void); + const String& getString(void); static String errorToString(int error); @@ -196,6 +198,7 @@ class HTTPClient }; bool beginInternal(String url, const char* expectedProtocol); + void disconnect(); void clear(); int returnError(int error); bool connect(void); @@ -228,6 +231,7 @@ class HTTPClient int _size = -1; bool _canReuse = false; transferEncoding_t _transferEncoding = HTTPC_TE_IDENTITY; + std::unique_ptr _payload; };