@@ -334,8 +334,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
334334 return HTTPC_ERROR_SEND_HEADER_FAILED;
335335 }
336336
337- // create buffer for read
338- uint8_t * buff = (uint8_t *) malloc (HTTP_TCP_BUFFER_SIZE);
337+ size_t buff_size = HTTP_TCP_BUFFER_SIZE;
339338
340339 int len = size;
341340 int bytesWritten = 0 ;
@@ -344,6 +343,15 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
344343 len = -1 ;
345344 }
346345
346+ // if possible create smaller buffer then HTTP_TCP_BUFFER_SIZE
347+ if ((len > 0 ) && (len < HTTP_TCP_BUFFER_SIZE)) {
348+ buff_size = len;
349+ }
350+
351+ // create buffer for read
352+ uint8_t * buff = (uint8_t *) malloc (buff_size);
353+
354+
347355 if (buff) {
348356 // read all data from stream and send it to server
349357 while (connected () && stream->available () && (len > 0 || len == -1 )) {
@@ -352,7 +360,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
352360 size_t s = stream->available ();
353361
354362 if (s) {
355- int c = stream->readBytes (buff, ((s > HTTP_TCP_BUFFER_SIZE ) ? HTTP_TCP_BUFFER_SIZE : s));
363+ int c = stream->readBytes (buff, ((s > buff_size ) ? buff_size : s));
356364
357365 // write it to Stream
358366 bytesWritten += _tcp->write ((const uint8_t *) buff, c);
@@ -367,14 +375,16 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
367375 }
368376 }
369377
378+ free (buff);
379+
370380 if (size && (int ) size != bytesWritten) {
371- 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! " );
372- free (buff );
381+ DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n " , bytesWritten, _size);
382+ DEBUG_HTTPCLIENT ( " [HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED! " );
373383 return HTTPC_ERROR_SEND_PAYLOAD_FAILED;
374384 } else {
375385 DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] Stream payload written: %d\n " , bytesWritten);
376386 }
377- free (buff);
387+
378388 } else {
379389 DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE);
380390 return HTTPC_ERROR_TOO_LESS_RAM;
@@ -439,9 +449,15 @@ int HTTPClient::writeToStream(Stream * stream) {
439449 int len = _size;
440450 int bytesWritten = 0 ;
441451
452+ size_t buff_size = HTTP_TCP_BUFFER_SIZE;
453+
454+ // if possible create smaller buffer then HTTP_TCP_BUFFER_SIZE
455+ if ((len > 0 ) && (len < HTTP_TCP_BUFFER_SIZE)) {
456+ buff_size = len;
457+ }
442458
443459 // create buffer for read
444- uint8_t * buff = (uint8_t *) malloc (HTTP_TCP_BUFFER_SIZE );
460+ uint8_t * buff = (uint8_t *) malloc (buff_size );
445461
446462 if (buff) {
447463 // read all data from server
@@ -451,7 +467,7 @@ int HTTPClient::writeToStream(Stream * stream) {
451467 size_t size = _tcp->available ();
452468
453469 if (size) {
454- int c = _tcp->readBytes (buff, ((size > HTTP_TCP_BUFFER_SIZE ) ? HTTP_TCP_BUFFER_SIZE : size));
470+ int c = _tcp->readBytes (buff, ((size > buff_size ) ? buff_size : size));
455471
456472 // write it to Stream
457473 bytesWritten += stream->write (buff, c);
@@ -476,7 +492,9 @@ int HTTPClient::writeToStream(Stream * stream) {
476492
477493 } else {
478494 DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE);
495+ return HTTPC_ERROR_TOO_LESS_RAM;
479496 }
497+
480498 end ();
481499 return bytesWritten;
482500}
0 commit comments