ESPHome 2026.10.2
Loading...
Searching...
No Matches
http_request_host.cpp
Go to the documentation of this file.
1#ifdef USE_HOST
2
3#define USE_HTTP_REQUEST_HOST_H
4#define CPPHTTPLIB_NO_EXCEPTIONS
5#include "httplib.h"
6#include "http_request_host.h"
7
8#include <cstring>
9
10#include <regex>
13
15#include "esphome/core/log.h"
16
17namespace esphome::http_request {
18
19static const char *const TAG = "http_request";
20
21std::shared_ptr<HttpContainer> HttpRequestHost::perform(const char *url, const char *method, const std::string &body,
22 const std::vector<Header> &request_headers,
23 const std::vector<std::string> &lower_case_collect_headers) {
24 if (!network::is_connected()) {
25 this->status_momentary_error("failed", 1000);
26 ESP_LOGW(TAG, "HTTP Request failed; Not connected to network");
27 return nullptr;
28 }
29
30 std::regex url_regex(R"(^(([^:\/?#]+):)?(//([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?)", std::regex::extended);
31 std::cmatch url_match_result;
32
33 if (!std::regex_match(url, url_match_result, url_regex) || url_match_result.length() < 7) {
34 ESP_LOGE(TAG, "HTTP Request failed; Malformed URL: %s", url);
35 return nullptr;
36 }
37 auto host = url_match_result[4].str();
38 auto scheme_host = url_match_result[1].str() + url_match_result[3].str();
39 auto path = url_match_result[5].str() + url_match_result[6].str();
40 if (path.empty())
41 path = "/";
42
43 std::shared_ptr<HttpContainerHost> container = std::make_shared<HttpContainerHost>();
44 container->set_parent(this);
45
46 const uint32_t start = millis();
47
49
50 httplib::Headers h_headers;
51 h_headers.emplace("Host", host.c_str());
52 h_headers.emplace("User-Agent", this->useragent_);
53 for (const auto &[name, value] : request_headers) {
54 h_headers.emplace(name, value);
55 }
56 httplib::Client client(scheme_host.c_str());
57 if (!client.is_valid()) {
58 ESP_LOGE(TAG, "HTTP Request failed; Invalid URL: %s", url);
59 return nullptr;
60 }
61 client.set_follow_location(this->follow_redirects_);
62#ifdef CPPHTTPLIB_OPENSSL_SUPPORT
63 if (this->ca_path_ != nullptr)
64 client.set_ca_cert_path(this->ca_path_);
65#endif
66
67 httplib::Result result;
68 if (strcmp(method, "GET") == 0) {
69 result = client.Get(path, h_headers, [&](const char *data, size_t data_length) {
70 ESP_LOGV(TAG, "Got data length: %zu", data_length);
71 container->response_body_.insert(container->response_body_.end(), (const uint8_t *) data,
72 (const uint8_t *) data + data_length);
73 return true;
74 });
75 } else if (strcmp(method, "HEAD") == 0) {
76 result = client.Head(path, h_headers);
77 } else if (strcmp(method, "PUT") == 0) {
78 result = client.Put(path, h_headers, body, "");
79 if (result) {
80 auto data = std::vector<uint8_t>(result->body.begin(), result->body.end());
81 container->response_body_.insert(container->response_body_.end(), data.begin(), data.end());
82 }
83 } else if (strcmp(method, "PATCH") == 0) {
84 result = client.Patch(path, h_headers, body, "");
85 if (result) {
86 auto data = std::vector<uint8_t>(result->body.begin(), result->body.end());
87 container->response_body_.insert(container->response_body_.end(), data.begin(), data.end());
88 }
89 } else if (strcmp(method, "POST") == 0) {
90 result = client.Post(path, h_headers, body, "");
91 if (result) {
92 auto data = std::vector<uint8_t>(result->body.begin(), result->body.end());
93 container->response_body_.insert(container->response_body_.end(), data.begin(), data.end());
94 }
95 } else {
96 ESP_LOGW(TAG, "HTTP Request failed - unsupported method %s; URL: %s", method, url);
97 container->end();
98 return nullptr;
99 }
100 App.feed_wdt();
101 if (!result) {
102 ESP_LOGW(TAG, "HTTP Request failed; URL: %s, error code: %u", url, (unsigned) result.error());
103 container->end();
104 this->status_momentary_error("failed", 1000);
105 return nullptr;
106 }
107 App.feed_wdt();
108 auto response = *result;
109 container->status_code = response.status;
110 if (!is_success(response.status)) {
111 ESP_LOGE(TAG, "HTTP Request failed; URL: %s; Code: %d", url, response.status);
112 this->status_momentary_error("failed", 1000);
113 // Still return the container, so it can be used to get the status code and error message
114 }
115
116 container->content_length = container->response_body_.size();
117 for (auto header : response.headers) {
118 ESP_LOGD(TAG, "Header: %s: %s", header.first.c_str(), header.second.c_str());
119 auto lower_name = str_lower_case(header.first); // NOLINT
120 if (should_collect_header(lower_case_collect_headers, lower_name)) {
121 container->response_headers_.push_back({lower_name, header.second});
122 }
123 }
124 container->duration_ms = millis() - start;
125 return container;
126}
127
128int HttpContainerHost::read(uint8_t *buf, size_t max_len) {
129 auto bytes_remaining = this->response_body_.size() - this->bytes_read_;
130 auto read_len = std::min(max_len, bytes_remaining);
131 memcpy(buf, this->response_body_.data() + this->bytes_read_, read_len);
132 this->bytes_read_ += read_len;
133 return read_len;
134}
135
137 watchdog::WatchdogManager wdm(this->parent_->get_watchdog_timeout());
138 this->response_body_ = std::vector<uint8_t>();
139 this->bytes_read_ = 0;
140}
141
142} // namespace esphome::http_request
143
144#endif // USE_HOST
void feed_wdt()
Feed the task watchdog.
void status_momentary_error(const char *name, uint32_t length=5000)
Set error status flag and automatically clear it after a timeout.
int read(uint8_t *buf, size_t max_len) override
std::shared_ptr< HttpContainer > start(const char *url, const char *method, const std::string &body, const std::vector< Header > &request_headers)
std::shared_ptr< HttpContainer > perform(const char *url, const char *method, const std::string &body, const std::vector< Header > &request_headers, const std::vector< std::string > &lower_case_collect_headers) override
bool should_collect_header(const std::vector< std::string > &lower_case_collect_headers, const std::string &lower_header_name)
Check if a header name should be collected (linear scan, fine for small lists)
bool is_success(int const status)
Checks if the given HTTP status code indicates a successful request.
ESPHOME_ALWAYS_INLINE bool is_connected()
Return whether the node is connected to the network (through wifi, eth, ...)
Definition util.h:28
std::string str_lower_case(const std::string &str)
Convert the string to lower case.
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t