Browse Source

save 22-11-6-2

test
nicolas-arnaud 2 years ago
parent
commit
48087a2563
  1. 1
      includes/Env.hpp
  2. 1
      includes/Parser.hpp
  3. 2
      includes/Route.hpp
  4. 2
      includes/Server.hpp
  5. 4
      includes/Socket.hpp
  6. 1
      srcs/json/Nodes.cpp
  7. 1
      srcs/json/Parser.cpp
  8. 14
      srcs/json/Token.cpp
  9. 6
      srcs/load/Env.cpp
  10. 4
      srcs/load/Route.cpp
  11. 15
      srcs/load/Server.cpp
  12. 12
      srcs/load/Socket.cpp
  13. 3
      srcs/tools.cpp
  14. 5
      srcs/webserv.cpp

1
includes/Env.hpp

@ -3,6 +3,7 @@
class Env {
std::vector<Server *> _servers;
public:
Env(JSONNode *conf);
void set_fds();

1
includes/Parser.hpp

@ -1,7 +1,6 @@
#pragma once
#include "webserv.hpp"
class JSONParser {
std::fstream file;
Tokenizer tokenizer;

2
includes/Route.hpp

@ -2,10 +2,12 @@
#include "webserv.hpp"
class Route {
protected:
string _root;
string _ret;
std::vector<string> _indexs;
bool _autoindex;
public:
Route(JSONNode *datas);
~Route(void);

2
includes/Server.hpp

@ -1,7 +1,7 @@
#pragma once
#include "webserv.hpp"
class Server: public Route{
class Server : public Route {
string _name;
std::vector<Socket *> _sockets;
std::map<string, Route *> _routes;

4
includes/Socket.hpp

@ -9,6 +9,7 @@ class Socket {
struct sockaddr_in _address;
int _clients_amount;
std::vector<int> _clients;
public:
static fd_set _readfds;
static int _max_fd;
@ -30,6 +31,3 @@ class Socket {
}
*/
};

1
srcs/json/Nodes.cpp

@ -1,4 +1,3 @@
#include "webserv.hpp"
#define DEBUG 0

1
srcs/json/Parser.cpp

@ -1,6 +1,5 @@
#include "webserv.hpp"
JSONParser::JSONParser(const string filename) : tokenizer(filename) {}
JSONNode *JSONParser::parse() {

14
srcs/json/Token.cpp

@ -1,18 +1,4 @@
#include "webserv.hpp"
/*
enum TOKEN {
CURLY_OPEN,
CURLY_CLOSE,
COLON,
STRING,
NUMBER,
ARRAY_OPEN,
ARRAY_CLOSE,
COMMA,
BOOLEAN,
NULL_TYPE
};
*/
Tokenizer::Tokenizer(string fileName) {
file.open(fileName.c_str(), std::ios::in);

6
srcs/load/Env.cpp

@ -1,6 +1,7 @@
#include "webserv.hpp"
Env::Env(JSONNode *conf) {
try {
JSONList servers = conf->obj()["servers"]->lst();
int i = 0;
string th[8] = {"first", "second", "third", "fourth",
@ -13,7 +14,10 @@ Env::Env(JSONNode *conf) {
cout << th[i] << " server launched.\n";
i++;
}
// delete conf;
} catch (std::exception &e) {
cout << e.what();
}
delete conf;
}
void Env::set_fds() {
for (std::vector<Server *>::iterator it = _servers.begin();

4
srcs/load/Route.cpp

@ -20,7 +20,5 @@ Route::~Route(void) {}
string getRoot(void);
string getReturn(void);
std::vector<string> Route::getIndexs(void) {
return _indexs;
}
std::vector<string> Route::getIndexs(void) { return _indexs; }
bool getAutoindex(void);

15
srcs/load/Server.cpp

@ -1,12 +1,13 @@
#include "webserv.hpp"
Server::Server(JSONNode *server):Route(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 it = listens.begin(); it < listens.end(); it++) {
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);
@ -16,16 +17,15 @@ Server::Server(JSONNode *server):Route(server) {
}
if (datas["locations"]) {
JSONObject locations = datas["locations"]->obj();
for (JSONObject::iterator it = locations.begin(); it != locations.end(); it++) {
for (JSONObject::iterator it = locations.begin(); it != locations.end();
it++) {
Route *route = new Route((*it).second);
_routes[(*it).first] = route;
}
}
}
Server::~Server(void) {
cout << "Server destroyed!\n";
}
Server::~Server(void) { cout << "Server destroyed!\n"; }
void Server::set_fds(void) {
for (std::vector<Socket *>::iterator it = _sockets.begin();
@ -42,7 +42,8 @@ void Server::refresh(void) {
}
Route *Server::get_route(string uri) {
for (std::map<string, Route *>::iterator it = _routes.begin(); it != _routes.end(); it++) {
for (std::map<string, Route *>::iterator it = _routes.begin();
it != _routes.end(); it++) {
if (uri == (*it).first)
return (*it).second;
}

12
srcs/load/Socket.cpp

@ -1,7 +1,7 @@
#include "webserv.hpp"
Socket::Socket(Server *server, string def): _server(server) {
Socket::Socket(Server *server, string def) : _server(server) {
size_t sep_pos = def.rfind(':');
size_t ip6_endpos = def.rfind(']');
@ -70,7 +70,7 @@ void Socket::refresh() {
std::vector<int>::iterator it;
int valread;
int addrlen = sizeof(_address);
char buffer[1024];
char buffer[10000];
if (FD_ISSET(_master_socket, &_readfds)) {
int new_socket = accept(_master_socket, (struct sockaddr *)&_address,
(socklen_t *)&addrlen);
@ -88,7 +88,7 @@ void Socket::refresh() {
}
for (it = _clients.begin(); it < _clients.end(); it++) {
if (FD_ISSET(*it, &_readfds)) {
valread = read(*it, buffer, 1024);
valread = read(*it, buffer, 10000);
if (valread == 0) {
getpeername(*it, (struct sockaddr *)&_address,
(socklen_t *)&addrlen);
@ -96,10 +96,12 @@ void Socket::refresh() {
<< ", port " << ntohs(_address.sin_port) << "\n";
close(*it);
_clients.erase(it);
} else
} else {
buffer[valread]='\0';
this->answer(*it, buffer);
}
}
}
}
void Socket::answer(int fd, string request) {
@ -108,7 +110,7 @@ void Socket::answer(int fd, string request) {
(void)route;
char r404[72] =
"HTTP/1.1 200 OK\nContent-Type: text/plain\nContent-Length: 4\n\n404!";
cout << request << "\n";
cout << request << "\n|===|===|===|\n";
#ifdef __linux__
send(fd, r404, strlen(r404), MSG_NOSIGNAL);
#elif __APPLE__

3
srcs/tools.cpp

@ -13,6 +13,7 @@ void *ft_memset(void *b, int c, size_t len) {
bool isInt(string str) {
for (string::iterator it = str.begin(); it < str.end(); it++)
if (*it < '0' || *it > '9') return false;
if (*it < '0' || *it > '9')
return false;
return true;
}

5
srcs/webserv.cpp

@ -1,5 +1,4 @@
#include "webserv.hpp"
#include <time.h>
fd_set Socket::_readfds;
int Socket::_max_fd;
@ -18,10 +17,12 @@ int main(int ac, char **av) {
cout << "Setting environment...\n";
Env env(conf);
while (1) {
cout << "|===|===|===| CYCLE |===|===|===|\n";
FD_ZERO(&Socket::_readfds);
Socket::_max_fd = Socket::_min_fd;
env.set_fds();
int activity = select(Socket::_max_fd + Socket::_amount, &(Socket::_readfds), NULL, NULL, NULL);
int activity = select(Socket::_max_fd + Socket::_amount,
&(Socket::_readfds), NULL, NULL, NULL);
if ((activity < 0) && (errno != EINTR))
cout << "Select: " << strerror(errno) << "\n";
env.refresh();

Loading…
Cancel
Save