Skip to content

Commit e67cc90

Browse files
authored
Honor timeout in HTTPClient (esp8266#6056)
* check for timeout in ESP8266HTTPClient::writeToStreamDataBlock
1 parent ac53c29 commit e67cc90

File tree

2 files changed

+82
-86
lines changed

2 files changed

+82
-86
lines changed

libraries/ESP8266HTTPClient/examples/StreamHttpClient/StreamHttpClient.ino

+18-14
Original file line numberDiff line numberDiff line change
@@ -63,31 +63,35 @@ void loop() {
6363
// create buffer for read
6464
uint8_t buff[128] = { 0 };
6565

66+
#if 0
67+
// with API
68+
Serial.println(http.getString());
69+
#else
70+
// or "by hand"
71+
6672
// get tcp stream
6773
WiFiClient * stream = &client;
6874

6975
// read all data from server
7076
while (http.connected() && (len > 0 || len == -1)) {
71-
// get available data size
72-
size_t size = stream->available();
73-
74-
if (size) {
75-
// read up to 128 byte
76-
int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));
77+
// read up to 128 byte
78+
int c = stream->readBytes(buff, std::min((size_t)len, sizeof(buff)));
79+
Serial.printf("readBytes: %d\n", c);
80+
if (!c) {
81+
Serial.println("read timeout");
82+
}
7783

78-
// write it to Serial
79-
Serial.write(buff, c);
84+
// write it to Serial
85+
Serial.write(buff, c);
8086

81-
if (len > 0) {
82-
len -= c;
83-
}
87+
if (len > 0) {
88+
len -= c;
8489
}
85-
delay(1);
8690
}
91+
#endif
8792

8893
Serial.println();
8994
Serial.print("[HTTP] connection closed or file end.\n");
90-
9195
}
9296
} else {
9397
Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
@@ -96,5 +100,5 @@ void loop() {
96100
http.end();
97101
}
98102

99-
delay(10000);
103+
delay(60000);
100104
}

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

+64-72
Original file line numberDiff line numberDiff line change
@@ -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()
13461346
int 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

Comments
 (0)