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

@@ -1,13 +1,13 @@
NAME = webserv
CXX = c++
CXX = clang++
CXXFLAGS = -Wall -Wextra -Werror
CXXFLAGS += $(HEADERS_D:%=-I%)
CXXFLAGS += -std=c++98
CXXFLAGS += -g
#CXXFLAGS += -fno-limit-debug-info
CXXFLAGS += -MMD -MP #header dependencie
CXXFLAGS += -MMD -MP # header dependencie
# for debug
VPATH = $(SRCS_D)
@@ -43,14 +43,16 @@ DEPS = $(OBJS:.o=.d) #header dependencie
all: $(NAME)
$(OBJS_D)/%.o: %.cpp | $(OBJS_D)
$(CXX) $(CXXFLAGS) -c $< -o $@
$(CXX) -c $< -o $@ $(CXXFLAGS)
printf "$(_CYAN)\r\33[2K\rCompling $@$(_END)"
$(OBJS_D):
mkdir $@
$(NAME): $(OBJS)
$(CXX) $^ -o $(NAME)
# $(CXX) $^ -o $(NAME)
# add -lpthread for semaphore library
$(CXX) $^ -o $(NAME) -lpthread
echo "$(_GREEN)\r\33[2K\r$(NAME) created 😎$(_END)"
# CGI

View File

@@ -14,8 +14,6 @@
# include "ServerConfig.hpp"
# include "colors.h"
extern family_state family;
struct Script
{
std::string path;

View File

@@ -5,10 +5,16 @@
#include "Webserv.hpp"
#include "ConfigParser.hpp"
family_state family;
// debug
family_state g_family;
sem_t* g_shmem;
// debug end
int main(int ac, char **av)
{
// debug
init_semaphore();
std::vector<ServerConfig>* servers_config = NULL;
try
{

View File

@@ -310,6 +310,49 @@ void print_special(std::string str)
}
}
// https://stackoverflow.com/questions/5656530/how-to-use-shared-memory-with-linux-in-c
void* create_shared_memory(size_t size) {
// Our memory buffer will be readable and writable:
int protection = PROT_READ | PROT_WRITE;
// The buffer will be shared (meaning other processes can access it), but
// anonymous (meaning third-party processes cannot obtain an address for it),
// so only this process and its children will be able to use it:
int visibility = MAP_SHARED | MAP_ANONYMOUS;
// The remaining parameters to `mmap()` are not important for this use case,
// but the manpage for `mmap` explains their purpose.
return mmap(NULL, size, protection, visibility, -1, 0);
}
void init_semaphore()
{
int ret;
g_shmem = static_cast<sem_t*>(create_shared_memory(4));
ret = sem_init(g_shmem, 1, 1);
if (ret != 0)
{
perror("err sem_init()");
exit(EXIT_FAILURE);
}
}
void print_secure(std::string message)
{
sem_wait(g_shmem);
std::cerr << g_family << "| " << message;
sem_post(g_shmem);
}
void print_secure(int fd, std::string message)
{
sem_wait(g_shmem);
std::cerr << g_family << "| (" << std::setw(2) << fd << ") " << message;
sem_post(g_shmem);
}
// OVERLOADS
bool operator==(const listen_socket& lhs, int fd)

View File

@@ -18,6 +18,10 @@
# include <unistd.h> // close, access
# include "colors.h" // for debug print_special
// debug
# include <sys/mman.h> // mmap (for shared memory)
# include <semaphore.h> // sem_init, sem_post, sem_wait
# define CR "\r"
# define LF "\n"
# define CRLF CR LF
@@ -63,6 +67,9 @@ struct listen_socket
bool operator==(const listen_socket& lhs, int fd);
bool operator==(int fd, const listen_socket& rhs);
extern family_state g_family;
extern sem_t* g_shmem;
std::vector<std::string> split(std::string input, char delimiter);
std::vector<std::string> split_trim(std::string input, std::string delim = "\n", char ctrim = '\0');
bool is_numeric(std::string str);
@@ -82,7 +89,10 @@ void str_map_key_tolower(std::map<std::string, std::string> & mp);
// debug
void throw_test();
void print_special(std::string str);
void* create_shared_memory(size_t size);
void init_semaphore();
void print_secure(int fd, std::string message);
void print_secure(std::string message);
/* Template */

View File

@@ -35,7 +35,6 @@
# include "autoindex.hpp"
# include "colors.h"
extern family_state family;
extern bool g_run;
extern int g_last_signal;
void signal_handler(int signum);

View File

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

View File

@@ -12,11 +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";
print_secure(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";
print_secure(client->get_cl_fd(), "....>_is_cgi()\n");
if (pos == NPOS)
break;
client->fill_script_path(path, pos);
@@ -44,7 +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";
print_secure(client->get_cl_fd(), "....._cgi_pos()\n");
v_ext = client->assigned_location->cgi_ext;
if (v_ext.empty())
return NPOS;
@@ -75,16 +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";
print_secure(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";
print_secure(client->get_cl_fd(), "open pipe rfp [" + ::itos(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";
print_secure(client->get_cl_fd(), "open pipe wtc [" + ::itos(client->cgi_pipe_w_to_child) + "]\n");
if (::pipe(pipe_fd) == -1)
{
@@ -96,9 +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";
print_secure(client->get_cl_fd(), "open pipe rfc [" + ::itos(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";
print_secure(client->get_cl_fd(), "open pipe wtp [" + ::itos(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);
@@ -112,7 +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";
print_secure(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)
{
@@ -130,7 +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";
print_secure(client->get_cl_fd(), "....._exec_cgi()\n");
_set_env_vector(client, env_vector);
try {
_set_env_cstr(env_cstr, env_vector);
@@ -171,7 +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";
print_secure(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")));
@@ -215,10 +215,10 @@ void Webserv::_exec_script(Client *client, char *env[])
char * const nll[1] = {NULL};
std::string path;
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") ....._exec_script()\n";
print_secure(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";
print_secure(client->get_cl_fd(), "fork()\n");
if (pid == -1)
{
std::perror("err fork()");
@@ -229,7 +229,7 @@ void Webserv::_exec_script(Client *client, char *env[])
{
std::signal(SIGPIPE, SIG_DFL);
std::signal(SIGINT, SIG_DFL);
family = CHILD;
g_family = CHILD;
if (dup2(client->cgi_pipe_r_from_parent, STDIN_FILENO) == -1)
{
@@ -247,7 +247,7 @@ void Webserv::_exec_script(Client *client, char *env[])
_close_all_clients_fd();
path = client->get_rq_script_path();
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") execve:[" << path << "]\n"; // DEBUG
print_secure(client->get_cl_fd(), "execve:[" + path + "]\n");
if (::execve(path.c_str(), nll, env) == -1) // replace path for debug error forcing
{
std::perror("err execve()");
@@ -260,19 +260,19 @@ void Webserv::_exec_script(Client *client, char *env[])
}
else //parent
{
usleep(1 * 1000);
//usleep(1 * 10000);
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") close pipe rfp [" << client->cgi_pipe_r_from_parent << "]\n";
print_secure(client->get_cl_fd(), "close pipe rfp [" + ::itos(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";
print_secure(client->get_cl_fd(), "close pipe wtp [" + ::itos(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";
print_secure(client->get_cl_fd(), "close pipe wtc [" + ::itos(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;
@@ -294,7 +294,7 @@ 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";
print_secure(client->get_cl_fd(), "....._check_script_output()\n");
if (pos == 0 || pos == NPOS)
{
client->status = 500;;
@@ -314,7 +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";
print_secure(client->get_cl_fd(), "....._check_script_status()\n");
pos = output.find("Status:");
if (pos != NPOS)
{
@@ -363,7 +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";
print_secure(client->get_cl_fd(), "....._check_fields_duplicates()\n");
// put server headers in map
tmp = client->response;
pos = tmp.find(CRLF CRLF);

View File

@@ -35,7 +35,7 @@ void Webserv::_handle_epollerr_cgi_output(uint32_t events, Client *client)
{
_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";
print_secure(client->get_cl_fd(), "close pipe rfc [" + ::itos(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;
@@ -56,7 +56,7 @@ void Webserv::_handle_epollhup_cgi_output(uint32_t events, Client *client)
{
_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";
print_secure(client->get_cl_fd(), "close pipe rfc [" + ::itos(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;

View File

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

View File

@@ -7,7 +7,7 @@
*/
void Webserv::_get(Client *client, std::string &path)
{
std::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") _get()\n"; // debug
print_secure(client->get_cl_fd(), "_get()\n");
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 << family << "| (" << std::setw(2) << client->get_cl_fd() << ") Added an index\n"; //debug
print_secure(client->get_cl_fd(), "Added an index\n");
return ;
}
}
@@ -41,7 +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::cerr << family << "| (" << std::setw(2) << client->get_cl_fd() << ") _get_file()\n";
print_secure(client->get_cl_fd(), "_get_file()\n");
client->status = ::eval_file_access(path, R_OK);
if (client->status)
return;

View File

@@ -36,7 +36,7 @@ int Webserv::_read_request(Client *client)
ssize_t ret;
ret = ::recv(client->get_cl_fd(), buf, BUFSIZE, 0);
std::cout << family << "| (" << std::setw(2) << client->get_cl_fd() << ") recv() ret = " << ret << std::endl;
print_secure(client->get_cl_fd(), "recv() ret = " + ::itos(ret) + "\n");
if (ret == -1)
{
std::perror("err recv()");

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);

View File

@@ -19,12 +19,16 @@ void Webserv::run()
std::signal(SIGPIPE, signal_handler);
std::signal(SIGINT, signal_handler);
g_run = true;
family = PARENT;
g_family = PARENT;
while (g_run)
{
std::cout << family << "|" << ++count_loop << "-----loop epoll() ";
sem_wait(g_shmem); // protect output here
std::cout << g_family << "|" << ++count_loop << "-----loop epoll() ";
nfds = ::epoll_wait(_epfd, events, MAX_EVENTS, TIMEOUT);
std::cerr << "(nfds=" << nfds << ")" << std::endl;
sem_post(g_shmem); // protect output here
if (nfds == -1)
{
int errno_copy = errno;