[HttpClientLight] Time out reading a body when the server goes silent

writeToStreamDataBlock() waited for the rest of a body for as long as
connected() held. A server that stops sending part way through a body
without closing the connection leaves connected() true, so getString()
never returned and the unit stopped responding. Give up after
_tcpTimeout without data and return HTTPC_ERROR_READ_TIMEOUT, as
handleHeaderResponse() already does. Same fix as arendst/Tasmota#25139.
This commit is contained in:
Adam Matthews committed 2026-10-15 00:30:28 +01:00
1 parent 0c5d756fd1
commit 08d2ab0500
1 file changed
+10
@@ -1376,6 +1376,10 @@ int HTTPClientLight::writeToStreamDataBlock(Stream * stream, int size)
uint8_t * buff = (uint8_t *) malloc(buff_size);
if(buff) {
// a server gone silent without closing stays connected(): give up
// after _tcpTimeout without data, as handleHeaderResponse() does
unsigned long lastDataTime = millis();
// read all data from server
while(connected() && (len > 0 || len == -1)) {
@@ -1448,8 +1452,14 @@ int HTTPClientLight::writeToStreamDataBlock(Stream * stream, int size)
len -= readBytes;
}
lastDataTime = millis();
delay(0);
} else {
if((millis() - lastDataTime) > _tcpTimeout) {
log_w("read timeout after %d bytes", bytesWritten);
free(buff);
return HTTPC_ERROR_READ_TIMEOUT;
}
delay(1);
}
}