From 0c868cbcc5e579459bc99c60bbc4f55efdc80521 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 28 Jan 2026 06:38:01 -1000 Subject: [PATCH] adjust comments, cases were reversed as I had the wrong file open --- .../http_request/http_request_arduino.cpp | 3 ++- .../http_request/http_request_idf.cpp | 18 +++++++++--------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/esphome/components/http_request/http_request_arduino.cpp b/esphome/components/http_request/http_request_arduino.cpp index 046e5decff..16f999c671 100644 --- a/esphome/components/http_request/http_request_arduino.cpp +++ b/esphome/components/http_request/http_request_arduino.cpp @@ -133,7 +133,8 @@ std::shared_ptr HttpRequestArduino::perform(const std::string &ur // HTTPClient::getSize() returns -1 for chunked transfer encoding (no Content-Length). // When cast to size_t, -1 becomes SIZE_MAX (4294967295 on 32-bit). - // The read() method handles this by checking content_length > 0 before using it. + // The read() method handles this: bytes_read_ can never reach SIZE_MAX, so the + // early return check (bytes_read_ >= content_length) will never trigger. int content_length = container->client_.getSize(); ESP_LOGD(TAG, "Content-Length: %d", content_length); container->content_length = (size_t) content_length; diff --git a/esphome/components/http_request/http_request_idf.cpp b/esphome/components/http_request/http_request_idf.cpp index 9ef5216a25..2b4dee953a 100644 --- a/esphome/components/http_request/http_request_idf.cpp +++ b/esphome/components/http_request/http_request_idf.cpp @@ -157,8 +157,7 @@ std::shared_ptr HttpRequestIDF::perform(const std::string &url, c } container->feed_wdt(); - // esp_http_client_fetch_headers() returns -1 for chunked transfer encoding (no Content-Length). - // When stored in size_t content_length, -1 becomes 0 due to the int64_t to size_t conversion. + // esp_http_client_fetch_headers() returns 0 for chunked transfer encoding (no Content-Length header). // The read() method handles content_length == 0 specially to support chunked responses. container->content_length = esp_http_client_fetch_headers(client); container->feed_wdt(); @@ -232,10 +231,10 @@ std::shared_ptr HttpRequestIDF::perform(const std::string &url, c // < 0: error/connection closed // // Note on chunked transfer encoding: -// esp_http_client_fetch_headers() returns -1 for chunked responses, which becomes 0 -// when stored in size_t content_length. We handle this by skipping the content_length -// check when content_length is 0, allowing esp_http_client_read() to handle chunked -// decoding internally and signal EOF by returning 0. +// esp_http_client_fetch_headers() returns 0 for chunked responses (no Content-Length header). +// We handle this by skipping the content_length check when content_length is 0, +// allowing esp_http_client_read() to handle chunked decoding internally and signal EOF +// by returning 0. int HttpContainerIDF::read(uint8_t *buf, size_t max_len) { const uint32_t start = millis(); watchdog::WatchdogManager wdm(this->parent_->get_watchdog_timeout()); @@ -260,10 +259,11 @@ int HttpContainerIDF::read(uint8_t *buf, size_t max_len) { // esp_http_client_read() returns 0 in two cases: // 1. Known content_length: connection closed before all data received (error) - // 2. Chunked encoding (content_length == 0): all data received (EOF) + // 2. Chunked encoding (content_length == 0): end of stream reached (EOF) // For case 1, returning HTTP_ERROR_CONNECTION_CLOSED is correct. - // For case 2, it's semantically an EOF not an error, but functionally correct - // because all data is already in the caller's buffer at this point. + // For case 2, 0 indicates that all chunked data has already been delivered + // in previous successful read() calls, so treating this as a closed + // connection does not cause any loss of response data. if (read_len_or_error == 0) { return HTTP_ERROR_CONNECTION_CLOSED; }