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();
string str();
int nbr();
bool boo();
private:
enum Type { OBJECT, LIST, STRING, NUMBER, BOOLEAN, NULL_TYPE };

4
includes/Route.hpp

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

3
includes/Server.hpp

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

3
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();

5
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;

18
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) {}

29
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<Socket *>::iterator it = _sockets.begin();
it < _sockets.end(); it++) {
(*it)->set_fds();
}
}
void Server::refresh() {
void Server::refresh(void) {
for (std::vector<Socket *>::iterator it = _sockets.begin();
it < _sockets.end(); it++) {
(*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"
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";

Loading…
Cancel
Save