@@ -80,6 +80,8 @@ static const char *htmlContent PROGMEM = R"(
8080)" ;
8181
8282static const size_t htmlContentLength = strlen_P(htmlContent);
83+ static constexpr char characters[] = " 0123456789ABCDEF" ;
84+ static size_t charactersIndex = 0 ;
8385
8486static AsyncWebServer server (80 );
8587static AsyncEventSource events (" /events" );
@@ -104,6 +106,41 @@ void setup() {
104106 request->send (200 , " text/html" , (uint8_t *)htmlContent, htmlContentLength);
105107 });
106108
109+ // IMPORTANT - DO NOT WRITE SUCH CODE IN PRODUCTON !
110+ //
111+ // This example simulates the slowdown that can happen when:
112+ // - downloading a huge file from sdcard
113+ // - doing some file listing on SDCard because it is horribly slow to get a file listing with file stats on SDCard.
114+ // So in both cases, ESP would deadlock or TWDT would trigger.
115+ //
116+ // This example simulats that by slowing down the chunk callback:
117+ // - d=2000 is the delay in ms in the callback
118+ // - l=10000 is the length of the response
119+ //
120+ // time curl -N -v -G -d 'd=2000' -d 'l=10000' http://192.168.4.1/slow.html --output -
121+ //
122+ server.on (" /slow.html" , HTTP_GET, [](AsyncWebServerRequest *request) {
123+ uint32_t d = request->getParam (" d" )->value ().toInt ();
124+ uint32_t l = request->getParam (" l" )->value ().toInt ();
125+ Serial.printf (" d = %" PRIu32 " , l = %" PRIu32 " \n " , d, l);
126+ AsyncWebServerResponse *response = request->beginChunkedResponse (" text/html" , [d, l](uint8_t *buffer, size_t maxLen, size_t index) -> size_t {
127+ Serial.printf (" %u\n " , index);
128+ // finished ?
129+ if (index >= l) {
130+ return 0 ;
131+ }
132+
133+ // slow down the task to simulate some heavy processing, like SD card reading
134+ delay (d);
135+
136+ memset (buffer, characters[charactersIndex], 256 );
137+ charactersIndex = (charactersIndex + 1 ) % sizeof (characters);
138+ return 256 ;
139+ });
140+
141+ request->send (response);
142+ });
143+
107144 // SSS endpoint
108145 //
109146 // launch 16 concurrent workers for 30 seconds
0 commit comments