mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-10 23:48:46 +02:00
「🏗️」 wip(Client): taking pollfd and not just fd in argument
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */
|
/* Created: 2025/04/14 14:14:39 by adjoly #+# #+# */
|
||||||
/* Updated: 2025/04/25 12:32:29 by mmoussou ### ########.fr */
|
/* Updated: 2025/04/25 12:43:54 by mmoussou ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -24,8 +24,8 @@ namespace server {
|
|||||||
class Client {
|
class Client {
|
||||||
public:
|
public:
|
||||||
Client();
|
Client();
|
||||||
Client(int, sockaddr_in, config::Config *);
|
Client(struct pollfd, sockaddr_in, config::Config *);
|
||||||
void parse(int, sockaddr_in, config::Config *);
|
void parse(struct pollfd, sockaddr_in, config::Config *);
|
||||||
virtual ~Client(void);
|
virtual ~Client(void);
|
||||||
|
|
||||||
void answer(void);
|
void answer(void);
|
||||||
@ -33,7 +33,7 @@ class Client {
|
|||||||
private:
|
private:
|
||||||
void _getRequest(std::string);
|
void _getRequest(std::string);
|
||||||
|
|
||||||
int _fd;
|
struct pollfd _fd;
|
||||||
struct sockaddr_in _client_addr;
|
struct sockaddr_in _client_addr;
|
||||||
http::IRequest *_request;
|
http::IRequest *_request;
|
||||||
//http::Response *_response;
|
//http::Response *_response;
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */
|
/* Created: 2025/02/03 16:07:01 by mmoussou #+# #+# */
|
||||||
/* Updated: 2025/04/24 15:31:56 by mmoussou ### ########.fr */
|
/* Updated: 2025/04/25 12:36:17 by mmoussou ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
|
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
|
||||||
/* Updated: 2025/04/25 12:33:38 by mmoussou ### ########.fr */
|
/* Updated: 2025/04/25 12:44:12 by mmoussou ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -17,7 +17,7 @@ using namespace server;
|
|||||||
|
|
||||||
Client::Client(void) {}
|
Client::Client(void) {}
|
||||||
|
|
||||||
Client::Client(int fd, sockaddr_in socket, config::Config *conf)
|
Client::Client(struct pollfd fd, sockaddr_in socket, config::Config *conf)
|
||||||
: _fd(fd), _client_addr(socket)
|
: _fd(fd), _client_addr(socket)
|
||||||
{
|
{
|
||||||
std::string received_data;
|
std::string received_data;
|
||||||
@ -25,7 +25,7 @@ Client::Client(int fd, sockaddr_in socket, config::Config *conf)
|
|||||||
ssize_t bytes_received;
|
ssize_t bytes_received;
|
||||||
do {
|
do {
|
||||||
std::memset(buffer, 0, BUFFER_SIZE);
|
std::memset(buffer, 0, BUFFER_SIZE);
|
||||||
bytes_received = recv(fd, buffer, BUFFER_SIZE - 1, 0);
|
bytes_received = recv(this->_fd.fd, buffer, BUFFER_SIZE - 1, 0);
|
||||||
if (bytes_received == -1) {
|
if (bytes_received == -1) {
|
||||||
_log->error("failed to receive request");
|
_log->error("failed to receive request");
|
||||||
throw std::runtime_error("failed to receive request");
|
throw std::runtime_error("failed to receive request");
|
||||||
@ -42,7 +42,7 @@ Client::Client(int fd, sockaddr_in socket, config::Config *conf)
|
|||||||
// throw error
|
// throw error
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::parse(int fd, sockaddr_in socket, config::Config *conf)
|
void Client::parse(struct pollfd fd, sockaddr_in socket, config::Config *conf)
|
||||||
{
|
{
|
||||||
this->_fd = fd;
|
this->_fd = fd;
|
||||||
this->_client_addr = socket;
|
this->_client_addr = socket;
|
||||||
@ -51,7 +51,7 @@ void Client::parse(int fd, sockaddr_in socket, config::Config *conf)
|
|||||||
ssize_t bytes_received;
|
ssize_t bytes_received;
|
||||||
do {
|
do {
|
||||||
std::memset(buffer, 0, BUFFER_SIZE);
|
std::memset(buffer, 0, BUFFER_SIZE);
|
||||||
bytes_received = recv(fd, buffer, BUFFER_SIZE - 1, 0);
|
bytes_received = recv(this->_fd.fd, buffer, BUFFER_SIZE - 1, 0);
|
||||||
if (bytes_received == -1) {
|
if (bytes_received == -1) {
|
||||||
_log->error("failed to receive request");
|
_log->error("failed to receive request");
|
||||||
throw std::runtime_error("failed to receive request");
|
throw std::runtime_error("failed to receive request");
|
||||||
@ -74,24 +74,24 @@ void Client::_getRequest(std::string request_str) {
|
|||||||
|
|
||||||
if (method == "GET")
|
if (method == "GET")
|
||||||
{
|
{
|
||||||
_log->info("get request received");
|
|
||||||
this->_request = new http::Get(request_str);
|
this->_request = new http::Get(request_str);
|
||||||
|
_log->info("get request received");
|
||||||
}
|
}
|
||||||
else if (method == "DELETE")
|
else if (method == "DELETE")
|
||||||
{
|
{
|
||||||
_log->info("delete request received");
|
|
||||||
this->_request = new http::Delete(request_str);
|
this->_request = new http::Delete(request_str);
|
||||||
|
_log->info("delete request received");
|
||||||
}
|
}
|
||||||
else if (method == "POST")
|
else if (method == "POST")
|
||||||
{
|
{
|
||||||
_log->info("post request received");
|
|
||||||
this->_request = new http::Post(request_str);
|
this->_request = new http::Post(request_str);
|
||||||
|
_log->info("post request received");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_log->info("unsupported request received");
|
|
||||||
this->_request = new http::Get();
|
this->_request = new http::Get();
|
||||||
this->_request->setMethod("501");
|
this->_request->setMethod("501");
|
||||||
|
_log->info("unsupported request received");
|
||||||
}
|
}
|
||||||
// set target to correct target with the conf
|
// set target to correct target with the conf
|
||||||
}
|
}
|
||||||
@ -106,7 +106,7 @@ void Client::answer(void) {
|
|||||||
response = this->_request->execute().str();
|
response = this->_request->execute().str();
|
||||||
else
|
else
|
||||||
response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: text/html\r\n\r\n<html><body><h1>501 Not Implemented</h1></body></html>";
|
response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: text/html\r\n\r\n<html><body><h1>501 Not Implemented</h1></body></html>";
|
||||||
send(this->_fd, response.c_str(), response.length(), 0);
|
send(this->_fd.fd, response.c_str(), response.length(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
Client::~Client(void) {
|
Client::~Client(void) {
|
||||||
|
Reference in New Issue
Block a user