@@ -847,7 +847,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size)
847847 }
848848
849849 } 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);
851851 return returnError (HTTPC_ERROR_TOO_LESS_RAM);
852852 }
853853
@@ -1033,7 +1033,7 @@ String HTTPClient::errorToString(int error)
10331033 case HTTPC_ERROR_NO_HTTP_SERVER:
10341034 return F (" no HTTP server" );
10351035 case HTTPC_ERROR_TOO_LESS_RAM:
1036- return F (" too less ram" );
1036+ return F (" not enough ram" );
10371037 case HTTPC_ERROR_ENCODING:
10381038 return F (" Transfer-Encoding not supported" );
10391039 case HTTPC_ERROR_STREAM_WRITE:
@@ -1346,7 +1346,7 @@ int HTTPClient::handleHeaderResponse()
13461346int HTTPClient::writeToStreamDataBlock (Stream * stream, int size)
13471347{
13481348 int buff_size = HTTP_TCP_BUFFER_SIZE;
1349- int len = size;
1349+ int len = size; // left size to read
13501350 int bytesWritten = 0 ;
13511351
13521352 // if possible create smaller buffer then HTTP_TCP_BUFFER_SIZE
@@ -1357,93 +1357,85 @@ int HTTPClient::writeToStreamDataBlock(Stream * stream, int size)
13571357 // create buffer for read
13581358 uint8_t * buff = (uint8_t *) malloc (buff_size);
13591359
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+ }
13751364
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;
13801369
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+ }
13831374
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+ }
13871383
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;
13911387
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 );
13951391
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 ());
13991395
1400- // some time for the stream
1401- delay (1 );
1396+ // reset write error for retry
1397+ stream->clearWriteError ();
1398+ }
14021399
1403- int leftBytes = (readBytes - bytesWrite);
1400+ // some time for the stream
1401+ delay (1 );
14041402
1405- // retry to send the missed bytes
1406- bytesWrite = stream->write ((buff + bytesWrite), leftBytes);
1407- bytesWritten += bytesWrite;
1403+ int leftBytes = (bytesRead - bytesWrite);
14081404
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;
14161408
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+ }
14231416
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+ }
14281423
1429- delay (0 );
1430- } else {
1431- delay (1 );
1432- }
1424+ // count bytes to read left
1425+ if (len > 0 ) {
1426+ len -= bytesRead;
14331427 }
14341428
1435- free (buff);
1429+ delay (0 );
1430+ }
14361431
1437- DEBUG_HTTPCLIENT ( " [HTTP-Client][writeToStreamDataBlock] connection closed or file end (written: %d). \n " , bytesWritten );
1432+ free (buff );
14381433
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);
14431435
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 ;
14471439 }
14481440
14491441 return bytesWritten;
0 commit comments