Compare commits

...

2 Commits
master ... test

  1. 2
      Makefile
  2. 2
      default.json
  3. 2
      includes/Env.hpp
  4. 6
      includes/Server.hpp
  5. 11
      includes/Sock6.hpp
  6. 23
      includes/Socket.hpp
  7. 8
      includes/webserv.hpp
  8. 73
      srcs/load/Env.cpp
  9. 2
      srcs/load/Route.cpp
  10. 56
      srcs/load/Server.cpp
  11. 82
      srcs/load/Sock6.cpp
  12. 61
      srcs/load/Socket.cpp
  13. 33
      srcs/tools.cpp

2
Makefile

@ -1,6 +1,6 @@
NAME= server NAME= server
SRCS= srcs/webserv.cpp srcs/tools.cpp srcs/load/Env.cpp srcs/load/Server.cpp \ SRCS= srcs/webserv.cpp srcs/tools.cpp srcs/load/Env.cpp srcs/load/Server.cpp \
srcs/load/Socket.cpp srcs/load/Route.cpp \ srcs/load/Socket.cpp srcs/load/Sock6.cpp srcs/load/Route.cpp \
srcs/json/Nodes.cpp srcs/json/Token.cpp srcs/json/Parser.cpp srcs/json/Nodes.cpp srcs/json/Token.cpp srcs/json/Parser.cpp
OBJS= $(SRCS:.cpp=.o) OBJS= $(SRCS:.cpp=.o)
CXX=c++ CXX=c++

2
default.json

