From af0c6fb8145c779e594411c243d4b26a2f3ad6a3 Mon Sep 17 00:00:00 2001 From: Pablu23 Date: Tue, 26 Nov 2024 17:29:36 +0100 Subject: [PATCH] Replace shared_pointer with RAII, also fix root path not routing correctly --- Makefile | 1 - router.cpp | 9 +++++++-- router.hpp | 4 ++-- tree.cpp | 42 ++++++++++++++++++++++++------------------ tree.hpp | 7 ++++--- 5 files changed, 37 insertions(+), 26 deletions(-) diff --git a/Makefile b/Makefile index ac591a3..b2a69e3 100644 --- a/Makefile +++ b/Makefile @@ -26,7 +26,6 @@ build: all g++ -std=c++20 -o server main.cpp -L. -lhttpablu run: build - g++ -std=c++20 -o server main.cpp -L. -lhttpablu ./server install: all diff --git a/router.cpp b/router.cpp index ef75eea..1fd77b1 100644 --- a/router.cpp +++ b/router.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include #include @@ -21,6 +20,12 @@ Router::Router(int port) { m_running = false; } +Router::~Router() { + for (auto t : m_routes) { + delete t.second; + } +} + int Router::start() { m_running = true; int err = bind(m_socket, (struct sockaddr *)&m_address, sizeof(m_address)); @@ -105,7 +110,7 @@ void Router::handle(std::string pathPattern, // TODO: UNSAFE CHECK BOUNDS auto tree = m_routes[route[0]]; if (!tree) { - tree = std::make_shared(Tree(route[0])); + tree = new Tree(route[0]); m_routes.insert_or_assign(route[0], tree); } tree->add_path(route[1], func); diff --git a/router.hpp b/router.hpp index edfac06..815143f 100644 --- a/router.hpp +++ b/router.hpp @@ -6,7 +6,6 @@ #include "tree.hpp" #include #include -#include #include #include #include @@ -15,7 +14,7 @@ namespace http { class Router { private: - std::map> m_routes; + std::map m_routes; int m_socket; sockaddr_in m_address; Response Route(Request req); @@ -34,6 +33,7 @@ private: public: Router(int port); + ~Router(); void handle(std::string path_pattern, std::function func); int start(); diff --git a/tree.cpp b/tree.cpp index ccf0ac0..8507507 100644 --- a/tree.cpp +++ b/tree.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include using namespace http; @@ -23,19 +22,23 @@ Node::Node(std::string sub, bool isValue, m_is_dummy = false; } +Node::~Node() { + for (auto n : m_next) { + delete n.second; + } +} + Tree::Tree(std::string method) { m_method = method; } -void add_node(std::shared_ptr const &parent, std::string path, - std::vector rest, - std::function func) { - std::shared_ptr curr = parent->m_next[path]; +void add_node(Node *parent, std::string path, std::vector rest, + std::function func) { + Node *curr = parent->m_next[path]; if (rest.size() == 0) { if (curr) { curr->m_is_dummy = false; curr->m_function = func; } else { - std::shared_ptr leaf = - std::make_shared(Node{path, false, func}); + Node *leaf = new Node{path, false, func}; parent->m_next.insert_or_assign(path, leaf); } return; @@ -49,24 +52,25 @@ void add_node(std::shared_ptr const &parent, std::string path, } else { auto newPath = rest.front(); rest.erase(rest.begin()); - std::shared_ptr leaf = std::make_shared(Node{path}); + Node *leaf = new Node{path}; parent->m_next.insert_or_assign(path, leaf); add_node(leaf, newPath, rest, func); } } void Tree::add_path(std::string path, - std::function func) { + std::function func) { auto subPaths = split(path, "/"); if (subPaths.size() == 0 && m_root == nullptr) { - m_root = std::make_shared(Node{"", false, func}); + m_root = new Node{"", false, func}; return; } else if (subPaths.size() == 0) { + m_root->m_is_dummy = false; m_root->m_function = func; return; } else if (m_root == nullptr) { - m_root = std::make_shared(Node{""}); + m_root = new Node{""}; } auto newPath = subPaths.front(); @@ -74,7 +78,9 @@ void Tree::add_path(std::string path, add_node(m_root, newPath, subPaths, func); } -void print_node(std::shared_ptr node, size_t depth, size_t max_depth) { +Tree::~Tree() { delete m_root; }; + +void print_node(Node *node, size_t depth, size_t max_depth) { if (depth >= max_depth) { return; } @@ -86,11 +92,11 @@ void print_node(std::shared_ptr node, size_t depth, size_t max_depth) { } } -std::optional> -traverse(std::shared_ptr const &parent, std::string path, - std::vector rest) { +auto traverse(Node *const &parent, std::string path, + std::vector rest) + -> std::optional> { - std::shared_ptr curr = parent->m_next[path]; + Node *curr = parent->m_next[path]; if (rest.size() == 0) { if (curr != nullptr && !curr->m_is_dummy) return curr->m_function; @@ -112,9 +118,9 @@ std::optional> Tree::get(std::string path) { auto subs = split(path, "/"); if (subs.size() == 0) { - if (!m_root->m_is_dummy) + if (!m_root->m_is_dummy) { return m_root->m_function; - else + } else return std::nullopt; } diff --git a/tree.hpp b/tree.hpp index 614dcb0..b6816e0 100644 --- a/tree.hpp +++ b/tree.hpp @@ -5,7 +5,6 @@ #include "response.hpp" #include #include -#include #include namespace http { @@ -14,23 +13,25 @@ public: bool m_is_value; bool m_is_dummy; std::string m_sub_path; - std::map> m_next; + std::map m_next; std::function m_function; public: Node(std::string subPath, bool isValue, std::function); Node(std::string subPath); + ~Node(); }; class Tree { private: - std::shared_ptr m_root; + Node *m_root; std::string m_method; size_t m_depth; public: Tree(std::string method); + ~Tree(); void add_path(std::string, std::function); std::optional> get(std::string); void debug_Print();