bugfix invalid ptr in "ev.data.ptr" after vector<Client> resize/erase

+ persistent connections reintroduced
+ bugfix crash if invalid path in request
This commit is contained in:
LuckyLaszlo
2022-07-31 13:19:11 +02:00
parent af8dc072c1
commit ea3f3a390a
5 changed files with 54 additions and 22 deletions

View File

@@ -27,15 +27,14 @@ void Webserv::_send_response(Client *client)
return ;
}
_close_client(client->fd);
/* if (client->raw_request.find("Connection: keep-alive") == std::string::npos)
if (client->raw_request.find("Connection: close") != std::string::npos)
_close_client(client->fd);
else
{
_epoll_update(client->fd, EPOLLIN, EPOLL_CTL_MOD, client);
_epoll_update(client->fd, EPOLLIN, EPOLL_CTL_MOD);
client->raw_request.clear();
client->response.clear();
} */
}
}
void Webserv::_construct_response(Client *client)
@@ -43,13 +42,17 @@ void Webserv::_construct_response(Client *client)
client->status = 200;
client->response.append("Server: Webserv/0.1\r\n");
client->response.append("Connection: close\r\n");
if (client->raw_request.find("Connection: close") != std::string::npos)
client->response.append("Connection: close\r\n");
else
client->response.append("Connection: keep-alive\r\n");
_get_ressource(client);
_insert_status_line(client);
}
#define E400 "\r\n<!DOCTYPE html><html><head><title>400 Bad Request</title></head><body><h1 style=\"text-align:center\">400 Bad Request</h1><hr><p style=\"text-align:center\">Le Webserv/0.1</p></body></html>"
#define E404 "\r\n<!DOCTYPE html><html><head><title>404 Not Found</title></head><body><h1 style=\"text-align:center\">404 Not Found</h1><hr><p style=\"text-align:center\">Le Webserv/0.1</p></body></html>"
#define E500 "\r\n<!DOCTYPE html><html><head><title>500 Internal Server Error</title></head><body><h1 style=\"text-align:center\">500 Internal Server Error</h1><hr><p style=\"text-align:center\">Le Webserv/0.1</p></body></html>"
void Webserv::_insert_status_line(Client *client)
@@ -63,6 +66,10 @@ void Webserv::_insert_status_line(Client *client)
case (200):
status_line.append("200 OK");
break;
case (400):
status_line.append("400 Not Found");
client->response.append(E400);
break;
case (404):
status_line.append("404 Not Found");
client->response.append(E404);
@@ -88,12 +95,21 @@ void Webserv::_get_ressource(Client *client)
// Mini parsing à l'arrache du PATH
std::string path;
path = client->raw_request.substr(0, client->raw_request.find("\r\n"));
path = path.substr(0, path.rfind(" "));
path = path.substr(path.find("/"));
if (path == "/")
path.append(INDEX);
path.insert(0, ROOT);
try
{
path = client->raw_request.substr(0, client->raw_request.find("\r\n"));
path = path.substr(0, path.rfind(" "));
path = path.substr(path.find("/"));
if (path == "/")
path.append(INDEX);
path.insert(0, ROOT);
}
catch (std::out_of_range& e)
{
std::cout << e.what() << '\n';
client->status = 400;
return ;
}
if (access(path.data(), R_OK) == -1)
{