ESPAsyncWebServer icon indicating copy to clipboard operation
ESPAsyncWebServer copied to clipboard

Increasing maxLen size in chunked responses

Open abratchik opened this issue 2 years ago • 3 comments

Is it possible to change/increase maxLen parameter when sending a chunked response? based on the documentation:

AsyncWebServerResponse *response = request->beginChunkedResponse("text/plain", [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t {
  //Write up to "maxLen" bytes into "buffer" and return the amount written.
  //index equals the amount of bytes that have been already sent
  //You will be asked for more data until 0 is returned
  //Keep in mind that you can not delay or yield waiting for more data!
  return mySource.read(buffer, maxLen);
});
response->addHeader("Server","ESP Async Web Server");
request->send(response);

This works fine but I cannot figure out how to increase the buffer size (maxLen parameter). Currently, the server sends 5736 bytes in it and this is not enough for all cases. Is it possible to make it 64k, for example? Is there a way I could increase it through the API?

abratchik avatar Jan 07 '23 12:01 abratchik

[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.

stale[bot] avatar May 21 '23 21:05 stale[bot]

Is your problem that you want to transfer a single file with a size of more than 6kB, or do you have a situation where a single chunk(one of multiple segments of the file) has to be more than 6kB?

If its the latter it might just be too much for an ESP anyway, or if the esp can handle it that would indeed require a change to this library.

If its the former, thats exactly what chunked responses are for. As the documentation there tries to tell you, your callback is supposed to generate a part of the file with the given length, not the entire file.

Then after that part is sent, your callback is called again to generate the next chunk of the file. That process is repeated until the file is fully sent. I used to use this to send files that are a few MB in size, so more than the total ram of an ESP. Here is an example of how to use chunked responses to send individual chunks, that could be extended to send a few MG.

#include <ESPAsyncWebServer.h>

#include <stdio.h>

extern const char WIFI_SSID[] = "WiFi_SSID";
extern const char WIFI_PASS[] = "WPA_PASSPHRASE";

AsyncWebServer server(80);

void onWiFiGotIp(WiFiEvent_t event, WiFiEventInfo_t eventInfo) {
	Serial.print("IP Address: ");
	Serial.println((IPAddress) eventInfo.got_ip.ip_info.ip.addr);
}

void onWiFiDisonnected(WiFiEvent_t event) {
	Serial.println("WiFi Disconnected");
	WiFi.disconnect(true);
	delay(100);
	WiFi.begin(WIFI_SSID, WIFI_PASS);
}

void setupWebServer() {
	server.on("/", HTTP_GET, [] (AsyncWebServerRequest *request) {
		std::shared_ptr<uint16_t> counter = std::make_shared<uint16_t>(0);
		AsyncWebServerResponse *response = request->beginChunkedResponse("text/plain", [counter](uint8_t *buffer, size_t maxLen, size_t index) {
			if (*counter < 50) {
				int len = sprintf((char*)buffer, "Content Line %d\n", *counter);
				(*counter)++;
				return len;
			}
			return 0;
		});
		request->send(response);
	});

	server.onNotFound([] (AsyncWebServerRequest *request) {
		request->send(404, "text/plain", "Not Found");
	});

	server.begin();
}

void setup() {
	Serial.begin(115200);
	Serial.println("setup start");

	WiFi.disconnect(true);
	WiFi.onEvent(onWiFiGotIp, SYSTEM_EVENT_STA_GOT_IP);
	WiFi.onEvent(onWiFiDisonnected, SYSTEM_EVENT_STA_DISCONNECTED);
	WiFi.begin(WIFI_SSID, WIFI_PASS);

	setupWebServer();
	Serial.println("setup end");
}

void loop() {
	delay(100);
}

When the root page of the server on the esp is requested, this sketch sends 50 lines of plain text as a response, each as their own chunk. This same example can easily be extended to 5000 lines, which is more than 80 kB.

ToMe25 avatar May 24 '23 18:05 ToMe25

[STALE_CLR] This issue has been removed from the stale queue. Please ensure activity to keep it openin the future.

stale[bot] avatar May 24 '23 18:05 stale[bot]