added semaphore and shared memory to print without superposition between parent and child process

This commit is contained in:
hugogogo
2022-08-22 14:52:31 +02:00
parent 5225e3258b
commit f7b31c7db7
14 changed files with 127 additions and 65 deletions

View File

@@ -12,7 +12,7 @@ void Webserv::_response(Client *client)
{
int ret = _send_response(client);
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._response()\n";
print_secure(client->get_cl_fd(), "....._response()\n");
if (g_last_signal)
_handle_last_signal();
@@ -39,11 +39,11 @@ int Webserv::_send_response(Client *client)
{
ssize_t ret;
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._send_response()\n";
print_secure(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";
print_secure(client->get_cl_fd(), "....>_send_response()\n");
}
if (!client->status)
@@ -52,18 +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";
print_secure(client->get_cl_fd(), "....>_send_response()\n");
}
_insert_status_line(client);
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....>_send_response()\n";
print_secure(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";
print_secure(client->get_cl_fd(), "....>_send_response()\n");
}
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") send()\n"; // Debug
print_secure(client->get_cl_fd(), "send()\n");
ret = ::send(client->get_cl_fd(), client->response.c_str(), client->response.size(), 0);
if (ret == -1)
{
@@ -83,7 +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";
print_secure(client->get_cl_fd(), "....._append_base_headers()\n");
if (client->get_rq_headers("Connection") == "close"
|| client->status == 400
|| client->status == 408
@@ -98,13 +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";
print_secure(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";
print_secure(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";
print_secure(client->get_cl_fd(), "....>_construct_response()\n");
if (client->status)
return;
_exec_cgi(client);
@@ -112,7 +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";
print_secure(client->get_cl_fd(), "....>_construct_response()\n");
if (client->status < 400 || client->status >= 600)
client->response += client->cgi_output;
}
@@ -124,7 +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";
print_secure(client->get_cl_fd(), "....._process_method()\n");
switch (client->get_rq_method())
{
case (GET):
@@ -140,7 +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";
print_secure(client->get_cl_fd(), "....._replace_url_root()\n");
if (client->assigned_location->path == "/")
path.insert(0, client->assigned_location->root);
else
@@ -156,7 +156,7 @@ 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";
print_secure(client->get_cl_fd(), "....._insert_status_line()\n");
if (status.empty())
status = ::itos(client->status);
@@ -169,7 +169,7 @@ void Webserv::_insert_status_line(Client *client)
void Webserv::_error_html_response(Client *client)
{
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._error_html_response()\n";
print_secure(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()
@@ -184,7 +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";
print_secure(client->get_cl_fd(), "....>_error_html_response()\n");
_append_body(client, html_page, "html");
}
}
@@ -193,7 +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";
print_secure(client->get_cl_fd(), "....._append_body()\n");
client->response.append("Content-Type: ");
if (mime_type.empty())
client->response.append(MIME_TYPE_DEFAULT);