check for "http" before allocating a http-client

This commit is contained in:
MichaelDvP
2026-07-11 19:12:34 +02:00
parent 193e5d591c
commit 188cc7b666

View File

@@ -707,8 +707,7 @@ std::string compute(const std::string & expr) {
std::string cmd = expr_new.substr(f, e - f).c_str(); std::string cmd = expr_new.substr(f, e - f).c_str();
JsonDocument doc; JsonDocument doc;
if (DeserializationError::Ok == deserializeJson(doc, cmd)) { if (DeserializationError::Ok == deserializeJson(doc, cmd)) {
HTTPClient * http = new HTTPClient; std::string url, header_s, value_s, method_s, key_s, keys_s;
std::string url, header_s, value_s, method_s, key_s, keys_s;
// search keys lower case // search keys lower case
for (JsonPair p : doc.as<JsonObject>()) { for (JsonPair p : doc.as<JsonObject>()) {
if (Helpers::toLower(p.key().c_str()) == "url") { if (Helpers::toLower(p.key().c_str()) == "url") {
@@ -727,58 +726,61 @@ std::string compute(const std::string & expr) {
keys_s = p.key().c_str(); keys_s = p.key().c_str();
} }
} }
if (http->begin(url.c_str())) { if (url.substr(0, 4) == "http") { // match http and https
int httpResult = 0; HTTPClient * http = new HTTPClient;
for (JsonPair p : doc[header_s].as<JsonObject>()) { if (http->begin(url.c_str())) {
http->addHeader(p.key().c_str(), p.value().as<std::string>().c_str()); int httpResult = 0;
} for (JsonPair p : doc[header_s].as<JsonObject>()) {
std::string value = doc[value_s] | ""; http->addHeader(p.key().c_str(), p.value().as<std::string>().c_str());
std::string method = doc[method_s] | "get";
// if there is data, force a POST
if (value.length() || Helpers::toLower(method) == "post") {
if (value.find_first_of('{') != std::string::npos) {
http->addHeader(asyncsrv::T_Content_Type, asyncsrv::T_application_json, false); // auto-set to JSON
} }
httpResult = http->POST(value.c_str()); std::string value = doc[value_s] | "";
} else { std::string method = doc[method_s] | "get";
httpResult = http->GET(); // normal GET
}
if (httpResult > 0) { // if there is data, force a POST
std::string result = http->getString().c_str(); if (value.length() || Helpers::toLower(method) == "post") {
std::string key = doc[key_s] | ""; if (value.find_first_of('{') != std::string::npos) {
JsonDocument keys_doc; // JsonDocument to hold "keys" after doc is parsed with HTTP body http->addHeader(asyncsrv::T_Content_Type, asyncsrv::T_application_json, false); // auto-set to JSON
if (doc[keys_s].is<JsonArray>()) { }
keys_doc.set(doc[keys_s].as<JsonArray>()); httpResult = http->POST(value.c_str());
} else {
httpResult = http->GET(); // normal GET
} }
JsonArray keys = keys_doc.as<JsonArray>();
if (key.length() || !keys.isNull()) { if (httpResult > 0) {
doc.clear(); std::string result = http->getString().c_str();
if (DeserializationError::Ok == deserializeJson(doc, result)) { std::string key = doc[key_s] | "";
if (key.length()) { JsonDocument keys_doc; // JsonDocument to hold "keys" after doc is parsed with HTTP body
result = doc[key.c_str()].as<std::string>(); if (doc[keys_s].is<JsonArray>()) {
} else { keys_doc.set(doc[keys_s].as<JsonArray>());
JsonVariant json = doc.as<JsonVariant>(); }
for (JsonVariant keys_key : keys) { JsonArray keys = keys_doc.as<JsonArray>();
if (keys_key.is<std::string>() && json.is<JsonObject>()) {
json = json[keys_key.as<std::string>()].as<JsonVariant>(); if (key.length() || !keys.isNull()) {
} else if (keys_key.is<int>() && json.is<JsonArray>()) { doc.clear();
json = json[keys_key.as<int>()].as<JsonVariant>(); if (DeserializationError::Ok == deserializeJson(doc, result)) {
} else { if (key.length()) {
break; // type mismatch result = doc[key.c_str()].as<std::string>();
} else {
JsonVariant json = doc.as<JsonVariant>();
for (JsonVariant keys_key : keys) {
if (keys_key.is<std::string>() && json.is<JsonObject>()) {
json = json[keys_key.as<std::string>()].as<JsonVariant>();
} else if (keys_key.is<int>() && json.is<JsonArray>()) {
json = json[keys_key.as<int>()].as<JsonVariant>();
} else {
break; // type mismatch
}
} }
result = json.as<std::string>();
} }
result = json.as<std::string>();
} }
} }
expr_new.replace(f, e - f, result.c_str());
} }
expr_new.replace(f, e - f, result.c_str()); http->end();
} }
http->end(); delete http;
} }
delete http;
} }
f = expr_new.find_first_of('{', e); f = expr_new.find_first_of('{', e);
} }