_request() and _response() refactoring

+ Wip redirection (weird behavior)
+ 505 HTTP Version Not Supported
This commit is contained in:
LuckyLaszlo
2022-08-09 01:46:59 +02:00
parent a44b9b493a
commit 643b09c4f7
8 changed files with 118 additions and 60 deletions

View File

@@ -4,14 +4,31 @@
#define BUFSIZE 8192
#define MAX_HEADER_SIZE 42000 // arbitrary
enum read_return
{
READ_IN_PROGRESS,
READ_COMPLETE,
READ_CLOSE,
};
void Webserv::_request(Client *client)
{
_read_request(client);
int ret = _read_request(client);
if (g_last_signal)
_handle_last_signal();
_handle_last_signal();
if (ret == READ_CLOSE)
{
_close_client(client->fd);
}
else if (ret == READ_COMPLETE)
{
_epoll_update(client->fd, EPOLLOUT, EPOLL_CTL_MOD);
}
}
void Webserv::_read_request(Client *client) // Messy, Need refactoring
int Webserv::_read_request(Client *client) // Messy, Need refactoring
{
char buf[BUFSIZE];
ssize_t ret;
@@ -24,13 +41,12 @@ void Webserv::_read_request(Client *client) // Messy, Need refactoring
std::perror("err recv()");
std::cerr << "client ptr =" << client << "\n"; // DEBUG
std::cerr << "client.fd =" << client->fd << "\n"; // DEBUG
_close_client(client->fd);
return ;
return READ_CLOSE;
}
if (ret == 0)
{
_close_client(client->fd);
return ;
std::cerr << "recv() read 0, then close client" << "\n"; // DEBUG
return READ_CLOSE;
}
client->raw_request.append(buf, ret);
@@ -38,23 +54,29 @@ void Webserv::_read_request(Client *client) // Messy, Need refactoring
{
if (client->raw_request.find(CRLF CRLF) != std::string::npos)
{
// std::cerr << "Raw_request :\n|||||||||||||||||||||||||||||\n " << client->raw_request << "\n|||||||||||||||||||||||||||||\n"; // DEBUG
client->header_complete = true;
client->parse_request(); // TODO : split function to avoid useless parsing ?
if (client->status) // WIP, need to change client->parse_request() for status update
return READ_COMPLETE;
client->assigned_server = _determine_process_server(client);
client->assigned_location = _determine_location(*client->assigned_server, client->get_path());
if (client->get_version().compare(0, sizeof("HTTP/1") - 1, "HTTP/1") != 0)
{ // TODO : move in Client parsing ?
client->status = 505;
return READ_COMPLETE;
}
if (!client->get_headers("Content-Length").empty()
&& ::atoi(client->get_headers("Content-Length").c_str()) > (int)client->assigned_server->client_body_limit)
{
client->status = 413;
_epoll_update(client->fd, EPOLLOUT, EPOLL_CTL_MOD);
return;
return READ_COMPLETE;
}
}
else if (client->raw_request.size() > MAX_HEADER_SIZE)
{
client->status = 400;
_epoll_update(client->fd, EPOLLOUT, EPOLL_CTL_MOD);
return;
return READ_COMPLETE;
}
}
else if (client->header_complete)
@@ -63,22 +85,21 @@ void Webserv::_read_request(Client *client) // Messy, Need refactoring
if (client->read_body_size > client->assigned_server->client_body_limit)
{
client->status = 413;
_epoll_update(client->fd, EPOLLOUT, EPOLL_CTL_MOD);
return;
return READ_COMPLETE;
}
if ((int)client->read_body_size > ::atoi(client->get_headers("Content-Length").c_str()))
if ((int)client->read_body_size >= ::atoi(client->get_headers("Content-Length").c_str()))
{
client->parse_request(); // reparse for the body
_epoll_update(client->fd, EPOLLOUT, EPOLL_CTL_MOD);
return;
return READ_COMPLETE;
}
}
if (client->header_complete && client->get_headers("Content-Type").empty() && client->get_headers("Content-Length").empty() )
{
_epoll_update(client->fd, EPOLLOUT, EPOLL_CTL_MOD);
return;
return READ_COMPLETE;
}
return READ_IN_PROGRESS;
}