30
30
31
31
#include " ESP8266HTTPClient.h"
32
32
33
-
34
33
/* *
35
34
* constractor
36
35
*/
@@ -117,7 +116,7 @@ void HTTPClient::begin(String url, String httpsFingerprint) {
117
116
if (index >= 0 ) {
118
117
// auth info
119
118
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 @
121
120
_base64Authorization = base64::encode (auth);
122
121
}
123
122
@@ -335,8 +334,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
335
334
return HTTPC_ERROR_SEND_HEADER_FAILED;
336
335
}
337
336
338
- // create buffer for read
339
- uint8_t buff[1460 ] = { 0 };
337
+ size_t buff_size = HTTP_TCP_BUFFER_SIZE;
340
338
341
339
int len = size;
342
340
int bytesWritten = 0 ;
@@ -345,34 +343,51 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
345
343
len = -1 ;
346
344
}
347
345
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 )) {
350
358
351
- // get available data size
352
- size_t s = stream->available ();
359
+ // get available data size
360
+ size_t s = stream->available ();
353
361
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));
356
364
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);
359
367
360
- if (len > 0 ) {
361
- len -= c;
368
+ if (len > 0 ) {
369
+ len -= c;
370
+ }
371
+
372
+ delay (0 );
373
+ } else {
374
+ delay (1 );
362
375
}
376
+ }
363
377
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;
365
384
} else {
366
- delay ( 1 );
385
+ DEBUG_HTTPCLIENT ( " [HTTP-Client][sendRequest] Stream payload written: %d \n " , bytesWritten );
367
386
}
368
- }
369
387
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;
374
388
} 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;
376
391
}
377
392
378
393
// handle Server Response (Header)
@@ -434,35 +449,50 @@ int HTTPClient::writeToStream(Stream * stream) {
434
449
int len = _size;
435
450
int bytesWritten = 0 ;
436
451
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
+
437
459
// create buffer for read
438
- uint8_t buff[ 1460 ] = { 0 } ;
460
+ uint8_t * buff = ( uint8_t *) malloc (buff_size) ;
439
461
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 )) {
442
465
443
- // get available data size
444
- size_t size = _tcp->available ();
466
+ // get available data size
467
+ size_t size = _tcp->available ();
445
468
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));
448
471
449
- // write it to Stream
450
- bytesWritten += stream->write (buff, c);
472
+ // write it to Stream
473
+ bytesWritten += stream->write (buff, c);
451
474
452
- if (len > 0 ) {
453
- len -= c;
454
- }
475
+ if (len > 0 ) {
476
+ len -= c;
477
+ }
455
478
456
- delay (0 );
457
- } else {
458
- delay (1 );
479
+ delay (0 );
480
+ } else {
481
+ delay (1 );
482
+ }
459
483
}
460
- }
461
484
462
- DEBUG_HTTPCLIENT ( " [HTTP-Client][writeToStream] connection closed or file end (written: %d). \n " , bytesWritten );
485
+ free (buff );
463
486
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;
466
496
}
467
497
468
498
end ();
@@ -509,12 +539,13 @@ String HTTPClient::errorToString(int error) {
509
539
return String (" no stream" );
510
540
case HTTPC_ERROR_NO_HTTP_SERVER:
511
541
return String (" no HTTP server" );
542
+ case HTTPC_ERROR_TOO_LESS_RAM:
543
+ return String (" too less ram" );
512
544
default :
513
545
return String ();
514
546
}
515
547
}
516
548
517
-
518
549
/* *
519
550
* adds Header to the request
520
551
* @param name
0 commit comments