ESPAsyncWebServer
ESPAsyncWebServer copied to clipboard
User shall be able to get a notification which AsyncWebServerRequest is released
When a data to be sent needs to be pulled from external MCU, a pointer into related AsyncWebServerRequest can be sent ESP->MCU, and then ESP<-MCU (response with the data).
When the data arrives at ESP user application code running on it can cast a pointer into AsyncWebServerRequest and use it for building AsyncWebServerResponse.
But the TCP connection and associated AsyncWebServerRequest can be released in a meantime (before ESP will get the data data from the MCU)...
In order to secure the case the pointer needs to be validated before casting it to AsyncWebServerRequest.
Application code shall then maintain a list of active AsyncWebServerRequest for this.
The change to ESPAsyncWebServer helps to do it. When the connection is released it passes the pointer into user's callback as a parameter, so user can remove the invalid pointe from the list.
Following can be example user's application code which makes it done:
https://github.com/me-no-dev/ESPAsyncWebServer/commit/39e9b67651255c5365e03ba1eea0579f018dd013
std::deque<AsyncWebServerRequest*> activeHttpRequests;
bool isRequestValid(AsyncWebServerRequest *request) { for(int i=0; i<activeHttpRequests.size(); i++) { if (activeHttpRequests[i]==request) return true; } return false; }
void httpClientRegister(AsyncWebServerRequest *request) { activeHttpRequests.push_back(request); }
void httpClientDisconnectionHandler(AsyncWebServerRequest request) // <- the callback needs 'this' (AsyncWebServerRequest) as a parameter, change the change to ESPAsyncWebServer code { std::deque<AsyncWebServerRequest*>::iterator it;
it = activeHttpRequests.begin();
while (it != activeHttpRequests.end()) {
if (*it == request) break;
it++;
}
activeHttpRequests.erase(it);
}
void ApiOnBody(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) //this is called when POST request comes to ESP over WiFi { ... httpClientRegister(request); request->onDisconnect(httpClientDisconnectionHandler); ... }
void sendApiResponseToHttpClient(const char* data) // the is called when ESP recives data from MCU { int32_t dataSeparatorOffset; AsyncWebServerRequest *request;
... get 'request' ptr value from msg incomming from MCU ...
if(!isRequestValid(request)) return; // too late, disconnected already
AsyncWebServerResponse *response = request->beginResponse(200, "text/xml", data); if(NULL!=response) request->send(response); }
https://github.com/me-no-dev/ESPAsyncWebServer/commit/39e9b67651255c5365e03ba1eea0579f018dd013 solves it.
[STALE_SET] This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions.