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. 4
      includes/Socket.hpp
  5. 1
      srcs/json/Nodes.cpp
  6. 1
      srcs/json/Parser.cpp
  7. 14
      srcs/json/Token.cpp
  8. 6
      srcs/load/Env.cpp
  9. 4
      srcs/load/Route.cpp
  10. 13
      srcs/load/Server.cpp
  11. 10
      srcs/load/Socket.cpp
  12. 3
      srcs/tools.cpp
  13. 5
      srcs/webserv.cpp

1
includes/Env.hpp

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

1
includes/Parser.hpp

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

2
includes/Route.hpp

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

4
includes/Socket.hpp

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

1
srcs/json/Nodes.cpp

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

1
srcs/json/Parser.cpp

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

14
srcs/json/Token.cpp

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

6
srcs/load/Env.cpp

@ -1,6 +1,7 @@
#include "webserv.hpp" #include "webserv.hpp"
Env::Env(JSONNode *conf) { Env::Env(JSONNode *conf) {
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",
@ -13,7 +14,10 @@ Env::Env(JSONNode *conf) {
cout << th[i] << " server launched.\n"; cout << th[i] << " server launched.\n";
i++; i++;
} }
// delete conf; } catch (std::exception &e) {
cout << e.what();
}
delete conf;
} }
void Env::set_fds() { void Env::set_fds() {
for (std::vector<Server *>::iterator it = _servers.begin(); for (std::vector<Server *>::iterator it = _servers.begin();

4
srcs/load/Route.cpp

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

13
srcs/load/Server.cpp

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

10
srcs/load/Socket.cpp

@ -70,7 +70,7 @@ void Socket::refresh() {
std::vector<int>::iterator it; std::vector<int>::iterator it;
int valread; int valread;
int addrlen = sizeof(_address); int addrlen = sizeof(_address);
char buffer[1024]; 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 *)&_address,
(socklen_t *)&addrlen); (socklen_t *)&addrlen);
@ -88,7 +88,7 @@ void Socket::refresh() {
} }
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, 1024); valread = read(*it, buffer, 10000);
if (valread == 0) { if (valread == 0) {
getpeername(*it, (struct sockaddr *)&_address, getpeername(*it, (struct sockaddr *)&_address,
(socklen_t *)&addrlen); (socklen_t *)&addrlen);
@ -96,11 +96,13 @@ void Socket::refresh() {
<< ", port " << ntohs(_address.sin_port) << "\n"; << ", port " << ntohs(_address.sin_port) << "\n";
close(*it); close(*it);
_clients.erase(it); _clients.erase(it);
} else } else {
buffer[valread]='\0';
this->answer(*it, buffer); this->answer(*it, buffer);
} }
} }
} }
}
void Socket::answer(int fd, string request) { void Socket::answer(int fd, string request) {
string uri = "path/to/page/"; string uri = "path/to/page/";
@ -108,7 +110,7 @@ void Socket::answer(int fd, string request) {
(void)route; (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|===|===|===|\n";
#ifdef __linux__ #ifdef __linux__
send(fd, r404, strlen(r404), MSG_NOSIGNAL); send(fd, r404, strlen(r404), MSG_NOSIGNAL);
#elif __APPLE__ #elif __APPLE__

3
srcs/tools.cpp

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

5
srcs/webserv.cpp

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

Loading…
Cancel
Save