mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-21 10:26:06 +03:00
only malloc needed ram if we know the response size and its less then 1460
This commit is contained in:
parent
b8769bf5eb
commit
b9d0807a56
@ -334,8 +334,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
|
|||||||
return HTTPC_ERROR_SEND_HEADER_FAILED;
|
return HTTPC_ERROR_SEND_HEADER_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
// create buffer for read
|
size_t buff_size = HTTP_TCP_BUFFER_SIZE;
|
||||||
uint8_t * buff = (uint8_t *) malloc(HTTP_TCP_BUFFER_SIZE);
|
|
||||||
|
|
||||||
int len = size;
|
int len = size;
|
||||||
int bytesWritten = 0;
|
int bytesWritten = 0;
|
||||||
@ -344,6 +343,15 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
|
|||||||
len = -1;
|
len = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if possible create smaller buffer then HTTP_TCP_BUFFER_SIZE
|
||||||
|
if((len > 0) && (len < HTTP_TCP_BUFFER_SIZE)) {
|
||||||
|
buff_size = len;
|
||||||
|
}
|
||||||
|
|
||||||
|
// create buffer for read
|
||||||
|
uint8_t * buff = (uint8_t *) malloc(buff_size);
|
||||||
|
|
||||||
|
|
||||||
if(buff) {
|
if(buff) {
|
||||||
// read all data from stream and send it to server
|
// read all data from stream and send it to server
|
||||||
while(connected() && stream->available() && (len > 0 || len == -1)) {
|
while(connected() && stream->available() && (len > 0 || len == -1)) {
|
||||||
@ -352,7 +360,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
|
|||||||
size_t s = stream->available();
|
size_t s = stream->available();
|
||||||
|
|
||||||
if(s) {
|
if(s) {
|
||||||
int c = stream->readBytes(buff, ((s > HTTP_TCP_BUFFER_SIZE) ? HTTP_TCP_BUFFER_SIZE : s));
|
int c = stream->readBytes(buff, ((s > buff_size) ? buff_size : s));
|
||||||
|
|
||||||
// write it to Stream
|
// write it to Stream
|
||||||
bytesWritten += _tcp->write((const uint8_t *) buff, c);
|
bytesWritten += _tcp->write((const uint8_t *) buff, c);
|
||||||
@ -367,14 +375,16 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(size && (int) size != bytesWritten) {
|
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, _size); DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!");
|
|
||||||
free(buff);
|
free(buff);
|
||||||
|
|
||||||
|
if(size && (int) size != bytesWritten) {
|
||||||
|
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, _size);
|
||||||
|
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!");
|
||||||
return HTTPC_ERROR_SEND_PAYLOAD_FAILED;
|
return HTTPC_ERROR_SEND_PAYLOAD_FAILED;
|
||||||
} else {
|
} else {
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten);
|
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten);
|
||||||
}
|
}
|
||||||
free(buff);
|
|
||||||
} else {
|
} else {
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE);
|
DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE);
|
||||||
return HTTPC_ERROR_TOO_LESS_RAM;
|
return HTTPC_ERROR_TOO_LESS_RAM;
|
||||||
@ -439,9 +449,15 @@ int HTTPClient::writeToStream(Stream * stream) {
|
|||||||
int len = _size;
|
int len = _size;
|
||||||
int bytesWritten = 0;
|
int bytesWritten = 0;
|
||||||
|
|
||||||
|
size_t buff_size = HTTP_TCP_BUFFER_SIZE;
|
||||||
|
|
||||||
|
// if possible create smaller buffer then HTTP_TCP_BUFFER_SIZE
|
||||||
|
if((len > 0) && (len < HTTP_TCP_BUFFER_SIZE)) {
|
||||||
|
buff_size = len;
|
||||||
|
}
|
||||||
|
|
||||||
// create buffer for read
|
// create buffer for read
|
||||||
uint8_t * buff = (uint8_t *) malloc(HTTP_TCP_BUFFER_SIZE);
|
uint8_t * buff = (uint8_t *) malloc(buff_size);
|
||||||
|
|
||||||
if(buff) {
|
if(buff) {
|
||||||
// read all data from server
|
// read all data from server
|
||||||
@ -451,7 +467,7 @@ int HTTPClient::writeToStream(Stream * stream) {
|
|||||||
size_t size = _tcp->available();
|
size_t size = _tcp->available();
|
||||||
|
|
||||||
if(size) {
|
if(size) {
|
||||||
int c = _tcp->readBytes(buff, ((size > HTTP_TCP_BUFFER_SIZE) ? HTTP_TCP_BUFFER_SIZE : size));
|
int c = _tcp->readBytes(buff, ((size > buff_size) ? buff_size : size));
|
||||||
|
|
||||||
// write it to Stream
|
// write it to Stream
|
||||||
bytesWritten += stream->write(buff, c);
|
bytesWritten += stream->write(buff, c);
|
||||||
@ -476,7 +492,9 @@ int HTTPClient::writeToStream(Stream * stream) {
|
|||||||
|
|
||||||
} else {
|
} else {
|
||||||
DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE);
|
DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE);
|
||||||
|
return HTTPC_ERROR_TOO_LESS_RAM;
|
||||||
}
|
}
|
||||||
|
|
||||||
end();
|
end();
|
||||||
return bytesWritten;
|
return bytesWritten;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user