@@ -334,8 +334,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
334
334
return HTTPC_ERROR_SEND_HEADER_FAILED;
335
335
}
336
336
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;
339
338
340
339
int len = size;
341
340
int bytesWritten = 0 ;
@@ -344,6 +343,15 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
344
343
len = -1 ;
345
344
}
346
345
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
+
347
355
if (buff) {
348
356
// read all data from stream and send it to server
349
357
while (connected () && stream->available () && (len > 0 || len == -1 )) {
@@ -352,7 +360,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
352
360
size_t s = stream->available ();
353
361
354
362
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));
356
364
357
365
// write it to Stream
358
366
bytesWritten += _tcp->write ((const uint8_t *) buff, c);
@@ -367,14 +375,16 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
367
375
}
368
376
}
369
377
378
+ free (buff);
379
+
370
380
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! " );
373
383
return HTTPC_ERROR_SEND_PAYLOAD_FAILED;
374
384
} else {
375
385
DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] Stream payload written: %d\n " , bytesWritten);
376
386
}
377
- free (buff);
387
+
378
388
} else {
379
389
DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE);
380
390
return HTTPC_ERROR_TOO_LESS_RAM;
@@ -439,9 +449,15 @@ int HTTPClient::writeToStream(Stream * stream) {
439
449
int len = _size;
440
450
int bytesWritten = 0 ;
441
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
+ }
442
458
443
459
// create buffer for read
444
- uint8_t * buff = (uint8_t *) malloc (HTTP_TCP_BUFFER_SIZE );
460
+ uint8_t * buff = (uint8_t *) malloc (buff_size );
445
461
446
462
if (buff) {
447
463
// read all data from server
@@ -451,7 +467,7 @@ int HTTPClient::writeToStream(Stream * stream) {
451
467
size_t size = _tcp->available ();
452
468
453
469
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));
455
471
456
472
// write it to Stream
457
473
bytesWritten += stream->write (buff, c);
@@ -476,7 +492,9 @@ int HTTPClient::writeToStream(Stream * stream) {
476
492
477
493
} else {
478
494
DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] too less ram! need " HTTP_TCP_BUFFER_SIZE);
495
+ return HTTPC_ERROR_TOO_LESS_RAM;
479
496
}
497
+
480
498
end ();
481
499
return bytesWritten;
482
500
}
0 commit comments