Debug in progress, somes fix but not the main problem

This commit is contained in:
lperrey
2022-08-18 06:03:09 +02:00
parent a008c12058
commit 3d17db996a
13 changed files with 136 additions and 117 deletions

View File

@@ -70,16 +70,27 @@ void Webserv::_cgi_open_pipes(Client *client)
{
#define R 0
#define W 1
int fd_in[2];
int fd_out[2];
::pipe(fd_in);
::pipe(fd_out);
int pipe_fd[2];
client->cgi_pipe_r_from_parent = fd_in[R];
client->cgi_pipe_w_to_child = fd_in[W];
client->cgi_pipe_r_from_child = fd_out[R];
client->cgi_pipe_w_to_parent = fd_out[W];
if (::pipe(pipe_fd) == -1)
{
std::perror("err pipe");
client->status = 500;
}
client->cgi_pipe_r_from_parent = pipe_fd[R];
client->cgi_pipe_w_to_child = pipe_fd[W];
if (::pipe(pipe_fd) == -1)
{
std::perror("err pipe");
if (::close(client->cgi_pipe_r_from_parent) == -1)
std::perror("err close()");
if (::close(client->cgi_pipe_w_to_child) == -1)
std::perror("err close()");
client->status = 500;
}
client->cgi_pipe_r_from_child = pipe_fd[R];
client->cgi_pipe_w_to_parent = pipe_fd[W];
// epoll add for writing body to child
_epoll_update(client->cgi_pipe_w_to_child, EPOLLOUT, EPOLL_CTL_ADD);
@@ -181,7 +192,11 @@ void Webserv::_exec_script(Client *client, char *env[])
pid = fork();
if (pid == -1)
{
std::perror("err fork()");
client->status = 500;
_close_client_cgi_pipes(client);
}
else if (pid == 0) // child
{
std::signal(SIGPIPE, SIG_DFL);
@@ -189,19 +204,13 @@ void Webserv::_exec_script(Client *client, char *env[])
if (dup2(client->cgi_pipe_r_from_parent, STDIN_FILENO) == -1)
{
std::perror("err dup2()");
if (::close(client->cgi_pipe_r_from_parent) == -1) // Valgind debug, not essential
std::perror("err close");
if (::close(client->cgi_pipe_w_to_parent) == -1) // Valgind debug, not essential
std::perror("err close");
std::perror("err dup2() STDIN_FILENO");
throw ExecFail();
}
if (dup2(client->cgi_pipe_w_to_parent, STDOUT_FILENO) == -1)
{
std::perror("err dup2()");
if (::close(client->cgi_pipe_r_from_parent) == -1) // Valgind debug, not essential
std::perror("err close");
if (::close(client->cgi_pipe_w_to_parent) == -1) // Valgind debug, not essential
std::perror("err dup2() STDOUT_FILENO");
if (::close(STDIN_FILENO) == -1) // Valgind debug, not essential
std::perror("err close");
throw ExecFail();
}
@@ -210,8 +219,8 @@ void Webserv::_exec_script(Client *client, char *env[])
if (::close(_epfd) == -1)
std::perror("err close");
path = client->get_rq_script_path(); // Wut ? Only relative path ?
/*DEBUG*/std::cerr << "execve:[" << path << "]\n";
path = client->get_rq_script_path();
std::cerr << "execve:[" << path << "]\n"; // DEBUG
if (::execve(path.c_str(), nll, env) == -1) // replace path for debug error forcing
{
std::perror("err execve()");
@@ -230,18 +239,17 @@ void Webserv::_exec_script(Client *client, char *env[])
std::perror("err close");
if (::close(client->cgi_pipe_w_to_child) == -1)
std::perror("err close");
// 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
_epoll_update(client->get_cl_fd(), 0, EPOLL_CTL_DEL);
client->cgi_pid = pid;
client->cgi_state = CGI_WAIT_FOR_OUTPUT;
client->cgi_state = CGI_OUTPUT_READING;
}
}
#define STATUS_500 std::string("Status: 500" CRLF CRLF);
void Webserv::_check_script_output(Client *client, std::string & output)
{
@@ -282,17 +290,16 @@ void Webserv::_check_script_status(Client *client, std::string & output)
client->status = 200;
}
size_t Webserv::_check_script_fields(const std::string & output, size_t status)
unsigned int Webserv::_check_script_fields(const std::string & output, unsigned int status)
{
std::string headers;
std::string body;
size_t pos;
std::cerr << "0\n";
std::cerr << "_check_script_fields()\n";
pos = output.find(CRLF CRLF);
if (pos == NPOS) // there is not empty line
return 500;
std::cerr << "1\n";
headers = output.substr(0, pos);
body = output.substr(pos + CRLF_SIZE * 2);
headers = str_tolower(headers);
@@ -301,18 +308,14 @@ std::cerr << "1\n";
{
if (!body.empty()) // there is body
return 500;
std::cerr << "2\n";
if (headers.find("location") == NPOS) // there is no location field
return 500;
std::cerr << "3\n";
}
else if (headers.find("location") != NPOS) // there is a location field
{
if (body.empty()) // there is no body
return 500;
std::cerr << "4\n";
}
std::cerr << "5\n";
return status;
}