mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-01 03:26:58 +03:00
WebServer: handle initial read/close timeouts asynchronously
If there is a delay before data becomes available on a new connection then the handler function blocks for up to 1 second, and there's another wait on connection close for up to 2 seconds. This doesn't make the whole server process asynchronous (there is another delay between headers/data) but it helps when there are other events that need attention while handling web requests.
This commit is contained in:
@ -80,6 +80,7 @@ ESP8266WebServer::~ESP8266WebServer() {
|
||||
}
|
||||
|
||||
void ESP8266WebServer::begin() {
|
||||
_currentStatus = HC_NONE;
|
||||
_server.begin();
|
||||
if(!_headerKeysCount)
|
||||
collectHeaders(0, 0);
|
||||
@ -155,28 +156,67 @@ void ESP8266WebServer::serveStatic(const char* uri, FS& fs, const char* path, co
|
||||
}
|
||||
|
||||
void ESP8266WebServer::handleClient() {
|
||||
WiFiClient client = _server.available();
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
if (_currentStatus == HC_NONE) {
|
||||
WiFiClient client = _server.available();
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_ESP_HTTP_SERVER
|
||||
DEBUG_OUTPUT.println("New client");
|
||||
DEBUG_OUTPUT.println("New client");
|
||||
#endif
|
||||
|
||||
// Wait for data from client to become available
|
||||
uint16_t maxWait = HTTP_MAX_DATA_WAIT;
|
||||
while(client.connected() && !client.available() && maxWait--){
|
||||
delay(1);
|
||||
_currentClient = client;
|
||||
_currentStatus = HC_WAIT_READ;
|
||||
_statusChange = millis();
|
||||
}
|
||||
|
||||
if (!_parseRequest(client)) {
|
||||
if (!_currentClient.connected()) {
|
||||
_currentClient = WiFiClient();
|
||||
_currentStatus = HC_NONE;
|
||||
return;
|
||||
}
|
||||
|
||||
_currentClient = client;
|
||||
_contentLength = CONTENT_LENGTH_NOT_SET;
|
||||
_handleRequest();
|
||||
// Wait for data from client to become available
|
||||
if (_currentStatus == HC_WAIT_READ) {
|
||||
if (!_currentClient.available()) {
|
||||
if (millis() - _statusChange > HTTP_MAX_DATA_WAIT) {
|
||||
_currentClient = WiFiClient();
|
||||
_currentStatus = HC_NONE;
|
||||
}
|
||||
yield();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_parseRequest(_currentClient)) {
|
||||
_currentClient = WiFiClient();
|
||||
_currentStatus = HC_NONE;
|
||||
return;
|
||||
}
|
||||
|
||||
_contentLength = CONTENT_LENGTH_NOT_SET;
|
||||
_handleRequest();
|
||||
|
||||
if (!_currentClient.connected()) {
|
||||
_currentClient = WiFiClient();
|
||||
_currentStatus = HC_NONE;
|
||||
return;
|
||||
} else {
|
||||
_currentStatus = HC_WAIT_CLOSE;
|
||||
_statusChange = millis();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (_currentStatus == HC_WAIT_CLOSE) {
|
||||
if (millis() - _statusChange > HTTP_MAX_CLOSE_WAIT) {
|
||||
_currentClient = WiFiClient();
|
||||
_currentStatus = HC_NONE;
|
||||
} else {
|
||||
yield();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ESP8266WebServer::close() {
|
||||
@ -443,12 +483,7 @@ void ESP8266WebServer::_handleRequest() {
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t maxWait = HTTP_MAX_CLOSE_WAIT;
|
||||
while(_currentClient.connected() && maxWait--) {
|
||||
delay(1);
|
||||
}
|
||||
_currentClient = WiFiClient();
|
||||
_currentUri = String();
|
||||
_currentUri = String();
|
||||
}
|
||||
|
||||
const char* ESP8266WebServer::_responseCodeToString(int code) {
|
||||
|
Reference in New Issue
Block a user