Something went wrong. Try again.
An HTTP server written in C++98 projects.intra.42.fr/projects/webserv
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118/* ************************************************************************** *//* *//* ::: :::::::: *//* Client.cpp :+: :+: :+: *//* +:+ +:+ +:+ *//* By: bcausseq <bcausseq@42angouleme.fr> +#+ +:+ +#+ *//* +#+#+#+#+#+ +#+ *//* Created: 2026/09/15 21:48:12 by bcausseq #+# #+# *//* Updated: 2026/09/27 22:43:01 by bcausseq ### ########.fr *//* *//* ************************************************************************** */
#include "Client.hpp"#include "http/Header.hpp"
#include <iostream>#include <sstream>#include <sys/types.h>#include <sys/socket.h>#include <unistd.h>
Client::Client(int fd, Config const& config) : _fd(fd), _in_buffer(""), _out_buffer(""), _should_disconnect(false), _config(config){}
Client::~Client(){ this->disconnect();}
void Client::receive(std::vector<Pollable*>& pollables){ (void) pollables;
char buf[4096]; ssize_t bytes = ::recv(get_fd(), buf, 4096, 0); if (bytes > 0) { std::stringstream ss; ss << "Received " << bytes << " bytes"; log(ss.str());
_in_buffer.append(buf, static_cast<size_t>(bytes)); parse_header(*this);//j'ai pas trouvé mieux //parse_header(this, &pfd, s); } else if (bytes == 0) { this->_should_disconnect = true; return; }}
void Client::send(){ if (_out_buffer.empty()) return;
std::stringstream ss; ss << "Sending " << _out_buffer.size() << " bytes..."; log(ss.str());
ssize_t bytes = ::send(get_fd(), _out_buffer.c_str(), _out_buffer.size(), 0);
if (bytes <= 0) { this->_should_disconnect = true; return; }
ss.clear(); ss << "Actually sent " << bytes << " bytes"; log(ss.str());
_out_buffer.erase(0 , static_cast<size_t>(bytes));}
void Client::disconnect(){ if (_fd > 2) { close(_fd); _fd = -1; }}
void Client::log(std::string message){ std::cout << "[Client " << this->_fd << "] " << message << std::endl;}
bool Client::should_disconnect() const{ return this->_should_disconnect;}
int Client::get_fd() const{ return (_fd);}
void Client::set_append_out_buffer(const std::string &message){ if (_out_buffer.empty()) _out_buffer = message; else _out_buffer += message;}
std::string Client::get_in_buffer() const{ return (_in_buffer);}
Config const& Client::get_config() const{ return _config;}