From a3a5280f172cc42df13e6827675b6b43541a4b6b Mon Sep 17 00:00:00 2001 From: Pablu23 Date: Wed, 13 Nov 2024 16:01:17 +0100 Subject: [PATCH] Change Variable names and all to snake case --- http.hpp | 4 ++-- main.cpp | 38 +++++++++++++++++++------------------- path.cpp | 8 ++++---- path.hpp | 8 ++++---- request.hpp | 12 ++++++------ response.hpp | 20 +++++++++----------- router.cpp | 38 +++++++++++++++++++------------------- router.hpp | 16 ++++++++-------- tree.cpp | 28 ++++++++++++++-------------- tree.hpp | 12 ++++++------ 10 files changed, 91 insertions(+), 93 deletions(-) diff --git a/http.hpp b/http.hpp index aed126b..7d55f9b 100644 --- a/http.hpp +++ b/http.hpp @@ -3,7 +3,7 @@ #include namespace http { -class statuscode { +class StatusCode { public: enum statusCode { CONTINUE = 100, @@ -56,7 +56,7 @@ public: HTTP_VERSION_NOT_SUPPORTED = 505 }; - constexpr static std::string StatusCodeString(const statusCode code) { + constexpr static std::string status_code_string(const statusCode code) { switch (code) { case CONTINUE: return "Continue"; diff --git a/main.cpp b/main.cpp index 4b50114..35563df 100644 --- a/main.cpp +++ b/main.cpp @@ -4,40 +4,40 @@ using namespace http; void HelloWorld(Request req, Response *res) { - res->SetPayload("Hello World!"); - res->SetContentType("text/plain"); + res->set_payload("Hello World!"); + res->set_content_type("text/plain"); } int main() { Router router(8181); // Allow all Methods - router.Handle("GET /helloWorld", HelloWorld); - router.Handle("GET /healthz", [](Request req, Response *res) { - res->SetStatusCode(statuscode::OK); - res->SetPayload(std::vector()); - res->SetContentType("text/plain"); + router.handle("GET /helloWorld", HelloWorld); + router.handle("GET /healthz", [](Request req, Response *res) { + res->set_status_code(StatusCode::OK); + res->set_payload(std::vector()); + res->set_content_type("text/plain"); }); // Only allow GET - router.Handle("GET /echo/{name}", [](Request req, Response *res) { - std::string name = req.path.Get("name").value_or("No Name given"); - res->SetPayload("Hello " + name); - res->SetContentType("text/plain"); + router.handle("GET /echo/{name}", [](Request req, Response *res) { + std::string name = req.path.get("name").value_or("No Name given"); + res->set_payload("Hello " + name); + res->set_content_type("text/plain"); }); // Only allow POST - router.Handle("POST /echo/{name}", [](Request req, Response *res) { - std::string name = req.path.Get("name").value_or("No Name given"); - res->SetPayload("Hello with Post" + name); - res->SetContentType("text/plain"); + router.handle("POST /echo/{name}", [](Request req, Response *res) { + std::string name = req.path.get("name").value_or("No Name given"); + res->set_payload("Hello with Post" + name); + res->set_content_type("text/plain"); }); - router.Handle("GET /", [](Request req, Response *res) { - res->SetPayload("Main"); - res->SetContentType("text/plain"); + router.handle("GET /", [](Request req, Response *res) { + res->set_payload("Main"); + res->set_content_type("text/plain"); }); - router.Start(); + router.start(); return 0; } diff --git a/path.cpp b/path.cpp index fffa8a0..dab6988 100644 --- a/path.cpp +++ b/path.cpp @@ -10,16 +10,16 @@ Path::Path(std::string path) { m_query = path; } -std::optional Path::Get(std::string name) { +std::optional Path::get(std::string name) { if (m_variables.contains(name)) return m_variables.at(name); return std::nullopt; } -std::string Path::Base() { return m_base; } -std::string Path::Query() { return m_query; } +std::string Path::base() { return m_base; } +std::string Path::query() { return m_query; } -void Path::Match(std::string pattern) { +void Path::match(std::string pattern) { int pos = 0; std::string path = m_base; while (pos != -1) { diff --git a/path.hpp b/path.hpp index 014fb8e..9908536 100644 --- a/path.hpp +++ b/path.hpp @@ -15,10 +15,10 @@ private: public: Path(std::string path); - std::optional Get(std::string name); - std::string Query(); - std::string Base(); - void Match(std::string pattern); + std::optional get(std::string name); + std::string query(); + std::string base(); + void match(std::string pattern); }; } // namespace http diff --git a/request.hpp b/request.hpp index 738a47e..dc437e5 100644 --- a/request.hpp +++ b/request.hpp @@ -12,20 +12,20 @@ class Request { private: std::map m_headers; std::string m_method; - std::string m_pathRaw; + std::string m_path_raw; std::vector m_payload; std::string m_protocol; private: - bool protocol(std::stringstream *ss, int *procPart, char c); + bool protocol(std::stringstream *ss, int *proc_part, char c); public: Path path; explicit Request(std::vector buf); - void Print(); - bool HasData(); - std::string Method(); - std::vector Data(); + void print(); + bool has_data(); + std::string method(); + std::vector data(); }; } // namespace http diff --git a/response.hpp b/response.hpp index 070645f..318a7c9 100644 --- a/response.hpp +++ b/response.hpp @@ -11,19 +11,17 @@ class Response { private: std::map m_headers; std::vector m_payload; - statuscode::statusCode m_statusCode; + StatusCode::statusCode m_statusCode; public: - Response(statuscode::statusCode statusCode); - // Response(std::vector data); - // Response(std::string data); - void SetHeader(const std::string name, const std::string value); - void SetPayload(const std::vector data); - void SetPayload(const std::string data); - void SetContentType(const std::string type); - void SetStatusCode(const statuscode::statusCode statuscode); - void Send(int clientSocket); - void Print(); + Response(StatusCode::statusCode status_code); + void set_header(const std::string name, const std::string value); + void set_payload(const std::vector data); + void set_payload(const std::string data); + void set_content_type(const std::string type); + void set_status_code(const StatusCode::statusCode status_code); + void send(int client_socket); + void print(); }; } // namespace http diff --git a/router.cpp b/router.cpp index 044023d..57180ac 100644 --- a/router.cpp +++ b/router.cpp @@ -20,12 +20,12 @@ Router::Router(int port) { m_address.sin_addr.s_addr = INADDR_ANY; } -int Router::Start() { +int Router::start() { int err = bind(m_socket, (struct sockaddr *)&m_address, sizeof(m_address)); if (err != 0) return err; - StartThreadLoop(); + start_thread_loop(); err = listen(m_socket, 5); if (err != 0) @@ -33,13 +33,13 @@ int Router::Start() { while (true) { int client = accept(m_socket, nullptr, nullptr); - QueueClient(client); + queue_client(client); } - StopThreadLoop(); + stop_thread_loop(); } -void Router::QueueClient(int fd) { +void Router::queue_client(int fd) { { std::unique_lock lock(m_mutex); m_clients.push(fd); @@ -47,17 +47,17 @@ void Router::QueueClient(int fd) { m_cond.notify_one(); } -void Router::StartThreadLoop() { +void Router::start_thread_loop() { const uint32_t numThreads = std::thread::hardware_concurrency(); for (uint32_t i = 0; i < numThreads; ++i) { - m_threads.emplace_back(std::thread(&Router::ThreadLoop, this)); + m_threads.emplace_back(std::thread(&Router::thread_loop, this)); } } -void Router::StopThreadLoop() { +void Router::stop_thread_loop() { { std::unique_lock lock(m_mutex); - m_shouldTerminate = true; + m_should_terminate = true; } m_cond.notify_all(); for (std::thread &active_thread : m_threads) { @@ -66,15 +66,15 @@ void Router::StopThreadLoop() { m_threads.clear(); } -void Router::ThreadLoop() { +void Router::thread_loop() { std::vector buffer(1024); while (true) { int client; { std::unique_lock lock(m_mutex); m_cond.wait(lock, - [this] { return !m_clients.empty() || m_shouldTerminate; }); - if (m_shouldTerminate) + [this] { return !m_clients.empty() || m_should_terminate; }); + if (m_should_terminate) return; client = m_clients.front(); m_clients.pop(); @@ -83,7 +83,7 @@ void Router::ThreadLoop() { recv(client, buffer.data(), buffer.size(), 0); Request req(buffer); Response res = Route(req); - res.Send(client); + res.send(client); shutdown(client, SHUT_WR); while (recv(client, buffer.data(), buffer.size(), 0) > 0) { @@ -93,7 +93,7 @@ void Router::ThreadLoop() { } } -void Router::Handle(std::string pathPattern, +void Router::handle(std::string pathPattern, std::function func) { auto route = split(pathPattern, " "); // TODO: UNSAFE CHECK BOUNDS @@ -102,18 +102,18 @@ void Router::Handle(std::string pathPattern, tree = std::make_shared(Tree(route[0])); m_routes.insert_or_assign(route[0], tree); } - tree->AddPath(route[1], func); + tree->add_path(route[1], func); } Response Router::Route(Request req) { - auto tree = m_routes[req.Method()]; - auto route = tree->Get(req.path.Base()); + auto tree = m_routes[req.method()]; + auto route = tree->get(req.path.base()); if (!route.has_value()) { - return Response(statuscode::NOT_FOUND); + return Response(StatusCode::NOT_FOUND); } - Response res(statuscode::OK); + Response res(StatusCode::OK); route.value()(req, &res); return res; } diff --git a/router.hpp b/router.hpp index af5a148..2b82b1f 100644 --- a/router.hpp +++ b/router.hpp @@ -25,18 +25,18 @@ private: std::condition_variable m_cond; std::vector m_threads; std::queue m_clients; - bool m_shouldTerminate = false; - void StartThreadLoop(); - void ThreadLoop(); - void QueueClient(int client); - void StopThreadLoop(); + bool m_should_terminate = false; + void start_thread_loop(); + void thread_loop(); + void queue_client(int client); + void stop_thread_loop(); public: Router(int port); - void Handle(std::string pathPattern, + void handle(std::string path_pattern, std::function func); - int Start(); - int Stop(); + int start(); + int stop(); }; } // namespace http diff --git a/tree.cpp b/tree.cpp index 6ddf8f4..ec45443 100644 --- a/tree.cpp +++ b/tree.cpp @@ -9,18 +9,18 @@ using namespace http; Node::Node(std::string sub) { - m_subPath = sub; - m_isDummy = true; - m_isValue = false; + m_sub_path = sub; + m_is_dummy = true; + m_is_value = false; m_function = nullptr; } Node::Node(std::string sub, bool isValue, std::function func) { - m_subPath = sub; - m_isValue = isValue; + m_sub_path = sub; + m_is_value = isValue; m_function = func; - m_isDummy = false; + m_is_dummy = false; } Tree::Tree(std::string method) { m_method = method; } @@ -31,7 +31,7 @@ void addNode(std::shared_ptr const &parent, std::string path, std::shared_ptr curr = parent->m_next[path]; if (rest.size() == 0) { if (curr) { - curr->m_isDummy = false; + curr->m_is_dummy = false; curr->m_function = func; } else { std::shared_ptr leaf = @@ -55,7 +55,7 @@ void addNode(std::shared_ptr const &parent, std::string path, } } -void Tree::AddPath(std::string path, +void Tree::add_path(std::string path, std::function func) { auto subPaths = split(path, "/"); @@ -79,8 +79,8 @@ void printNode(std::shared_ptr node, size_t depth, size_t max_depth) { return; } - std::cout << std::string(depth, ' ') << "sub: \"" << node->m_subPath - << "\" IsDummy: " << node->m_isDummy << std::endl; + std::cout << std::string(depth, ' ') << "sub: \"" << node->m_sub_path + << "\" IsDummy: " << node->m_is_dummy << std::endl; for (auto &next : node->m_next) { printNode(next.second, depth + 1, max_depth); } @@ -92,7 +92,7 @@ traverse(std::shared_ptr const &parent, std::string path, std::shared_ptr curr = parent->m_next[path]; if (rest.size() == 0) { - if (curr != nullptr && !curr->m_isDummy) + if (curr != nullptr && !curr->m_is_dummy) return curr->m_function; else return std::nullopt; @@ -109,10 +109,10 @@ traverse(std::shared_ptr const &parent, std::string path, } std::optional> -Tree::Get(std::string path) { +Tree::get(std::string path) { auto subs = split(path, "/"); if (subs.size() == 0) { - if (!m_root->m_isDummy) + if (!m_root->m_is_dummy) return m_root->m_function; else return std::nullopt; @@ -123,4 +123,4 @@ Tree::Get(std::string path) { return traverse(m_root, newPath, subs); } -void Tree::DebugPrint() { printNode(m_root, 0, 10); } +void Tree::debug_Print() { printNode(m_root, 0, 10); } diff --git a/tree.hpp b/tree.hpp index 5ee5011..614dcb0 100644 --- a/tree.hpp +++ b/tree.hpp @@ -11,9 +11,9 @@ namespace http { class Node { public: - bool m_isValue; - bool m_isDummy; - std::string m_subPath; + bool m_is_value; + bool m_is_dummy; + std::string m_sub_path; std::map> m_next; std::function m_function; @@ -31,9 +31,9 @@ private: public: Tree(std::string method); - void AddPath(std::string, std::function); - std::optional> Get(std::string); - void DebugPrint(); + void add_path(std::string, std::function); + std::optional> get(std::string); + void debug_Print(); }; } // namespace http