Skip to content

Commit 1060db9

Browse files
committed
handle possible dead lock in HTTP client see: #1520
1 parent 27f1a63 commit 1060db9

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ void HTTPClient::setAuthorization(const char * auth) {
264264

265265
/**
266266
* set the timeout for the TCP connection
267-
* @param timeout unsigned int
267+
* @param timeout unsigned int
268268
*/
269269
void HTTPClient::setTimeout(uint16_t timeout) {
270270
_tcpTimeout = timeout;
@@ -273,14 +273,12 @@ void HTTPClient::setTimeout(uint16_t timeout) {
273273
}
274274
}
275275

276-
277-
278276
/**
279277
* use HTTP1.0
280278
* @param timeout
281279
*/
282280
void HTTPClient::useHTTP10(bool useHTTP10) {
283-
_useHTTP10 = useHTTP10;
281+
_useHTTP10 = useHTTP10;
284282
}
285283

286284
/**
@@ -392,7 +390,6 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
392390
// create buffer for read
393391
uint8_t * buff = (uint8_t *) malloc(buff_size);
394392

395-
396393
if(buff) {
397394
// read all data from stream and send it to server
398395
while(connected() && (stream->available() > -1) && (len > 0 || len == -1)) {
@@ -471,8 +468,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
471468
free(buff);
472469

473470
if(size && (int) size != bytesWritten) {
474-
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, size);
475-
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!");
471+
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, size); DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!");
476472
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
477473
} else {
478474
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten);
@@ -829,17 +825,21 @@ int HTTPClient::handleHeaderResponse() {
829825
if(!connected()) {
830826
return HTTPC_ERROR_NOT_CONNECTED;
831827
}
828+
832829
String transferEncoding;
833830
_returnCode = -1;
834831
_size = -1;
835832
_transferEncoding = HTTPC_TE_IDENTITY;
833+
unsigned long lastDataTime = millis();
836834

837835
while(connected()) {
838836
size_t len = _tcp->available();
839837
if(len > 0) {
840838
String headerLine = _tcp->readStringUntil('\n');
841839
headerLine.trim(); // remove \r
842840

841+
lastDataTime = millis();
842+
843843
DEBUG_HTTPCLIENT("[HTTP-Client][handleHeaderResponse] RX: '%s'\n", headerLine.c_str());
844844

845845
if(headerLine.startsWith("HTTP/1.")) {
@@ -895,15 +895,16 @@ int HTTPClient::handleHeaderResponse() {
895895
}
896896

897897
} else {
898+
if((millis() - lastDataTime) > _tcpTimeout) {
899+
return HTTPC_ERROR_READ_TIMEOUT;
900+
}
898901
delay(0);
899902
}
900903
}
901904

902905
return HTTPC_ERROR_CONNECTION_LOST;
903906
}
904907

905-
906-
907908
/**
908909
* write one Data Block to Stream
909910
* @param stream Stream *

0 commit comments

Comments
 (0)