From 87b717545bcde07b223b5760f050505eb5088155 Mon Sep 17 00:00:00 2001 From: Pablu23 Date: Thu, 11 Jul 2024 18:34:11 +0200 Subject: [PATCH] Add namespace --- http.hpp | 5 +++-- main.cpp | 8 ++++---- path.cpp | 2 ++ path.hpp | 2 ++ request.cpp | 2 ++ request.hpp | 3 +++ response.cpp | 9 +++++++-- response.hpp | 6 ++++-- router.cpp | 7 +++++-- router.hpp | 2 ++ 10 files changed, 34 insertions(+), 12 deletions(-) diff --git a/http.hpp b/http.hpp index 4ec1a94..aed126b 100644 --- a/http.hpp +++ b/http.hpp @@ -2,8 +2,8 @@ #define STATUSCODE_H #include - -class http { +namespace http { +class statuscode { public: enum statusCode { CONTINUE = 100, @@ -157,5 +157,6 @@ public: } } }; +} // namespace http #endif // !STATUSCODE_H diff --git a/main.cpp b/main.cpp index 1e49c71..561831e 100644 --- a/main.cpp +++ b/main.cpp @@ -1,17 +1,17 @@ -#include "request.hpp" -#include "response.hpp" #include "router.hpp" +using namespace http; + int main() { - Router router(8080); + http::Router router(8080); // Allow all Methods router.Handle("/helloWorld", [](Request req, Response res) -> Response { res.SetPayload("Hello World!"); res.SetContentType("text/plain"); return res; }); - + // Only allow GET router.Handle("GET /echo/{name}", [](Request req, Response res) -> Response { std::string name = req.path.Get("name").value_or("No Name given"); diff --git a/path.cpp b/path.cpp index 621de70..fffa8a0 100644 --- a/path.cpp +++ b/path.cpp @@ -1,6 +1,8 @@ #include "path.hpp" #include +using namespace http; + Path::Path(std::string path) { int pos = path.find("?"); m_base = path.substr(0, pos); diff --git a/path.hpp b/path.hpp index 18fb58c..014fb8e 100644 --- a/path.hpp +++ b/path.hpp @@ -5,6 +5,7 @@ #include #include +namespace http { class Path { private: // std::string m_path; @@ -19,5 +20,6 @@ public: std::string Base(); void Match(std::string pattern); }; +} // namespace http #endif // !PATH_H diff --git a/request.cpp b/request.cpp index fa07695..76ffcfa 100644 --- a/request.cpp +++ b/request.cpp @@ -1,6 +1,8 @@ #include "request.hpp" #include +using namespace http; + bool Request::protocol(std::stringstream *ss, int *procPart, char c) { if (c == ' ' || c == '\n') { switch (*procPart) { diff --git a/request.hpp b/request.hpp index 6258779..738a47e 100644 --- a/request.hpp +++ b/request.hpp @@ -7,6 +7,7 @@ #include #include +namespace http { class Request { private: std::map m_headers; @@ -26,4 +27,6 @@ public: std::string Method(); std::vector Data(); }; +} // namespace http + #endif // !REQUEST_HEADER_H diff --git a/response.cpp b/response.cpp index d0b1607..ccd5dd6 100644 --- a/response.cpp +++ b/response.cpp @@ -1,11 +1,16 @@ #include "response.hpp" +#include "http.hpp" #include #include #include #include #include -Response::Response(http::statusCode statusCode) { m_statusCode = statusCode; } +using namespace http; + +Response::Response(statuscode::statusCode statusCode) { + m_statusCode = statusCode; +} void Response::SetPayload(std::vector data) { m_headers.insert(std::pair( @@ -29,7 +34,7 @@ void Response::SetContentType(const std::string type) { void Response::Send(int clientSocket) { std::stringstream ss; ss << "HTTP/1.1 " << m_statusCode << " " - << http::StatusCodeString(m_statusCode) << "\n"; + << statuscode::StatusCodeString(m_statusCode) << "\n"; for (const auto &[key, value] : m_headers) { ss << key << ": " << value << "\n"; } diff --git a/response.hpp b/response.hpp index bc8a97e..5210ae2 100644 --- a/response.hpp +++ b/response.hpp @@ -6,14 +6,15 @@ #include #include +namespace http { class Response { private: std::map m_headers; std::vector m_payload; - http::statusCode m_statusCode; + statuscode::statusCode m_statusCode; public: - Response(http::statusCode statusCode); + Response(statuscode::statusCode statusCode); // Response(std::vector data); // Response(std::string data); void SetPayload(std::vector data); @@ -22,5 +23,6 @@ public: void Send(int clientSocket); void Print(); }; +} // namespace http #endif // !RESPONSE_H diff --git a/router.cpp b/router.cpp index b54c3ef..1b6b271 100644 --- a/router.cpp +++ b/router.cpp @@ -1,8 +1,11 @@ #include "router.hpp" +#include "http.hpp" #include #include #include +using namespace http; + Router::Router(int port) { m_socket = socket(AF_INET, SOCK_STREAM, 0); m_address.sin_family = AF_INET; @@ -118,11 +121,11 @@ Response Router::Route(Request req) { path.erase(0, uPos + 1); } if (found) { - Response res(http::OK); + Response res(statuscode::OK); req.path.Match(patternCopy); return value(req, res); } } - return Response(http::NOT_FOUND); + return Response(statuscode::NOT_FOUND); } diff --git a/router.hpp b/router.hpp index 0dc5d64..870ec07 100644 --- a/router.hpp +++ b/router.hpp @@ -11,6 +11,7 @@ #include #include +namespace http { class Router { private: std::map> m_routes; @@ -36,5 +37,6 @@ public: int Start(); int Stop(); }; +} // namespace http #endif // !ROUTER_H