From 1927f923581492c8dc60a1881f5876d61673e6f8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 29 Jun 2025 19:49:01 -0500 Subject: [PATCH] cleanup --- .../web_server_idf/multipart_reader.cpp | 37 +++++++------------ .../web_server_idf/multipart_reader.h | 5 +-- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/esphome/components/web_server_idf/multipart_reader.cpp b/esphome/components/web_server_idf/multipart_reader.cpp index d60331d64f..4810f34738 100644 --- a/esphome/components/web_server_idf/multipart_reader.cpp +++ b/esphome/components/web_server_idf/multipart_reader.cpp @@ -53,50 +53,41 @@ size_t MultipartReader::parse(const char *data, size_t len) { return parsed; } -void MultipartReader::process_header_() { +void MultipartReader::process_header_(const std::string &value) { + // Process the completed header (field + value pair) if (str_startswith_case_insensitive(current_header_field_, "content-disposition")) { // Parse name and filename from Content-Disposition - current_part_.name = extract_header_param(current_header_value_, "name"); - current_part_.filename = extract_header_param(current_header_value_, "filename"); + current_part_.name = extract_header_param(value, "name"); + current_part_.filename = extract_header_param(value, "filename"); } else if (str_startswith_case_insensitive(current_header_field_, "content-type")) { - current_part_.content_type = str_trim(current_header_value_); + current_part_.content_type = str_trim(value); } + + // Clear field for next header + current_header_field_.clear(); } int MultipartReader::on_header_field(multipart_parser *parser, const char *at, size_t length) { MultipartReader *reader = static_cast(multipart_parser_get_data(parser)); - // If we were processing a value, save it - if (!reader->current_header_value_.empty()) { - reader->process_header_(); - reader->current_header_value_.clear(); - } - - // Start new header field + // Store the header field name reader->current_header_field_.assign(at, length); - reader->in_headers_ = true; - return 0; } int MultipartReader::on_header_value(multipart_parser *parser, const char *at, size_t length) { MultipartReader *reader = static_cast(multipart_parser_get_data(parser)); - reader->current_header_value_.append(at, length); + + // Process the header immediately with the value + std::string value(at, length); + reader->process_header_(value); + return 0; } int MultipartReader::on_headers_complete(multipart_parser *parser) { MultipartReader *reader = static_cast(multipart_parser_get_data(parser)); - // Process last header if any - if (!reader->current_header_value_.empty()) { - reader->process_header_(); - } - - reader->in_headers_ = false; - reader->current_header_field_.clear(); - reader->current_header_value_.clear(); - ESP_LOGV(TAG, "Part headers complete: name='%s', filename='%s', content_type='%s'", reader->current_part_.name.c_str(), reader->current_part_.filename.c_str(), reader->current_part_.content_type.c_str()); diff --git a/esphome/components/web_server_idf/multipart_reader.h b/esphome/components/web_server_idf/multipart_reader.h index 563e90e3cf..9d8f52cb1c 100644 --- a/esphome/components/web_server_idf/multipart_reader.h +++ b/esphome/components/web_server_idf/multipart_reader.h @@ -56,14 +56,11 @@ class MultipartReader { Part current_part_; std::string current_header_field_; - std::string current_header_value_; DataCallback data_callback_; PartCompleteCallback part_complete_callback_; - bool in_headers_{false}; - - void process_header_(); + void process_header_(const std::string &value); }; } // namespace web_server_idf