Skip to content

Commit ecdbda8

Browse files
committed
Merge pull request esp8266#1401 from lp0/webserver-async1
WebServer: handle initial read/close timeouts asynchronously
2 parents f14ecdb + d099404 commit ecdbda8

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

libraries/ESP8266WebServer/src/ESP8266WebServer.cpp

+54-19
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ ESP8266WebServer::~ESP8266WebServer() {
8080
}
8181

8282
void ESP8266WebServer::begin() {
83+
_currentStatus = HC_NONE;
8384
_server.begin();
8485
if(!_headerKeysCount)
8586
collectHeaders(0, 0);
@@ -155,28 +156,67 @@ void ESP8266WebServer::serveStatic(const char* uri, FS& fs, const char* path, co
155156
}
156157

157158
void ESP8266WebServer::handleClient() {
158-
WiFiClient client = _server.available();
159-
if (!client) {
160-
return;
161-
}
159+
if (_currentStatus == HC_NONE) {
160+
WiFiClient client = _server.available();
161+
if (!client) {
162+
return;
163+
}
162164

163165
#ifdef DEBUG_ESP_HTTP_SERVER
164-
DEBUG_OUTPUT.println("New client");
166+
DEBUG_OUTPUT.println("New client");
165167
#endif
166168

167-
// Wait for data from client to become available
168-
uint16_t maxWait = HTTP_MAX_DATA_WAIT;
169-
while(client.connected() && !client.available() && maxWait--){
170-
delay(1);
169+
_currentClient = client;
170+
_currentStatus = HC_WAIT_READ;
171+
_statusChange = millis();
171172
}
172173

173-
if (!_parseRequest(client)) {
174+
if (!_currentClient.connected()) {
175+
_currentClient = WiFiClient();
176+
_currentStatus = HC_NONE;
174177
return;
175178
}
176179

177-
_currentClient = client;
178-
_contentLength = CONTENT_LENGTH_NOT_SET;
179-
_handleRequest();
180+
// Wait for data from client to become available
181+
if (_currentStatus == HC_WAIT_READ) {
182+
if (!_currentClient.available()) {
183+
if (millis() - _statusChange > HTTP_MAX_DATA_WAIT) {
184+
_currentClient = WiFiClient();
185+
_currentStatus = HC_NONE;
186+
}
187+
yield();
188+
return;
189+
}
190+
191+
if (!_parseRequest(_currentClient)) {
192+
_currentClient = WiFiClient();
193+
_currentStatus = HC_NONE;
194+
return;
195+
}
196+
197+
_contentLength = CONTENT_LENGTH_NOT_SET;
198+
_handleRequest();
199+
200+
if (!_currentClient.connected()) {
201+
_currentClient = WiFiClient();
202+
_currentStatus = HC_NONE;
203+
return;
204+
} else {
205+
_currentStatus = HC_WAIT_CLOSE;
206+
_statusChange = millis();
207+
return;
208+
}
209+
}
210+
211+
if (_currentStatus == HC_WAIT_CLOSE) {
212+
if (millis() - _statusChange > HTTP_MAX_CLOSE_WAIT) {
213+
_currentClient = WiFiClient();
214+
_currentStatus = HC_NONE;
215+
} else {
216+
yield();
217+
return;
218+
}
219+
}
180220
}
181221

182222
void ESP8266WebServer::close() {
@@ -442,12 +482,7 @@ void ESP8266WebServer::_handleRequest() {
442482
}
443483
}
444484

445-
uint16_t maxWait = HTTP_MAX_CLOSE_WAIT;
446-
while(_currentClient.connected() && maxWait--) {
447-
delay(1);
448-
}
449-
_currentClient = WiFiClient();
450-
_currentUri = String();
485+
_currentUri = String();
451486
}
452487

453488
const char* ESP8266WebServer::_responseCodeToString(int code) {

libraries/ESP8266WebServer/src/ESP8266WebServer.h

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
enum HTTPMethod { HTTP_ANY, HTTP_GET, HTTP_POST, HTTP_PUT, HTTP_PATCH, HTTP_DELETE, HTTP_OPTIONS };
3030
enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END,
3131
UPLOAD_FILE_ABORTED };
32+
enum HTTPClientStatus { HC_NONE, HC_WAIT_READ, HC_WAIT_CLOSE };
3233

3334
#define HTTP_DOWNLOAD_UNIT_SIZE 1460
3435
#define HTTP_UPLOAD_BUFLEN 2048
@@ -151,6 +152,8 @@ template<typename T> size_t streamFile(T &file, const String& contentType){
151152
WiFiClient _currentClient;
152153
HTTPMethod _currentMethod;
153154
String _currentUri;
155+
HTTPClientStatus _currentStatus;
156+
unsigned long _statusChange;
154157

155158
RequestHandler* _currentHandler;
156159
RequestHandler* _firstHandler;

0 commit comments

Comments
 (0)