@@ -847,7 +847,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size)
847
847
}
848
848
849
849
} else {
850
- DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] too less ram! need %d\n " , HTTP_TCP_BUFFER_SIZE);
850
+ DEBUG_HTTPCLIENT (" [HTTP-Client][sendRequest] not enough ram! need %d\n " , HTTP_TCP_BUFFER_SIZE);
851
851
return returnError (HTTPC_ERROR_TOO_LESS_RAM);
852
852
}
853
853
@@ -1033,7 +1033,7 @@ String HTTPClient::errorToString(int error)
1033
1033
case HTTPC_ERROR_NO_HTTP_SERVER:
1034
1034
return F (" no HTTP server" );
1035
1035
case HTTPC_ERROR_TOO_LESS_RAM:
1036
- return F (" too less ram" );
1036
+ return F (" not enough ram" );
1037
1037
case HTTPC_ERROR_ENCODING:
1038
1038
return F (" Transfer-Encoding not supported" );
1039
1039
case HTTPC_ERROR_STREAM_WRITE:
@@ -1346,7 +1346,7 @@ int HTTPClient::handleHeaderResponse()
1346
1346
int HTTPClient::writeToStreamDataBlock (Stream * stream, int size)
1347
1347
{
1348
1348
int buff_size = HTTP_TCP_BUFFER_SIZE;
1349
- int len = size;
1349
+ int len = size; // left size to read
1350
1350
int bytesWritten = 0 ;
1351
1351
1352
1352
// if possible create smaller buffer then HTTP_TCP_BUFFER_SIZE
@@ -1357,93 +1357,85 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size)
1357
1357
// create buffer for read
1358
1358
uint8_t * buff = (uint8_t *) malloc (buff_size);
1359
1359
1360
- if (buff) {
1361
- // read all data from server
1362
- while (connected () && (len > 0 || len == -1 )) {
1363
-
1364
- // get available data size
1365
- size_t sizeAvailable = _client->available ();
1366
-
1367
- if (sizeAvailable) {
1368
-
1369
- int readBytes = sizeAvailable;
1370
-
1371
- // read only the asked bytes
1372
- if (len > 0 && readBytes > len) {
1373
- readBytes = len;
1374
- }
1360
+ if (!buff) {
1361
+ DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStreamDataBlock] not enough ram! need %d\n " , HTTP_TCP_BUFFER_SIZE);
1362
+ return HTTPC_ERROR_TOO_LESS_RAM;
1363
+ }
1375
1364
1376
- // not read more the buffer can handle
1377
- if (readBytes > buff_size) {
1378
- readBytes = buff_size;
1379
- }
1365
+ // read all data from server
1366
+ while ( connected () && (len > 0 || len == - 1 ))
1367
+ {
1368
+ int readBytes = len;
1380
1369
1381
- // read data
1382
- int bytesRead = _client->readBytes (buff, readBytes);
1370
+ // not read more the buffer can handle
1371
+ if (readBytes > buff_size) {
1372
+ readBytes = buff_size;
1373
+ }
1383
1374
1384
- // write it to Stream
1385
- int bytesWrite = stream->write (buff, bytesRead);
1386
- bytesWritten += bytesWrite;
1375
+ // read data
1376
+ int bytesRead = _client->readBytes (buff, readBytes);
1377
+ if (!bytesRead)
1378
+ {
1379
+ DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStreamDataBlock] input stream timeout\n " );
1380
+ free (buff);
1381
+ return HTTPC_ERROR_READ_TIMEOUT;
1382
+ }
1387
1383
1388
- // are all Bytes a writen to stream ?
1389
- if ( bytesWrite != bytesRead) {
1390
- DEBUG_HTTPCLIENT ( " [HTTP-Client][writeToStream] short write asked for %d but got %d retry... \n " , bytesRead, bytesWrite) ;
1384
+ // write it to Stream
1385
+ int bytesWrite = stream-> write (buff, bytesRead);
1386
+ bytesWritten += bytesWrite;
1391
1387
1392
- // check for write error
1393
- if (stream-> getWriteError () ) {
1394
- DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStreamDataBlock] stream write error %d \n " , stream-> getWriteError () );
1388
+ // are all Bytes a writen to stream ?
1389
+ if (bytesWrite != bytesRead ) {
1390
+ DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] short write asked for %d but got %d retry... \n " , bytesRead, bytesWrite );
1395
1391
1396
- // reset write error for retry
1397
- stream->clearWriteError ();
1398
- }
1392
+ // check for write error
1393
+ if ( stream->getWriteError ()) {
1394
+ DEBUG_HTTPCLIENT ( " [HTTP-Client][writeToStreamDataBlock] stream write error %d \n " , stream-> getWriteError ());
1399
1395
1400
- // some time for the stream
1401
- delay (1 );
1396
+ // reset write error for retry
1397
+ stream->clearWriteError ();
1398
+ }
1402
1399
1403
- int leftBytes = (readBytes - bytesWrite);
1400
+ // some time for the stream
1401
+ delay (1 );
1404
1402
1405
- // retry to send the missed bytes
1406
- bytesWrite = stream->write ((buff + bytesWrite), leftBytes);
1407
- bytesWritten += bytesWrite;
1403
+ int leftBytes = (bytesRead - bytesWrite);
1408
1404
1409
- if (bytesWrite != leftBytes) {
1410
- // failed again
1411
- DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] short write asked for %d but got %d failed.\n " , leftBytes, bytesWrite);
1412
- free (buff);
1413
- return HTTPC_ERROR_STREAM_WRITE;
1414
- }
1415
- }
1405
+ // retry to send the missed bytes
1406
+ bytesWrite = stream->write ((buff + bytesWrite), leftBytes);
1407
+ bytesWritten += bytesWrite;
1416
1408
1417
- // check for write error
1418
- if (stream->getWriteError ()) {
1419
- DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStreamDataBlock] stream write error %d\n " , stream->getWriteError ());
1420
- free (buff);
1421
- return HTTPC_ERROR_STREAM_WRITE;
1422
- }
1409
+ if (bytesWrite != leftBytes) {
1410
+ // failed again
1411
+ DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStream] short write asked for %d but got %d failed.\n " , leftBytes, bytesWrite);
1412
+ free (buff);
1413
+ return HTTPC_ERROR_STREAM_WRITE;
1414
+ }
1415
+ }
1423
1416
1424
- // count bytes to read left
1425
- if (len > 0 ) {
1426
- len -= readBytes;
1427
- }
1417
+ // check for write error
1418
+ if (stream->getWriteError ()) {
1419
+ DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStreamDataBlock] stream write error %d\n " , stream->getWriteError ());
1420
+ free (buff);
1421
+ return HTTPC_ERROR_STREAM_WRITE;
1422
+ }
1428
1423
1429
- delay (0 );
1430
- } else {
1431
- delay (1 );
1432
- }
1424
+ // count bytes to read left
1425
+ if (len > 0 ) {
1426
+ len -= bytesRead;
1433
1427
}
1434
1428
1435
- free (buff);
1429
+ delay (0 );
1430
+ }
1436
1431
1437
- DEBUG_HTTPCLIENT ( " [HTTP-Client][writeToStreamDataBlock] connection closed or file end (written: %d). \n " , bytesWritten );
1432
+ free (buff );
1438
1433
1439
- if ((size > 0 ) && (size != bytesWritten)) {
1440
- DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStreamDataBlock] bytesWritten %d and size %d mismatch!.\n " , bytesWritten, size);
1441
- return HTTPC_ERROR_STREAM_WRITE;
1442
- }
1434
+ DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStreamDataBlock] connection closed or file end (written: %d).\n " , bytesWritten);
1443
1435
1444
- } else {
1445
- DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStreamDataBlock] too less ram! need %d\n " , HTTP_TCP_BUFFER_SIZE );
1446
- return HTTPC_ERROR_TOO_LESS_RAM ;
1436
+ if ((size > 0 ) && (size != bytesWritten)) {
1437
+ DEBUG_HTTPCLIENT (" [HTTP-Client][writeToStreamDataBlock] bytesWritten %d and size %d mismatch!. \n " , bytesWritten, size );
1438
+ return HTTPC_ERROR_STREAM_WRITE ;
1447
1439
}
1448
1440
1449
1441
return bytesWritten;
0 commit comments