Skip to content

Commit e71ec77

Browse files
Christian Schusterigrr
Christian Schuster
authored andcommitted
minimize number of exit paths in ESP8266WebServer::handleClient (#2557)
1 parent 2fbc619 commit e71ec77

File tree

1 file changed

+38
-45
lines changed

1 file changed

+38
-45
lines changed

libraries/ESP8266WebServer/src/ESP8266WebServer.cpp

+38-45
Original file line numberDiff line numberDiff line change
@@ -276,56 +276,49 @@ void ESP8266WebServer::handleClient() {
276276
_statusChange = millis();
277277
}
278278

279-
if (!_currentClient.connected()) {
280-
_currentClient = WiFiClient();
281-
_currentStatus = HC_NONE;
282-
_currentUpload.reset();
283-
return;
284-
}
285-
286-
// Wait for data from client to become available
287-
if (_currentStatus == HC_WAIT_READ) {
288-
if (!_currentClient.available()) {
289-
if (millis() - _statusChange > HTTP_MAX_DATA_WAIT) {
290-
_currentClient = WiFiClient();
291-
_currentStatus = HC_NONE;
292-
_currentUpload.reset();
279+
bool keepCurrentClient = false;
280+
bool callYield = false;
281+
282+
if (_currentClient.connected()) {
283+
switch (_currentStatus) {
284+
case HC_WAIT_READ:
285+
// Wait for data from client to become available
286+
if (_currentClient.available()) {
287+
if (_parseRequest(_currentClient)) {
288+
_currentClient.setTimeout(HTTP_MAX_SEND_WAIT);
289+
_contentLength = CONTENT_LENGTH_NOT_SET;
290+
_handleRequest();
291+
292+
if (_currentClient.connected()) {
293+
_currentStatus = HC_WAIT_CLOSE;
294+
_statusChange = millis();
295+
keepCurrentClient = true;
296+
}
297+
}
298+
} else { // !_currentClient.available()
299+
if (millis() - _statusChange <= HTTP_MAX_DATA_WAIT) {
300+
keepCurrentClient = true;
301+
}
302+
callYield = true;
303+
}
304+
break;
305+
case HC_WAIT_CLOSE:
306+
// Wait for client to close the connection
307+
if (millis() - _statusChange <= HTTP_MAX_CLOSE_WAIT) {
308+
keepCurrentClient = true;
309+
callYield = true;
293310
}
294-
yield();
295-
return;
296311
}
312+
}
297313

298-
if (!_parseRequest(_currentClient)) {
299-
_currentClient = WiFiClient();
300-
_currentStatus = HC_NONE;
301-
_currentUpload.reset();
302-
return;
303-
}
304-
_currentClient.setTimeout(HTTP_MAX_SEND_WAIT);
305-
_contentLength = CONTENT_LENGTH_NOT_SET;
306-
_handleRequest();
307-
308-
if (!_currentClient.connected()) {
309-
_currentClient = WiFiClient();
310-
_currentStatus = HC_NONE;
311-
_currentUpload.reset();
312-
return;
313-
} else {
314-
_currentStatus = HC_WAIT_CLOSE;
315-
_statusChange = millis();
316-
return;
317-
}
314+
if (!keepCurrentClient) {
315+
_currentClient = WiFiClient();
316+
_currentStatus = HC_NONE;
317+
_currentUpload.reset();
318318
}
319319

320-
if (_currentStatus == HC_WAIT_CLOSE) {
321-
if (millis() - _statusChange > HTTP_MAX_CLOSE_WAIT) {
322-
_currentClient = WiFiClient();
323-
_currentStatus = HC_NONE;
324-
_currentUpload.reset();
325-
} else {
326-
yield();
327-
return;
328-
}
320+
if (callYield) {
321+
yield();
329322
}
330323
}
331324

0 commit comments

Comments
 (0)