ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
utils.cpp
Go to the documentation of this file.
1#ifdef USE_ESP32
2#include <cstring>
3#include <cctype>
5#include "http_parser.h"
6
7#include "utils.h"
8
10
11size_t url_decode(char *str) {
12 char *start = str;
13 char *ptr = str, buf;
14 for (; *str; str++, ptr++) {
15 if (*str == '%') {
16 str++;
17 if (parse_hex(str, 2, reinterpret_cast<uint8_t *>(&buf), 1) == 2) {
18 *ptr = buf;
19 str++;
20 } else {
21 str--;
22 *ptr = *str;
23 }
24 } else if (*str == '+') {
25 *ptr = ' ';
26 } else {
27 *ptr = *str;
28 }
29 }
30 *ptr = '\0';
31 return ptr - start;
32}
33
34bool request_has_header(httpd_req_t *req, const char *name) { return httpd_req_get_hdr_value_len(req, name); }
35
36optional<std::string> request_get_header(httpd_req_t *req, const char *name) {
37 size_t len = httpd_req_get_hdr_value_len(req, name);
38 if (len == 0) {
39 return {};
40 }
41
42 std::string str;
43 str.resize(len);
44
45 auto res = httpd_req_get_hdr_value_str(req, name, &str[0], len + 1);
46 if (res != ESP_OK) {
47 return {};
48 }
49
50 return {str};
51}
52
53size_t request_get_header_to(httpd_req_t *req, const char *name, std::span<char> buf) {
54 const size_t len = httpd_req_get_hdr_value_len(req, name);
55 if (len == 0)
56 return 0;
57 if (len >= buf.size() || httpd_req_get_hdr_value_str(req, name, buf.data(), buf.size()) != ESP_OK)
58 return SIZE_MAX;
59 return len;
60}
61
62optional<std::string> query_key_value(const char *query_url, size_t query_len, const char *key) {
63 if (query_url == nullptr || query_len == 0) {
64 return {};
65 }
66
67 // Value can't exceed query_len. Use small stack buffer for typical values,
68 // heap fallback for long ones (e.g. base64 IR data) to limit stack usage
69 // since callers may also have stack buffers for the query string.
71 if (httpd_query_key_value(query_url, key, val.get(), query_len) != ESP_OK) {
72 return {};
73 }
74
75 url_decode(val.get());
76 return {val.get()};
77}
78
79bool query_has_key(const char *query_url, size_t query_len, const char *key) {
80 if (query_url == nullptr || query_len == 0) {
81 return false;
82 }
83 // Minimal buffer — we only care if the key exists, not the value
84 char buf[1];
85 // httpd_query_key_value returns ESP_OK if found, ESP_ERR_HTTPD_RESULT_TRUNC if found
86 // but value truncated (expected with 1-byte buffer), or other errors for invalid input
87 auto err = httpd_query_key_value(query_url, key, buf, sizeof(buf));
88 return err == ESP_OK || err == ESP_ERR_HTTPD_RESULT_TRUNC;
89}
90
91// Helper function for case-insensitive string region comparison
92bool str_ncmp_ci(const char *s1, const char *s2, size_t n) {
93 for (size_t i = 0; i < n; i++) {
94 if (!char_equals_ci(s1[i], s2[i])) {
95 return false;
96 }
97 }
98 return true;
99}
100
101// Bounded case-insensitive string search (like strcasestr but length-bounded)
102const char *strcasestr_n(const char *haystack, size_t haystack_len, const char *needle) {
103 if (!haystack) {
104 return nullptr;
105 }
106
107 size_t needle_len = strlen(needle);
108 if (needle_len == 0) {
109 return haystack;
110 }
111
112 if (haystack_len < needle_len) {
113 return nullptr;
114 }
115
116 const char *end = haystack + haystack_len - needle_len + 1;
117 for (const char *p = haystack; p < end; p++) {
118 if (str_ncmp_ci(p, needle, needle_len)) {
119 return p;
120 }
121 }
122
123 return nullptr;
124}
125
126} // namespace esphome::web_server_idf
127#endif // USE_ESP32
Helper class for efficient buffer allocation - uses stack for small sizes, heap for large This is use...
Definition helpers.h:872
const char * key
mopeka_std_values val[3]
bool query_has_key(const char *query_url, size_t query_len, const char *key)
Definition utils.cpp:79
bool char_equals_ci(char a, char b)
Definition utils.h:24
optional< std::string > request_get_header(httpd_req_t *req, const char *name)
Definition utils.cpp:36
size_t request_get_header_to(httpd_req_t *req, const char *name, std::span< char > buf)
Copy a header value into buf, nul-terminated, without heap allocation.
Definition utils.cpp:53
optional< std::string > query_key_value(const char *query_url, size_t query_len, const char *key)
Definition utils.cpp:62
const char * strcasestr_n(const char *haystack, size_t haystack_len, const char *needle)
Definition utils.cpp:102
bool str_ncmp_ci(const char *s1, const char *s2, size_t n)
Definition utils.cpp:92
size_t url_decode(char *str)
Decode URL-encoded string in-place (e.g., %20 -> space, + -> space) Returns the new length of the dec...
Definition utils.cpp:11
bool request_has_header(httpd_req_t *req, const char *name)
Definition utils.cpp:34
const void size_t len
Definition hal.h:68
size_t parse_hex(const char *str, size_t length, uint8_t *data, size_t count)
Parse bytes from a hex-encoded string into a byte array.
Definition helpers.cpp:370
uint8_t end[39]
Definition sun_gtil2.cpp:17