mirror of
https://github.com/esphome/esphome.git
synced 2026-03-03 03:08:21 -07:00
Extract search_query_sources to deduplicate hasArg/find_query_value_
Both methods iterated post_query_ then url_query with the same pattern. Extracted a file-local template that takes a callback, avoiding duplicated request_get_url_query heap allocation logic.
This commit is contained in:
@@ -412,27 +412,29 @@ AsyncWebParameter *AsyncWebServerRequest::getParam(const char *name) {
|
||||
return param;
|
||||
}
|
||||
|
||||
optional<std::string> AsyncWebServerRequest::find_query_value_(const char *name) const {
|
||||
auto val = query_key_value(this->post_query_.c_str(), this->post_query_.size(), name);
|
||||
if (val.has_value()) {
|
||||
return val;
|
||||
/// Search post_query then URL query with a callback.
|
||||
/// Returns first truthy result, or value-initialized default.
|
||||
template<typename Func>
|
||||
static auto search_query_sources(const AsyncWebServerRequest &req, const std::string &post_query, const char *name,
|
||||
Func func) -> decltype(func(nullptr, size_t{0}, name)) {
|
||||
auto result = func(post_query.c_str(), post_query.size(), name);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
auto url_query = request_get_url_query(*this);
|
||||
auto url_query = request_get_url_query(req);
|
||||
if (url_query.has_value()) {
|
||||
return query_key_value(url_query.value().c_str(), url_query.value().size(), name);
|
||||
return func(url_query.value().c_str(), url_query.value().size(), name);
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
optional<std::string> AsyncWebServerRequest::find_query_value_(const char *name) const {
|
||||
return search_query_sources(*this, this->post_query_, name,
|
||||
[](const char *q, size_t len, const char *k) { return query_key_value(q, len, k); });
|
||||
}
|
||||
|
||||
bool AsyncWebServerRequest::hasArg(const char *name) {
|
||||
if (query_has_key(this->post_query_.c_str(), this->post_query_.size(), name)) {
|
||||
return true;
|
||||
}
|
||||
auto url_query = request_get_url_query(*this);
|
||||
if (url_query.has_value()) {
|
||||
return query_has_key(url_query.value().c_str(), url_query.value().size(), name);
|
||||
}
|
||||
return false;
|
||||
return search_query_sources(*this, this->post_query_, name, query_has_key);
|
||||
}
|
||||
|
||||
std::string AsyncWebServerRequest::arg(const char *name) {
|
||||
|
||||
Reference in New Issue
Block a user