Skip to content

Commit

Permalink
Manually implement chunking -- the default implementation hangs and f…
Browse files Browse the repository at this point in the history
…ails
  • Loading branch information
sidoh committed Oct 17, 2024
1 parent 166949d commit 658176e
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions lib/WebServer/MiLightHttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down

0 comments on commit 658176e

Please sign in to comment.