Browse Source

started to add routes

test
nicolas-arnaud 2 years ago
parent
commit
7e80a9e422
  1. 1
      includes/Nodes.hpp
  2. 4
      includes/Route.hpp
  3. 3
      includes/Server.hpp
  4. 3
      includes/Socket.hpp
  5. 5
      srcs/json/Nodes.cpp
  6. 18
      srcs/load/Route.cpp
  7. 29
      srcs/load/Server.cpp
  8. 5
      srcs/load/Socket.cpp

1
includes/Nodes.hpp

@ -7,6 +7,7 @@ class JSONNode {
JSONList lst(); JSONList lst();
string str(); string str();
int nbr(); int nbr();
bool boo();
private: private:
enum Type { OBJECT, LIST, STRING, NUMBER, BOOLEAN, NULL_TYPE }; enum Type { OBJECT, LIST, STRING, NUMBER, BOOLEAN, NULL_TYPE };

4
includes/Route.hpp

@ -7,6 +7,6 @@ class Route {
std::vector<string> _indexs; std::vector<string> _indexs;
bool _autoindex; bool _autoindex;
public: public:
Route(); Route(JSONNode *datas);
~Route(); ~Route(void);
}; };

3
includes/Server.hpp

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "webserv.hpp" #include "webserv.hpp"
class Server { class Server: public Route{
string _name; string _name;
std::vector<Socket *> _sockets; std::vector<Socket *> _sockets;
std::map<string, Route *> _routes; std::map<string, Route *> _routes;
@ -11,4 +11,5 @@ class Server {
~Server(); ~Server();
void set_fds(); void set_fds();
void refresh(); void refresh();
Route *get_route(string uri);
}; };

3
includes/Socket.hpp

@ -2,6 +2,7 @@
#include "webserv.hpp" #include "webserv.hpp"
class Socket { class Socket {
Server *_server;
string _ip; string _ip;
int _port; int _port;
int _master_socket; int _master_socket;
@ -13,7 +14,7 @@ class Socket {
static int _max_fd; static int _max_fd;
static int _min_fd; static int _min_fd;
static int _amount; static int _amount;
Socket(string def); Socket(Server *server, string def);
~Socket(); ~Socket();
int launch(); int launch();
void set_fds(); void set_fds();

5
srcs/json/Nodes.cpp

@ -23,6 +23,11 @@ int JSONNode::nbr() {
return values.nbr; return values.nbr;
throw std::logic_error("Improper return"); 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) { void JSONNode::setObject(JSONObject *object) {
this->values.object = object; this->values.object = object;

18
srcs/load/Route.cpp

@ -1 +1,19 @@
#include "webserv.hpp" #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) {}

29
srcs/load/Server.cpp

@ -1,37 +1,50 @@
#include "webserv.hpp" #include "webserv.hpp"
Server::Server(JSONNode *server) { Server::Server(JSONNode *server):Route(server) {
JSONObject datas = server->obj(); JSONObject datas = server->obj();
if (datas["server_name"]) if (datas["server_name"])
_name = datas["server_name"]->str(); _name = datas["server_name"]->str();
if (datas["listens"]) { if (datas["listens"]) {
JSONList listens = datas["listens"]->lst(); JSONList listens = datas["listens"]->lst();
for (JSONList::iterator i = listens.begin(); i < listens.end(); i++) { for (JSONList::iterator it = listens.begin(); it < listens.end(); it++) {
Socket *sock = new Socket((*i)->str()); Socket *sock = new Socket(this, (*it)->str());
if (sock->launch() == EXIT_SUCCESS) if (sock->launch() == EXIT_SUCCESS)
_sockets.push_back(sock); _sockets.push_back(sock);
else else
delete sock; delete sock;
} }
} }
//_routes["default"] = new Route(datas["root"], datas["return"], if (datas["locations"]) {
// datas["index"], datas["autoindex"]); 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"; cout << "Server destroyed!\n";
} }
void Server::set_fds() { void Server::set_fds(void) {
for (std::vector<Socket *>::iterator it = _sockets.begin(); for (std::vector<Socket *>::iterator it = _sockets.begin();
it < _sockets.end(); it++) { it < _sockets.end(); it++) {
(*it)->set_fds(); (*it)->set_fds();
} }
} }
void Server::refresh() { void Server::refresh(void) {
for (std::vector<Socket *>::iterator it = _sockets.begin(); for (std::vector<Socket *>::iterator it = _sockets.begin();
it < _sockets.end(); it++) { it < _sockets.end(); it++) {
(*it)->refresh(); (*it)->refresh();
} }
} }
Route *Server::get_route(string uri) {
for (std::map<string, Route *>::iterator it = _routes.begin(); it != _routes.end(); it++) {
if (uri == (*it).first)
return (*it).second;
}
return this;
}

5
srcs/load/Socket.cpp

@ -1,7 +1,7 @@
#include "webserv.hpp" #include "webserv.hpp"
Socket::Socket(string def) { Socket::Socket(Server *server, string def): _server(server) {
size_t sep_pos = def.rfind(':'); size_t sep_pos = def.rfind(':');
size_t ip6_endpos = def.rfind(']'); size_t ip6_endpos = def.rfind(']');
@ -103,6 +103,9 @@ void Socket::refresh() {
} }
void Socket::answer(int fd, string request) { void Socket::answer(int fd, string request) {
string uri = "path/to/page/";
Route *route = _server->get_route(uri);
(void)route;
char r404[72] = char r404[72] =
"HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 4\n\n404!"; "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 4\n\n404!";
cout << request << "\n"; cout << request << "\n";

Loading…
Cancel
Save