From 7e80a9e42295d9dbf6a55c4c7d262fb2aa16f7a1 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Sun, 6 Nov 2022 20:54:34 +0100 Subject: [PATCH] started to add routes --- includes/Nodes.hpp | 1 + includes/Route.hpp | 4 ++-- includes/Server.hpp | 3 ++- includes/Socket.hpp | 3 ++- srcs/json/Nodes.cpp | 5 +++++ srcs/load/Route.cpp | 18 ++++++++++++++++++ srcs/load/Server.cpp | 29 +++++++++++++++++++++-------- srcs/load/Socket.cpp | 5 ++++- 8 files changed, 55 insertions(+), 13 deletions(-) diff --git a/includes/Nodes.hpp b/includes/Nodes.hpp index 59c6223..3c8c5a5 100644 --- a/includes/Nodes.hpp +++ b/includes/Nodes.hpp @@ -7,6 +7,7 @@ class JSONNode { JSONList lst(); string str(); int nbr(); + bool boo(); private: enum Type { OBJECT, LIST, STRING, NUMBER, BOOLEAN, NULL_TYPE }; diff --git a/includes/Route.hpp b/includes/Route.hpp index aa3ade6..4ea478d 100644 --- a/includes/Route.hpp +++ b/includes/Route.hpp @@ -7,6 +7,6 @@ class Route { std::vector _indexs; bool _autoindex; public: - Route(); - ~Route(); + Route(JSONNode *datas); + ~Route(void); }; diff --git a/includes/Server.hpp b/includes/Server.hpp index 31ec8f5..e740f27 100644 --- a/includes/Server.hpp +++ b/includes/Server.hpp @@ -1,7 +1,7 @@ #pragma once #include "webserv.hpp" -class Server { +class Server: public Route{ string _name; std::vector _sockets; std::map _routes; @@ -11,4 +11,5 @@ class Server { ~Server(); void set_fds(); void refresh(); + Route *get_route(string uri); }; diff --git a/includes/Socket.hpp b/includes/Socket.hpp index e81cce2..5320d02 100644 --- a/includes/Socket.hpp +++ b/includes/Socket.hpp @@ -2,6 +2,7 @@ #include "webserv.hpp" class Socket { + Server *_server; string _ip; int _port; int _master_socket; @@ -13,7 +14,7 @@ class Socket { static int _max_fd; static int _min_fd; static int _amount; - Socket(string def); + Socket(Server *server, string def); ~Socket(); int launch(); void set_fds(); diff --git a/srcs/json/Nodes.cpp b/srcs/json/Nodes.cpp index 2ca3f49..ece14b4 100644 --- a/srcs/json/Nodes.cpp +++ b/srcs/json/Nodes.cpp @@ -23,6 +23,11 @@ int JSONNode::nbr() { return values.nbr; throw std::logic_error("Improper return"); } +bool JSONNode::boo() { + if (type == BOOLEAN) + return values.bValue; + throw std::logic_error("Improper return"); +} void JSONNode::setObject(JSONObject *object) { this->values.object = object; diff --git a/srcs/load/Route.cpp b/srcs/load/Route.cpp index 8da4649..46af13b 100644 --- a/srcs/load/Route.cpp +++ b/srcs/load/Route.cpp @@ -1 +1,19 @@ #include "webserv.hpp" + +Route::Route(JSONNode *datas) { + JSONObject object = datas->obj(); + if (object["root"]) + _root = object["root"]->str(); + if (object["return"]) + _ret = object["return"]->str(); + if (object["autoindex"]) + _autoindex = object["autoindex"]->boo(); + if (object["indexs"]) { + JSONList indexs = object["indexs"]->lst(); + for (JSONList::iterator it = indexs.begin(); it < indexs.end(); it++) { + _indexs.push_back((*it)->str()); + } + } +} + +Route::~Route(void) {} diff --git a/srcs/load/Server.cpp b/srcs/load/Server.cpp index 14f7f1f..6a34b92 100644 --- a/srcs/load/Server.cpp +++ b/srcs/load/Server.cpp @@ -1,37 +1,50 @@ #include "webserv.hpp" -Server::Server(JSONNode *server) { +Server::Server(JSONNode *server):Route(server) { JSONObject datas = server->obj(); if (datas["server_name"]) _name = datas["server_name"]->str(); if (datas["listens"]) { JSONList listens = datas["listens"]->lst(); - for (JSONList::iterator i = listens.begin(); i < listens.end(); i++) { - Socket *sock = new Socket((*i)->str()); + for (JSONList::iterator it = listens.begin(); it < listens.end(); it++) { + Socket *sock = new Socket(this, (*it)->str()); if (sock->launch() == EXIT_SUCCESS) _sockets.push_back(sock); else delete sock; } } - //_routes["default"] = new Route(datas["root"], datas["return"], - // datas["index"], datas["autoindex"]); + if (datas["locations"]) { + JSONObject locations = datas["locations"]->obj(); + for (JSONObject::iterator it = locations.begin(); it != locations.end(); it++) { + Route *route = new Route((*it).second); + _routes[(*it).first] = route; + } + } } -Server::~Server() { +Server::~Server(void) { cout << "Server destroyed!\n"; } -void Server::set_fds() { +void Server::set_fds(void) { for (std::vector::iterator it = _sockets.begin(); it < _sockets.end(); it++) { (*it)->set_fds(); } } -void Server::refresh() { +void Server::refresh(void) { for (std::vector::iterator it = _sockets.begin(); it < _sockets.end(); it++) { (*it)->refresh(); } } + +Route *Server::get_route(string uri) { + for (std::map::iterator it = _routes.begin(); it != _routes.end(); it++) { + if (uri == (*it).first) + return (*it).second; + } + return this; +} diff --git a/srcs/load/Socket.cpp b/srcs/load/Socket.cpp index 2d8dfd5..9aa60a2 100644 --- a/srcs/load/Socket.cpp +++ b/srcs/load/Socket.cpp @@ -1,7 +1,7 @@ #include "webserv.hpp" -Socket::Socket(string def) { +Socket::Socket(Server *server, string def): _server(server) { size_t sep_pos = def.rfind(':'); size_t ip6_endpos = def.rfind(']'); @@ -103,6 +103,9 @@ void Socket::refresh() { } void Socket::answer(int fd, string request) { + string uri = "path/to/page/"; + Route *route = _server->get_route(uri); + (void)route; char r404[72] = "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 4\n\n404!"; cout << request << "\n";