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
|
class TransportTraits
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
virtual ~TransportTraits()
|
virtual ~TransportTraits()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,6 +126,7 @@ void HTTPClient::clear()
|
|||||||
_returnCode = 0;
|
_returnCode = 0;
|
||||||
_size = -1;
|
_size = -1;
|
||||||
_headers = "";
|
_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
|
* called after the payload is handled
|
||||||
*/
|
*/
|
||||||
void HTTPClient::end(void)
|
void HTTPClient::end(void)
|
||||||
|
{
|
||||||
|
disconnect();
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* disconnect
|
||||||
|
* close the TCP socket
|
||||||
|
*/
|
||||||
|
void HTTPClient::disconnect()
|
||||||
{
|
{
|
||||||
if(connected()) {
|
if(connected()) {
|
||||||
if(_tcp->available() > 0) {
|
if(_tcp->available() > 0) {
|
||||||
@ -734,7 +745,7 @@ int HTTPClient::writeToStream(Stream * stream)
|
|||||||
return returnError(HTTPC_ERROR_ENCODING);
|
return returnError(HTTPC_ERROR_ENCODING);
|
||||||
}
|
}
|
||||||
|
|
||||||
end();
|
disconnect();
|
||||||
return ret;
|
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 all payload as String (may need lot of ram or trigger out of memory!)
|
||||||
* @return String
|
* @return String
|
||||||
*/
|
*/
|
||||||
String HTTPClient::getString(void)
|
const String& HTTPClient::getString(void)
|
||||||
{
|
{
|
||||||
StreamString sstring;
|
if (_payload) {
|
||||||
|
return *_payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
_payload.reset(new StreamString());
|
||||||
|
|
||||||
if(_size) {
|
if(_size) {
|
||||||
// try to reserve needed memmory
|
// 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));
|
DEBUG_HTTPCLIENT("[HTTP-Client][getString] not enough memory to reserve a string! need: %d\n", (_size + 1));
|
||||||
return "";
|
return *_payload;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
writeToStream(&sstring);
|
writeToStream(_payload.get());
|
||||||
return sstring;
|
return *_payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -127,6 +127,8 @@ typedef enum {
|
|||||||
class TransportTraits;
|
class TransportTraits;
|
||||||
typedef std::unique_ptr<TransportTraits> TransportTraitsPtr;
|
typedef std::unique_ptr<TransportTraits> TransportTraitsPtr;
|
||||||
|
|
||||||
|
class StreamString;
|
||||||
|
|
||||||
class HTTPClient
|
class HTTPClient
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@ -185,7 +187,7 @@ public:
|
|||||||
WiFiClient& getStream(void);
|
WiFiClient& getStream(void);
|
||||||
WiFiClient* getStreamPtr(void);
|
WiFiClient* getStreamPtr(void);
|
||||||
int writeToStream(Stream* stream);
|
int writeToStream(Stream* stream);
|
||||||
String getString(void);
|
const String& getString(void);
|
||||||
|
|
||||||
static String errorToString(int error);
|
static String errorToString(int error);
|
||||||
|
|
||||||
@ -196,6 +198,7 @@ protected:
|
|||||||
};
|
};
|
||||||
|
|
||||||
bool beginInternal(String url, const char* expectedProtocol);
|
bool beginInternal(String url, const char* expectedProtocol);
|
||||||
|
void disconnect();
|
||||||
void clear();
|
void clear();
|
||||||
int returnError(int error);
|
int returnError(int error);
|
||||||
bool connect(void);
|
bool connect(void);
|
||||||
@ -228,6 +231,7 @@ protected:
|
|||||||
int _size = -1;
|
int _size = -1;
|
||||||
bool _canReuse = false;
|
bool _canReuse = false;
|
||||||
transferEncoding_t _transferEncoding = HTTPC_TE_IDENTITY;
|
transferEncoding_t _transferEncoding = HTTPC_TE_IDENTITY;
|
||||||
|
std::unique_ptr<StreamString> _payload;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user