From 188cc7b6669e17a72dbd13ecfe316033200e3145 Mon Sep 17 00:00:00 2001 From: MichaelDvP Date: Sat, 11 Jul 2026 19:12:34 +0200 Subject: [PATCH] check for "http" before allocating a http-client --- src/core/shuntingYard.cpp | 88 ++++++++++++++++++++------------------- 1 file changed, 45 insertions(+), 43 deletions(-) diff --git a/src/core/shuntingYard.cpp b/src/core/shuntingYard.cpp index ee03769bd..e0c795da1 100644 --- a/src/core/shuntingYard.cpp +++ b/src/core/shuntingYard.cpp @@ -707,8 +707,7 @@ std::string compute(const std::string & expr) { std::string cmd = expr_new.substr(f, e - f).c_str(); JsonDocument doc; 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 for (JsonPair p : doc.as()) { 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(); } } - if (http->begin(url.c_str())) { - int httpResult = 0; - for (JsonPair p : doc[header_s].as()) { - http->addHeader(p.key().c_str(), p.value().as().c_str()); - } - std::string value = doc[value_s] | ""; - 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 + if (url.substr(0, 4) == "http") { // match http and https + HTTPClient * http = new HTTPClient; + if (http->begin(url.c_str())) { + int httpResult = 0; + for (JsonPair p : doc[header_s].as()) { + http->addHeader(p.key().c_str(), p.value().as().c_str()); } - httpResult = http->POST(value.c_str()); - } else { - httpResult = http->GET(); // normal GET - } + std::string value = doc[value_s] | ""; + std::string method = doc[method_s] | "get"; - if (httpResult > 0) { - std::string result = http->getString().c_str(); - std::string key = doc[key_s] | ""; - JsonDocument keys_doc; // JsonDocument to hold "keys" after doc is parsed with HTTP body - if (doc[keys_s].is()) { - keys_doc.set(doc[keys_s].as()); + // 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()); + } else { + httpResult = http->GET(); // normal GET } - JsonArray keys = keys_doc.as(); - if (key.length() || !keys.isNull()) { - doc.clear(); - if (DeserializationError::Ok == deserializeJson(doc, result)) { - if (key.length()) { - result = doc[key.c_str()].as(); - } else { - JsonVariant json = doc.as(); - for (JsonVariant keys_key : keys) { - if (keys_key.is() && json.is()) { - json = json[keys_key.as()].as(); - } else if (keys_key.is() && json.is()) { - json = json[keys_key.as()].as(); - } else { - break; // type mismatch + if (httpResult > 0) { + std::string result = http->getString().c_str(); + std::string key = doc[key_s] | ""; + JsonDocument keys_doc; // JsonDocument to hold "keys" after doc is parsed with HTTP body + if (doc[keys_s].is()) { + keys_doc.set(doc[keys_s].as()); + } + JsonArray keys = keys_doc.as(); + + if (key.length() || !keys.isNull()) { + doc.clear(); + if (DeserializationError::Ok == deserializeJson(doc, result)) { + if (key.length()) { + result = doc[key.c_str()].as(); + } else { + JsonVariant json = doc.as(); + for (JsonVariant keys_key : keys) { + if (keys_key.is() && json.is()) { + json = json[keys_key.as()].as(); + } else if (keys_key.is() && json.is()) { + json = json[keys_key.as()].as(); + } else { + break; // type mismatch + } } + result = json.as(); } - result = json.as(); } } + 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); }