From 004f4b51d111dd506180ad743023654e639718c6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 29 Jun 2025 21:41:57 -0500 Subject: [PATCH] preen --- .../web_server_idf/web_server_idf.cpp | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/esphome/components/web_server_idf/web_server_idf.cpp b/esphome/components/web_server_idf/web_server_idf.cpp index eb3a6b8401..d51b3485cc 100644 --- a/esphome/components/web_server_idf/web_server_idf.cpp +++ b/esphome/components/web_server_idf/web_server_idf.cpp @@ -569,6 +569,14 @@ void AsyncEventSourceResponse::deferrable_send_state(void *source, const char *e #ifdef USE_WEBSERVER_OTA esp_err_t AsyncWebServer::handle_multipart_upload_(httpd_req_t *r, const char *content_type) { + // Constants for upload handling + static constexpr size_t MULTIPART_CHUNK_SIZE = 1460; // Match Arduino AsyncWebServer buffer size + static constexpr size_t YIELD_INTERVAL_BYTES = 16 * 1024; // Yield every 16KB to prevent watchdog + + // Upload indices for handleUpload callbacks + static constexpr size_t UPLOAD_INDEX_BEGIN = 0; + static constexpr size_t UPLOAD_INDEX_WRITE = 1; + static constexpr size_t UPLOAD_INDEX_END = 2; // Parse boundary from content type const char *boundary_start = nullptr; size_t boundary_len = 0; @@ -617,29 +625,28 @@ esp_err_t AsyncWebServer::handle_multipart_upload_(httpd_req_t *r, const char *c } if (!ctx.started) { - ctx.handler->handleUpload(ctx.req, ctx.filename, 0, nullptr, 0, false); + ctx.handler->handleUpload(ctx.req, ctx.filename, UPLOAD_INDEX_BEGIN, nullptr, 0, false); ctx.started = true; } - ctx.handler->handleUpload(ctx.req, ctx.filename, 1, const_cast(data), len, false); + ctx.handler->handleUpload(ctx.req, ctx.filename, UPLOAD_INDEX_WRITE, const_cast(data), len, false); }); reader.set_part_complete_callback([&ctx]() { if (ctx.started) { - ctx.handler->handleUpload(ctx.req, ctx.filename, 2, nullptr, 0, true); + ctx.handler->handleUpload(ctx.req, ctx.filename, UPLOAD_INDEX_END, nullptr, 0, true); ctx.filename.clear(); ctx.started = false; } }); // Process chunks - static constexpr size_t CHUNK_SIZE = 1460; - std::unique_ptr buffer(new char[CHUNK_SIZE]); + std::unique_ptr buffer(new char[MULTIPART_CHUNK_SIZE]); size_t remaining = r->content_len; size_t bytes_since_yield = 0; while (remaining > 0) { - size_t to_read = std::min(remaining, CHUNK_SIZE); + size_t to_read = std::min(remaining, MULTIPART_CHUNK_SIZE); int recv_len = httpd_req_recv(r, buffer.get(), to_read); if (recv_len <= 0) { @@ -659,7 +666,7 @@ esp_err_t AsyncWebServer::handle_multipart_upload_(httpd_req_t *r, const char *c bytes_since_yield += recv_len; // Yield periodically to let main loop run - if (bytes_since_yield > 16 * 1024) { + if (bytes_since_yield > YIELD_INTERVAL_BYTES) { vTaskDelay(1); bytes_since_yield = 0; }