diff --git a/memo.txt b/memo.txt index 99f9ffe..1604417 100644 --- a/memo.txt +++ b/memo.txt @@ -13,6 +13,8 @@ - merge changes from hugo5 to master (attention a pas casse svp :clown:) +-- changer le 4040 port par default cgi + ----Priorité modérée------------------------ - namespace utils ? - change "std::string" to reference "std::string &" in most functions diff --git a/srcs/Client.cpp b/srcs/Client.cpp index eec2e52..e34e5fc 100644 --- a/srcs/Client.cpp +++ b/srcs/Client.cpp @@ -433,9 +433,9 @@ void Client::_parse_port_hostname(std::string host) void Client::_check_request_errors() { - /* Debug */ std::cerr << "Content-Length=" << get_rq_headers("Content-Length") << "\n"; - /* Debug */ std::cerr << "strtoul=" << std::strtoul(get_rq_headers("Content-Length").c_str(), NULL, 10) << "\n"; - /* Debug */ std::cerr << "client_body_limit=" << assigned_server->client_body_limit << "\n"; + // /* Debug */ std::cerr << "Content-Length=" << get_rq_headers("Content-Length") << "\n"; + // /* Debug */ std::cerr << "strtoul=" << std::strtoul(get_rq_headers("Content-Length").c_str(), NULL, 10) << "\n"; + // /* Debug */ std::cerr << "client_body_limit=" << assigned_server->client_body_limit << "\n"; /////////////////////// // Request line checks if (_request.method == UNKNOWN) diff --git a/srcs/webserv/Webserv.hpp b/srcs/webserv/Webserv.hpp index ba1514d..2c6ef2e 100644 --- a/srcs/webserv/Webserv.hpp +++ b/srcs/webserv/Webserv.hpp @@ -124,6 +124,7 @@ class Webserv bool _is_cgi(Client *client, std::string path); size_t _cgi_pos(Client *client, std::string &path, size_t pos); void _cgi_open_pipes(Client *client); + void _write_body_to_cgi(Client *client); void _exec_cgi(Client *client); void _set_env_vector(Client *client, std::vector &env_vector); void _set_env_cstr(char *env_cstr[], std::vector &env_vector); diff --git a/srcs/webserv/base.cpp b/srcs/webserv/base.cpp index ee59bbd..cce34ac 100644 --- a/srcs/webserv/base.cpp +++ b/srcs/webserv/base.cpp @@ -5,7 +5,7 @@ Webserv::Webserv() { std::cerr << "Server init\n"; - _epfd = ::epoll_create1(0); // (EPOLL_CLOEXEC) for CGI fork ? + _epfd = ::epoll_create1(EPOLL_CLOEXEC); if (_epfd == -1) { std::perror("err epoll_create1()"); diff --git a/srcs/webserv/cgi.cpp b/srcs/webserv/cgi.cpp index 8ad5e90..4adb7d1 100644 --- a/srcs/webserv/cgi.cpp +++ b/srcs/webserv/cgi.cpp @@ -9,7 +9,7 @@ bool Webserv::_is_cgi(Client *client, std::string path) { std::string script_path; size_t file_type; - size_t file_mode = client->status; + size_t file_access = client->status; size_t pos = 0; while (pos != NPOS) @@ -24,13 +24,13 @@ bool Webserv::_is_cgi(Client *client, std::string path) continue; if (file_type == IS_FILE) { - file_mode = ::eval_file_access( script_path, X_OK ); - if (!file_mode) + file_access = ::eval_file_access( script_path, X_OK ); + if (!file_access) return true; } } client->clear_script(); - client->status = file_mode; // 404 not_found OR 403 forbidden + client->status = file_access; // 404 not_found OR 403 forbidden return false; } @@ -99,6 +99,22 @@ void Webserv::_cgi_open_pipes(Client *client) client->cgi_state = CGI_WAIT_TO_EXEC; } +void Webserv::_write_body_to_cgi(Client *client) +{ + std::cerr << "_write_body_to_cgi()"; // DEBUG + ssize_t ret; + std::string body = client->get_rq_body(); + + ret = ::write(client->cgi_pipe_w_to_child, body.c_str(), body.size()); + if (ret == -1) + { + std::perror("err write()"); + _close_client_cgi_pipes(client); + client->status = 500; + client->cgi_state = CGI_NO_CGI; + } +} + void Webserv::_exec_cgi(Client *client) { char* env_cstr[19] = {NULL}; @@ -187,9 +203,11 @@ void Webserv::_exec_script(Client *client, char *env[]) { pid_t pid; char * const nll[1] = {NULL}; - std::string script_output; std::string path; + std::cerr << "_exec_script() client " << client->get_cl_fd() << "\n"; + + // pid = 1; // DEBUG, if no fork, no problem pid = fork(); if (pid == -1) { @@ -216,8 +234,6 @@ void Webserv::_exec_script(Client *client, char *env[]) } _close_all_clients_fd(); - if (::close(_epfd) == -1) - std::perror("err close"); path = client->get_rq_script_path(); std::cerr << "execve:[" << path << "]\n"; // DEBUG diff --git a/srcs/webserv/close.cpp b/srcs/webserv/close.cpp index 49533df..5871980 100644 --- a/srcs/webserv/close.cpp +++ b/srcs/webserv/close.cpp @@ -10,7 +10,7 @@ void Webserv::_close_client(int fd) if (*it == fd) { // _epoll_update(fd, 0, EPOLL_CTL_DEL); // normalement superflu, DEBUG - std::cerr << "close fd " << fd << "\n"; + std::cerr << "close client fd " << fd << "\n"; if (::close(fd) == -1) std::perror("err close()"); _close_client_cgi_pipes(&(*it)); @@ -29,19 +29,19 @@ void Webserv::_close_client_cgi_pipes(Client *client) std::cerr << "close cgi-pipes" << "\n"; if (::close(client->cgi_pipe_w_to_child) == -1) std::perror("err close()"); - // client->cgi_pipe_w_to_child = -1; + client->cgi_pipe_w_to_child = -1; if (::close(client->cgi_pipe_r_from_child) == -1) std::perror("err close()"); - // client->cgi_pipe_r_from_child = -1; + client->cgi_pipe_r_from_child = -1; if (::close(client->cgi_pipe_w_to_parent) == -1) std::perror("err close()"); - // client->cgi_pipe_w_to_parent = -1; + client->cgi_pipe_w_to_parent = -1; if (::close(client->cgi_pipe_r_from_parent) == -1) std::perror("err close()"); - // client->cgi_pipe_r_from_parent = -1; + client->cgi_pipe_r_from_parent = -1; } } @@ -67,7 +67,7 @@ void Webserv::_close_all_clients_fd() while (it != it_end) { // _epoll_update(_clients.back().fd, 0, EPOLL_CTL_DEL); // normalement superflu, DEBUG - std::cerr << "close fd " << it->get_cl_fd() << "\n"; + std::cerr << "close client fd " << it->get_cl_fd() << "\n"; if (::close(it->get_cl_fd()) == -1) std::perror("err close()"); ++it; @@ -92,7 +92,7 @@ void Webserv::_close_all_listen_sockets() while (!_listen_sockets.empty()) { // _epoll_update(_listen_sockets.back().fd, 0, EPOLL_CTL_DEL); // normalement superflu, DEBUG - std::cerr << "close fd " << _listen_sockets.back().fd << "\n"; + std::cerr << "close lsocket fd " << _listen_sockets.back().fd << "\n"; if (::close(_listen_sockets.back().fd) == -1) std::perror("err close()"); _listen_sockets.pop_back(); diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index e0c1908..d000481 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -73,11 +73,6 @@ int Webserv::_send_response(Client *client) std::cerr << "SEND RET 0 for client.fd =" << client->get_cl_fd() << "\n"; // DEBUG return SEND_CLOSE; } - if (ret < (int)client->response.size()) - { - std::cerr << "send() as not send all data, IT CAN HAPPEN !" << "\n"; // DEBUG - throw ExecFail(); // DEBUG, TODO, A ENLEVER ABSOLEMENT, WHILE TEMP =D - } // /* Debug */ std::cerr << "ret send() = " << ret << "\n"; // DEBUG return SEND_COMPLETE; @@ -85,6 +80,8 @@ int Webserv::_send_response(Client *client) void Webserv::_append_base_headers(Client *client) { + std::cerr << "_append_base_headers()\n"; // debug + client->response.append("Server: Webserv/0.1" CRLF); if (client->get_rq_headers("Connection") == "close" @@ -104,17 +101,9 @@ void Webserv::_construct_response(Client *client) path = _replace_url_root(client, client->get_rq_abs_path()); if (client->cgi_state == CGI_READY_TO_EXEC) { - std::string body = client->get_rq_body(); - std::cerr << "client.fd for write body =" << client->get_cl_fd() << "\n"; // DEBUG - std::cerr << "client.cgi_pipe_w_to_child for write body =" << client->cgi_pipe_w_to_child << "\n"; // DEBUG - if (::write(client->cgi_pipe_w_to_child, body.c_str(), body.size()) == -1) - { - std::perror("err write()"); - _close_client_cgi_pipes(client); - client->status = 500; - client->cgi_state = CGI_NO_CGI; + _write_body_to_cgi(client); + if (client->status) return; - } _exec_cgi(client); } else if (client->cgi_state == CGI_OUTPUT_COMPLETE) diff --git a/temp_memo_debug_fork.txt b/temp_memo_debug_fork.txt new file mode 100644 index 0000000..7120433 --- /dev/null +++ b/temp_memo_debug_fork.txt @@ -0,0 +1,47 @@ + +/* +//// RECREATE EPOLL // + std::cerr << "epfd from parent = " << _epfd << "\n"; + if (::close(_epfd) == -1) + std::perror("err close"); + _epfd = ::epoll_create1(EPOLL_CLOEXEC); // (EPOLL_CLOEXEC) for CGI fork ? + if (_epfd == -1) + { + std::perror("err epoll_create1()"); + } + + std::vector::iterator it_ls = _listen_sockets.begin(); + std::vector::iterator it_ls_end = _listen_sockets.end(); + while (it_ls != it_ls_end) + { + _epoll_update(it_ls->fd, EPOLLIN, EPOLL_CTL_ADD); + ++it_ls; + } + + std::vector::iterator it = _clients.begin(); + std::vector::iterator it_end = _clients.end(); + while (it != it_end) + { + if (it->cgi_state == CGI_WAIT_TO_EXEC) + _epoll_update(it->cgi_pipe_w_to_child, EPOLLOUT, EPOLL_CTL_ADD); + else if (it->cgi_state == CGI_OUTPUT_READING) + _epoll_update(it->cgi_pipe_r_from_child, EPOLLIN, EPOLL_CTL_ADD); + else if (client->request_complete) + _epoll_update(it->get_cl_fd(), EPOLLOUT, EPOLL_CTL_ADD); + else + _epoll_update(it->get_cl_fd(), EPOLLIN, EPOLL_CTL_ADD); + ++it; + } +//// */ + + + +//// DEBUG, MINIMAL ACTIONS // + // ::sleep(1); // rescue team + client->status = 442; // rescue_team + _close_client_cgi_pipes(client);// rescue_team + // _epoll_update(client->get_cl_fd(), EPOLLOUT, EPOLL_CTL_ADD); // rescue_team + client->cgi_state = CGI_NO_CGI; // rescue_team + return; // rescue_team +//// +