changed function for sending big data from WEB server

pull/60/head
Miroslav Pivovarsky 2024-07-12 21:56:09 +02:00
parent 44e6028a0a
commit 3eb1262366
1 changed files with 12 additions and 0 deletions

View File

@ -1121,8 +1121,20 @@ void Server_resume() {
* @param const char* - data * @param const char* - data
*/ */
void Server_handleCacheRequest(AsyncWebServerRequest* request, const char* contentType, const char* data) { void Server_handleCacheRequest(AsyncWebServerRequest* request, const char* contentType, const char* data) {
/*
AsyncWebServerResponse* response = request->beginResponse(200, contentType, data); AsyncWebServerResponse* response = request->beginResponse(200, contentType, data);
response->addHeader("Cache-Control", "public, max-age=" + String(WEB_CACHE_INTERVAL)); response->addHeader("Cache-Control", "public, max-age=" + String(WEB_CACHE_INTERVAL));
request->send(response);
*/
AsyncWebServerResponse* response = request->beginChunkedResponse(contentType, [data](uint8_t *buffer, size_t maxLen, size_t index) -> size_t {
const char* dataStart = data + index; // current position in data
size_t dataLeft = strlen(data) - index; // how many bytes are left to send
size_t chunkSize = dataLeft < maxLen ? dataLeft : maxLen; // how many bytes we can send now
memcpy(buffer, dataStart, chunkSize); // copy chunk of data to buffer
return chunkSize; // return chunk size
});
response->addHeader("Cache-Control", "public, max-age=" + String(WEB_CACHE_INTERVAL));
request->send(response); request->send(response);
} }