Add namespace
This commit is contained in:
5
http.hpp
5
http.hpp
@@ -2,8 +2,8 @@
|
||||
#define STATUSCODE_H
|
||||
|
||||
#include <string>
|
||||
|
||||
class http {
|
||||
namespace http {
|
||||
class statuscode {
|
||||
public:
|
||||
enum statusCode {
|
||||
CONTINUE = 100,
|
||||
@@ -157,5 +157,6 @@ public:
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace http
|
||||
|
||||
#endif // !STATUSCODE_H
|
||||
|
||||
8
main.cpp
8
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");
|
||||
|
||||
2
path.cpp
2
path.cpp
@@ -1,6 +1,8 @@
|
||||
#include "path.hpp"
|
||||
#include <optional>
|
||||
|
||||
using namespace http;
|
||||
|
||||
Path::Path(std::string path) {
|
||||
int pos = path.find("?");
|
||||
m_base = path.substr(0, pos);
|
||||
|
||||
2
path.hpp
2
path.hpp
@@ -5,6 +5,7 @@
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
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
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "request.hpp"
|
||||
#include <iostream>
|
||||
|
||||
using namespace http;
|
||||
|
||||
bool Request::protocol(std::stringstream *ss, int *procPart, char c) {
|
||||
if (c == ' ' || c == '\n') {
|
||||
switch (*procPart) {
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace http {
|
||||
class Request {
|
||||
private:
|
||||
std::map<std::string, std::string> m_headers;
|
||||
@@ -26,4 +27,6 @@ public:
|
||||
std::string Method();
|
||||
std::vector<std::byte> Data();
|
||||
};
|
||||
} // namespace http
|
||||
|
||||
#endif // !REQUEST_HEADER_H
|
||||
|
||||
@@ -1,11 +1,16 @@
|
||||
#include "response.hpp"
|
||||
#include "http.hpp"
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <netinet/in.h>
|
||||
#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) {
|
||||
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) {
|
||||
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";
|
||||
}
|
||||
|
||||
@@ -6,14 +6,15 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace http {
|
||||
class Response {
|
||||
private:
|
||||
std::map<std::string, std::string> m_headers;
|
||||
std::vector<std::byte> m_payload;
|
||||
http::statusCode m_statusCode;
|
||||
statuscode::statusCode m_statusCode;
|
||||
|
||||
public:
|
||||
Response(http::statusCode statusCode);
|
||||
Response(statuscode::statusCode statusCode);
|
||||
// Response(std::vector<std::byte> data);
|
||||
// Response(std::string data);
|
||||
void SetPayload(std::vector<std::byte> data);
|
||||
@@ -22,5 +23,6 @@ public:
|
||||
void Send(int clientSocket);
|
||||
void Print();
|
||||
};
|
||||
} // namespace http
|
||||
|
||||
#endif // !RESPONSE_H
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
#include "router.hpp"
|
||||
#include "http.hpp"
|
||||
#include <csignal>
|
||||
#include <mutex>
|
||||
#include <strings.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <queue>
|
||||
#include <string>
|
||||
|
||||
namespace http {
|
||||
class Router {
|
||||
private:
|
||||
std::map<std::string, std::function<Response(Request, Response)>> m_routes;
|
||||
@@ -36,5 +37,6 @@ public:
|
||||
int Start();
|
||||
int Stop();
|
||||
};
|
||||
} // namespace http
|
||||
|
||||
#endif // !ROUTER_H
|
||||
|
||||
Reference in New Issue
Block a user