Change router input logic and more
This commit is contained in:
41
main.cpp
41
main.cpp
@@ -1,37 +1,42 @@
|
|||||||
|
#include "http.hpp"
|
||||||
#include "router.hpp"
|
#include "router.hpp"
|
||||||
|
|
||||||
using namespace http;
|
using namespace http;
|
||||||
|
|
||||||
int main() {
|
void HelloWorld(Request req, Response *res) {
|
||||||
|
res->SetPayload("Hello World!");
|
||||||
|
res->SetContentType("text/plain");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
Router router(8181);
|
||||||
|
|
||||||
http::Router router(8080);
|
|
||||||
// Allow all Methods
|
// Allow all Methods
|
||||||
router.Handle("/helloWorld", [](Request req, Response res) -> Response {
|
router.Handle("/helloWorld", HelloWorld);
|
||||||
res.SetPayload("Hello World!");
|
|
||||||
res.SetContentType("text/plain");
|
router.Handle("/healthz", [](Request req, Response *res) {
|
||||||
return res;
|
res->SetStatusCode(statuscode::OK);
|
||||||
|
res->SetPayload(std::vector<std::byte>());
|
||||||
|
res->SetContentType("text/plain");
|
||||||
});
|
});
|
||||||
|
|
||||||
// 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) {
|
||||||
std::string name = req.path.Get("name").value_or("No Name given");
|
std::string name = req.path.Get("name").value_or("No Name given");
|
||||||
res.SetPayload("Hello " + name);
|
res->SetPayload("Hello " + name);
|
||||||
res.SetContentType("text/plain");
|
res->SetContentType("text/plain");
|
||||||
return res;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Only allow POST
|
// Only allow POST
|
||||||
router.Handle("POST /echo/{name}", [](Request req, Response res) -> Response {
|
router.Handle("POST /echo/{name}", [](Request req, Response *res) {
|
||||||
std::string name = req.path.Get("name").value_or("No Name given");
|
std::string name = req.path.Get("name").value_or("No Name given");
|
||||||
res.SetPayload("Hello with Post" + name);
|
res->SetPayload("Hello with Post" + name);
|
||||||
res.SetContentType("text/plain");
|
res->SetContentType("text/plain");
|
||||||
return res;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
router.Handle("/", [](Request req, Response res) -> Response {
|
router.Handle("/", [](Request req, Response *res) {
|
||||||
res.SetPayload("Main");
|
res->SetPayload("Main");
|
||||||
res.SetContentType("text/plain");
|
res->SetContentType("text/plain");
|
||||||
return res;
|
|
||||||
});
|
});
|
||||||
|
|
||||||
router.Start();
|
router.Start();
|
||||||
|
|||||||
15
response.cpp
15
response.cpp
@@ -10,11 +10,16 @@ using namespace http;
|
|||||||
|
|
||||||
Response::Response(statuscode::statusCode statusCode) {
|
Response::Response(statuscode::statusCode statusCode) {
|
||||||
m_statusCode = statusCode;
|
m_statusCode = statusCode;
|
||||||
|
SetHeader("Connection", "close");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::SetPayload(std::vector<std::byte> data) {
|
void Response::SetHeader(const std::string name, const std::string value) {
|
||||||
|
m_headers.insert_or_assign(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Response::SetPayload(const std::vector<std::byte> data) {
|
||||||
m_headers.insert(std::pair<std::string, std::string>(
|
m_headers.insert(std::pair<std::string, std::string>(
|
||||||
"content-length", std::to_string(data.size())));
|
"Content-Length", std::to_string(data.size())));
|
||||||
m_payload = data;
|
m_payload = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -22,9 +27,9 @@ void Response::SetStatusCode(statuscode::statusCode statuscode) {
|
|||||||
m_statusCode = statuscode;
|
m_statusCode = statuscode;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::SetPayload(std::string data) {
|
void Response::SetPayload(const std::string data) {
|
||||||
m_headers.insert(std::pair<std::string, std::string>(
|
m_headers.insert(std::pair<std::string, std::string>(
|
||||||
"content-length", std::to_string(std::strlen(data.data()))));
|
"Content-Length", std::to_string(std::strlen(data.data()))));
|
||||||
|
|
||||||
m_payload = std::vector<std::byte>(data.size() + 1);
|
m_payload = std::vector<std::byte>(data.size() + 1);
|
||||||
std::transform(data.begin(), data.end(), m_payload.begin(),
|
std::transform(data.begin(), data.end(), m_payload.begin(),
|
||||||
@@ -32,7 +37,7 @@ void Response::SetPayload(std::string data) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Response::SetContentType(const std::string type) {
|
void Response::SetContentType(const std::string type) {
|
||||||
m_headers.insert_or_assign("content-type", type);
|
m_headers.insert_or_assign("Content-Type", type);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Response::Send(int clientSocket) {
|
void Response::Send(int clientSocket) {
|
||||||
|
|||||||
@@ -17,8 +17,9 @@ public:
|
|||||||
Response(statuscode::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 SetHeader(const std::string name, const std::string value);
|
||||||
void SetPayload(std::string data);
|
void SetPayload(const std::vector<std::byte> data);
|
||||||
|
void SetPayload(const std::string data);
|
||||||
void SetContentType(const std::string type);
|
void SetContentType(const std::string type);
|
||||||
void SetStatusCode(const statuscode::statusCode statuscode);
|
void SetStatusCode(const statuscode::statusCode statuscode);
|
||||||
void Send(int clientSocket);
|
void Send(int clientSocket);
|
||||||
|
|||||||
16
router.cpp
16
router.cpp
@@ -2,8 +2,12 @@
|
|||||||
#include "http.hpp"
|
#include "http.hpp"
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <iostream>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
|
#include <sys/select.h>
|
||||||
|
#include <sys/socket.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
using namespace http;
|
using namespace http;
|
||||||
|
|
||||||
@@ -78,17 +82,23 @@ void Router::ThreadLoop() {
|
|||||||
Request req(buffer);
|
Request req(buffer);
|
||||||
Response res = Route(req);
|
Response res = Route(req);
|
||||||
res.Send(client);
|
res.Send(client);
|
||||||
|
|
||||||
|
shutdown(client, SHUT_WR);
|
||||||
|
while (recv(client, buffer.data(), buffer.size(), 0) > 0) {
|
||||||
|
std::cout << buffer.data() << std::endl;
|
||||||
|
}
|
||||||
close(client);
|
close(client);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Router::Handle(std::string pathPattern,
|
void Router::Handle(std::string pathPattern,
|
||||||
std::function<Response(Request, Response)> func) {
|
std::function<void(Request, Response *)> func) {
|
||||||
m_routes.insert_or_assign(pathPattern, func);
|
m_routes.insert_or_assign(pathPattern, func);
|
||||||
}
|
}
|
||||||
|
|
||||||
// This should be better
|
// This should be better
|
||||||
// Probably dont use map but a tree for it, then traverse tree for routing
|
// Probably dont use map but a tree for it, then traverse tree for routing
|
||||||
|
// Also this isnt accurate
|
||||||
Response Router::Route(Request req) {
|
Response Router::Route(Request req) {
|
||||||
for (const auto &[key, value] : m_routes) {
|
for (const auto &[key, value] : m_routes) {
|
||||||
std::string pattern = key;
|
std::string pattern = key;
|
||||||
@@ -121,10 +131,12 @@ Response Router::Route(Request req) {
|
|||||||
pattern.erase(0, pos + 1);
|
pattern.erase(0, pos + 1);
|
||||||
path.erase(0, uPos + 1);
|
path.erase(0, uPos + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (found) {
|
if (found) {
|
||||||
Response res(statuscode::OK);
|
Response res(statuscode::OK);
|
||||||
req.path.Match(patternCopy);
|
req.path.Match(patternCopy);
|
||||||
return value(req, res);
|
value(req, &res);
|
||||||
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,7 +14,7 @@
|
|||||||
namespace http {
|
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<void(Request, Response *)>> m_routes;
|
||||||
int m_socket;
|
int m_socket;
|
||||||
sockaddr_in m_address;
|
sockaddr_in m_address;
|
||||||
Response Route(Request req);
|
Response Route(Request req);
|
||||||
@@ -33,7 +33,7 @@ private:
|
|||||||
public:
|
public:
|
||||||
Router(int port);
|
Router(int port);
|
||||||
void Handle(std::string pathPattern,
|
void Handle(std::string pathPattern,
|
||||||
std::function<Response(Request, Response)> func);
|
std::function<void(Request, Response *)> func);
|
||||||
int Start();
|
int Start();
|
||||||
int Stop();
|
int Stop();
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user