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
@@ -336,7 +335,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
336335 }
337336
338337 // create buffer for read
339- uint8_t buff[ 1460 ] = { 0 } ;
338+ uint8_t * buff = ( uint8_t *) malloc (HTTP_TCP_BUFFER_SIZE) ;
340339
341340 int len = size;
342341 int bytesWritten = 0 ;
@@ -345,34 +344,40 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
345344 len = -1 ;
346345 }
347346
348- // read all data from stream and send it to server
349- while (connected () && stream->available () && (len > 0 || len == -1 )) {
347+ if (buff) {
348+ // read all data from stream and send it to server
349+ while (connected () && stream->available () && (len > 0 || len == -1 )) {
350+
351+ // get available data size
352+ size_t s = stream->available ();
350353
351- // get available data size
352- size_t s = stream->available ( );
354+ if (s) {
355+ int c = stream->readBytes (buff, ((s > HTTP_TCP_BUFFER_SIZE) ? HTTP_TCP_BUFFER_SIZE : s) );
353356
354- if (s) {
355- int c = stream-> readBytes (buff, ((s > sizeof (buff)) ? sizeof ( buff) : s) );
357+ // write it to Stream
358+ bytesWritten += _tcp-> write (( const uint8_t *) buff, c );
356359
357- // write it to Stream
358- bytesWritten += _tcp->write ((const uint8_t *)buff, c);
360+ if (len > 0 ) {
361+ len -= c;
362+ }
359363
360- if (len > 0 ) {
361- len -= c;
364+ delay (0 );
365+ } else {
366+ delay (1 );
362367 }
368+ }
363369
364- delay (0 );
370+ 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);
373+ return HTTPC_ERROR_SEND_PAYLOAD_FAILED;
365374 } else {
366- delay ( 1 );
375+ DEBUG_HTTPCLIENT ( " [HTTP-Client][sendRequest] Stream payload written: %d \n " , bytesWritten );
367376 }
368- }
369-
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;
377+ free (buff);
374378 } else {
375- DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] Stream payload written: %d\n " , bytesWritten);
379+ DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE);
380+ return HTTPC_ERROR_TOO_LESS_RAM;
376381 }
377382
378383 // handle Server Response (Header)
@@ -434,37 +439,44 @@ int HTTPClient::writeToStream(Stream * stream) {
434439 int len = _size;
435440 int bytesWritten = 0 ;
436441
442+
437443 // create buffer for read
438- uint8_t buff[ 1460 ] = { 0 } ;
444+ uint8_t * buff = ( uint8_t *) malloc (HTTP_TCP_BUFFER_SIZE) ;
439445
440- // read all data from server
441- while (connected () && (len > 0 || len == -1 )) {
446+ if (buff) {
447+ // read all data from server
448+ while (connected () && (len > 0 || len == -1 )) {
442449
443- // get available data size
444- size_t size = _tcp->available ();
450+ // get available data size
451+ size_t size = _tcp->available ();
445452
446- if (size) {
447- int c = _tcp->readBytes (buff, ((size > sizeof (buff)) ? sizeof (buff) : size));
453+ if (size) {
454+ int c = _tcp->readBytes (buff, ((size > HTTP_TCP_BUFFER_SIZE) ? HTTP_TCP_BUFFER_SIZE : size));
448455
449- // write it to Stream
450- bytesWritten += stream->write (buff, c);
456+ // write it to Stream
457+ bytesWritten += stream->write (buff, c);
451458
452- if (len > 0 ) {
453- len -= c;
454- }
459+ if (len > 0 ) {
460+ len -= c;
461+ }
455462
456- delay (0 );
457- } else {
458- delay (1 );
463+ delay (0 );
464+ } else {
465+ delay (1 );
466+ }
459467 }
460- }
461468
462- DEBUG_HTTPCLIENT ( " [HTTP-Client][writeToStream] connection closed or file end (written: %d). \n " , bytesWritten );
469+ free (buff );
463470
464- if (_size && _size != bytesWritten) {
465- DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] bytesWritten %d and size %d mismatch!.\n " , bytesWritten, _size);
466- }
471+ DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] connection closed or file end (written: %d).\n " , bytesWritten);
467472
473+ if (_size && _size != bytesWritten) {
474+ DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] bytesWritten %d and size %d mismatch!.\n " , bytesWritten, _size);
475+ }
476+
477+ } else {
478+ DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE);
479+ }
468480 end ();
469481 return bytesWritten;
470482}
@@ -509,12 +521,13 @@ String HTTPClient::errorToString(int error) {
509521 return String (" no stream" );
510522 case HTTPC_ERROR_NO_HTTP_SERVER:
511523 return String (" no HTTP server" );
524+ case HTTPC_ERROR_TOO_LESS_RAM:
525+ return String (" too less ram" );
512526 default :
513527 return String ();
514528 }
515529}
516530
517-
518531/* *
519532 * adds Header to the request
520533 * @param name
0 commit comments