Replace shared_pointer with RAII, also fix root path not routing correctly
This commit is contained in:
1
Makefile
1
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
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#include <csignal>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <strings.h>
|
||||
#include <sys/select.h>
|
||||
@@ -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>(Tree(route[0]));
|
||||
tree = new Tree(route[0]);
|
||||
m_routes.insert_or_assign(route[0], tree);
|
||||
}
|
||||
tree->add_path(route[1], func);
|
||||
|
||||
@@ -6,7 +6,6 @@
|
||||
#include "tree.hpp"
|
||||
#include <condition_variable>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <netinet/in.h>
|
||||
#include <queue>
|
||||
@@ -15,7 +14,7 @@
|
||||
namespace http {
|
||||
class Router {
|
||||
private:
|
||||
std::map<std::string, std::shared_ptr<Tree>> m_routes;
|
||||
std::map<std::string, Tree *> 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<void(Request, Response *)> func);
|
||||
int start();
|
||||
|
||||
38
tree.cpp
38
tree.cpp
@@ -3,7 +3,6 @@
|
||||
#include <cstddef>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
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<Node> const &parent, std::string path,
|
||||
std::vector<std::string> rest,
|
||||
void add_node(Node *parent, std::string path, std::vector<std::string> rest,
|
||||
std::function<void(Request, Response *)> func) {
|
||||
std::shared_ptr<Node> curr = parent->m_next[path];
|
||||
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<Node> leaf =
|
||||
std::make_shared<Node>(Node{path, false, func});
|
||||
Node *leaf = new Node{path, false, func};
|
||||
parent->m_next.insert_or_assign(path, leaf);
|
||||
}
|
||||
return;
|
||||
@@ -49,7 +52,7 @@ void add_node(std::shared_ptr<Node> const &parent, std::string path,
|
||||
} else {
|
||||
auto newPath = rest.front();
|
||||
rest.erase(rest.begin());
|
||||
std::shared_ptr<Node> leaf = std::make_shared<Node>(Node{path});
|
||||
Node *leaf = new Node{path};
|
||||
parent->m_next.insert_or_assign(path, leaf);
|
||||
add_node(leaf, newPath, rest, func);
|
||||
}
|
||||
@@ -60,13 +63,14 @@ void Tree::add_path(std::string path,
|
||||
auto subPaths = split(path, "/");
|
||||
|
||||
if (subPaths.size() == 0 && m_root == nullptr) {
|
||||
m_root = std::make_shared<Node>(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>(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> 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> node, size_t depth, size_t max_depth) {
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::function<void(Request, Response *)>>
|
||||
traverse(std::shared_ptr<Node> const &parent, std::string path,
|
||||
std::vector<std::string> rest) {
|
||||
auto traverse(Node *const &parent, std::string path,
|
||||
std::vector<std::string> rest)
|
||||
-> std::optional<std::function<void(Request, Response *)>> {
|
||||
|
||||
std::shared_ptr<Node> 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<std::function<void(Request, Response *)>>
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
7
tree.hpp
7
tree.hpp
@@ -5,7 +5,6 @@
|
||||
#include "response.hpp"
|
||||
#include <cstddef>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
namespace http {
|
||||
|
||||
@@ -14,23 +13,25 @@ public:
|
||||
bool m_is_value;
|
||||
bool m_is_dummy;
|
||||
std::string m_sub_path;
|
||||
std::map<std::string, std::shared_ptr<Node>> m_next;
|
||||
std::map<std::string, Node *> m_next;
|
||||
std::function<void(Request, Response *)> m_function;
|
||||
|
||||
public:
|
||||
Node(std::string subPath, bool isValue,
|
||||
std::function<void(Request, Response *)>);
|
||||
Node(std::string subPath);
|
||||
~Node();
|
||||
};
|
||||
|
||||
class Tree {
|
||||
private:
|
||||
std::shared_ptr<Node> 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<void(Request, Response *)>);
|
||||
std::optional<std::function<void(Request, Response *)>> get(std::string);
|
||||
void debug_Print();
|
||||
|
||||
Reference in New Issue
Block a user