From 945f1fd3b6bba950deee42e87acb65be4f4313cb Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Mon, 11 Apr 2022 18:19:03 +0200 Subject: [PATCH] get firewall rule over url parameter and regex --- SourceCode/arduino/lib/Firewall/Firewall.cpp | 16 ++++++++++++++-- SourceCode/arduino/lib/Firewall/Firewall.h | 3 ++- SourceCode/arduino/platformio.ini | 12 ++++++++---- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/SourceCode/arduino/lib/Firewall/Firewall.cpp b/SourceCode/arduino/lib/Firewall/Firewall.cpp index 281d1c8..fa85121 100644 --- a/SourceCode/arduino/lib/Firewall/Firewall.cpp +++ b/SourceCode/arduino/lib/Firewall/Firewall.cpp @@ -26,7 +26,7 @@ void ESPFirewall::add_rule_to_firewall(firewall_rule_t *rule) return; } -void ESPFirewall::get_firewall_handler(AsyncWebServerRequest *request) +void ESPFirewall::get_firewall_rules_handler(AsyncWebServerRequest *request) { firewall_rule_t *ptr = this->head; DynamicJsonDocument json(1024); @@ -47,6 +47,17 @@ void ESPFirewall::get_firewall_handler(AsyncWebServerRequest *request) request->send(200, "application/json", response); } +void ESPFirewall::get_firewall_rule_handler(AsyncWebServerRequest *request) +{ + int rule_number = request->pathArg(0).toInt(); + DynamicJsonDocument json(1024); + String response; + json["message"] = "get firewall rule"; + json["key"] = rule_number; + serializeJson(json, response); + request->send(404, "application/json", response); +} + void ESPFirewall::post_firewall_handler(AsyncWebServerRequest *request) { DynamicJsonDocument json(1024); @@ -103,7 +114,8 @@ bool ESPFirewall::request_has_firewall_parameter(AsyncWebServerRequest *request) void ESPFirewall::setup_routing() { - firewall_api->on("/api/v1/firewall", HTTP_GET, std::bind(&ESPFirewall::get_firewall_handler, this, std::placeholders::_1)); + firewall_api->on("^\\/api/v1/firewall\\/([0-9]+)$", HTTP_GET, std::bind(&ESPFirewall::get_firewall_rule_handler, this, std::placeholders::_1)); + firewall_api->on("/api/v1/firewall", HTTP_GET, std::bind(&ESPFirewall::get_firewall_rules_handler, this, std::placeholders::_1)); firewall_api->on("/api/v1/firewall", HTTP_POST, std::bind(&ESPFirewall::post_firewall_handler, this, std::placeholders::_1)); firewall_api->onNotFound(std::bind(&ESPFirewall::not_found, this, std::placeholders::_1)); this->firewall_api->begin(); diff --git a/SourceCode/arduino/lib/Firewall/Firewall.h b/SourceCode/arduino/lib/Firewall/Firewall.h index 77c916d..b41d0cf 100644 --- a/SourceCode/arduino/lib/Firewall/Firewall.h +++ b/SourceCode/arduino/lib/Firewall/Firewall.h @@ -33,7 +33,8 @@ class ESPFirewall struct firewall_rule *head = NULL; void add_rule_to_firewall(firewall_rule_t *rule); - void get_firewall_handler(AsyncWebServerRequest *request); + void get_firewall_rules_handler(AsyncWebServerRequest *request); + void get_firewall_rule_handler(AsyncWebServerRequest *request); void post_firewall_handler(AsyncWebServerRequest *request); void not_found(AsyncWebServerRequest *request); bool request_has_firewall_parameter(AsyncWebServerRequest *request); diff --git a/SourceCode/arduino/platformio.ini b/SourceCode/arduino/platformio.ini index ad40439..4c4e078 100644 --- a/SourceCode/arduino/platformio.ini +++ b/SourceCode/arduino/platformio.ini @@ -13,8 +13,10 @@ platform = espressif32 board = esp32-evb framework = arduino monitor_speed = 115200 -build_flags = -DCORE_DEBUG_LEVEL=3 -lib_deps = +build_flags = + -DCORE_DEBUG_LEVEL=3 + -DASYNCWEBSERVER_REGEX +lib_deps = bblanchon/ArduinoJson@^6.19.4 ottowinter/ESPAsyncWebServer-esphome@^2.1.0 @@ -23,7 +25,9 @@ platform = espressif32 board = az-delivery-devkit-v4 framework = arduino monitor_speed = 115200 -build_flags = -DCORE_DEBUG_LEVEL=3 -lib_deps = +build_flags = + -DCORE_DEBUG_LEVEL=3 + -DASYNCWEBSERVER_REGEX +lib_deps = bblanchon/ArduinoJson@^6.19.4 ottowinter/ESPAsyncWebServer-esphome@^2.1.0