lots of log message and fixed bad_file_descriptor issue

This commit is contained in:
hugogogo
2022-08-22 01:50:28 +02:00
parent 469eca8aa9
commit 5225e3258b
18 changed files with 186 additions and 112 deletions

View File

@@ -17,28 +17,28 @@ server {
root ./www/; 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; # error_page 403 ./www/error_pages/error_404.html;
location /kapouet { # location /kapouet {
root /tmp/www; # root /tmp/www;
} # }
location / { location / {
allow_methods GET; allow_methods GET;
root ./www/; root ./www/;
} }
location /srcs/cgi-bin/ { # location /srcs/cgi-bin/ {
root ./srcs/cgi-bin/; # root ./srcs/cgi-bin/;
allow_methods POST; # allow_methods POST;
cgi_ext php; # cgi_ext php;
} # }
#
location /list { # location /list {
autoindex on; # autoindex on;
} # }
location /cgi-bin { location /cgi-bin {
@@ -46,64 +46,64 @@ server {
cgi_ext out php sh; cgi_ext out php sh;
} }
location /upload { # location /upload {
allow_methods POST; # allow_methods POST;
upload_dir ./www/user_files/; # upload_dir ./www/user_files/;
# root doesnt matter if used only with POST and no CGI # # root doesnt matter if used only with POST and no CGI
} # }
#
location /the_dump { # location /the_dump {
allow_methods GET DELETE; # allow_methods GET DELETE;
root ./www/user_files; # root ./www/user_files;
autoindex on; # autoindex on;
} # }
#
location /redirect { # location /redirect {
redirect 307 https://fr.wikipedia.org/wiki/Ketchup; # redirect 307 https://fr.wikipedia.org/wiki/Ketchup;
# redirect 307 https://www.youtube.com/watch?v=rG6b8gjMEkw; ## 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 /test {
# index index1.html subdex.html;
server { # root ./www/test/;
server_name server2; # }
#
listen 0.0.0.0:8080; # location /stylesheet {
# root ./stylesheet/;
index index.html; # }
root ./www2/; #
# 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/;
} }

View File

@@ -282,10 +282,10 @@ void Client::clear_cgi_vars()
void Client::print_client(std::string message) void Client::print_client(std::string message)
{ {
std::map<std::string, std::string>::iterator it; std::map<std::string, std::string>::iterator it;
std::cout << "\n=== DEBUG PRINT CLIENT ===\n"; std::cerr << "\n=== DEBUG PRINT CLIENT ===\n";
std::cout << message << ":\n----------\n\n" << "raw_request:\n__\n"; std::cerr << message << ":\n----------\n\n" << "raw_request:\n__\n";
::print_special(raw_request); ::print_special(raw_request);
std::cout << "\n__\n" std::cerr << "\n__\n"
<< "get_cl_fd() : [" << get_cl_fd() << "]\n" << "get_cl_fd() : [" << get_cl_fd() << "]\n"
<< "get_cl_port() : [" << get_cl_port() << "]\n" << "get_cl_port() : [" << get_cl_port() << "]\n"
<< "get_cl_ip() : [" << get_cl_ip() << "]\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" << "get_rq_script_info() : [" << get_rq_script_info() << "]\n"
<< "headers :\n"; << "headers :\n";
for (it = _request.headers.begin(); it != _request.headers.end(); it++) for (it = _request.headers.begin(); it != _request.headers.end(); it++)
std::cout << " " << it->first << ": [" << it->second << "]\n"; std::cerr << " " << it->first << ": [" << it->second << "]\n";
std::cout << "\n=== END PRINT CLIENT ===\n\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<LocationConfig>::const_iterator it = server.locations.begin(); it != server.locations.end(); it++) for (std::vector<LocationConfig>::const_iterator it = server.locations.begin(); it != server.locations.end(); it++)
{ {
// std::cout << it->path << " -- ";
if (it->path.size() > uri.size()) if (it->path.size() > uri.size())
continue ; continue ;

View File

@@ -3,6 +3,7 @@
# define CLIENT_HPP # define CLIENT_HPP
# include <iostream> # include <iostream>
# include <iomanip> // setw
# include <string> # include <string>
# include <map> # include <map>
# include <vector> # include <vector>
@@ -13,6 +14,8 @@
# include "ServerConfig.hpp" # include "ServerConfig.hpp"
# include "colors.h" # include "colors.h"
extern family_state family;
struct Script struct Script
{ {
std::string path; std::string path;

View File

@@ -5,6 +5,8 @@
#include "Webserv.hpp" #include "Webserv.hpp"
#include "ConfigParser.hpp" #include "ConfigParser.hpp"
family_state family;
int main(int ac, char **av) int main(int ac, char **av)
{ {
std::vector<ServerConfig>* servers_config = NULL; std::vector<ServerConfig>* servers_config = NULL;

View File

@@ -301,11 +301,11 @@ void print_special(std::string str)
{ {
c = str[i]; c = str[i];
if (c == '\r') if (c == '\r')
std::cout << YELLOW << "\\r" << RESET; std::cerr << YELLOW << "\\r" << RESET;
else if (c == '\n') else if (c == '\n')
std::cout << YELLOW << "\\n" << RESET << "\n"; std::cerr << YELLOW << "\\n" << RESET << "\n";
else else
std::cout << c; std::cerr << c;
std::fflush(stdout); std::fflush(stdout);
} }
} }

View File

@@ -7,6 +7,7 @@
# include <string> # include <string>
# include <sstream> # include <sstream>
# include <iostream> # include <iostream>
# include <iomanip> // setw
# include <cstdlib> // strtol, strtoul # include <cstdlib> // strtol, strtoul
# include <climits> // LONG_MAX # include <climits> // LONG_MAX
# include <cerrno> // errno # include <cerrno> // errno
@@ -31,6 +32,12 @@
** CRLF_SIZE*2 ** CRLF_SIZE*2
*/ */
enum family_state
{
PARENT,
CHILD
};
enum file_type enum file_type
{ {
IS_OTHER, IS_OTHER,

View File

@@ -15,6 +15,7 @@
# include <cerrno> // errno # include <cerrno> // errno
# include <unistd.h> // close, access # include <unistd.h> // close, access
# include <iostream> // cout, cin # include <iostream> // cout, cin
# include <iomanip> // setw
# include <cstring> // memset # include <cstring> // memset
# include <sys/socket.h> // socket, accept, listen, send, recv, bind, connect, setsockopt, getsockname # include <sys/socket.h> // socket, accept, listen, send, recv, bind, connect, setsockopt, getsockname
# include <arpa/inet.h> // htonl, htons, ntohl, ntohs, inet_addr, inet_ntoa # include <arpa/inet.h> // htonl, htons, ntohl, ntohs, inet_addr, inet_ntoa
@@ -34,6 +35,7 @@
# include "autoindex.hpp" # include "autoindex.hpp"
# include "colors.h" # include "colors.h"
extern family_state family;
extern bool g_run; extern bool g_run;
extern int g_last_signal; extern int g_last_signal;
void signal_handler(int signum); void signal_handler(int signum);

View File

@@ -8,7 +8,7 @@ void Webserv::_accept_connection(listen_socket &lsocket)
int accepted_fd; int accepted_fd;
std::map<std::string, std::string> infos; std::map<std::string, std::string> infos;
std::cerr << "accept()\n"; std::cerr << family << "| " << " accept() socket (" << lsocket.fd << ")\n";
addr_len = sizeof addr; addr_len = sizeof addr;
accepted_fd = ::accept(lsocket.fd, (sockaddr*)&addr, &addr_len); accepted_fd = ::accept(lsocket.fd, (sockaddr*)&addr, &addr_len);
if (accepted_fd == -1) if (accepted_fd == -1)

View File

@@ -12,9 +12,11 @@ bool Webserv::_is_cgi(Client *client, std::string path)
size_t file_access = client->status; size_t file_access = client->status;
size_t pos = 0; size_t pos = 0;
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._is_cgi()\n";
while (pos != NPOS) while (pos != NPOS)
{ {
pos = _cgi_pos(client, path, pos); pos = _cgi_pos(client, path, pos);
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....>_is_cgi()\n";
if (pos == NPOS) if (pos == NPOS)
break; break;
client->fill_script_path(path, pos); 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; size_t len;
std::locale loc; // for isalpha() 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; v_ext = client->assigned_location->cgi_ext;
if (v_ext.empty()) if (v_ext.empty())
return NPOS; return NPOS;
@@ -72,13 +75,16 @@ void Webserv::_cgi_open_pipes(Client *client)
#define W 1 #define W 1
int pipe_fd[2]; int pipe_fd[2];
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._cgi_open_pipes()\n";
if (::pipe(pipe_fd) == -1) if (::pipe(pipe_fd) == -1)
{ {
std::perror("err pipe"); std::perror("err pipe");
client->status = 500; client->status = 500;
} }
client->cgi_pipe_r_from_parent = pipe_fd[R]; 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]; 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) if (::pipe(pipe_fd) == -1)
{ {
@@ -90,7 +96,9 @@ void Webserv::_cgi_open_pipes(Client *client)
client->status = 500; client->status = 500;
} }
client->cgi_pipe_r_from_child = pipe_fd[R]; 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]; 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 add for writing body to child
_epoll_update(client->cgi_pipe_w_to_child, EPOLLOUT, EPOLL_CTL_ADD); _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; ssize_t ret;
std::string body = client->get_rq_body(); 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()); ret = ::write(client->cgi_pipe_w_to_child, body.c_str(), body.size());
if (ret == -1) if (ret == -1)
{ {
@@ -121,6 +130,7 @@ void Webserv::_exec_cgi(Client *client)
env_vector.reserve(18); env_vector.reserve(18);
int i = 0; int i = 0;
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._exec_cgi()\n";
_set_env_vector(client, env_vector); _set_env_vector(client, env_vector);
try { try {
_set_env_cstr(env_cstr, env_vector); _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<std::string> &env_vector) void Webserv::_set_env_vector(Client *client, std::vector<std::string> &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("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_LENGTH" , client->get_rq_body().size()));
env_vector.push_back(_dup_env("CONTENT_TYPE" , client->get_rq_headers("Content-Type"))); 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<std::string> &env_vect
std::vector<std::string>::const_iterator it = env_vector.begin(); std::vector<std::string>::const_iterator it = env_vector.begin();
std::vector<std::string>::const_iterator it_end = env_vector.end(); std::vector<std::string>::const_iterator it_end = env_vector.end();
int i = 0; int i = 0;
while (it != it_end) while (it != it_end)
{ {
env_cstr[i] = new char[it->size()+1]; 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}; char * const nll[1] = {NULL};
std::string path; 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 = 1; // DEBUG, if no fork, no problem
pid = fork(); pid = fork();
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") fork()\n";
if (pid == -1) if (pid == -1)
{ {
std::perror("err fork()"); std::perror("err fork()");
@@ -217,6 +229,7 @@ void Webserv::_exec_script(Client *client, char *env[])
{ {
std::signal(SIGPIPE, SIG_DFL); std::signal(SIGPIPE, SIG_DFL);
std::signal(SIGINT, SIG_DFL); std::signal(SIGINT, SIG_DFL);
family = CHILD;
if (dup2(client->cgi_pipe_r_from_parent, STDIN_FILENO) == -1) 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(); _close_all_clients_fd();
path = client->get_rq_script_path(); 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 if (::execve(path.c_str(), nll, env) == -1) // replace path for debug error forcing
{ {
std::perror("err execve()"); std::perror("err execve()");
@@ -247,12 +260,23 @@ void Webserv::_exec_script(Client *client, char *env[])
} }
else //parent 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) if (::close(client->cgi_pipe_r_from_parent) == -1)
std::perror("err close"); 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) if (::close(client->cgi_pipe_w_to_parent) == -1)
std::perror("err close"); 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) if (::close(client->cgi_pipe_w_to_child) == -1)
std::perror("err close"); std::perror("err close");
client->cgi_pipe_w_to_child = -1;
// add client->cgi_pipe_r_from_child to epoll, // add client->cgi_pipe_r_from_child to epoll,
_epoll_update(client->cgi_pipe_r_from_child, EPOLLIN, EPOLL_CTL_ADD); _epoll_update(client->cgi_pipe_r_from_child, EPOLLIN, EPOLL_CTL_ADD);
// stop monitoring client->fd until the cgi-script as done is job // 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; size_t pos;
pos = client->cgi_output.find(CRLF CRLF); 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) if (pos == 0 || pos == NPOS)
{ {
client->status = 500;; client->status = 500;;
@@ -288,6 +314,7 @@ void Webserv::_check_script_status(Client *client, std::string & output)
size_t pos; size_t pos;
int status_pos; int status_pos;
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._check_script_status()\n";
pos = output.find("Status:"); pos = output.find("Status:");
if (pos != NPOS) if (pos != NPOS)
{ {
@@ -305,7 +332,6 @@ unsigned int Webserv::_check_script_fields(const std::string & output, unsigned
std::string body; std::string body;
size_t pos; size_t pos;
std::cerr << "_check_script_fields()\n";
pos = output.find(CRLF CRLF); pos = output.find(CRLF CRLF);
if (pos == NPOS) // there is not empty line if (pos == NPOS) // there is not empty line
return 500; return 500;
@@ -337,6 +363,7 @@ void Webserv::_check_fields_duplicates(Client *client, std::string & output)
std::string tmp; std::string tmp;
size_t pos; size_t pos;
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._check_fields_duplicates()\n";
// put server headers in map // put server headers in map
tmp = client->response; tmp = client->response;
pos = tmp.find(CRLF CRLF); pos = tmp.find(CRLF CRLF);

View File

@@ -34,9 +34,12 @@ void Webserv::_handle_epollerr_cgi_output(uint32_t events, Client *client)
if (wait_ret == client->cgi_pid) if (wait_ret == client->cgi_pid)
{ {
_epoll_update(client->cgi_pipe_r_from_child, 0, EPOLL_CTL_DEL); _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) if (::close(client->cgi_pipe_r_from_child) == -1)
std::perror("err close()"); std::perror("err close()");
client->cgi_pipe_r_from_child = -1; client->cgi_pipe_r_from_child = -1;
_epoll_update(client->get_cl_fd(), EPOLLOUT, EPOLL_CTL_ADD); _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) if (wait_ret == client->cgi_pid)
{ {
_epoll_update(client->cgi_pipe_r_from_child, 0, EPOLL_CTL_DEL); _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) if (::close(client->cgi_pipe_r_from_child) == -1)
std::perror("err close()"); std::perror("err close()");
client->cgi_pipe_r_from_child = -1; client->cgi_pipe_r_from_child = -1;
_epoll_update(client->get_cl_fd(), EPOLLOUT, EPOLL_CTL_ADD); _epoll_update(client->get_cl_fd(), EPOLLOUT, EPOLL_CTL_ADD);
client->cgi_state = CGI_OUTPUT_COMPLETE; client->cgi_state = CGI_OUTPUT_COMPLETE;
} }

View File

@@ -9,7 +9,7 @@ void Webserv::_close_client(int fd)
{ {
if (*it == 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) if (::close(fd) == -1)
std::perror("err close()"); std::perror("err close()");
_close_client_cgi_pipes(&(*it)); _close_client_cgi_pipes(&(*it));
@@ -24,21 +24,28 @@ void Webserv::_close_client_cgi_pipes(Client *client)
{ {
if (client->cgi_state) if (client->cgi_state)
{ // No need to reset the fd to -1 normaly { // No need to reset the fd to -1 normaly
std::cerr << "close cgi-pipes" << "\n"; 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) if (client->cgi_pipe_w_to_child != -1)
std::perror("err close()"); 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::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") close pipe rfc [" << client->cgi_pipe_r_from_child << "]\n";
std::perror("err close()"); 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; client->cgi_pipe_r_from_child = -1;
if (::close(client->cgi_pipe_w_to_parent) == -1) std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") close pipe wtp [" << client->cgi_pipe_w_to_parent << "]\n";
std::perror("err close()"); 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; client->cgi_pipe_w_to_parent = -1;
if (::close(client->cgi_pipe_r_from_parent) == -1) std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") close pipe rfp [" << client->cgi_pipe_r_from_parent << "]\n";
std::perror("err close()"); 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; client->cgi_pipe_r_from_parent = -1;
} }
} }
@@ -56,7 +63,7 @@ void Webserv::_close_all_clients_fd()
std::vector<Client>::iterator it_end = _clients.end(); std::vector<Client>::iterator it_end = _clients.end();
while (it != it_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) if (::close(it->get_cl_fd()) == -1)
std::perror("err close()"); std::perror("err close()");
++it; ++it;

View File

@@ -11,7 +11,7 @@ void Webserv::_delete(Client *client, const std::string &path)
void Webserv::_delete_file(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); client->status = ::eval_file_access(path, W_OK);
if (client->status) if (client->status)
return; return;

View File

@@ -7,7 +7,7 @@
*/ */
void Webserv::_get(Client *client, std::string &path) 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 (eval_file_type(path) == IS_DIR)
{ {
if (path[path.size() - 1] != '/') if (path[path.size() - 1] != '/')
@@ -18,7 +18,7 @@ void Webserv::_get(Client *client, std::string &path)
{ {
path.append(client->assigned_location->index[i]); path.append(client->assigned_location->index[i]);
_get_file(client, path); _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 ; 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::ifstream ifd; // For chunk, ifstream directly in struct CLient for multiples read without close() ?
std::stringstream buf; 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); client->status = ::eval_file_access(path, R_OK);
if (client->status) if (client->status)
return; return;
@@ -90,7 +89,7 @@ if (size > MAX_FILESIZE)
*/ */
void Webserv::_autoindex(Client *client, const std::string &path) void Webserv::_autoindex(Client *client, const std::string &path)
{ {
std::cout << "_autoindex()\n"; std::cerr << "_autoindex()\n";
std::string dir_list; std::string dir_list;
DIR *dir; DIR *dir;

View File

@@ -7,7 +7,7 @@
void Webserv::_post(Client *client, const std::string &path) void Webserv::_post(Client *client, const std::string &path)
{ {
(void)path; // unused, we use "assigned_location->upload_dir" instead (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) if (client->get_rq_abs_path() != client->assigned_location->path)
client->status = 404; // 404 or other status better ? 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" #define DEFAULT_NAME "unnamed_file"
void Webserv::_upload_files(Client *client) void Webserv::_upload_files(Client *client)
{ {
std::cout << "_upload_files()\n"; std::cerr << "_upload_files()\n";
std::ofstream ofd; std::ofstream ofd;
std::vector<MultipartBody>::const_iterator body_it = client->get_rq_multi_bodys().begin(); std::vector<MultipartBody>::const_iterator body_it = client->get_rq_multi_bodys().begin();
std::string path; std::string path;

View File

@@ -36,7 +36,7 @@ int Webserv::_read_request(Client *client)
ssize_t ret; ssize_t ret;
ret = ::recv(client->get_cl_fd(), buf, BUFSIZE, 0); 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) if (ret == -1)
{ {
std::perror("err recv()"); std::perror("err recv()");

View File

@@ -12,6 +12,7 @@ void Webserv::_response(Client *client)
{ {
int ret = _send_response(client); int ret = _send_response(client);
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._response()\n";
if (g_last_signal) if (g_last_signal)
_handle_last_signal(); _handle_last_signal();
@@ -38,10 +39,12 @@ int Webserv::_send_response(Client *client)
{ {
ssize_t ret; ssize_t ret;
std::cerr << "send()\n"; std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._send_response()\n";
if (client->response.empty()) if (client->response.empty())
{
_append_base_headers(client); _append_base_headers(client);
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....>_send_response()\n";
}
if (!client->status) if (!client->status)
{ {
@@ -49,12 +52,18 @@ int Webserv::_send_response(Client *client)
if (client->cgi_state == CGI_WAIT_TO_EXEC if (client->cgi_state == CGI_WAIT_TO_EXEC
|| client->cgi_state == CGI_OUTPUT_READING) || client->cgi_state == CGI_OUTPUT_READING)
return SEND_IN_PROGRESS; return SEND_IN_PROGRESS;
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....>_send_response()\n";
} }
_insert_status_line(client); _insert_status_line(client);
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....>_send_response()\n";
if (client->status >= 400) if (client->status >= 400)
{
_error_html_response(client); _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); ret = ::send(client->get_cl_fd(), client->response.c_str(), client->response.size(), 0);
if (ret == -1) if (ret == -1)
{ {
@@ -74,6 +83,7 @@ void Webserv::_append_base_headers(Client *client)
{ {
client->response.append("Server: Webserv/0.1" CRLF); 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" if (client->get_rq_headers("Connection") == "close"
|| client->status == 400 || client->status == 400
|| client->status == 408 || client->status == 408
@@ -88,10 +98,13 @@ void Webserv::_construct_response(Client *client)
std::string path; std::string path;
std::string script_output; 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()); 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) if (client->cgi_state == CGI_READY_TO_EXEC)
{ {
_write_body_to_cgi(client); _write_body_to_cgi(client);
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....>_construct_response()\n";
if (client->status) if (client->status)
return; return;
_exec_cgi(client); _exec_cgi(client);
@@ -99,6 +112,7 @@ void Webserv::_construct_response(Client *client)
else if (client->cgi_state == CGI_OUTPUT_COMPLETE) else if (client->cgi_state == CGI_OUTPUT_COMPLETE)
{ {
_check_script_output(client, client->cgi_output); _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) if (client->status < 400 || client->status >= 600)
client->response += client->cgi_output; client->response += client->cgi_output;
} }
@@ -110,6 +124,7 @@ void Webserv::_construct_response(Client *client)
void Webserv::_process_method(Client *client, std::string &path) 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()) switch (client->get_rq_method())
{ {
case (GET): 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::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 == "/") if (client->assigned_location->path == "/")
path.insert(0, client->assigned_location->root); path.insert(0, client->assigned_location->root);
else else
@@ -139,6 +155,8 @@ void Webserv::_insert_status_line(Client *client)
{ {
std::string status_line; std::string status_line;
std::string status = _http_status[client->status]; std::string status = _http_status[client->status];
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._insert_status_line()\n";
if (status.empty()) if (status.empty())
status = ::itos(client->status); status = ::itos(client->status);
@@ -150,8 +168,9 @@ void Webserv::_insert_status_line(Client *client)
void Webserv::_error_html_response(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 if (client->assigned_server
&& !client->assigned_server->error_pages[client->status].empty() && !client->assigned_server->error_pages[client->status].empty()
&& ::eval_file_access(client->assigned_server->error_pages[client->status], R_OK) == 0 ) && ::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); status = "Error " + ::itos(client->status);
std::string html_page = HTML_ERROR; std::string html_page = HTML_ERROR;
::replace_all_substr(html_page, STATUS_PLACEHOLDER, status); ::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"); _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]; 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: "); client->response.append("Content-Type: ");
if (mime_type.empty()) if (mime_type.empty())
client->response.append(MIME_TYPE_DEFAULT); client->response.append(MIME_TYPE_DEFAULT);

View File

@@ -19,11 +19,12 @@ void Webserv::run()
std::signal(SIGPIPE, signal_handler); std::signal(SIGPIPE, signal_handler);
std::signal(SIGINT, signal_handler); std::signal(SIGINT, signal_handler);
g_run = true; g_run = true;
family = PARENT;
while (g_run) while (g_run)
{ {
std::cerr << ++count_loop << "----loop epoll() "; std::cout << family << "|" << ++count_loop << "-----loop epoll() ";
nfds = ::epoll_wait(_epfd, events, MAX_EVENTS, TIMEOUT); nfds = ::epoll_wait(_epfd, events, MAX_EVENTS, TIMEOUT);
std::cerr << "(nfds=" << nfds << ")\n"; std::cerr << "(nfds=" << nfds << ")" << std::endl;
if (nfds == -1) if (nfds == -1)
{ {
int errno_copy = errno; int errno_copy = errno;

View File

@@ -1 +1 @@
http://localhost:4040/cgi-bin/cgi_cpp_empty.out?fname=John&lname=Doe http://localhost:4040/cgi-bin/cgi_cpp.out?fname=John&lname=Doe