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