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

@@ -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<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("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<std::string> &env_vect
std::vector<std::string>::const_iterator it = env_vector.begin();
std::vector<std::string>::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);