Change Variable names and all to snake case

This commit is contained in:
Pablu23
2024-11-13 16:01:17 +01:00
parent 1797b3c0ef
commit a3a5280f17
10 changed files with 91 additions and 93 deletions

View File

@@ -3,7 +3,7 @@
#include <string>
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";

View File

@@ -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<std::byte>());
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<std::byte>());
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;
}

View File

@@ -10,16 +10,16 @@ Path::Path(std::string path) {
m_query = path;
}
std::optional<std::string> Path::Get(std::string name) {
std::optional<std::string> 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) {

View File

@@ -15,10 +15,10 @@ private:
public:
Path(std::string path);
std::optional<std::string> Get(std::string name);
std::string Query();
std::string Base();
void Match(std::string pattern);
std::optional<std::string> get(std::string name);
std::string query();
std::string base();
void match(std::string pattern);
};
} // namespace http

View File

@@ -12,20 +12,20 @@ class Request {
private:
std::map<std::string, std::string> m_headers;
std::string m_method;
std::string m_pathRaw;
std::string m_path_raw;
std::vector<std::byte> 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<std::byte> buf);
void Print();
bool HasData();
std::string Method();
std::vector<std::byte> Data();
void print();
bool has_data();
std::string method();
std::vector<std::byte> data();
};
} // namespace http

View File

@@ -11,19 +11,17 @@ class Response {
private:
std::map<std::string, std::string> m_headers;
std::vector<std::byte> m_payload;
statuscode::statusCode m_statusCode;
StatusCode::statusCode m_statusCode;
public:
Response(statuscode::statusCode statusCode);
// Response(std::vector<std::byte> data);
// Response(std::string data);
void SetHeader(const std::string name, const std::string value);
void SetPayload(const std::vector<std::byte> 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<std::byte> 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

View File

@@ -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<std::mutex> 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<std::mutex> 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<std::byte> buffer(1024);
while (true) {
int client;
{
std::unique_lock<std::mutex> 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<void(Request, Response *)> func) {
auto route = split(pathPattern, " ");
// TODO: UNSAFE CHECK BOUNDS
@@ -102,18 +102,18 @@ void Router::Handle(std::string pathPattern,
tree = std::make_shared<Tree>(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;
}

View File

@@ -25,18 +25,18 @@ private:
std::condition_variable m_cond;
std::vector<std::thread> m_threads;
std::queue<int> 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<void(Request, Response *)> func);
int Start();
int Stop();
int start();
int stop();
};
} // namespace http

View File

@@ -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<void(Request, Response *)> 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<Node> const &parent, std::string path,
std::shared_ptr<Node> 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<Node> leaf =
@@ -55,7 +55,7 @@ void addNode(std::shared_ptr<Node> const &parent, std::string path,
}
}
void Tree::AddPath(std::string path,
void Tree::add_path(std::string path,
std::function<void(Request, Response *)> func) {
auto subPaths = split(path, "/");
@@ -79,8 +79,8 @@ void printNode(std::shared_ptr<Node> 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<Node> const &parent, std::string path,
std::shared_ptr<Node> 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<Node> const &parent, std::string path,
}
std::optional<std::function<void(Request, Response *)>>
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); }

View File

@@ -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<std::string, std::shared_ptr<Node>> m_next;
std::function<void(Request, Response *)> m_function;
@@ -31,9 +31,9 @@ private:
public:
Tree(std::string method);
void AddPath(std::string, std::function<void(Request, Response *)>);
std::optional<std::function<void(Request, Response *)>> Get(std::string);
void DebugPrint();
void add_path(std::string, std::function<void(Request, Response *)>);
std::optional<std::function<void(Request, Response *)>> get(std::string);
void debug_Print();
};
} // namespace http