
#include "Webserv.hpp"

#define BUFSIZE 8192

void	Webserv::_request(Client *client)
{
	_read_request(client);
	if (g_last_signal)
		_handle_last_signal();	
}

void	Webserv::_read_request(Client *client)
{
	char	buf[BUFSIZE+1];
	ssize_t	ret;

	ret = ::recv(client->get_cl_fd(), buf, BUFSIZE, 0);
	std::cerr << "recv() on fd(" << client->get_cl_fd() << ") returned = " << ret << "\n" ;
	if (ret == -1)
	{
		std::perror("err recv()");
		std::cerr << "client ptr =" << client << "\n"; // DEBUG
		std::cerr << "client.get_cl_fd() =" << client->get_cl_fd() << "\n"; // DEBUG
		_close_client(client->get_cl_fd());
		return ;
	}
	if (ret == 0) // Not sure what to do in case of 0. Just close ?
	{
		_close_client(client->get_cl_fd());
		return ;
	}
	/*
	if (ret == BUFSIZE)
		// send error like "request too long" to client
	*/

	buf[ret] = '\0';
	client->raw_request.append(buf);
	client->parse_request();

	_epoll_update(client->get_cl_fd(), EPOLLOUT, EPOLL_CTL_MOD);
}

