From 935d2775fa250515ffb1449220d191b7cb3d1027 Mon Sep 17 00:00:00 2001 From: nicolas-arnaud Date: Tue, 24 Jan 2023 14:18:35 +0100 Subject: [PATCH] fix choose_route --- Makefile | 2 ++ default.json | 2 +- includes/webserv.hpp | 4 ++++ srcs/load/Env.cpp | 6 +++--- srcs/load/Server.cpp | 7 +++++-- srcs/sock/Client.cpp | 11 +++++++---- srcs/sock/Master.cpp | 28 +++++++++++++++++----------- 7 files changed, 39 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 1719f68..e25bbc0 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,8 @@ $(NAME): $(OBJS) debug: $(CXX) -I includes -Werror -Wextra -Wall -std=c++98 -g $(SRCS) -o $(NAME) -D DEBUG=1 +verbose: + $(CXX) -I includes -Werror -Wextra -Wall -std=c++98 -g $(SRCS) -o $(NAME) -D SILENT=0 clean: rm -rf $(OBJS) diff --git a/default.json b/default.json index d3435ad..461fcae 100644 --- a/default.json +++ b/default.json @@ -25,7 +25,7 @@ "root": "public/html/", "indexs": ["basique.html"], "cgi": { - ".php": "/usr/bin/php", + ".php": "/usr/bin/php-cgi", ".py": "/usr/bin/python" }, "client_max_body_size": 10000, diff --git a/includes/webserv.hpp b/includes/webserv.hpp index 59cba68..5781fc2 100644 --- a/includes/webserv.hpp +++ b/includes/webserv.hpp @@ -1,6 +1,10 @@ #pragma once #ifndef DEBUG #define DEBUG 0 + #define SILENT 0 +#endif +#ifndef SILENT + #define SILENT 1 #endif #ifndef MAX_CLIENTS #define MAX_CLIENTS 5000 diff --git a/srcs/load/Env.cpp b/srcs/load/Env.cpp index 6b6b76c..0a733f7 100644 --- a/srcs/load/Env.cpp +++ b/srcs/load/Env.cpp @@ -27,7 +27,7 @@ Env::Env(JSONNode *conf) { this->_masters.insert(this->_masters.end(), tmp_s.begin(), tmp_s.end()); } } - Master::_first_cli_id = Master::_poll_id_amount - 1; + Master::_first_cli_id = Master::_poll_id_amount; if ((node = conf->obj()["allowed_methods"])) { JSONList lst = node->lst(); for (JSONList::iterator it = lst.begin(); it < lst.end(); it++) { @@ -55,7 +55,7 @@ Env::~Env() { * - refresh and handle requests */ void Env::cycle(void) { - cout << "|===||===| Waiting some HTTP request... |===||===|\n"; + if (!SILENT) cout << "|===||===| Waiting some HTTP request... |===||===|\n"; int pollResult = poll(Master::_pollfds, Master::_poll_id_amount + 1, 5000); if ((pollResult < 0) && (errno != EINTR)) std::cerr << "Select: " << strerror(errno) << "\n"; if (pollResult > 0) post_poll(); @@ -66,7 +66,7 @@ void Env::cycle(void) { * connection, etc..) and parse requests recieved. */ void Env::post_poll() { - cout << "==> Handle requests and answers:\n"; + if (!SILENT) cout << "==> Handle requests and answers:\n"; for (std::vector::iterator it = this->_masters.begin(); it < this->_masters.end(); it++) try { (*it)->post_poll(this); } catch (std::exception &e) { std::cerr << e.what(); } diff --git a/srcs/load/Server.cpp b/srcs/load/Server.cpp index af11447..ee506c7 100644 --- a/srcs/load/Server.cpp +++ b/srcs/load/Server.cpp @@ -86,11 +86,14 @@ Route *Server::choose_route(string uri) { for (std::map::iterator loc_it = _routes.begin(); loc_it != _routes.end(); loc_it++) { loc_words = split((*loc_it).first, "/"); vec_string::iterator loc_word = loc_words.begin(); - for (vec_string::iterator uri_word = uri_words.begin(); uri_word < uri_words.end(); uri_word++) { + vec_string::iterator uri_word = uri_words.begin(); + while (uri_word != uri_words.end()) { while (uri_word != uri_words.end() && *uri_word == "") uri_word++; - if (*uri_word != *(loc_word++)) break; while (loc_word != loc_words.end() && *loc_word == "") loc_word++; if (loc_word == loc_words.end()) return ((*loc_it).second); + if (*uri_word != *loc_word) break; + uri_word++; + loc_word++; } } return this; diff --git a/srcs/sock/Client.cpp b/srcs/sock/Client.cpp index df532a5..04e6e63 100644 --- a/srcs/sock/Client.cpp +++ b/srcs/sock/Client.cpp @@ -15,13 +15,15 @@ inline string get_extension(string str) { Client::Client(int fd, ip_port_t ip_port, Master *parent) : _fd(fd), _ip_port(ip_port), _parent(parent) { init(); - cout << "New connection, socket fd is " << fd << ", ip is : " << _ip_port.ip << ", port : " << _ip_port.port << "\n"; + if (!SILENT) + cout << "New connection, socket fd is " << fd << ", ip is : " << _ip_port.ip << ", port : " << _ip_port.port + << "\n"; } Client::~Client(void) { close(_fd); _headers.clear(); - cout << "Host disconnected, ip " << _ip_port.ip << ", port " << _ip_port.port << "\n"; + if (!SILENT) cout << "Host disconnected, ip " << _ip_port.ip << ", port " << _ip_port.port << "\n"; } void Client::init(void) { @@ -125,7 +127,7 @@ void Client::handleRequest(void) { } string ret; string req_path = _route->getRoot() + _uri; - std::cout << "||-> Request for " << req_path << " received <-||\n"; + if (!SILENT) std::cout << "||-> Request for " << req_path << " received <-||\n"; string cgi_path = _route->_cgi.size() ? _route->_cgi[get_extension(req_path)] : _server->_cgi.size() ? _server->_cgi[get_extension(req_path)] : ""; @@ -164,7 +166,7 @@ void Client::cgi(string cgi_path, string path) { send(_fd, "HTTP/1.1 200 OK\r\n", 17, MSG_NOSIGNAL); if (!std::ifstream(cgi_path.c_str()).good()) return send_error(404); - if (DEBUG) std::cout << "Send cgi\n"; + if (DEBUG) std::cout << "Send cgi\n"; if (fork() == 0) { const char **args = new const char *[cgi_path.length() + 1]; args[0] = cgi_path.c_str(); @@ -228,4 +230,5 @@ void Client::send_answer(string msg) { write(_fd, msg.c_str(), msg.length()); #endif init(); + _finish = true; } diff --git a/srcs/sock/Master.cpp b/srcs/sock/Master.cpp index 0c520d4..cd38ee4 100644 --- a/srcs/sock/Master.cpp +++ b/srcs/sock/Master.cpp @@ -13,7 +13,7 @@ */ Master::~Master(void) { close(_fd); - cout << "Destroyed master socket\n"; + if (DEBUG) cout << "Destroyed master socket\n"; } /** @@ -40,7 +40,7 @@ Master::Master(ip_port_t list) : _listen(list) { #ifdef __APPLE__ fcntl(socket, F_SETFL, O_NONBLOCK); #endif - cout << "New master socket with fd " << _fd << " which listen " << ip << ":" << port << "\n"; + if(!SILENT) cout << "New master socket with fd " << _fd << " which listen " << ip << ":" << port << "\n"; _pollfds[_poll_id_amount].fd = _fd; _pollfds[_poll_id_amount].events = POLLIN | POLLPRI; _poll_id = _poll_id_amount; @@ -67,14 +67,19 @@ void Master::post_poll(Env *env) { #endif ip_port_t cli_listen = get_ip_port_t(inet_ntoa(_address.sin_addr), ntohs(_address.sin_port)); Client *new_cli = new Client(new_socket, cli_listen, this); - _childs.push_back(new_cli); - for (int i = _first_cli_id; i < MAX_CLIENTS; i++) { - if (_pollfds[i].fd != 0) continue; - _pollfds[i].fd = new_socket; - _pollfds[i].events = POLLIN | POLLPRI; - new_cli->_poll_id = i; - _poll_id_amount++; - break; + if (_poll_id_amount > MAX_CLIENTS) { + new_cli->send_error(503); + delete new_cli; + } else { + _childs.push_back(new_cli); + for (int i = _first_cli_id; i < MAX_CLIENTS; i++) { + if (_pollfds[i].fd != 0) continue; + _pollfds[i].fd = new_socket; + _pollfds[i].events = POLLIN | POLLPRI; + new_cli->_poll_id = i; + _poll_id_amount++; + break; + } } } int child_fd; @@ -96,6 +101,7 @@ void Master::post_poll(Env *env) { _poll_id_amount--; } else if ((*it)->getRequest(env, buffer)) { (*it)->handleRequest(); + _pollfds[i].events = POLLOUT; if ((*it)->_finish) { delete (*it); _childs.erase(it); @@ -104,7 +110,7 @@ void Master::post_poll(Env *env) { _pollfds[i].revents = 0; _poll_id_amount--; } - } + } else _pollfds[i].events = POLLIN | POLLPRI; } } }