Improve 'body' handling in http_request on_response triggers (#6968)

This commit is contained in:
Kevin P. Fleming
2024-06-25 19:50:54 -04:00
committed by GitHub
parent d8a5c1ea0c
commit 0179358f9c
4 changed files with 17 additions and 6 deletions
+1 -1
View File
@@ -257,7 +257,7 @@ async def http_request_action_to_code(config, action_id, template_arg, args):
trigger,
[
(cg.std_shared_ptr.template(HttpContainer), "response"),
(cg.std_string, "body"),
(cg.std_string_ref, "body"),
],
conf,
)
+14 -5
View File
@@ -43,10 +43,10 @@ class HttpContainer : public Parented<HttpRequestComponent> {
bool secure_{false};
};
class HttpRequestResponseTrigger : public Trigger<std::shared_ptr<HttpContainer>, std::string> {
class HttpRequestResponseTrigger : public Trigger<std::shared_ptr<HttpContainer>, std::string &> {
public:
void process(std::shared_ptr<HttpContainer> container, std::string response_body) {
this->trigger(std::move(container), std::move(response_body));
void process(std::shared_ptr<HttpContainer> container, std::string &response_body) {
this->trigger(std::move(container), response_body);
}
};
@@ -153,8 +153,17 @@ template<typename... Ts> class HttpRequestSendAction : public Action<Ts...> {
}
}
for (auto *trigger : this->response_triggers_) {
trigger->process(container, response_body);
if (this->response_triggers_.size() == 1) {
// if there is only one trigger, no need to copy the response body
this->response_triggers_[0]->process(container, response_body);
} else {
for (auto *trigger : this->response_triggers_) {
// with multiple triggers, pass a copy of the response body to each
// one so that modifications made in one trigger are not visible to
// the others
auto response_body_copy = std::string(response_body);
trigger->process(container, response_body_copy);
}
}
container->end();
}