3030
3131#include " ESP8266HTTPClient.h"
3232
33-
3433/* *
3534 * constractor
3635 */
@@ -117,7 +116,7 @@ void HTTPClient::begin(String url, String httpsFingerprint) {
117116 if (index >= 0 ) {
118117 // auth info
119118 String auth = host.substring (0 , index);
120- host.remove (0 , index +1 ); // remove auth part including @
119+ host.remove (0 , index + 1 ); // remove auth part including @
121120 _base64Authorization = base64::encode (auth);
122121 }
123122
@@ -335,8 +334,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
335334 return HTTPC_ERROR_SEND_HEADER_FAILED;
336335 }
337336
338- // create buffer for read
339- uint8_t buff[1460 ] = { 0 };
337+ size_t buff_size = HTTP_TCP_BUFFER_SIZE;
340338
341339 int len = size;
342340 int bytesWritten = 0 ;
@@ -345,34 +343,51 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
345343 len = -1 ;
346344 }
347345
348- // read all data from stream and send it to server
349- while (connected () && stream->available () && (len > 0 || len == -1 )) {
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+
355+ if (buff) {
356+ // read all data from stream and send it to server
357+ while (connected () && stream->available () && (len > 0 || len == -1 )) {
350358
351- // get available data size
352- size_t s = stream->available ();
359+ // get available data size
360+ size_t s = stream->available ();
353361
354- if (s) {
355- int c = stream->readBytes (buff, ((s > sizeof (buff)) ? sizeof (buff) : s));
362+ if (s) {
363+ int c = stream->readBytes (buff, ((s > buff_size) ? buff_size : s));
356364
357- // write it to Stream
358- bytesWritten += _tcp->write ((const uint8_t *)buff, c);
365+ // write it to Stream
366+ bytesWritten += _tcp->write ((const uint8_t *) buff, c);
359367
360- if (len > 0 ) {
361- len -= c;
368+ if (len > 0 ) {
369+ len -= c;
370+ }
371+
372+ delay (0 );
373+ } else {
374+ delay (1 );
362375 }
376+ }
363377
364- delay (0 );
378+ free (buff);
379+
380+ if (size && (int ) size != bytesWritten) {
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!" );
383+ return HTTPC_ERROR_SEND_PAYLOAD_FAILED;
365384 } else {
366- delay ( 1 );
385+ DEBUG_HTTPCLIENT ( " [HTTP-Client][sendRequest] Stream payload written: %d \n " , bytesWritten );
367386 }
368- }
369387
370- if (size && (int )size != bytesWritten) {
371- DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n " , bytesWritten, _size);
372- DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!" );
373- return HTTPC_ERROR_SEND_PAYLOAD_FAILED;
374388 } else {
375- DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] Stream payload written: %d\n " , bytesWritten);
389+ DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE);
390+ return HTTPC_ERROR_TOO_LESS_RAM;
376391 }
377392
378393 // handle Server Response (Header)
@@ -434,35 +449,50 @@ int HTTPClient::writeToStream(Stream * stream) {
434449 int len = _size;
435450 int bytesWritten = 0 ;
436451
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+ }
458+
437459 // create buffer for read
438- uint8_t buff[ 1460 ] = { 0 } ;
460+ uint8_t * buff = ( uint8_t *) malloc (buff_size) ;
439461
440- // read all data from server
441- while (connected () && (len > 0 || len == -1 )) {
462+ if (buff) {
463+ // read all data from server
464+ while (connected () && (len > 0 || len == -1 )) {
442465
443- // get available data size
444- size_t size = _tcp->available ();
466+ // get available data size
467+ size_t size = _tcp->available ();
445468
446- if (size) {
447- int c = _tcp->readBytes (buff, ((size > sizeof (buff)) ? sizeof (buff) : size));
469+ if (size) {
470+ int c = _tcp->readBytes (buff, ((size > buff_size) ? buff_size : size));
448471
449- // write it to Stream
450- bytesWritten += stream->write (buff, c);
472+ // write it to Stream
473+ bytesWritten += stream->write (buff, c);
451474
452- if (len > 0 ) {
453- len -= c;
454- }
475+ if (len > 0 ) {
476+ len -= c;
477+ }
455478
456- delay (0 );
457- } else {
458- delay (1 );
479+ delay (0 );
480+ } else {
481+ delay (1 );
482+ }
459483 }
460- }
461484
462- DEBUG_HTTPCLIENT ( " [HTTP-Client][writeToStream] connection closed or file end (written: %d). \n " , bytesWritten );
485+ free (buff );
463486
464- if (_size && _size != bytesWritten) {
465- DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] bytesWritten %d and size %d mismatch!.\n " , bytesWritten, _size);
487+ DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] connection closed or file end (written: %d).\n " , bytesWritten);
488+
489+ if (_size && _size != bytesWritten) {
490+ DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] bytesWritten %d and size %d mismatch!.\n " , bytesWritten, _size);
491+ }
492+
493+ } else {
494+ DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE);
495+ return HTTPC_ERROR_TOO_LESS_RAM;
466496 }
467497
468498 end ();
@@ -509,12 +539,13 @@ String HTTPClient::errorToString(int error) {
509539 return String (" no stream" );
510540 case HTTPC_ERROR_NO_HTTP_SERVER:
511541 return String (" no HTTP server" );
542+ case HTTPC_ERROR_TOO_LESS_RAM:
543+ return String (" too less ram" );
512544 default :
513545 return String ();
514546 }
515547}
516548
517-
518549/* *
519550 * adds Header to the request
520551 * @param name
0 commit comments