added timeout response (status 408)

+ added EPOLLERR and EPOLLHUP handling
+ fix root substitution for default "/" location (temp or permanent ?)
+ tested siege a little, seems good
This commit is contained in:
LuckyLaszlo
2022-08-11 07:12:13 +02:00
parent 08f6929db9
commit ab0bc2c4c0
13 changed files with 139 additions and 38 deletions

View File

@@ -42,3 +42,71 @@ void Webserv::_close_all_listen_sockets()
_listen_sockets.pop_back();
}
}
void Webserv::_reopen_lsocket(std::vector<listen_socket>::iterator it)
{
/*
** Many common code with init_virtual_servers(). Could refactor it.
*/
int ret;
std::cerr << "close lsocket " << it->fd << "\n";
if (::close(it->fd) == -1)
std::perror("err close()");
std::cerr << "try to reopen lsocket\n";
ret = ::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0); // (SOCK_CLOEXEC) for CGI fork ?
if (ret == -1)
{
std::perror("err socket()");
_listen_sockets.erase(it);
return;
}
it->fd = ret;
// HUGO ADD
// allow socket descriptor to be reuseable
// I just copied it from https://www.ibm.com/docs/en/i/7.2?topic=designs-example-nonblocking-io-select
int on = 1;
if (setsockopt(it->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
{
::perror("err setsockopt()");
_listen_sockets.erase(it);
return;
}
// HUGO ADD END
try {
_bind(it->fd, std::atoi(it->port.c_str()), it->host);
_listen(it->fd, 42); // 42 arbitrary
}
catch (const std::exception& e) {
std::cerr << e.what() << '\n';
_listen_sockets.erase(it);
return;
}
if (_epoll_update(it->fd, EPOLLIN, EPOLL_CTL_ADD) == -1)
{
_listen_sockets.erase(it);
return;
}
std::cerr << "reopen success\n";
}
void Webserv::_handle_epoll_error_lsocket(uint32_t events, std::vector<listen_socket>::iterator it)
{
if (events & EPOLLERR)
std::cerr << "EPOLLERR on lsocket fd " << it->fd << "\n"; // DEBUG
if (events & EPOLLHUP)
std::cerr << "EPOLLHUP on lsocket fd " << it->fd << "\n"; // DEBUG
_reopen_lsocket(it);
}
void Webserv::_handle_epoll_error_client(uint32_t events, int fd)
{
if (events & EPOLLERR)
std::cerr << "EPOLLERR on client fd " << fd << "\n"; // DEBUG
if (events & EPOLLHUP)
std::cerr << "EPOLLHUP on client fd " << fd << "\n"; // DEBUG
_close_client(fd);
}