Add namespace

This commit is contained in:
Pablu23
2024-07-11 18:34:11 +02:00
parent e9a7062c07
commit 87b717545b
10 changed files with 34 additions and 12 deletions

View File

@@ -2,8 +2,8 @@
#define STATUSCODE_H #define STATUSCODE_H
#include <string> #include <string>
namespace http {
class http { class statuscode {
public: public:
enum statusCode { enum statusCode {
CONTINUE = 100, CONTINUE = 100,
@@ -157,5 +157,6 @@ public:
} }
} }
}; };
} // namespace http
#endif // !STATUSCODE_H #endif // !STATUSCODE_H

View File

@@ -1,17 +1,17 @@
#include "request.hpp"
#include "response.hpp"
#include "router.hpp" #include "router.hpp"
using namespace http;
int main() { int main() {
Router router(8080); http::Router router(8080);
// Allow all Methods // Allow all Methods
router.Handle("/helloWorld", [](Request req, Response res) -> Response { router.Handle("/helloWorld", [](Request req, Response res) -> Response {
res.SetPayload("Hello World!"); res.SetPayload("Hello World!");
res.SetContentType("text/plain"); res.SetContentType("text/plain");
return res; return res;
}); });
// Only allow GET // Only allow GET
router.Handle("GET /echo/{name}", [](Request req, Response res) -> Response { router.Handle("GET /echo/{name}", [](Request req, Response res) -> Response {
std::string name = req.path.Get("name").value_or("No Name given"); std::string name = req.path.Get("name").value_or("No Name given");

View File

@@ -1,6 +1,8 @@
#include "path.hpp" #include "path.hpp"
#include <optional> #include <optional>
using namespace http;
Path::Path(std::string path) { Path::Path(std::string path) {
int pos = path.find("?"); int pos = path.find("?");
m_base = path.substr(0, pos); m_base = path.substr(0, pos);

View File

@@ -5,6 +5,7 @@
#include <optional> #include <optional>
#include <string> #include <string>
namespace http {
class Path { class Path {
private: private:
// std::string m_path; // std::string m_path;
@@ -19,5 +20,6 @@ public:
std::string Base(); std::string Base();
void Match(std::string pattern); void Match(std::string pattern);
}; };
} // namespace http
#endif // !PATH_H #endif // !PATH_H

View File

@@ -1,6 +1,8 @@
#include "request.hpp" #include "request.hpp"
#include <iostream> #include <iostream>
using namespace http;
bool Request::protocol(std::stringstream *ss, int *procPart, char c) { bool Request::protocol(std::stringstream *ss, int *procPart, char c) {
if (c == ' ' || c == '\n') { if (c == ' ' || c == '\n') {
switch (*procPart) { switch (*procPart) {

View File

@@ -7,6 +7,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
namespace http {
class Request { class Request {
private: private:
std::map<std::string, std::string> m_headers; std::map<std::string, std::string> m_headers;
@@ -26,4 +27,6 @@ public:
std::string Method(); std::string Method();
std::vector<std::byte> Data(); std::vector<std::byte> Data();
}; };
} // namespace http
#endif // !REQUEST_HEADER_H #endif // !REQUEST_HEADER_H

View File

@@ -1,11 +1,16 @@
#include "response.hpp" #include "response.hpp"
#include "http.hpp"
#include <algorithm> #include <algorithm>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <netinet/in.h> #include <netinet/in.h>
#include <sstream> #include <sstream>
Response::Response(http::statusCode statusCode) { m_statusCode = statusCode; } using namespace http;
Response::Response(statuscode::statusCode statusCode) {
m_statusCode = statusCode;
}
void Response::SetPayload(std::vector<std::byte> data) { void Response::SetPayload(std::vector<std::byte> data) {
m_headers.insert(std::pair<std::string, std::string>( m_headers.insert(std::pair<std::string, std::string>(
@@ -29,7 +34,7 @@ void Response::SetContentType(const std::string type) {
void Response::Send(int clientSocket) { void Response::Send(int clientSocket) {
std::stringstream ss; std::stringstream ss;
ss << "HTTP/1.1 " << m_statusCode << " " ss << "HTTP/1.1 " << m_statusCode << " "
<< http::StatusCodeString(m_statusCode) << "\n"; << statuscode::StatusCodeString(m_statusCode) << "\n";
for (const auto &[key, value] : m_headers) { for (const auto &[key, value] : m_headers) {
ss << key << ": " << value << "\n"; ss << key << ": " << value << "\n";
} }

View File

@@ -6,14 +6,15 @@
#include <string> #include <string>
#include <vector> #include <vector>
namespace http {
class Response { class Response {
private: private:
std::map<std::string, std::string> m_headers; std::map<std::string, std::string> m_headers;
std::vector<std::byte> m_payload; std::vector<std::byte> m_payload;
http::statusCode m_statusCode; statuscode::statusCode m_statusCode;
public: public:
Response(http::statusCode statusCode); Response(statuscode::statusCode statusCode);
// Response(std::vector<std::byte> data); // Response(std::vector<std::byte> data);
// Response(std::string data); // Response(std::string data);
void SetPayload(std::vector<std::byte> data); void SetPayload(std::vector<std::byte> data);
@@ -22,5 +23,6 @@ public:
void Send(int clientSocket); void Send(int clientSocket);
void Print(); void Print();
}; };
} // namespace http
#endif // !RESPONSE_H #endif // !RESPONSE_H

View File

@@ -1,8 +1,11 @@
#include "router.hpp" #include "router.hpp"
#include "http.hpp"
#include <csignal> #include <csignal>
#include <mutex> #include <mutex>
#include <strings.h> #include <strings.h>
using namespace http;
Router::Router(int port) { Router::Router(int port) {
m_socket = socket(AF_INET, SOCK_STREAM, 0); m_socket = socket(AF_INET, SOCK_STREAM, 0);
m_address.sin_family = AF_INET; m_address.sin_family = AF_INET;
@@ -118,11 +121,11 @@ Response Router::Route(Request req) {
path.erase(0, uPos + 1); path.erase(0, uPos + 1);
} }
if (found) { if (found) {
Response res(http::OK); Response res(statuscode::OK);
req.path.Match(patternCopy); req.path.Match(patternCopy);
return value(req, res); return value(req, res);
} }
} }
return Response(http::NOT_FOUND); return Response(statuscode::NOT_FOUND);
} }

View File

@@ -11,6 +11,7 @@
#include <queue> #include <queue>
#include <string> #include <string>
namespace http {
class Router { class Router {
private: private:
std::map<std::string, std::function<Response(Request, Response)>> m_routes; std::map<std::string, std::function<Response(Request, Response)>> m_routes;
@@ -36,5 +37,6 @@ public:
int Start(); int Start();
int Stop(); int Stop();
}; };
} // namespace http
#endif // !ROUTER_H #endif // !ROUTER_H