1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-13 13:01:55 +03:00

handle possible dead lock in HTTP client see: #1520

This commit is contained in:
Markus Sattler
2016-01-29 14:02:09 +01:00
parent 27f1a63170
commit 1060db94c2

View File

@ -264,7 +264,7 @@ void HTTPClient::setAuthorization(const char * auth) {
/** /**
* set the timeout for the TCP connection * set the timeout for the TCP connection
* @param timeout unsigned int * @param timeout unsigned int
*/ */
void HTTPClient::setTimeout(uint16_t timeout) { void HTTPClient::setTimeout(uint16_t timeout) {
_tcpTimeout = timeout; _tcpTimeout = timeout;
@ -273,14 +273,12 @@ void HTTPClient::setTimeout(uint16_t timeout) {
} }
} }
/** /**
* use HTTP1.0 * use HTTP1.0
* @param timeout * @param timeout
*/ */
void HTTPClient::useHTTP10(bool useHTTP10) { void HTTPClient::useHTTP10(bool useHTTP10) {
_useHTTP10 = useHTTP10; _useHTTP10 = useHTTP10;
} }
/** /**
@ -392,7 +390,6 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
// create buffer for read // create buffer for read
uint8_t * buff = (uint8_t *) malloc(buff_size); uint8_t * buff = (uint8_t *) malloc(buff_size);
if(buff) { if(buff) {
// read all data from stream and send it to server // read all data from stream and send it to server
while(connected() && (stream->available() > -1) && (len > 0 || len == -1)) { while(connected() && (stream->available() > -1) && (len > 0 || len == -1)) {
@ -471,8 +468,7 @@ int HTTPClient::sendRequest(const char * type, Stream * stream, size_t size) {
free(buff); free(buff);
if(size && (int) size != bytesWritten) { if(size && (int) size != bytesWritten) {
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload bytesWritten %d and size %d mismatch!.\n", bytesWritten, size); 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!");
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] ERROR SEND PAYLOAD FAILED!");
return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED); return returnError(HTTPC_ERROR_SEND_PAYLOAD_FAILED);
} else { } else {
DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten); DEBUG_HTTPCLIENT("[HTTP-Client][sendRequest] Stream payload written: %d\n", bytesWritten);
@ -829,10 +825,12 @@ int HTTPClient::handleHeaderResponse() {
if(!connected()) { if(!connected()) {
return HTTPC_ERROR_NOT_CONNECTED; return HTTPC_ERROR_NOT_CONNECTED;
} }
String transferEncoding; String transferEncoding;
_returnCode = -1; _returnCode = -1;
_size = -1; _size = -1;
_transferEncoding = HTTPC_TE_IDENTITY; _transferEncoding = HTTPC_TE_IDENTITY;
unsigned long lastDataTime = millis();
while(connected()) { while(connected()) {
size_t len = _tcp->available(); size_t len = _tcp->available();
@ -840,6 +838,8 @@ int HTTPClient::handleHeaderResponse() {
String headerLine = _tcp->readStringUntil('\n'); String headerLine = _tcp->readStringUntil('\n');
headerLine.trim(); // remove \r headerLine.trim(); // remove \r
lastDataTime = millis();
DEBUG_HTTPCLIENT("[HTTP-Client][handleHeaderResponse] RX: '%s'\n", headerLine.c_str()); DEBUG_HTTPCLIENT("[HTTP-Client][handleHeaderResponse] RX: '%s'\n", headerLine.c_str());
if(headerLine.startsWith("HTTP/1.")) { if(headerLine.startsWith("HTTP/1.")) {
@ -895,6 +895,9 @@ int HTTPClient::handleHeaderResponse() {
} }
} else { } else {
if((millis() - lastDataTime) > _tcpTimeout) {
return HTTPC_ERROR_READ_TIMEOUT;
}
delay(0); delay(0);
} }
} }
@ -902,8 +905,6 @@ int HTTPClient::handleHeaderResponse() {
return HTTPC_ERROR_CONNECTION_LOST; return HTTPC_ERROR_CONNECTION_LOST;
} }
/** /**
* write one Data Block to Stream * write one Data Block to Stream
* @param stream Stream * * @param stream Stream *