mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-10 20:48:46 +02:00
「🏗️」 wip: main loop should be working
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/11 17:45:43 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/17 11:48:36 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/04/17 18:08:38 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -41,9 +41,9 @@ class Server {
|
||||
/**
|
||||
* @brief Used to handle client request
|
||||
*
|
||||
* @param The number of the client fd
|
||||
* @param The fd of the client
|
||||
*/
|
||||
void _handle_client(Client);
|
||||
void _handle_client(int, sockaddr_in, config::Config *, struct pollfd);
|
||||
|
||||
/**
|
||||
* @brief Can be used to fill the vector passed as parameters with all the
|
||||
|
@ -6,66 +6,42 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/17 14:35:01 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/04/17 18:21:34 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <cmath>
|
||||
#include <iterator>
|
||||
#include <log.hpp>
|
||||
#include <netinet/in.h>
|
||||
#include <poll.h>
|
||||
#include <requests/default.hpp>
|
||||
#include <server/default.hpp>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <sys/socket.h>
|
||||
#include <webserv.hpp>
|
||||
|
||||
using namespace webserv;
|
||||
|
||||
std::string convertIPToString(const struct in_addr *addr) {
|
||||
unsigned int ip = ntohl(addr->s_addr);
|
||||
std::stringstream ss;
|
||||
ss << ((ip >> 24) & 0xFF) << "." << ((ip >> 16) & 0xFF) << "."
|
||||
<< ((ip >> 8) & 0xFF) << "." << (ip & 0xFF);
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string convertPortToString(const struct sockaddr_in *sa) {
|
||||
int port = ntohs(sa->sin_port);
|
||||
std::stringstream ss;
|
||||
ss << port;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string getMethod(std::string &data) {
|
||||
return (data.substr(0, data.substr(0, 4).find_last_not_of(" ") + 1));
|
||||
}
|
||||
|
||||
void Server::_handle_client(int fd) {
|
||||
std::string received_data;
|
||||
char buffer[BUFFER_SIZE];
|
||||
ssize_t bytes_received;
|
||||
do {
|
||||
std::memset(buffer, 0, BUFFER_SIZE);
|
||||
bytes_received = recv(fd, buffer, BUFFER_SIZE - 1, 0);
|
||||
if (bytes_received == -1) {
|
||||
_log->error("failed to receive request");
|
||||
continue;
|
||||
}
|
||||
received_data += std::string(buffer, bytes_received);
|
||||
} while (buffer[bytes_received]);
|
||||
|
||||
std::string response;
|
||||
|
||||
if (getMethod(received_data) == "GET") {
|
||||
_log->info("get request received");
|
||||
http::Get request(received_data);
|
||||
|
||||
response = request.execute().str();
|
||||
} else if (getMethod(received_data) == "DELETE") {
|
||||
_log->info("delete request received");
|
||||
http::Delete request(received_data);
|
||||
|
||||
response = request.execute().str();
|
||||
} else if (getMethod(received_data) == "POST") {
|
||||
_log->info("post request received");
|
||||
http::Post request(received_data);
|
||||
|
||||
response = request.execute().str();
|
||||
} 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>";
|
||||
}
|
||||
|
||||
send(fd, response.c_str(), response.length(), 0);
|
||||
}
|
||||
|
||||
int Server::_fillHostsPorts(std::vector<std::string> &hosts,
|
||||
std::vector<int> &ports) {
|
||||
std::vector<config::Server *> *config = _conf->getServers();
|
||||
@ -106,9 +82,32 @@ void Server::_run(void) {
|
||||
it != _fds_server.end(); it++, nbr_client++) {
|
||||
fd.fd = *it;
|
||||
fd.events = POLLIN;
|
||||
_client_fds.push_back(fd);
|
||||
}
|
||||
|
||||
while (727) {
|
||||
int ret = poll(_client_fds.data(), nbr_client, -1);
|
||||
if (ret < 0) {
|
||||
_log->error("poll failed :(");
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int i = 0; i < nbr_client; i++) {
|
||||
if (_client_fds[i].revents & POLLIN) {
|
||||
struct sockaddr_in client_addr;
|
||||
socklen_t addrlen = sizeof(client_addr);
|
||||
int client_fd =
|
||||
accept(_client_fds[i].fd, (struct sockaddr *)&client_addr,
|
||||
&addrlen);
|
||||
if (client_fd < 0) {
|
||||
_log->error("accept failed with : " +
|
||||
convertIPToString(&client_addr.sin_addr) + ":" +
|
||||
convertPortToString(&client_addr));
|
||||
continue ;
|
||||
}
|
||||
_handle_client(client_fd, client_addr, _conf, _client_fds[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user