From 658176e02092be984d4ea7ebc1e2be542bef0ca5 Mon Sep 17 00:00:00 2001 From: Chris Mullins Date: Wed, 16 Oct 2024 21:08:48 -0700 Subject: [PATCH] Manually implement chunking -- the default implementation hangs and fails --- lib/WebServer/MiLightHttpServer.cpp | 30 +++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/lib/WebServer/MiLightHttpServer.cpp b/lib/WebServer/MiLightHttpServer.cpp index a0c1baa8..e7bddadc 100644 --- a/lib/WebServer/MiLightHttpServer.cpp +++ b/lib/WebServer/MiLightHttpServer.cpp @@ -662,11 +662,33 @@ void MiLightHttpServer::handlePacketSent(uint8_t *packet, const MiLightRemoteCon } void MiLightHttpServer::handleServe_P(const char* data, size_t length) { - server.sendHeader("Content-Encoding", "gzip"); + const size_t CHUNK_SIZE = 16384; + server.setContentLength(CONTENT_LENGTH_UNKNOWN); - server.send(200, "text/html", ""); - server.sendContent_P(data, length); - server.sendContent(""); + server.sendHeader("Content-Encoding", "gzip"); + server.send(200, "text/html"); + + WiFiClient client = server.client(); + + size_t remaining = length; + while (remaining > 0) { + size_t chunk = remaining > CHUNK_SIZE ? CHUNK_SIZE : remaining; + + // Send chunk size in hexadecimal format + client.printf("%zx\r\n", chunk); + + // Send chunk data + client.write_P(data, chunk); + client.print("\r\n"); + + data += chunk; + remaining -= chunk; + yield(); + } + + // Send the terminal chunk + client.print("0\r\n\r\n"); + server.client().stop(); }