From 3b6c5e0561e417547d0080166abbbf4dde56bf79 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Mon, 11 Apr 2022 13:49:48 +0200 Subject: [PATCH] add post request --- SourceCode/arduino/lib/Firewall/Firewall.cpp | 30 ++++++++++++++++---- SourceCode/arduino/lib/Firewall/Firewall.h | 5 ++-- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/SourceCode/arduino/lib/Firewall/Firewall.cpp b/SourceCode/arduino/lib/Firewall/Firewall.cpp index c60622e..3f68a61 100644 --- a/SourceCode/arduino/lib/Firewall/Firewall.cpp +++ b/SourceCode/arduino/lib/Firewall/Firewall.cpp @@ -13,7 +13,8 @@ ESPFirewall::ESPFirewall(int port) void ESPFirewall::setup_routing() { - this->firewall_api->on("/api/v1/firewall", HTTP_GET, std::bind(&ESPFirewall::get_firewall_rules, this)); + this->firewall_api->on("/api/v1/firewall", HTTP_GET, std::bind(&ESPFirewall::get_firewall_handler, this)); + this->firewall_api->on("/api/v1/firewall", HTTP_POST, std::bind(&ESPFirewall::post_firewall_handler, this)); this->firewall_api->begin(); } @@ -28,15 +29,15 @@ void ESPFirewall::custom_message_response(const char *message, int response_code void ESPFirewall::prepare_firewall_json(cJSON *jsonResponse, firewall_rule_t *link) { - cJSON_AddStringToObject(jsonResponse, "status", "success"); - cJSON_AddNumberToObject(jsonResponse, "rule", link->key); + cJSON_AddBoolToObject(jsonResponse, "ok", true); + cJSON_AddNumberToObject(jsonResponse, "number", link->key); cJSON_AddStringToObject(jsonResponse, "source", link->source); cJSON_AddStringToObject(jsonResponse, "destination", link->destination); cJSON_AddStringToObject(jsonResponse, "protocol", link->protocol); cJSON_AddStringToObject(jsonResponse, "target", link->target); } -firewall_rule_t *ESPFirewall::add_rule_to_firewall(char *source, char *destination, char *protocol, char *target) +firewall_rule_t *ESPFirewall::add_rule_to_firewall(const char *source, const char *destination, const char *protocol, const char *target) { firewall_rule_t *temp; firewall_rule_t *link = (firewall_rule_t *)malloc(sizeof(firewall_rule_t)); @@ -61,7 +62,26 @@ firewall_rule_t *ESPFirewall::add_rule_to_firewall(char *source, char *destinati return link; } -void ESPFirewall::get_firewall_rules() +void ESPFirewall::post_firewall_handler() +{ + if ((firewall_api->hasArg("source") || firewall_api->hasArg("destination") || firewall_api->hasArg("protocol") || firewall_api->hasArg("target")) == false) + { + this->custom_message_response("not enough erguments provided", 400); + } + else + { + const char *source = firewall_api->arg("source").c_str(); + const char *destination = firewall_api->arg("destination").c_str(); + const char *protocol = firewall_api->arg("protocol").c_str(); + const char *target = firewall_api->arg("target").c_str(); + firewall_rule_t *ptr = this->add_rule_to_firewall(source, destination, protocol, target); + cJSON *json_response = cJSON_CreateObject(); + prepare_firewall_json(json_response, ptr); + this->firewall_api->send(200, "application/json", cJSON_Print(json_response)); + } +} + +void ESPFirewall::get_firewall_handler() { firewall_rule_t *ptr = head; cJSON *json_response = cJSON_CreateArray(); diff --git a/SourceCode/arduino/lib/Firewall/Firewall.h b/SourceCode/arduino/lib/Firewall/Firewall.h index e1e9386..9d17314 100644 --- a/SourceCode/arduino/lib/Firewall/Firewall.h +++ b/SourceCode/arduino/lib/Firewall/Firewall.h @@ -24,8 +24,9 @@ class ESPFirewall void setup_routing(); void custom_message_response(const char *message, int response_code); void prepare_firewall_json(cJSON *jsonResponse, firewall_rule_t *link); - firewall_rule_t *add_rule_to_firewall(char *source, char *destination, char *protocol, char *target); - void get_firewall_rules(); + firewall_rule_t *add_rule_to_firewall(const char *source, const char *destination, const char *protocol, const char *target); + void post_firewall_handler(); + void get_firewall_handler(); public: ESPFirewall(int port = 8080);