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

@@ -21,35 +21,39 @@ void Webserv::run()
if (nfds == -1)
{
std::perror("err epoll_wait()");
throw std::runtime_error("Epoll wait");
}
else if (nfds == 0)
{
if (!_clients.empty())
{
std::cerr << "Timeout " << TIMEOUT << "ms\n";
_close_all_clients();
}
if (errno == EINTR)
g_run = false;
else
throw std::runtime_error("Epoll wait");
}
else if (nfds == 0 && !_clients.empty())
_timeout();
i = 0;
while (i < nfds)
{
try
{
// TODO : handle EPOLLERR and EPOLLHUP
try {
it_socket = std::find(_listen_sockets.begin(), _listen_sockets.end(), events[i].data.fd);
if (it_socket != _listen_sockets.end() && events[i].events & EPOLLIN)
_accept_connection(*it_socket);
else if (events[i].events & EPOLLIN)
_request( &(*std::find(_clients.begin(), _clients.end(), events[i].data.fd)) );
else if (events[i].events & EPOLLOUT)
_response( &(*std::find(_clients.begin(), _clients.end(), events[i].data.fd)) );
if (it_socket != _listen_sockets.end())
{
if (events[i].events & EPOLLERR || events[i].events & EPOLLHUP)
_handle_epoll_error_lsocket(events[i].events, it_socket);
else if (events[i].events & EPOLLIN)
_accept_connection(*it_socket);
}
else
{
if (events[i].events & EPOLLERR || events[i].events & EPOLLHUP)
_handle_epoll_error_client(events[i].events, events[i].data.fd);
else if (events[i].events & EPOLLIN)
_request( &(*std::find(_clients.begin(), _clients.end(), events[i].data.fd)) );
else if (events[i].events & EPOLLOUT)
_response( &(*std::find(_clients.begin(), _clients.end(), events[i].data.fd)) );
}
++i;
if (!g_run)
break;
}
catch (const std::bad_alloc& e)
{
catch (const std::bad_alloc& e) {
std::cerr << e.what() << '\n';
_close_all_clients();
/* Swap to free the memory
@@ -57,8 +61,7 @@ void Webserv::run()
std::vector<Client>().swap(_clients);
break;
}
catch (const std::exception& e)
{
catch (const std::exception& e) {
std::cerr << e.what() << '\n';
++i;
}