mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-06 05:21:22 +03:00
ESP8266HTTPClient: allow getString() more than once (#5091)
fixes #4951
This commit is contained in:
parent
4fdff072e8
commit
bbaea5a358
@ -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;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -127,6 +127,8 @@ typedef enum {
|
||||
class TransportTraits;
|
||||
typedef std::unique_ptr<TransportTraits> TransportTraitsPtr;
|
||||
|
||||
class StreamString;
|
||||
|
||||
class HTTPClient
|
||||
{
|
||||
public:
|
||||
@ -185,7 +187,7 @@ public:
|
||||
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 @@ protected:
|
||||
};
|
||||
|
||||
bool beginInternal(String url, const char* expectedProtocol);
|
||||
void disconnect();
|
||||
void clear();
|
||||
int returnError(int error);
|
||||
bool connect(void);
|
||||
@ -228,6 +231,7 @@ protected:
|
||||
int _size = -1;
|
||||
bool _canReuse = false;
|
||||
transferEncoding_t _transferEncoding = HTTPC_TE_IDENTITY;
|
||||
std::unique_ptr<StreamString> _payload;
|
||||
};
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user