@ -4,7 +4,7 @@
"servers": [ "servers": [
{ {
"server_name": "localhost", "server_name": "localhost",
"listens": ["80", "localhost:80", "localhost", "[::]:443"], "listens": ["80", "localhost:80", "localhost", "[::]:5555"],
"return": "301 https://$host$uri" "return": "301 https://$host$uri"
}, },
{ {

2
includes/Env.hpp

@ -3,9 +3,11 @@
class Env { class Env {
std::vector<Server *> _servers; std::vector<Server *> _servers;
std::vector<Socket *> _sockets;
public: public:
Env(JSONNode *conf); Env(JSONNode *conf);
void set_fds(); void set_fds();
void refresh(); void refresh();
Server *choose_server(Socket *sock, string host);
}; };

6
includes/Server.hpp

@ -3,13 +3,11 @@
class Server : public Route { class Server : public Route {
string _name; string _name;
std::vector<Socket *> _sockets;
std::map<string, Route *> _routes; std::map<string, Route *> _routes;
public: public:
std::vector<listen_t> _listens;
Server(JSONNode *server); Server(JSONNode *server);
~Server(); ~Server();
void set_fds(); std::vector<Socket *> get_sockets(JSONNode *server);
void refresh();
Route *get_route(string uri); Route *get_route(string uri);
}; };

11
includes/Sock6.hpp

@ -0,0 +1,11 @@
#pragma once
#include "webserv.hpp"
class Sock6: public Socket {
struct sockaddr_in6 _addr6;
public:
Sock6(listen_t listen);
~Sock6();
int launch();
void refresh(Env *env);
};

23
includes/Socket.hpp

@ -2,33 +2,24 @@
#include "webserv.hpp" #include "webserv.hpp"
class Socket { class Socket {
Server *_server; struct sockaddr_in _addr;
string _ip; protected:
int _port;
int _master_socket; int _master_socket;
struct sockaddr_in _address;
int _clients_amount; int _clients_amount;
std::vector<int> _clients; std::vector<int> _clients;
int _type;
public: public:
listen_t _listen;
static fd_set _readfds; static fd_set _readfds;
static int _max_fd; static int _max_fd;
static int _min_fd; static int _min_fd;
static int _amount; static int _amount;
Socket(Server *server, string def); Socket(listen_t listen);
~Socket(); ~Socket();
int launch(); int launch();
void set_fds(); void set_fds();
void refresh(); void refresh(Env *env);
void answer(int fd, string request); void answer(Env *env, int fd, string request);
void send_answer(int fd, string msg); void send_answer(int fd, string msg);
/*
Socket& operator=(Socket &src) {
_ip = src._ip;
_port = src._port;
_master_socket = src._master_socket;
_address = src._address;
return src;
}
*/
}; };

8
includes/webserv.hpp

@ -41,9 +41,16 @@ class JSONNode;
typedef std::map<string, JSONNode *> JSONObject; typedef std::map<string, JSONNode *> JSONObject;
typedef std::vector<JSONNode *> JSONList; typedef std::vector<JSONNode *> JSONList;
typedef struct listen_s {
string ip;
int port;
int type;
} listen_t;
void *ft_memset(void *b, int c, size_t len); void *ft_memset(void *b, int c, size_t len);
bool isInt(string str); bool isInt(string str);
std::vector<string> split(string str, char delim); std::vector<string> split(string str, char delim);
listen_t get_listen_t(string listen);
#include "Nodes.hpp" #include "Nodes.hpp"
#include "Token.hpp" #include "Token.hpp"
@ -51,5 +58,6 @@ std::vector<string> split(string str, char delim);
#include "Route.hpp" #include "Route.hpp"
#include "Socket.hpp" #include "Socket.hpp"
#include "Sock6.hpp"
#include "Server.hpp" #include "Server.hpp"
#include "Env.hpp" #include "Env.hpp"

73
srcs/load/Env.cpp

@ -1,34 +1,57 @@
#include "webserv.hpp" #include "webserv.hpp"
Env::Env(JSONNode *conf) { Env::Env(JSONNode *conf) {
try { try {
JSONList servers = conf->obj()["servers"]->lst(); JSONList servers = conf->obj()["servers"]->lst();
int i = 0; int i = 0;
string th[8] = {"first", "second", "third", "fourth", string th[8] = {"first", "second", "third", "fourth",
"fifth", "sixth", "seventh", "eigth"}; "fifth", "sixth", "seventh", "eigth"};
for (std::vector<JSONNode *>::iterator it = servers.begin(); for (std::vector<JSONNode *>::iterator it = servers.begin();
it < servers.end(); it++) { it < servers.end(); it++) {
Server *server = new Server(*it); Server *server = new Server(*it);
_servers.push_back(server); _servers.push_back(server);
// delete *it; std::vector<Socket *> tmp_s = server->get_sockets(*it);
cout << th[i] << " server launched.\n"; _sockets.insert(_sockets.end(), tmp_s.begin(), tmp_s.end());
i++; // delete *it;
} cout << th[i] << " server launched.\n";
} catch (std::exception &e) { i++;
cout << e.what(); }
} } catch (std::exception &e) {
delete conf; cout << e.what();
}
delete conf;
} }
Server *Env::choose_server(Socket *sock, string host) {
std::vector<Server *> exact;
std::vector<Server *> inrange;
(void)host;
for (std::vector<Server *>::iterator sit = _servers.begin();
sit < _servers.end(); sit++) {
std::vector<listen_t> serv_listens = (*sit)->_listens;
for (std::vector<listen_t>::iterator it = serv_listens.begin();
it < serv_listens.end(); it++) {
if (sock->_listen.port != (*it).port)
continue;
if (sock->_listen.ip == (*it).ip)
exact.push_back(*sit);
// else if (is_ip_into(sock->_listen.ip, (*it).ip))
}
}
return (exact.at(0));
}
void Env::set_fds() { void Env::set_fds() {
for (std::vector<Server *>::iterator it = _servers.begin(); for (std::vector<Socket *>::iterator it = _sockets.begin();
it < _servers.end(); it++) { it < _sockets.end(); it++) {
(*it)->set_fds(); (*it)->set_fds();
} }
} }
void Env::refresh() { void Env::refresh() {
for (std::vector<Server *>::iterator it = _servers.begin(); for (std::vector<Socket *>::iterator it = _sockets.begin();
it < _servers.end(); it++) { it < _sockets.end(); it++) {
(*it)->refresh(); (*it)->refresh(this);
} }
} }

2
srcs/load/Route.cpp

@ -33,7 +33,7 @@ string Route::getAutoindex(string uri) {
struct stat info; struct stat info;
if ((dir = opendir(path.c_str())) == NULL) if ((dir = opendir(path.c_str())) == NULL)
ret << " 19\n\nFolder unaccesible."; page << "Folder unaccesible.";
else { else {
page << path << " files :\n"; page << path << " files :\n";
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL) {

56
srcs/load/Server.cpp

@ -4,17 +4,6 @@ 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"]) {
JSONList listens = datas["listens"]->lst();
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;
}
}
if (datas["locations"]) { if (datas["locations"]) {
JSONObject locations = datas["locations"]->obj(); JSONObject locations = datas["locations"]->obj();
for (JSONObject::iterator it = locations.begin(); it != locations.end(); for (JSONObject::iterator it = locations.begin(); it != locations.end();
@ -25,21 +14,36 @@ Server::Server(JSONNode *server) : Route(server) {
} }
} }
Server::~Server(void) { cout << "Server destroyed!\n"; } std::vector<Socket *> Server::get_sockets(JSONNode *server) {
JSONObject datas = server->obj();
void Server::set_fds(void) { std::vector<Socket *> ret;
for (std::vector<Socket *>::iterator it = _sockets.begin(); listen_t listen;
it < _sockets.end(); it++) { if (datas["listens"]) {
(*it)->set_fds(); JSONList listens = datas["listens"]->lst();
for (JSONList::iterator it = listens.begin(); it < listens.end();
it++) {
listen = get_listen_t((*it)->str());
_listens.push_back(listen);
Socket *sock;
if (listen.type == AF_INET)
sock = new Socket(listen);
else
sock = new Sock6(listen);
if (sock->launch() == EXIT_SUCCESS)
ret.push_back(sock);
else
delete sock;
}
} else {
Socket *sock = new Socket(get_listen_t("localhost:80"));
if (sock->launch() == EXIT_SUCCESS) {
ret.push_back(sock);
}
} }
return ret;
} }
void Server::refresh(void) { Server::~Server(void) { cout << "Server destroyed!\n"; }
for (std::vector<Socket *>::iterator it = _sockets.begin();
it < _sockets.end(); it++) {
(*it)->refresh();
}
}
Route *Server::get_route(string uri) { Route *Server::get_route(string uri) {
cout << uri << "\n"; cout << uri << "\n";
@ -50,10 +54,10 @@ Route *Server::get_route(string uri) {
root = split((*rit).first, '/'); root = split((*rit).first, '/');
std::vector<string>::iterator root_it = root.begin(); std::vector<string>::iterator root_it = root.begin();
for (std::vector<string>::iterator it = req.begin(); for (std::vector<string>::iterator it = req.begin(); it < req.end();
it < req.end(); it++) { it++) {
if (*it == "") if (*it == "")
continue ; continue;
cout << *it << " - " << *root_it << "\n"; cout << *it << " - " << *root_it << "\n";
if (*it != *(root_it++)) if (*it != *(root_it++))
break; break;

82
srcs/load/Sock6.cpp

@ -0,0 +1,82 @@
#include "webserv.hpp"
Sock6::Sock6(listen_t listen) : Socket(listen) { _type = AF_INET6; }
int Sock6::launch() {
int opt = 1;
string ip = _listen.ip;
int port = _listen.port;
_addr6.sin6_family = AF_INET6;
inet_pton(AF_INET6, ip.c_str(), (void *)&(_addr6.sin6_addr.s6_addr));
_addr6.sin6_port = htons(port);
_master_socket = socket(_addr6.sin6_family, SOCK_STREAM, 0);
if (_master_socket == 0) {
cout << "Socket creation: " << strerror(errno) << "\n";
return (EXIT_FAILURE);
}
int opt_ret = setsockopt(_master_socket, SOL_SOCKET, SO_REUSEADDR,
(char *)&opt, sizeof(opt));
if (opt_ret < 0) {
cout << "Sockopt: " << strerror(errno) << "\n";
return (EXIT_FAILURE);
}
if (bind(_master_socket, (struct sockaddr *)&_addr6, sizeof(_addr6)) < 0) {
cout << "Bind: " << strerror(errno) << "\n";
return (EXIT_FAILURE);
}
cout << "Listener " << ip << " on port " << port << "\n";
if (listen(_master_socket, 3) < 0) {
cout << "Listen: " << strerror(errno) << "\n";
return (EXIT_FAILURE);
}
cout << "Socket::_master_socket: " << _master_socket << "\n";
if (_master_socket < _min_fd)
_min_fd = _master_socket;
_amount++;
return (EXIT_SUCCESS);
}
void Sock6::refresh(Env *env) {
std::vector<int>::iterator it;
int valread;
int addrlen = sizeof(_addr6);
char buffer[10000];
if (FD_ISSET(_master_socket, &_readfds)) {
int new_socket = accept(_master_socket, (struct sockaddr *)&_addr6,
(socklen_t *)&addrlen);
if (new_socket < 0) {
cout << "Accept: " << strerror(errno) << "\n";
exit(EXIT_FAILURE);
}
#ifdef __APPLE__
fcntl(new_socket, F_GETNOSIGPIPE);
#endif
cout << "New connection, socket fd is "
<< new_socket
// << ", ip is : " << inet_ntoa(_addr6.sin6_addr)
<< ", port : " << ntohs(_addr6.sin6_port) << "\n";
_clients.push_back(new_socket);
}
for (it = _clients.begin(); it < _clients.end(); it++) {
if (FD_ISSET(*it, &_readfds)) {
valread = read(*it, buffer, 10000);
if (valread == 0) {
getpeername(*it, (struct sockaddr *)&_addr6,
(socklen_t *)&addrlen);
// cout << "Host disconnected, ip " <<
//inet_ntoa(_addr6.sin6_addr)
// << ", port " << ntohs(_addr6.sin6_port) << "\n";
close(*it);
_clients.erase(it);
} else {
buffer[valread] = '\0';
answer(env, *it, buffer);
}
}
}
}

61
srcs/load/Socket.cpp

@ -1,18 +1,9 @@
#include "webserv.hpp" #include "webserv.hpp"
Socket::Socket(Server *server, string def) : _server(server) { Socket::Socket(listen_t listen) : _listen(listen) {
size_t sep_pos = def.rfind(':');
size_t ip6_endpos = def.rfind(']');
string tmp = def.substr(0, sep_pos);
if (ip6_endpos > sep_pos)
_ip = def;
else
_ip = isInt(tmp) || tmp == "localhost" ? "127.0.0.1" : tmp;
tmp = def.substr(sep_pos + 1, def.length() - sep_pos - 1).c_str();
_port = !isInt(tmp) ? 80 : std::atoi(tmp.c_str());
_clients_amount = 0; _clients_amount = 0;
_type = AF_INET;
} }
Socket::~Socket() { Socket::~Socket() {
close(_master_socket); close(_master_socket);
@ -21,7 +12,15 @@ Socket::~Socket() {
int Socket::launch() { int Socket::launch() {
int opt = 1; int opt = 1;
_master_socket = socket(AF_INET, SOCK_STREAM, 0); string ip = _listen.ip;
int port = _listen.port;
_type = AF_INET;
_addr.sin_family = AF_INET;
_addr.sin_addr.s_addr = inet_addr(ip.c_str());
_addr.sin_port = htons(port);
_master_socket = socket(_addr.sin_family, SOCK_STREAM, 0);
if (_master_socket == 0) { if (_master_socket == 0) {
cout << "Socket creation: " << strerror(errno) << "\n"; cout << "Socket creation: " << strerror(errno) << "\n";
return (EXIT_FAILURE); return (EXIT_FAILURE);
@ -33,19 +32,11 @@ int Socket::launch() {
return (EXIT_FAILURE); return (EXIT_FAILURE);
} }
if (_ip.at(0) == '[') if (bind(_master_socket, (struct sockaddr *)&_addr, sizeof(_addr)) < 0) {
_address.sin_family = AF_INET6;
else
_address.sin_family = AF_INET;
_address.sin_addr.s_addr = inet_addr(_ip.c_str());
_address.sin_port = htons(_port);
if (bind(_master_socket, (struct sockaddr *)&_address, sizeof(_address)) <
0) {
cout << "Bind: " << strerror(errno) << "\n"; cout << "Bind: " << strerror(errno) << "\n";
return (EXIT_FAILURE); return (EXIT_FAILURE);
} }
cout << "Listener " << _ip << " on port " << _port << "\n"; cout << "Listener " << ip << " on port " << port << "\n";
if (listen(_master_socket, 3) < 0) { if (listen(_master_socket, 3) < 0) {
cout << "Listen: " << strerror(errno) << "\n"; cout << "Listen: " << strerror(errno) << "\n";
@ -69,13 +60,13 @@ void Socket::set_fds() {
} }
} }
void Socket::refresh() { void Socket::refresh(Env *env) {
std::vector<int>::iterator it; std::vector<int>::iterator it;
int valread; int valread;
int addrlen = sizeof(_address); int addrlen = sizeof(_addr);
char buffer[10000]; char buffer[10000];
if (FD_ISSET(_master_socket, &_readfds)) { if (FD_ISSET(_master_socket, &_readfds)) {
int new_socket = accept(_master_socket, (struct sockaddr *)&_address, int new_socket = accept(_master_socket, (struct sockaddr *)&_addr,
(socklen_t *)&addrlen); (socklen_t *)&addrlen);
if (new_socket < 0) { if (new_socket < 0) {
cout << "Accept: " << strerror(errno) << "\n"; cout << "Accept: " << strerror(errno) << "\n";
@ -85,37 +76,39 @@ void Socket::refresh() {
fcntl(new_socket, F_GETNOSIGPIPE); fcntl(new_socket, F_GETNOSIGPIPE);
#endif #endif
cout << "New connection, socket fd is " << new_socket cout << "New connection, socket fd is " << new_socket
<< ", ip is : " << inet_ntoa(_address.sin_addr) << ", ip is : " << inet_ntoa(_addr.sin_addr)
<< ", port : " << ntohs(_address.sin_port) << "\n"; << ", port : " << ntohs(_addr.sin_port) << "\n";
_clients.push_back(new_socket); _clients.push_back(new_socket);
} }
for (it = _clients.begin(); it < _clients.end(); it++) { for (it = _clients.begin(); it < _clients.end(); it++) {
if (FD_ISSET(*it, &_readfds)) { if (FD_ISSET(*it, &_readfds)) {
valread = read(*it, buffer, 10000); valread = read(*it, buffer, 10000);
if (valread == 0) { if (valread == 0) {
getpeername(*it, (struct sockaddr *)&_address, getpeername(*it, (struct sockaddr *)&_addr,
(socklen_t *)&addrlen); (socklen_t *)&addrlen);
cout << "Host disconnected, ip " << inet_ntoa(_address.sin_addr) cout << "Host disconnected, ip " << inet_ntoa(_addr.sin_addr)
<< ", port " << ntohs(_address.sin_port) << "\n"; << ", port " << ntohs(_addr.sin_port) << "\n";
close(*it); close(*it);
_clients.erase(it); _clients.erase(it);
} else { } else {
buffer[valread] = '\0'; buffer[valread] = '\0';
answer(*it, buffer); answer(env, *it, buffer);
} }
} }
} }
} }
void Socket::answer(int fd, string request) { void Socket::answer(Env *env, int fd, string request) {
string uri = split(request, ' ').at(1); std::vector<string> lines = split(request, '\n');
string uri = split(lines.at(0), ' ').at(1);
cout << uri << "\n"; cout << uri << "\n";
cout << request << "\n|===|===|===|\n"; cout << request << "\n|===|===|===|\n";
std::stringstream answer; std::stringstream answer;
answer << "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: "; answer << "HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: ";
Route *route = _server->get_route(uri); Server *server = env->choose_server(this, split(lines.at(1), ' ').at(1));
Route *route = server->get_route(uri);
answer << route->getAutoindex(uri); answer << route->getAutoindex(uri);
cout << answer.str() << "\n|===|===|===|\n"; cout << answer.str() << "\n|===|===|===|\n";
send_answer(fd, answer.str()); send_answer(fd, answer.str());

33
srcs/tools.cpp

@ -20,10 +20,33 @@ bool isInt(string str) {
std::vector<string> split(string str, char delim) { std::vector<string> split(string str, char delim) {
std::vector<std::string> tokens; std::vector<std::string> tokens;
std::string token; std::string token;
std::stringstream ss(str); std::stringstream ss(str);
while (getline(ss, token, delim)){ while (getline(ss, token, delim)) {
tokens.push_back(token); tokens.push_back(token);
} }
return tokens; return tokens;
} }
listen_t get_listen_t(string listen) {
listen_t ret;
size_t sep_pos = listen.rfind(':');
size_t ip6_endpos = listen.rfind(']');
string tmp = listen.substr(0, sep_pos);
if (ip6_endpos > sep_pos)
ret.ip = listen;
else
ret.ip = isInt(tmp) ? "0.0.0.0" : \
(tmp == "localhost" ? "127.0.0.1" : \
(tmp == "[::]" ? "[0000:0000:0000:0000:0000:0000:0000:0000]" : tmp));
tmp = listen.substr(sep_pos + 1, listen.length() - sep_pos - 1).c_str();
ret.port = !isInt(tmp) ? 80 : std::atoi(tmp.c_str());
if (ret.ip.at(0) == '[') {
ret.type = AF_INET6;
ret.ip.erase(ret.ip.length() - 1, ret.ip.length());
ret.ip.erase(0, 1);
}
else
ret.type = AF_INET;
return ret;
}

Loading…
Cancel
Save