From bbebb4060e0a5b2504487ac001739152a4e4b9a7 Mon Sep 17 00:00:00 2001 From: Florian Hoss Date: Mon, 2 May 2022 18:01:32 +0200 Subject: [PATCH] api help is shown --- ESPFirewall/lib/Firewall/src/API.cpp | 55 ++++++++++++++++++---------- ESPFirewall/lib/Firewall/src/API.hpp | 6 ++- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/ESPFirewall/lib/Firewall/src/API.cpp b/ESPFirewall/lib/Firewall/src/API.cpp index 3ee7f6b..aa6ab10 100644 --- a/ESPFirewall/lib/Firewall/src/API.cpp +++ b/ESPFirewall/lib/Firewall/src/API.cpp @@ -29,6 +29,15 @@ namespace fw this->server->handleClient(); } + String API::get_url_base() + { +#ifdef ESP32 + return "http://" + this->server_ip + ":" + this->server_port; +#elif defined(ESP8266) + return "https://" + this->server_ip + ":" + this->server_port; +#endif + } + ok_t API::setup_auth(const char *username, const char *password) { if (!username || *username == 0x00 || strlen(username) > CREDENTIALS_LENGTH) @@ -77,17 +86,14 @@ namespace fw this->server->on(UriRegex("/firewall/([0-9]+)"), HTTP_DELETE, std::bind(&API::delete_firewall_handler, this)); add_api_endpoint("/firewall/1", "DELETE", "Delete Firewall Rule by key"); + this->server->on("/api", HTTP_GET, std::bind(&API::api_endpoints_handler, this)); this->server->onNotFound(std::bind(&API::not_found_handler, this)); } void API::add_api_endpoint(const String uri, const char *method, const char *description) { api_endpoint_t *temp; -#ifdef ESP32 - const String url = "http://" + this->server_ip + ":" + this->server_port + uri; -#elif defined(ESP8266) - const String url = "https://" + this->server_ip + ":" + this->server_port + uri; -#endif + const String url = get_url_base() + uri; api_endpoint_t *api_ptr = (api_endpoint_t *)malloc(sizeof(api_endpoint_t)); strncpy(api_ptr->uri, url.c_str(), sizeof(api_ptr->uri)); @@ -112,8 +118,14 @@ namespace fw void API::not_found_handler() { - uint16_t response_code = 404; - this->json_generic_response(this->construct_json_api(response_code), response_code); + this->json_message_response( + "see " + get_url_base() + "/api for available routes", + 404); + } + + void API::api_endpoints_handler() + { + this->json_generic_response(this->construct_json_api(), 200); } void API::get_firewall_rule_handler() @@ -126,7 +138,7 @@ namespace fw if (rule_ptr == NULL) this->json_message_response("rule does not exist", 404); else - this->json_generic_response(construct_json_firewall_rule(rule_ptr), 200); + this->json_generic_response(this->construct_json_firewall_rule(rule_ptr), 200); } void API::get_firewall_rules_handler() @@ -196,13 +208,15 @@ namespace fw void API::json_generic_response(String serialized_string, const uint16_t response_code) { - this->server->send(response_code, "application/json; charset=utf-8", serialized_string); + this->server->send( + response_code, + "application/json; charset=utf-8", + construct_json_begin(response_code) + "\"result\": [" + serialized_string + "]}"); } void API::json_message_response(String message, const uint16_t response_code) { - String serialized_string = "{"; - serialized_string += json_new_attribute("status", response_code_to_string(response_code)); + String serialized_string = construct_json_begin(response_code); serialized_string += json_new_attribute("message", message, true); serialized_string += "}"; this->server->send(response_code, "application/json; charset=utf-8", serialized_string); @@ -223,9 +237,7 @@ namespace fw String API::construct_json_firewall() { firewall_rule_t *rule_ptr = rule_head; - String serialized_string = "{"; - serialized_string += json_new_attribute("status", response_code_to_string(200)); - serialized_string += "\"rules\": ["; + String serialized_string; while (rule_ptr != NULL) { serialized_string += construct_json_firewall_rule(rule_ptr); @@ -233,7 +245,6 @@ namespace fw if (rule_ptr != NULL) serialized_string += ","; } - serialized_string += "]}"; return serialized_string; } @@ -247,12 +258,10 @@ namespace fw return serialized_string; } - String API::construct_json_api(const uint16_t response_code) + String API::construct_json_api() { api_endpoint_t *api_ptr = this->endpoint_head; - String serialized_string = "{"; - serialized_string += json_new_attribute("status", response_code_to_string(response_code)); - serialized_string += "\"endpoints\": ["; + String serialized_string; while (api_ptr != NULL) { serialized_string += construct_json_api_endpoint(api_ptr); @@ -260,7 +269,13 @@ namespace fw if (api_ptr != NULL) serialized_string += ","; } - serialized_string += "]}"; + return serialized_string; + } + + String API::construct_json_begin(const uint16_t response_code) + { + String serialized_string = "{"; + serialized_string += json_new_attribute("status", response_code_to_string(response_code)); return serialized_string; } } diff --git a/ESPFirewall/lib/Firewall/src/API.hpp b/ESPFirewall/lib/Firewall/src/API.hpp index 28c934a..effac7d 100644 --- a/ESPFirewall/lib/Firewall/src/API.hpp +++ b/ESPFirewall/lib/Firewall/src/API.hpp @@ -44,6 +44,7 @@ namespace fw void post_firewall_handler(); void delete_firewall_handler(); void not_found_handler(); + void api_endpoints_handler(); bool request_has_firewall_parameter(); String json_new_attribute(String key, String value, bool last = false); @@ -53,13 +54,14 @@ namespace fw String construct_json_firewall_rule(firewall_rule_t *); String construct_json_firewall(); String construct_json_api_endpoint(api_endpoint_t *); - String construct_json_api(const uint16_t response_code); - String construct_json_array(String message, String array_name); + String construct_json_api(); + String construct_json_begin(const uint16_t response_code); protected: String server_ip; uint16_t server_port; void handle_client(); + String get_url_base(); public: API(const char *cert, const char *key, const char *username, const char *password, const String ip, const uint16_t port);