14static const char *
const TAG =
"http_request.ota";
36 if (this->
url_.empty()) {
37 ESP_LOGE(TAG,
"URL not set; cannot start update");
41 ESP_LOGI(TAG,
"Starting update");
42#ifdef USE_OTA_STATE_LISTENER
46 auto ota_status = this->
do_ota_();
50#ifdef USE_OTA_STATE_LISTENER
58#ifdef USE_OTA_STATE_LISTENER
69 ESP_LOGV(TAG,
"Aborting OTA backend");
72 ESP_LOGV(TAG,
"Aborting HTTP connection");
81 char md5_receive_str[33];
87 ESP_LOGD(TAG,
"MD5 expected: %s", this->
md5_expected_.c_str());
90 if (url_with_auth.empty()) {
93 ESP_LOGVV(TAG,
"url_with_auth: %s", url_with_auth.c_str());
94 ESP_LOGI(TAG,
"Connecting to: %s", this->
url_.c_str());
96 auto container = this->
parent_->get(url_with_auth);
98 if (container ==
nullptr || container->status_code !=
HTTP_STATUS_OK) {
104 ESP_LOGV(TAG,
"MD5Digest initialized, OTA backend begin");
106 auto error_code = backend->begin(container->content_length);
108 ESP_LOGW(TAG,
"backend->begin error: %d", error_code);
109 this->
cleanup_(std::move(backend), container);
118 while (container->get_bytes_read() < container->content_length) {
121 ESP_LOGVV(TAG,
"bytes_read_ = %u, body_length_ = %u, bufsize_or_error = %i", container->get_bytes_read(),
122 container->content_length, bufsize_or_error);
128 auto result =
http_read_loop_result(bufsize_or_error, last_data_time, read_timeout, container->is_read_complete());
139 ESP_LOGE(TAG,
"Timeout reading data");
141 ESP_LOGE(TAG,
"Error reading data: %d", bufsize_or_error);
143 this->
cleanup_(std::move(backend), container);
150 md5_receive.
add(buf, bufsize_or_error);
154 error_code = backend->write(buf, bufsize_or_error);
158 ESP_LOGE(TAG,
"Error code (%02X) writing binary data to flash at offset %d and size %d", error_code,
159 container->get_bytes_read() - bufsize_or_error, container->content_length);
160 this->
cleanup_(std::move(backend), container);
166 if ((now - last_progress > 1000) or (container->get_bytes_read() == container->content_length)) {
168 float percentage = container->get_bytes_read() * 100.0f / container->content_length;
169 ESP_LOGD(TAG,
"Progress: %0.1f%%", percentage);
170#ifdef USE_OTA_STATE_LISTENER
176 ESP_LOGI(TAG,
"Done in %.0f seconds",
float(
millis() - update_start_time) / 1000);
180 md5_receive.
get_hex(md5_receive_str);
182 if (strncmp(this->
md5_computed_.c_str(), this->md5_expected_.c_str(), MD5_SIZE) != 0) {
183 ESP_LOGE(TAG,
"MD5 computed: %s - Aborting due to MD5 mismatch", this->
md5_computed_.c_str());
184 this->
cleanup_(std::move(backend), container);
187 backend->set_update_md5(md5_receive_str);
197 error_code = backend->end();
199 ESP_LOGW(TAG,
"Error ending update! error_code: %d", error_code);
200 this->
cleanup_(std::move(backend), container);
204 ESP_LOGI(TAG,
"Update complete");
210static std::string url_encode(
const std::string &str) {
212 result.reserve(str.size());
214 if (std::isalnum(
static_cast<unsigned char>(c)) || c ==
'-' || c ==
'_' || c ==
'.' || c ==
'~') {
229 if (this->
username_.empty() || this->password_.empty()) {
233 auto start_char = url.find(
"://");
234 if ((start_char == std::string::npos) || (start_char < 4)) {
235 ESP_LOGE(TAG,
"Incorrect URL prefix");
239 ESP_LOGD(TAG,
"Using basic HTTP authentication");
243 url.substr(0, start_char) + this->
username_ +
":" + this->
password_ +
"@" + url.substr(start_char);
244 return url_with_auth;
253 if (url_with_auth.empty()) {
257 ESP_LOGVV(TAG,
"url_with_auth: %s", url_with_auth.c_str());
258 ESP_LOGI(TAG,
"Connecting to: %s", this->
md5_url_.c_str());
259 auto container = this->
parent_->get(url_with_auth);
260 if (container ==
nullptr) {
261 ESP_LOGE(TAG,
"Failed to connect to MD5 URL");
264 size_t length = container->content_length;
270 ESP_LOGE(TAG,
"MD5 file must be %u bytes; %u bytes reported by HTTP server. Aborting", MD5_SIZE,
length);
276 auto result =
http_read_fully(container.get(), (uint8_t *) this->md5_expected_.data(), MD5_SIZE, MD5_SIZE,
277 this->parent_->get_timeout());
282 ESP_LOGE(TAG,
"Timeout reading MD5");
284 ESP_LOGE(TAG,
"Error reading MD5: %d", result.error_code);
292 if ((url.length() < 8) || !url.starts_with(
"http") || (url.find(
"://") == std::string::npos)) {
293 ESP_LOGE(TAG,
"URL is invalid and/or must be prefixed with 'http://' or 'https://'");
void feed_wdt()
Feed the task watchdog.
void get_hex(char *output)
Retrieve the hash as hex characters. Output buffer must hold get_size() * 2 + 1 bytes.
HttpRequestComponent * parent_
void set_url(const std::string &url)
void dump_config() override
std::string md5_computed_
static const uint16_t HTTP_RECV_BUFFER
void set_password(const std::string &password)
bool validate_url_(const std::string &url)
void cleanup_(ota::OTABackendPtr backend, const std::shared_ptr< HttpContainer > &container)
void set_username(const std::string &username)
std::string get_url_with_auth_(const std::string &url)
std::string md5_expected_
void set_md5_url(const std::string &md5_url)
void calculate() override
Compute the digest, based on the provided data.
void add(const uint8_t *data, size_t len) override
Add bytes of data for the digest.
void init() override
Initialize a new MD5 digest computation.
void notify_state_(OTAState state, float progress, uint8_t error)
@ TIMEOUT
Timeout waiting for data, caller should exit loop.
@ COMPLETE
All content has been read, caller should exit loop.
@ RETRY
No data yet, already delayed, caller should continue loop.
@ DATA
Data was read, process it.
HttpReadLoopResult http_read_loop_result(int bytes_read_or_error, uint32_t &last_data_time, uint32_t timeout_ms, bool is_read_complete)
Process a read result with timeout tracking and delay handling.
HttpReadResult http_read_fully(HttpContainer *container, uint8_t *buffer, size_t total_size, size_t chunk_size, uint32_t timeout_ms)
Read data from HTTP container into buffer with timeout handling Handles feed_wdt, yield,...
@ TIMEOUT
Timeout waiting for data.
@ OK
Read completed successfully.
decltype(make_ota_backend()) OTABackendPtr
@ OTA_RESPONSE_ERROR_MD5_MISMATCH
std::unique_ptr< ArduinoLibreTinyOTABackend > make_ota_backend()
ESPHOME_ALWAYS_INLINE char format_hex_pretty_char(uint8_t v)
Convert a nibble (0-15) to uppercase hex char (used for pretty printing)
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.