15namespace http_request {
17static const char *
const TAG =
"http_request.ota";
39 if (this->
url_.empty()) {
40 ESP_LOGE(TAG,
"URL not set; cannot start update");
44 ESP_LOGI(TAG,
"Starting update");
45#ifdef USE_OTA_STATE_LISTENER
49 auto ota_status = this->
do_ota_();
53#ifdef USE_OTA_STATE_LISTENER
61#ifdef USE_OTA_STATE_LISTENER
71 const std::shared_ptr<HttpContainer> &container) {
73 ESP_LOGV(TAG,
"Aborting OTA backend");
76 ESP_LOGV(TAG,
"Aborting HTTP connection");
82 uint32_t last_progress = 0;
83 uint32_t update_start_time =
millis();
85 std::unique_ptr<char[]> md5_receive_str(
new char[33]);
91 ESP_LOGD(TAG,
"MD5 expected: %s", this->
md5_expected_.c_str());
94 if (url_with_auth.empty()) {
97 ESP_LOGVV(TAG,
"url_with_auth: %s", url_with_auth.c_str());
98 ESP_LOGI(TAG,
"Connecting to: %s", this->
url_.c_str());
100 auto container = this->
parent_->get(url_with_auth);
102 if (container ==
nullptr || container->status_code !=
HTTP_STATUS_OK) {
108 ESP_LOGV(TAG,
"MD5Digest initialized\n"
109 "OTA backend begin");
111 auto error_code = backend->begin(container->content_length);
113 ESP_LOGW(TAG,
"backend->begin error: %d", error_code);
114 this->
cleanup_(std::move(backend), container);
120 uint32_t last_data_time =
millis();
121 const uint32_t read_timeout = this->
parent_->get_timeout();
123 while (container->get_bytes_read() < container->content_length) {
126 ESP_LOGVV(TAG,
"bytes_read_ = %u, body_length_ = %u, bufsize_or_error = %i", container->get_bytes_read(),
127 container->content_length, bufsize_or_error);
133 auto result =
http_read_loop_result(bufsize_or_error, last_data_time, read_timeout, container->is_read_complete());
142 ESP_LOGE(TAG,
"Timeout reading data");
144 ESP_LOGE(TAG,
"Error reading data: %d", bufsize_or_error);
146 this->
cleanup_(std::move(backend), container);
153 md5_receive.
add(buf, bufsize_or_error);
157 error_code = backend->write(buf, bufsize_or_error);
161 ESP_LOGE(TAG,
"Error code (%02X) writing binary data to flash at offset %d and size %d", error_code,
162 container->get_bytes_read() - bufsize_or_error, container->content_length);
163 this->
cleanup_(std::move(backend), container);
169 if ((now - last_progress > 1000) or (container->get_bytes_read() == container->content_length)) {
171 float percentage = container->get_bytes_read() * 100.0f / container->content_length;
172 ESP_LOGD(TAG,
"Progress: %0.1f%%", percentage);
173#ifdef USE_OTA_STATE_LISTENER
179 ESP_LOGI(TAG,
"Done in %.0f seconds",
float(
millis() - update_start_time) / 1000);
183 md5_receive.
get_hex(md5_receive_str.get());
185 if (strncmp(this->
md5_computed_.c_str(), this->md5_expected_.c_str(), MD5_SIZE) != 0) {
186 ESP_LOGE(TAG,
"MD5 computed: %s - Aborting due to MD5 mismatch", this->
md5_computed_.c_str());
187 this->
cleanup_(std::move(backend), container);
190 backend->set_update_md5(md5_receive_str.get());
200 error_code = backend->end();
202 ESP_LOGW(TAG,
"Error ending update! error_code: %d", error_code);
203 this->
cleanup_(std::move(backend), container);
207 ESP_LOGI(TAG,
"Update complete");
212 if (this->
username_.empty() || this->password_.empty()) {
216 auto start_char = url.find(
"://");
217 if ((start_char == std::string::npos) || (start_char < 4)) {
218 ESP_LOGE(TAG,
"Incorrect URL prefix");
222 ESP_LOGD(TAG,
"Using basic HTTP authentication");
226 url.substr(0, start_char) + this->
username_ +
":" + this->
password_ +
"@" + url.substr(start_char);
227 return url_with_auth;
236 if (url_with_auth.empty()) {
240 ESP_LOGVV(TAG,
"url_with_auth: %s", url_with_auth.c_str());
241 ESP_LOGI(TAG,
"Connecting to: %s", this->
md5_url_.c_str());
242 auto container = this->
parent_->get(url_with_auth);
243 if (container ==
nullptr) {
244 ESP_LOGE(TAG,
"Failed to connect to MD5 URL");
247 size_t length = container->content_length;
253 ESP_LOGE(TAG,
"MD5 file must be %u bytes; %u bytes reported by HTTP server. Aborting", MD5_SIZE,
length);
259 auto result =
http_read_fully(container.get(), (uint8_t *) this->md5_expected_.data(), MD5_SIZE, MD5_SIZE,
260 this->parent_->get_timeout());
265 ESP_LOGE(TAG,
"Timeout reading MD5");
267 ESP_LOGE(TAG,
"Error reading MD5: %d", result.error_code);
275 if ((url.length() < 8) || !url.starts_with(
"http") || (url.find(
"://") == std::string::npos)) {
276 ESP_LOGE(TAG,
"URL is invalid and/or must be prefixed with 'http://' or 'https://'");
void feed_wdt(uint32_t time=0)
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_
void cleanup_(std::unique_ptr< ota::OTABackend > backend, const std::shared_ptr< HttpContainer > &container)
static const uint16_t HTTP_RECV_BUFFER
bool validate_url_(const std::string &url)
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.
std::unique_ptr< ota::OTABackend > make_ota_backend()
@ OTA_RESPONSE_ERROR_MD5_MISMATCH
Providing packet encoding functions for exchanging data with a remote host.
void IRAM_ATTR HOT yield()
void IRAM_ATTR HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.