🏗️」 wip: started client handling (need to redo a part of the client fd handling) not yet usable

This commit is contained in:
2025-04-17 19:04:30 +02:00
parent 49fcb87395
commit 0b77d7b80c
5 changed files with 38 additions and 9 deletions

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
/* Updated: 2025/04/17 14:25:50 by mmoussou ### ########.fr */
/* Updated: 2025/04/17 18:48:17 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -14,8 +14,31 @@
using namespace server;
Client::Client(int fd, sockaddr_in socket, config::Servr *conf, Logger *log): _fd(fd), _client_addr(socker), _conf(conf), _log(log)
/*class Client {
public:
Client(int, sockaddr_in, config::Config *);
~Client(void);
void answer(void);
private:
void getRequest(void);
int _fd;
struct sockaddr_in _client_addr;
http::IRequest *_request;
http::Response *_response;
config::Server *_conf;
std::string _request_method;
};*/
Client::Client(int fd, sockaddr_in socket, config::Server *conf, Logger *log)
{
this->_fd = fd;
this->_client_addr = socket;
this->_conf = conf;
this->_log = log;
std::string received_data;
char buffer[BUFFER_SIZE];
ssize_t bytes_received;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
/* Updated: 2025/04/17 18:21:34 by adjoly ### ########.fr */
/* Updated: 2025/04/17 18:57:20 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */

77
src/server/Socket.cpp Normal file
View File

@ -0,0 +1,77 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Socket.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */
/* Updated: 2025/04/17 19:03:27 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include "server/Client.hpp"
#include <netinet/in.h>
#include <server/default.hpp>
#include <sys/socket.h>
using namespace webserv;
bool convertStringToIP(const char *ip_str, struct in_addr *addr) {
// Split the IP string into four octets
unsigned int a, b, c, d;
if (sscanf(ip_str, "%u.%u.%u.%u", &a, &b, &c, &d) != 4) {
return false;
}
// Check if each octet is within the valid range
if (a > 255 || b > 255 || c > 255 || d > 255) {
return false;
}
// Combine the octets into a single 32-bit address
addr->s_addr = htonl((a << 24) | (b << 16) | (c << 8) | d);
return true;
}
int Server::_createSocket(std::string host, int port) {
int fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
if (fd == -1) {
std::ostringstream str;
str << port;
throw std::runtime_error("socket binding failed for : " + host + ":" +
str.str());
}
return -1;
int opt = 1;
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) {
close(fd);
throw std::runtime_error("setsockopt failed");
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
convertStringToIP(host.c_str(), &addr.sin_addr);
addr.sin_port = htons(port);
if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
close(fd);
std::ostringstream str;
str << port;
throw std::runtime_error("bind failed for : " + host + ":" + str.str());
}
if (listen(fd, SOMAXCONN) < 0) {
close(fd);
std::ostringstream str;
str << port;
throw std::runtime_error("listen failed for : " + host + ":" + str.str());
}
return (fd);
}
void Server::_handle_client(int fd, sockaddr_in client_addr, struct pollfd poll_fd) {
server::Client *client = new server::Client(fd, client_addr, );
}