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
@@ -336,7 +335,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
336
335
}
337
336
338
337
// create buffer for read
339
- uint8_t buff[ 1460 ] = { 0 } ;
338
+ uint8_t * buff = ( uint8_t *) malloc (HTTP_TCP_BUFFER_SIZE) ;
340
339
341
340
int len = size;
342
341
int bytesWritten = 0 ;
@@ -345,34 +344,40 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
345
344
len = -1 ;
346
345
}
347
346
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 ();
350
353
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) );
353
356
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 );
356
359
357
- // write it to Stream
358
- bytesWritten += _tcp->write ((const uint8_t *)buff, c);
360
+ if (len > 0 ) {
361
+ len -= c;
362
+ }
359
363
360
- if (len > 0 ) {
361
- len -= c;
364
+ delay (0 );
365
+ } else {
366
+ delay (1 );
362
367
}
368
+ }
363
369
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;
365
374
} else {
366
- delay ( 1 );
375
+ DEBUG_HTTPCLIENT ( " [HTTP-Client][sendRequest] Stream payload written: %d \n " , bytesWritten );
367
376
}
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);
374
378
} 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;
376
381
}
377
382
378
383
// handle Server Response (Header)
@@ -434,37 +439,44 @@ int HTTPClient::writeToStream(Stream * stream) {
434
439
int len = _size;
435
440
int bytesWritten = 0 ;
436
441
442
+
437
443
// create buffer for read
438
- uint8_t buff[ 1460 ] = { 0 } ;
444
+ uint8_t * buff = ( uint8_t *) malloc (HTTP_TCP_BUFFER_SIZE) ;
439
445
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 )) {
442
449
443
- // get available data size
444
- size_t size = _tcp->available ();
450
+ // get available data size
451
+ size_t size = _tcp->available ();
445
452
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));
448
455
449
- // write it to Stream
450
- bytesWritten += stream->write (buff, c);
456
+ // write it to Stream
457
+ bytesWritten += stream->write (buff, c);
451
458
452
- if (len > 0 ) {
453
- len -= c;
454
- }
459
+ if (len > 0 ) {
460
+ len -= c;
461
+ }
455
462
456
- delay (0 );
457
- } else {
458
- delay (1 );
463
+ delay (0 );
464
+ } else {
465
+ delay (1 );
466
+ }
459
467
}
460
- }
461
468
462
- DEBUG_HTTPCLIENT ( " [HTTP-Client][writeToStream] connection closed or file end (written: %d). \n " , bytesWritten );
469
+ free (buff );
463
470
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);
467
472
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
+ }
468
480
end ();
469
481
return bytesWritten;
470
482
}
@@ -509,12 +521,13 @@ String HTTPClient::errorToString(int error) {
509
521
return String (" no stream" );
510
522
case HTTPC_ERROR_NO_HTTP_SERVER:
511
523
return String (" no HTTP server" );
524
+ case HTTPC_ERROR_TOO_LESS_RAM:
525
+ return String (" too less ram" );
512
526
default :
513
527
return String ();
514
528
}
515
529
}
516
530
517
-
518
531
/* *
519
532
* adds Header to the request
520
533
* @param name
0 commit comments