Skip to content

HTTP client move stream buffer the heap #1226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ void loop() {
}

http.end();

}

delay(1000);
Expand Down
115 changes: 73 additions & 42 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

#include "ESP8266HTTPClient.h"


/**
* constractor
*/
Expand Down Expand Up @@ -117,7 +116,7 @@ void HTTPClient::begin(String url, String httpsFingerprint) {
if(index >= 0) {
// auth info
String auth = host.substring(0, index);
host.remove(0, index +1); // remove auth part including @
host.remove(0, index + 1); // remove auth part including @
_base64Authorization = base64::encode(auth);
}

Expand Down Expand Up @@ -335,8 +334,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
return HTTPC_ERROR_SEND_HEADER_FAILED;
}

// create buffer for read
uint8_t buff[1460] = { 0 };
size_t buff_size = HTTP_TCP_BUFFER_SIZE;

int len = size;
int bytesWritten = 0;
Expand All @@ -345,34 +343,51 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
len = -1;
}

// read all data from stream and send it to server
while(connected() && stream->available() && (len > 0 || 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) {
// read all data from stream and send it to server
while(connected() && stream->available() && (len > 0 || len == -1)) {

// get available data size
size_t s = stream->available();
// get available data size
size_t s = stream->available();

if(s) {
int c = stream->readBytes(buff, ((s > sizeof(buff)) ? sizeof(buff) : s));
if(s) {
int c = stream->readBytes(buff, ((s > buff_size) ? buff_size : s));

// write it to Stream
bytesWritten += _tcp->write((const uint8_t *)buff, c);
// write it to Stream
bytesWritten += _tcp->write((const uint8_t *) buff, c);

if(len > 0) {
len -= c;
if(len > 0) {
len -= c;
}

delay(0);
} else {
delay(1);
}
}

delay(0);
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;
} else {
delay(1);
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten);
}
}

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;
} else {
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten);
DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE);
return HTTPC_ERROR_TOO_LESS_RAM;
}

// handle Server Response (Header)
Expand Down Expand Up @@ -434,35 +449,50 @@ int HTTPClient::writeToStream(Stream * stream) {
int len = _size;
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
uint8_t buff[1460] = { 0 };
uint8_t * buff = (uint8_t *) malloc(buff_size);

// read all data from server
while(connected() && (len > 0 || len == -1)) {
if(buff) {
// read all data from server
while(connected() && (len > 0 || len == -1)) {

// get available data size
size_t size = _tcp->available();
// get available data size
size_t size = _tcp->available();

if(size) {
int c = _tcp->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
if(size) {
int c = _tcp->readBytes(buff, ((size > buff_size) ? buff_size : size));

// write it to Stream
bytesWritten += stream->write(buff, c);
// write it to Stream
bytesWritten += stream->write(buff, c);

if(len > 0) {
len -= c;
}
if(len > 0) {
len -= c;
}

delay(0);
} else {
delay(1);
delay(0);
} else {
delay(1);
}
}
}

DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] connection closed or file end (written: %d).\n", bytesWritten);
free(buff);

if(_size && _size != bytesWritten) {
DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] bytesWritten %d and size %d mismatch!.\n", bytesWritten, _size);
DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] connection closed or file end (written: %d).\n", bytesWritten);

if(_size && _size != bytesWritten) {
DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] bytesWritten %d and size %d mismatch!.\n", bytesWritten, _size);
}

} else {
DEBUG_HTTPCLIENT("[HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE);
return HTTPC_ERROR_TOO_LESS_RAM;
}

end();
Expand Down Expand Up @@ -509,12 +539,13 @@ String HTTPClient::errorToString(int error) {
return String("no stream");
case HTTPC_ERROR_NO_HTTP_SERVER:
return String("no HTTP server");
case HTTPC_ERROR_TOO_LESS_RAM:
return String("too less ram");
default:
return String();
}
}


/**
* adds Header to the request
* @param name
Expand Down
4 changes: 4 additions & 0 deletions libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
#define HTTPC_ERROR_CONNECTION_LOST (-5)
#define HTTPC_ERROR_NO_STREAM (-6)
#define HTTPC_ERROR_NO_HTTP_SERVER (-7)
#define HTTPC_ERROR_TOO_LESS_RAM (-8)

/// size for the stream handling
#define HTTP_TCP_BUFFER_SIZE (1460)

/// HTTP codes see RFC7231
typedef enum {
Expand Down