Merge pull request #5649 from adamsthws/fix/httpclientlight-read-timeout

[HttpClientLight] Time out reading a body when the server goes silent
This commit is contained in:
TD-er authored and GitHub committed 2026-10-18 19:59:55 +02:00
commit 6d83d96a82
2 files changed
+13 -3

No files matched your search

+11 -1
View File
@@ -503,7 +503,7 @@ void HTTPClientLight::setConnectTimeout(int32_t connectTimeout)
* set the timeout for the TCP connection
* @param timeout unsigned int
*/
void HTTPClientLight::setTimeout(uint16_t timeout)
void HTTPClientLight::setTimeout(uint32_t timeout)
{
_tcpTimeout = timeout;
if(connected()) {
@@ -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);
}
}
+2 -2
View File
@@ -176,7 +176,7 @@ public:
void setAuthorization(const char * user, const char * password);
void setAuthorization(const char * auth);
void setConnectTimeout(int32_t connectTimeout);
void setTimeout(uint16_t timeout);
void setTimeout(uint32_t timeout);
// Redirections
void setFollowRedirects(followRedirects_t follow);
@@ -248,7 +248,7 @@ protected:
uint16_t _port = 0;
int32_t _connectTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT; // Do not set to -1 as it fails WiFiClient connect()
bool _reuse = true;
uint16_t _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT;
uint32_t _tcpTimeout = HTTPCLIENT_DEFAULT_TCP_TIMEOUT;
bool _useHTTP10 = false;
bool _secure = false;