redone _read_request(). Should work with any BUFSIZE.

+ WIP in some aspects. Need to adjust parse_request() among others things.
+ update memo.txt
This commit is contained in:
LuckyLaszlo
2022-08-07 05:41:01 +02:00
parent 8ec1353723
commit 4ab099ee4d
4 changed files with 75 additions and 13 deletions

View File

@@ -2,6 +2,8 @@
#include "Webserv.hpp"
#define BUFSIZE 8192
#define MAX_HEADER_SIZE 42000
#define MAX_BODY_SIZE 500000 // test macro, replace with server config
void Webserv::_request(Client *client)
{
@@ -12,9 +14,10 @@ void Webserv::_request(Client *client)
void Webserv::_read_request(Client *client)
{
char buf[BUFSIZE+1];
char buf[BUFSIZE];
ssize_t ret;
std::cerr << "call recv()" << "\n" ;
ret = ::recv(client->fd, buf, BUFSIZE, 0);
std::cerr << "recv() on fd(" << client->fd << ") returned = " << ret << "\n" ;
if (ret == -1)
@@ -25,20 +28,57 @@ void Webserv::_read_request(Client *client)
_close_client(client->fd);
return ;
}
if (ret == 0) // Not sure what to do in case of 0. Just close ?
if (ret == 0)
{
_close_client(client->fd);
return ;
}
/*
if (ret == BUFSIZE)
// send error like "request too long" to client
*/
buf[ret] = '\0';
client->raw_request.append(buf);
client->parse_request();
client->raw_request.append(buf, ret);
if (!client->header_complete)
{
if (client->raw_request.find(CRLF CRLF) != std::string::npos)
{
client->header_complete = true;
client->parse_request(); // TODO : split function to avoid useless parsing ?
// TODO : determine server here for body size limit
if (!client->get_headers("Content-Length").empty()
&& ::atoi(client->get_headers("Content-Length").c_str()) > MAX_BODY_SIZE)
{
client->status = 413;
_epoll_update(client->fd, EPOLLOUT, EPOLL_CTL_MOD);
return;
}
}
else if (client->raw_request.size() > MAX_HEADER_SIZE)
{
client->status = 400;
_epoll_update(client->fd, EPOLLOUT, EPOLL_CTL_MOD);
return;
}
}
else if (client->header_complete)
{
client->read_body_size += ret;
if (client->read_body_size > MAX_BODY_SIZE)
{
client->status = 413;
_epoll_update(client->fd, EPOLLOUT, EPOLL_CTL_MOD);
return;
}
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;
}
}
_epoll_update(client->fd, EPOLLOUT, EPOLL_CTL_MOD);
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;
}
}