diff --git a/default.config b/default.config index 356638f..65bb657 100644 --- a/default.config +++ b/default.config @@ -17,28 +17,28 @@ server { root ./www/; - error_page 404 ./www/error_pages/error_404.html; +# error_page 404 ./www/error_pages/error_404.html; # error_page 403 ./www/error_pages/error_404.html; - location /kapouet { - root /tmp/www; - } +# location /kapouet { +# root /tmp/www; +# } location / { allow_methods GET; root ./www/; } - location /srcs/cgi-bin/ { - root ./srcs/cgi-bin/; - allow_methods POST; - cgi_ext php; - } - - location /list { - autoindex on; - } +# location /srcs/cgi-bin/ { +# root ./srcs/cgi-bin/; +# allow_methods POST; +# cgi_ext php; +# } +# +# location /list { +# autoindex on; +# } location /cgi-bin { @@ -46,64 +46,64 @@ server { cgi_ext out php sh; } - location /upload { - allow_methods POST; - upload_dir ./www/user_files/; - # root doesn’t matter if used only with POST and no CGI - } - - location /the_dump { - allow_methods GET DELETE; - root ./www/user_files; - autoindex on; - } - - location /redirect { - redirect 307 https://fr.wikipedia.org/wiki/Ketchup; -# redirect 307 https://www.youtube.com/watch?v=rG6b8gjMEkw; - } - - location /test { - index index1.html subdex.html; - root ./www/test/; - } - - location /stylesheet { - root ./stylesheet/; - } - - location /test/index1.html { - root ./www/test/index1.html; - index index1.html subdex.html; - } - - location /hilarious_404/ { - redirect 301 https://berniesanders.com/404/; - } - - location /test/something.html { - # allow_methods DELETE; - root ./www/test/something.html; - } - - location /test/test_deeper/ { -# allow_methods - root ./www/test/test_deeper/; - index index1.html; - } - - location /test/test_deeper/super_deep { - root ./www/test/test_deeper/super_deep/; - index something.html; - } +# location /upload { +# allow_methods POST; +# upload_dir ./www/user_files/; +# # root doesn’t matter if used only with POST and no CGI +# } +# +# location /the_dump { +# allow_methods GET DELETE; +# root ./www/user_files; +# autoindex on; +# } +# +# location /redirect { +# redirect 307 https://fr.wikipedia.org/wiki/Ketchup; +## redirect 307 https://www.youtube.com/watch?v=rG6b8gjMEkw; +# } -} - -server { - server_name server2; - - listen 0.0.0.0:8080; - - index index.html; - root ./www2/; +# location /test { +# index index1.html subdex.html; +# root ./www/test/; +# } +# +# location /stylesheet { +# root ./stylesheet/; +# } +# +# location /test/index1.html { +# root ./www/test/index1.html; +# index index1.html subdex.html; +# } +# +# location /hilarious_404/ { +# redirect 301 https://berniesanders.com/404/; +# } +# +# location /test/something.html { +# # allow_methods DELETE; +# root ./www/test/something.html; +# } +# +# location /test/test_deeper/ { +## allow_methods +# root ./www/test/test_deeper/; +# index index1.html; +# } +# +# location /test/test_deeper/super_deep { +# root ./www/test/test_deeper/super_deep/; +# index something.html; +# } +# +#} +# +#server { +# server_name server2; +# +# listen 0.0.0.0:8080; +# +# index index.html; +# root ./www2/; } diff --git a/srcs/Client.cpp b/srcs/Client.cpp index 972bbb6..ddcddc0 100644 --- a/srcs/Client.cpp +++ b/srcs/Client.cpp @@ -282,10 +282,10 @@ void Client::clear_cgi_vars() void Client::print_client(std::string message) { std::map::iterator it; -std::cout << "\n=== DEBUG PRINT CLIENT ===\n"; - std::cout << message << ":\n----------\n\n" << "raw_request:\n__\n"; +std::cerr << "\n=== DEBUG PRINT CLIENT ===\n"; + std::cerr << message << ":\n----------\n\n" << "raw_request:\n__\n"; ::print_special(raw_request); - std::cout << "\n__\n" + std::cerr << "\n__\n" << "get_cl_fd() : [" << get_cl_fd() << "]\n" << "get_cl_port() : [" << get_cl_port() << "]\n" << "get_cl_ip() : [" << get_cl_ip() << "]\n" @@ -299,8 +299,8 @@ std::cout << "\n=== DEBUG PRINT CLIENT ===\n"; << "get_rq_script_info() : [" << get_rq_script_info() << "]\n" << "headers :\n"; for (it = _request.headers.begin(); it != _request.headers.end(); it++) - std::cout << " " << it->first << ": [" << it->second << "]\n"; - std::cout << "\n=== END PRINT CLIENT ===\n\n"; + std::cerr << " " << it->first << ": [" << it->second << "]\n"; + std::cerr << "\n=== END PRINT CLIENT ===\n\n"; } /********************************************* @@ -508,7 +508,6 @@ If we get a url that ends in / ignore the last / for (std::vector::const_iterator it = server.locations.begin(); it != server.locations.end(); it++) { -// std::cout << it->path << " -- "; if (it->path.size() > uri.size()) continue ; diff --git a/srcs/Client.hpp b/srcs/Client.hpp index ef1a011..cf0ecc2 100644 --- a/srcs/Client.hpp +++ b/srcs/Client.hpp @@ -3,6 +3,7 @@ # define CLIENT_HPP # include +# include // setw # include # include # include @@ -13,6 +14,8 @@ # include "ServerConfig.hpp" # include "colors.h" +extern family_state family; + struct Script { std::string path; diff --git a/srcs/main.cpp b/srcs/main.cpp index 1b40294..0ccaeea 100644 --- a/srcs/main.cpp +++ b/srcs/main.cpp @@ -5,6 +5,8 @@ #include "Webserv.hpp" #include "ConfigParser.hpp" +family_state family; + int main(int ac, char **av) { std::vector* servers_config = NULL; diff --git a/srcs/utils.cpp b/srcs/utils.cpp index 2cb27ab..9f25792 100644 --- a/srcs/utils.cpp +++ b/srcs/utils.cpp @@ -301,11 +301,11 @@ void print_special(std::string str) { c = str[i]; if (c == '\r') - std::cout << YELLOW << "\\r" << RESET; + std::cerr << YELLOW << "\\r" << RESET; else if (c == '\n') - std::cout << YELLOW << "\\n" << RESET << "\n"; + std::cerr << YELLOW << "\\n" << RESET << "\n"; else - std::cout << c; + std::cerr << c; std::fflush(stdout); } } diff --git a/srcs/utils.hpp b/srcs/utils.hpp index 7090c2c..29e5ca9 100644 --- a/srcs/utils.hpp +++ b/srcs/utils.hpp @@ -7,6 +7,7 @@ # include # include # include +# include // setw # include // strtol, strtoul # include // LONG_MAX # include // errno @@ -31,6 +32,12 @@ ** CRLF_SIZE*2 */ +enum family_state +{ + PARENT, + CHILD +}; + enum file_type { IS_OTHER, diff --git a/srcs/webserv/Webserv.hpp b/srcs/webserv/Webserv.hpp index 625a0e1..9b352ac 100644 --- a/srcs/webserv/Webserv.hpp +++ b/srcs/webserv/Webserv.hpp @@ -15,6 +15,7 @@ # include // errno # include // close, access # include // cout, cin +# include // setw # include // memset # include // socket, accept, listen, send, recv, bind, connect, setsockopt, getsockname # include // htonl, htons, ntohl, ntohs, inet_addr, inet_ntoa @@ -34,6 +35,7 @@ # include "autoindex.hpp" # include "colors.h" +extern family_state family; extern bool g_run; extern int g_last_signal; void signal_handler(int signum); diff --git a/srcs/webserv/accept.cpp b/srcs/webserv/accept.cpp index 9fb5e4e..f62085e 100644 --- a/srcs/webserv/accept.cpp +++ b/srcs/webserv/accept.cpp @@ -8,7 +8,7 @@ void Webserv::_accept_connection(listen_socket &lsocket) int accepted_fd; std::map infos; - std::cerr << "accept()\n"; + std::cerr << family << "| " << " accept() socket (" << lsocket.fd << ")\n"; addr_len = sizeof addr; accepted_fd = ::accept(lsocket.fd, (sockaddr*)&addr, &addr_len); if (accepted_fd == -1) diff --git a/srcs/webserv/cgi.cpp b/srcs/webserv/cgi.cpp index 7c5979c..27c57a9 100644 --- a/srcs/webserv/cgi.cpp +++ b/srcs/webserv/cgi.cpp @@ -12,9 +12,11 @@ bool Webserv::_is_cgi(Client *client, std::string path) size_t file_access = client->status; size_t pos = 0; + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._is_cgi()\n"; while (pos != NPOS) { pos = _cgi_pos(client, path, pos); + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....>_is_cgi()\n"; if (pos == NPOS) break; client->fill_script_path(path, pos); @@ -42,6 +44,7 @@ size_t Webserv::_cgi_pos(Client *client, std::string &path, size_t pos) size_t len; std::locale loc; // for isalpha() + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._cgi_pos()\n"; v_ext = client->assigned_location->cgi_ext; if (v_ext.empty()) return NPOS; @@ -72,13 +75,16 @@ void Webserv::_cgi_open_pipes(Client *client) #define W 1 int pipe_fd[2]; + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._cgi_open_pipes()\n"; if (::pipe(pipe_fd) == -1) { std::perror("err pipe"); client->status = 500; } client->cgi_pipe_r_from_parent = pipe_fd[R]; + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") open pipe rfp [" << client->cgi_pipe_r_from_parent << "]\n"; client->cgi_pipe_w_to_child = pipe_fd[W]; + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") open pipe wtc [" << client->cgi_pipe_w_to_child << "]\n"; if (::pipe(pipe_fd) == -1) { @@ -90,7 +96,9 @@ void Webserv::_cgi_open_pipes(Client *client) client->status = 500; } client->cgi_pipe_r_from_child = pipe_fd[R]; + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") open pipe rfc [" << client->cgi_pipe_r_from_child << "]\n"; client->cgi_pipe_w_to_parent = pipe_fd[W]; + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") open pipe wtp [" << client->cgi_pipe_w_to_parent << "]\n"; // epoll add for writing body to child _epoll_update(client->cgi_pipe_w_to_child, EPOLLOUT, EPOLL_CTL_ADD); @@ -104,6 +112,7 @@ void Webserv::_write_body_to_cgi(Client *client) ssize_t ret; std::string body = client->get_rq_body(); + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._write_body_to_cgi()\n"; ret = ::write(client->cgi_pipe_w_to_child, body.c_str(), body.size()); if (ret == -1) { @@ -121,6 +130,7 @@ void Webserv::_exec_cgi(Client *client) env_vector.reserve(18); int i = 0; + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._exec_cgi()\n"; _set_env_vector(client, env_vector); try { _set_env_cstr(env_cstr, env_vector); @@ -161,6 +171,7 @@ std::string Webserv::_dup_env(std::string var, int i) */ void Webserv::_set_env_vector(Client *client, std::vector &env_vector) { + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._set_env_vector()\n"; env_vector.push_back(_dup_env("AUTH_TYPE")); // authentification not supported env_vector.push_back(_dup_env("CONTENT_LENGTH" , client->get_rq_body().size())); env_vector.push_back(_dup_env("CONTENT_TYPE" , client->get_rq_headers("Content-Type"))); @@ -186,6 +197,7 @@ void Webserv::_set_env_cstr(char *env_cstr[], std::vector &env_vect std::vector::const_iterator it = env_vector.begin(); std::vector::const_iterator it_end = env_vector.end(); int i = 0; + while (it != it_end) { env_cstr[i] = new char[it->size()+1]; @@ -203,10 +215,10 @@ void Webserv::_exec_script(Client *client, char *env[]) char * const nll[1] = {NULL}; std::string path; - std::cerr << "_exec_script() client " << client->get_cl_fd() << "\n"; - + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._exec_script()\n"; // pid = 1; // DEBUG, if no fork, no problem pid = fork(); + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") fork()\n"; if (pid == -1) { std::perror("err fork()"); @@ -217,6 +229,7 @@ void Webserv::_exec_script(Client *client, char *env[]) { std::signal(SIGPIPE, SIG_DFL); std::signal(SIGINT, SIG_DFL); + family = CHILD; if (dup2(client->cgi_pipe_r_from_parent, STDIN_FILENO) == -1) { @@ -234,7 +247,7 @@ void Webserv::_exec_script(Client *client, char *env[]) _close_all_clients_fd(); path = client->get_rq_script_path(); - std::cerr << "execve:[" << path << "]\n"; // DEBUG + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") execve:[" << path << "]\n"; // DEBUG if (::execve(path.c_str(), nll, env) == -1) // replace path for debug error forcing { std::perror("err execve()"); @@ -247,12 +260,23 @@ void Webserv::_exec_script(Client *client, char *env[]) } else //parent { + usleep(1 * 1000); + + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") close pipe rfp [" << client->cgi_pipe_r_from_parent << "]\n"; if (::close(client->cgi_pipe_r_from_parent) == -1) std::perror("err close"); + client->cgi_pipe_r_from_parent = -1; + + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") close pipe wtp [" << client->cgi_pipe_w_to_parent << "]\n"; if (::close(client->cgi_pipe_w_to_parent) == -1) std::perror("err close"); + client->cgi_pipe_w_to_parent = -1; + + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") close pipe wtc [" << client->cgi_pipe_w_to_child << "]\n"; if (::close(client->cgi_pipe_w_to_child) == -1) std::perror("err close"); + client->cgi_pipe_w_to_child = -1; + // add client->cgi_pipe_r_from_child to epoll, _epoll_update(client->cgi_pipe_r_from_child, EPOLLIN, EPOLL_CTL_ADD); // stop monitoring client->fd until the cgi-script as done is job @@ -269,6 +293,8 @@ void Webserv::_check_script_output(Client *client, std::string & output) { size_t pos; pos = client->cgi_output.find(CRLF CRLF); + + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._check_script_output()\n"; if (pos == 0 || pos == NPOS) { client->status = 500;; @@ -288,6 +314,7 @@ void Webserv::_check_script_status(Client *client, std::string & output) size_t pos; int status_pos; + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._check_script_status()\n"; pos = output.find("Status:"); if (pos != NPOS) { @@ -305,7 +332,6 @@ unsigned int Webserv::_check_script_fields(const std::string & output, unsigned std::string body; size_t pos; -std::cerr << "_check_script_fields()\n"; pos = output.find(CRLF CRLF); if (pos == NPOS) // there is not empty line return 500; @@ -337,6 +363,7 @@ void Webserv::_check_fields_duplicates(Client *client, std::string & output) std::string tmp; size_t pos; + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._check_fields_duplicates()\n"; // put server headers in map tmp = client->response; pos = tmp.find(CRLF CRLF); diff --git a/srcs/webserv/cgi_epoll.cpp b/srcs/webserv/cgi_epoll.cpp index 543e4bd..514bf2f 100644 --- a/srcs/webserv/cgi_epoll.cpp +++ b/srcs/webserv/cgi_epoll.cpp @@ -34,9 +34,12 @@ void Webserv::_handle_epollerr_cgi_output(uint32_t events, Client *client) if (wait_ret == client->cgi_pid) { _epoll_update(client->cgi_pipe_r_from_child, 0, EPOLL_CTL_DEL); + + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") close pipe rfc [" << client->cgi_pipe_r_from_child << "]\n"; if (::close(client->cgi_pipe_r_from_child) == -1) std::perror("err close()"); client->cgi_pipe_r_from_child = -1; + _epoll_update(client->get_cl_fd(), EPOLLOUT, EPOLL_CTL_ADD); } @@ -52,9 +55,12 @@ void Webserv::_handle_epollhup_cgi_output(uint32_t events, Client *client) if (wait_ret == client->cgi_pid) { _epoll_update(client->cgi_pipe_r_from_child, 0, EPOLL_CTL_DEL); + + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") close pipe rfc [" << client->cgi_pipe_r_from_child << "]\n"; if (::close(client->cgi_pipe_r_from_child) == -1) std::perror("err close()"); client->cgi_pipe_r_from_child = -1; + _epoll_update(client->get_cl_fd(), EPOLLOUT, EPOLL_CTL_ADD); client->cgi_state = CGI_OUTPUT_COMPLETE; } diff --git a/srcs/webserv/close.cpp b/srcs/webserv/close.cpp index a3102dc..222a419 100644 --- a/srcs/webserv/close.cpp +++ b/srcs/webserv/close.cpp @@ -9,7 +9,7 @@ void Webserv::_close_client(int fd) { if (*it == fd) { - std::cerr << "close client fd " << fd << "\n"; + std::cout << family << "| (" << std::setw(2) << it->get_cl_fd() << ") close client fd " << fd << std::endl; if (::close(fd) == -1) std::perror("err close()"); _close_client_cgi_pipes(&(*it)); @@ -24,21 +24,28 @@ void Webserv::_close_client_cgi_pipes(Client *client) { if (client->cgi_state) { // No need to reset the fd to -1 normaly - std::cerr << "close cgi-pipes" << "\n"; - if (::close(client->cgi_pipe_w_to_child) == -1) - std::perror("err close()"); + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") close pipe wtc [" << client->cgi_pipe_w_to_child << "]\n"; + if (client->cgi_pipe_w_to_child != -1) + if (::close(client->cgi_pipe_w_to_child) == -1) + std::perror("err close()"); client->cgi_pipe_w_to_child = -1; - if (::close(client->cgi_pipe_r_from_child) == -1) - std::perror("err close()"); + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") close pipe rfc [" << client->cgi_pipe_r_from_child << "]\n"; + if (client->cgi_pipe_r_from_child != -1) + if (::close(client->cgi_pipe_r_from_child) == -1) + std::perror("err close()"); client->cgi_pipe_r_from_child = -1; - if (::close(client->cgi_pipe_w_to_parent) == -1) - std::perror("err close()"); + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") close pipe wtp [" << client->cgi_pipe_w_to_parent << "]\n"; + if (client->cgi_pipe_w_to_parent != -1) + if (::close(client->cgi_pipe_w_to_parent) == -1) + std::perror("err close()"); client->cgi_pipe_w_to_parent = -1; - if (::close(client->cgi_pipe_r_from_parent) == -1) - std::perror("err close()"); + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") close pipe rfp [" << client->cgi_pipe_r_from_parent << "]\n"; + if (client->cgi_pipe_r_from_parent != -1) + if (::close(client->cgi_pipe_r_from_parent) == -1) + std::perror("err close()"); client->cgi_pipe_r_from_parent = -1; } } @@ -56,7 +63,7 @@ void Webserv::_close_all_clients_fd() std::vector::iterator it_end = _clients.end(); while (it != it_end) { - std::cerr << "close client fd " << it->get_cl_fd() << "\n"; + std::cout << family << "| (" << std::setw(2) << it->get_cl_fd() << ") close client fd " << it->get_cl_fd() << std::endl; if (::close(it->get_cl_fd()) == -1) std::perror("err close()"); ++it; diff --git a/srcs/webserv/method_delete.cpp b/srcs/webserv/method_delete.cpp index 248471e..09f3ed4 100644 --- a/srcs/webserv/method_delete.cpp +++ b/srcs/webserv/method_delete.cpp @@ -11,7 +11,7 @@ void Webserv::_delete(Client *client, const std::string &path) void Webserv::_delete_file(Client *client, const std::string &path) { - std::cout << "_delete_file()\n"; + std::cerr << "_delete_file()\n"; client->status = ::eval_file_access(path, W_OK); if (client->status) return; diff --git a/srcs/webserv/method_get.cpp b/srcs/webserv/method_get.cpp index 2543ee2..0445480 100644 --- a/srcs/webserv/method_get.cpp +++ b/srcs/webserv/method_get.cpp @@ -7,7 +7,7 @@ */ void Webserv::_get(Client *client, std::string &path) { - std::cout << "_get()\n"; + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") _get()\n"; // debug if (eval_file_type(path) == IS_DIR) { if (path[path.size() - 1] != '/') @@ -18,7 +18,7 @@ void Webserv::_get(Client *client, std::string &path) { path.append(client->assigned_location->index[i]); _get_file(client, path); - std::cerr << "Added an index\n"; //debug + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") Added an index\n"; //debug return ; } } @@ -41,8 +41,7 @@ void Webserv::_get_file(Client *client, const std::string &path) std::ifstream ifd; // For chunk, ifstream directly in struct CLient for multiples read without close() ? std::stringstream buf; - std::cout << "_get_file()\n"; - + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") _get_file()\n"; client->status = ::eval_file_access(path, R_OK); if (client->status) return; @@ -90,7 +89,7 @@ if (size > MAX_FILESIZE) */ void Webserv::_autoindex(Client *client, const std::string &path) { - std::cout << "_autoindex()\n"; + std::cerr << "_autoindex()\n"; std::string dir_list; DIR *dir; diff --git a/srcs/webserv/method_post.cpp b/srcs/webserv/method_post.cpp index 8f576ef..9179ffd 100644 --- a/srcs/webserv/method_post.cpp +++ b/srcs/webserv/method_post.cpp @@ -7,7 +7,7 @@ void Webserv::_post(Client *client, const std::string &path) { (void)path; // unused, we use "assigned_location->upload_dir" instead - std::cout << "_post()\n"; + std::cerr << "_post()\n"; if (client->get_rq_abs_path() != client->assigned_location->path) client->status = 404; // 404 or other status better ? @@ -27,7 +27,7 @@ void Webserv::_post(Client *client, const std::string &path) #define DEFAULT_NAME "unnamed_file" void Webserv::_upload_files(Client *client) { - std::cout << "_upload_files()\n"; + std::cerr << "_upload_files()\n"; std::ofstream ofd; std::vector::const_iterator body_it = client->get_rq_multi_bodys().begin(); std::string path; diff --git a/srcs/webserv/request.cpp b/srcs/webserv/request.cpp index 7c2cedd..9490455 100644 --- a/srcs/webserv/request.cpp +++ b/srcs/webserv/request.cpp @@ -36,7 +36,7 @@ int Webserv::_read_request(Client *client) ssize_t ret; ret = ::recv(client->get_cl_fd(), buf, BUFSIZE, 0); - std::cerr << "recv() on fd(" << client->get_cl_fd() << ") ret = " << ret << "\n" ; + std::cout << family << "| (" << std::setw(2) << client->get_cl_fd() << ") recv() ret = " << ret << std::endl; if (ret == -1) { std::perror("err recv()"); diff --git a/srcs/webserv/response.cpp b/srcs/webserv/response.cpp index ef1d10a..b61150e 100644 --- a/srcs/webserv/response.cpp +++ b/srcs/webserv/response.cpp @@ -12,6 +12,7 @@ void Webserv::_response(Client *client) { int ret = _send_response(client); + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._response()\n"; if (g_last_signal) _handle_last_signal(); @@ -38,10 +39,12 @@ int Webserv::_send_response(Client *client) { ssize_t ret; - std::cerr << "send()\n"; - + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._send_response()\n"; if (client->response.empty()) + { _append_base_headers(client); + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....>_send_response()\n"; + } if (!client->status) { @@ -49,12 +52,18 @@ int Webserv::_send_response(Client *client) if (client->cgi_state == CGI_WAIT_TO_EXEC || client->cgi_state == CGI_OUTPUT_READING) return SEND_IN_PROGRESS; + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....>_send_response()\n"; } _insert_status_line(client); + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....>_send_response()\n"; if (client->status >= 400) + { _error_html_response(client); + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....>_send_response()\n"; + } + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") send()\n"; // Debug ret = ::send(client->get_cl_fd(), client->response.c_str(), client->response.size(), 0); if (ret == -1) { @@ -74,6 +83,7 @@ void Webserv::_append_base_headers(Client *client) { client->response.append("Server: Webserv/0.1" CRLF); + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._append_base_headers()\n"; if (client->get_rq_headers("Connection") == "close" || client->status == 400 || client->status == 408 @@ -88,10 +98,13 @@ void Webserv::_construct_response(Client *client) std::string path; std::string script_output; + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._construct_response()\n"; path = _replace_url_root(client, client->get_rq_abs_path()); + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....>_construct_response()\n"; if (client->cgi_state == CGI_READY_TO_EXEC) { _write_body_to_cgi(client); + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....>_construct_response()\n"; if (client->status) return; _exec_cgi(client); @@ -99,6 +112,7 @@ void Webserv::_construct_response(Client *client) else if (client->cgi_state == CGI_OUTPUT_COMPLETE) { _check_script_output(client, client->cgi_output); + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....>_construct_response()\n"; if (client->status < 400 || client->status >= 600) client->response += client->cgi_output; } @@ -110,6 +124,7 @@ void Webserv::_construct_response(Client *client) void Webserv::_process_method(Client *client, std::string &path) { + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._process_method()\n"; switch (client->get_rq_method()) { case (GET): @@ -125,6 +140,7 @@ void Webserv::_process_method(Client *client, std::string &path) std::string Webserv::_replace_url_root(Client *client, std::string path) { + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._replace_url_root()\n"; if (client->assigned_location->path == "/") path.insert(0, client->assigned_location->root); else @@ -139,6 +155,8 @@ void Webserv::_insert_status_line(Client *client) { std::string status_line; std::string status = _http_status[client->status]; + + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._insert_status_line()\n"; if (status.empty()) status = ::itos(client->status); @@ -150,8 +168,9 @@ void Webserv::_insert_status_line(Client *client) void Webserv::_error_html_response(Client *client) { - std::cout << "_error_html_response()\n"; + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._error_html_response()\n"; + std::cerr << "_error_html_response()\n"; if (client->assigned_server && !client->assigned_server->error_pages[client->status].empty() && ::eval_file_access(client->assigned_server->error_pages[client->status], R_OK) == 0 ) @@ -165,6 +184,7 @@ void Webserv::_error_html_response(Client *client) status = "Error " + ::itos(client->status); std::string html_page = HTML_ERROR; ::replace_all_substr(html_page, STATUS_PLACEHOLDER, status); + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....>_error_html_response()\n"; _append_body(client, html_page, "html"); } } @@ -173,6 +193,7 @@ void Webserv::_append_body(Client *client, const std::string &body, const std::s { const std::string &mime_type = _mime_types[file_extension]; + std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._append_body()\n"; client->response.append("Content-Type: "); if (mime_type.empty()) client->response.append(MIME_TYPE_DEFAULT); diff --git a/srcs/webserv/run_loop.cpp b/srcs/webserv/run_loop.cpp index f0751d6..b0877f5 100644 --- a/srcs/webserv/run_loop.cpp +++ b/srcs/webserv/run_loop.cpp @@ -19,11 +19,12 @@ void Webserv::run() std::signal(SIGPIPE, signal_handler); std::signal(SIGINT, signal_handler); g_run = true; + family = PARENT; while (g_run) { - std::cerr << ++count_loop << "----loop epoll() "; + std::cout << family << "|" << ++count_loop << "-----loop epoll() "; nfds = ::epoll_wait(_epfd, events, MAX_EVENTS, TIMEOUT); - std::cerr << "(nfds=" << nfds << ")\n"; + std::cerr << "(nfds=" << nfds << ")" << std::endl; if (nfds == -1) { int errno_copy = errno; diff --git a/urls_cgi.txt b/urls_cgi.txt index cd2f146..ddfbc68 100644 --- a/urls_cgi.txt +++ b/urls_cgi.txt @@ -1 +1 @@ -http://localhost:4040/cgi-bin/cgi_cpp_empty.out?fname=John&lname=Doe \ No newline at end of file +http://localhost:4040/cgi-bin/cgi_cpp.out?fname=John&lname=Doe