@@ -331,12 +331,12 @@ int HTTPClient::sendRequest(const char * type, uint8_t * payload, size_t size) {
331
331
int HTTPClient::sendRequest (const char * type, Stream * stream, size_t size) {
332
332
333
333
if (!stream) {
334
- return HTTPC_ERROR_NO_STREAM;
334
+ return returnError ( HTTPC_ERROR_NO_STREAM) ;
335
335
}
336
336
337
337
// connect to server
338
338
if (!connect ()) {
339
- return HTTPC_ERROR_CONNECTION_REFUSED;
339
+ return returnError ( HTTPC_ERROR_CONNECTION_REFUSED) ;
340
340
}
341
341
342
342
if (size > 0 ) {
@@ -345,7 +345,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
345
345
346
346
// send Header
347
347
if (!sendHeader (type)) {
348
- return HTTPC_ERROR_SEND_HEADER_FAILED;
348
+ return returnError ( HTTPC_ERROR_SEND_HEADER_FAILED) ;
349
349
}
350
350
351
351
int buff_size = HTTP_TCP_BUFFER_SIZE;
@@ -371,25 +371,68 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
371
371
while (connected () && (stream->available () > -1 ) && (len > 0 || len == -1 )) {
372
372
373
373
// get available data size
374
- int s = stream->available ();
374
+ int sizeAvailable = stream->available ();
375
375
376
- if (len) {
377
- s = ((s > len) ? len : s);
378
- }
376
+ if (sizeAvailable) {
377
+
378
+ int readBytes = sizeAvailable;
379
+
380
+ // read only the asked bytes
381
+ if (len > 0 && readBytes > len) {
382
+ readBytes = len;
383
+ }
384
+
385
+ // not read more the buffer can handle
386
+ if (readBytes > buff_size) {
387
+ readBytes = buff_size;
388
+ }
379
389
380
- if (s) {
381
- int c = stream->readBytes (buff, ((s > buff_size) ? buff_size : s) );
390
+ // read data
391
+ int bytesRead = stream->readBytes (buff, readBytes );
382
392
383
393
// write it to Stream
384
- int w = _tcp->write ((const uint8_t *) buff, c);
385
- bytesWritten += w;
386
- if (w != c) {
387
- DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] short write asked for %d but got %d\n " , c, w);
388
- break ;
394
+ int bytesWrite = _tcp->write ((const uint8_t *) buff, bytesRead);
395
+ bytesWritten += bytesWrite;
396
+
397
+ // are all Bytes a writen to stream ?
398
+ if (bytesWrite != bytesRead) {
399
+ DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] short write, asked for %d but got %d retry...\n " , bytesRead, bytesWrite);
400
+
401
+ // check for write error
402
+ if (_tcp->getWriteError ()) {
403
+ DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] stream write error %d\n " , _tcp->getWriteError ());
404
+
405
+ // reset write error for retry
406
+ _tcp->clearWriteError ();
407
+ }
408
+
409
+ // some time for the stream
410
+ delay (1 );
411
+
412
+ int leftBytes = (readBytes - bytesWrite);
413
+
414
+ // retry to send the missed bytes
415
+ bytesWrite = _tcp->write ((const uint8_t *) (buff + bytesWrite), leftBytes);
416
+ bytesWritten += bytesWrite;
417
+
418
+ if (bytesWrite != leftBytes) {
419
+ // failed again
420
+ DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] short write, asked for %d but got %d failed.\n " , leftBytes, bytesWrite);
421
+ free (buff);
422
+ return returnError (HTTPC_ERROR_SEND_PAYLOAD_FAILED);
423
+ }
424
+ }
425
+
426
+ // check for write error
427
+ if (_tcp->getWriteError ()) {
428
+ DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] stream write error %d\n " , _tcp->getWriteError ());
429
+ free (buff);
430
+ return returnError (HTTPC_ERROR_SEND_PAYLOAD_FAILED);
389
431
}
390
432
433
+ // count bytes to read left
391
434
if (len > 0 ) {
392
- len -= c ;
435
+ len -= readBytes ;
393
436
}
394
437
395
438
delay (0 );
@@ -403,18 +446,18 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
403
446
if (size && (int ) size != bytesWritten) {
404
447
DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n " , bytesWritten, size);
405
448
DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!" );
406
- return HTTPC_ERROR_SEND_PAYLOAD_FAILED;
449
+ return returnError ( HTTPC_ERROR_SEND_PAYLOAD_FAILED) ;
407
450
} else {
408
451
DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] Stream payload written: %d\n " , bytesWritten);
409
452
}
410
453
411
454
} else {
412
455
DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] too less ram! need %d\n " , HTTP_TCP_BUFFER_SIZE);
413
- return HTTPC_ERROR_TOO_LESS_RAM;
456
+ return returnError ( HTTPC_ERROR_TOO_LESS_RAM) ;
414
457
}
415
458
416
459
// handle Server Response (Header)
417
- return handleHeaderResponse ();
460
+ return returnError ( handleHeaderResponse () );
418
461
}
419
462
420
463
/* *
@@ -851,7 +894,7 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size) {
851
894
int readBytes = sizeAvailable;
852
895
853
896
// read only the asked bytes
854
- if (readBytes > len) {
897
+ if (len > 0 && readBytes > len) {
855
898
readBytes = len;
856
899
}
857
900
@@ -899,6 +942,7 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size) {
899
942
// check for write error
900
943
if (stream->getWriteError ()) {
901
944
DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStreamDataBlock] stream write error %d\n " , stream->getWriteError ());
945
+ free (buff);
902
946
return HTTPC_ERROR_STREAM_WRITE;
903
947
}
904
948
0 commit comments