fix rest api

This commit is contained in:
Florian Hoss 2022-04-23 23:50:46 +02:00
parent 17a1a5176f
commit 0a7f0faa39
4 changed files with 57 additions and 13 deletions

View file

@ -72,17 +72,62 @@ namespace firewall
void API::get_firewall_rules_handler()
{
this->json_message_response("firewall rules", 200);
this->json_generic_response(this->construct_json_firewall(), 200);
}
void API::post_firewall_handler()
{
this->json_message_response("post firewall rule", 200);
if (request_has_firewall_parameter())
{
firewall_rule_t *rule_ptr = (firewall_rule_t *)malloc(sizeof(firewall_rule_t));
rule_ptr->key = ++amount_of_rules;
String source = this->server->arg("source");
strncpy(rule_ptr->source, source.c_str(), sizeof(rule_ptr->source));
String destination = this->server->arg("destination");
strncpy(rule_ptr->destination, destination.c_str(), sizeof(rule_ptr->destination));
String protocol = this->server->arg("protocol");
rule_ptr->protocol = string_to_protocol(protocol);
String target = this->server->arg("target");
rule_ptr->target = string_to_target(target);
add_rule_to_firewall(rule_ptr);
this->json_generic_response(this->construct_json_firewall_rule(rule_ptr), 200);
}
else
{
this->json_message_response("not enough parameter", 400);
}
}
void API::delete_firewall_handler()
{
this->json_message_response("delete firewall rule: " + this->server->pathArg(0), 200);
String param = this->server->pathArg(0);
int rule_number = atoi(param.c_str());
if (delete_rule_from_firewall(rule_number) == SUCCESS)
{
this->json_message_response("firewall rule deleted", 200);
}
else
{
this->json_message_response("cannot delete firewall rule", 500);
}
}
bool API::request_has_firewall_parameter()
{
if (!this->server->args())
{
return false;
}
else
{
return this->server->hasArg("source") ||
this->server->hasArg("destination") ||
this->server->hasArg("protocol") ||
this->server->hasArg("target");
}
}
String API::json_new_attribute(String key, String value, bool last)
@ -107,7 +152,6 @@ namespace firewall
void API::json_message_response(String message, const uint16_t response_code)
{
String serialized_string = "{";
serialized_string += json_new_attribute("uri", this->server->uri());
serialized_string += json_new_attribute("message", message, true);
serialized_string += "}";
this->server->send(response_code, "application/json; charset=utf-8", serialized_string);