ESPHome 2025.12.0-dev
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 <regex>
11
13#include "esphome/core/log.h"
14
15namespace esphome {
16namespace http_request {
17
18static const char *const TAG = "http_request.host";
19
20std::shared_ptr<HttpContainer> HttpRequestHost::perform(const std::string &url, const std::string &method,
21 const std::string &body,
22 const std::list<Header> &request_headers,
23 const std::set<std::string> &response_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::smatch 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.c_str());
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.c_str());
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 (method == "GET") {
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 (method == "HEAD") {
76 result = client.Head(path, h_headers);
77 } else if (method == "PUT") {
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 (method == "PATCH") {
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 (method == "POST") {
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.c_str(), url.c_str());
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.c_str(), (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.c_str(), 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);
120 if (response_headers.find(lower_name) != response_headers.end()) {
121 container->response_headers_[lower_name].emplace_back(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 http_request
143} // namespace esphome
144
145#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(const std::string &url, const std::string &method, const std::string &body, const std::list< Header > &request_headers, const 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:26
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:189
uint32_t IRAM_ATTR HOT millis()
Definition core.cpp:30
Application App
Global storage of Application pointer - only one Application can exist.