mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-10 21:18:46 +02:00
「✨」 feat(src/server.cpp): handle_client :D
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/11 13:29:21 by mmoussou #+# #+# */
|
||||
/* Updated: 2025/04/10 13:58:56 by adjoly ### ########.fr */
|
||||
/* Updated: 2025/04/13 11:24:52 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -24,6 +24,8 @@
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#define BUFFER_SIZE 4096
|
||||
|
||||
namespace webserv {
|
||||
|
||||
} //-namespace webserv
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
|
||||
/* Updated: 2025/04/12 18:53:45 by mmoussou ### ########.fr */
|
||||
/* Updated: 2025/04/13 11:53:40 by mmoussou ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -17,12 +17,64 @@
|
||||
#include <stdexcept>
|
||||
#include <sys/socket.h>
|
||||
#include <webserv.hpp>
|
||||
#include <requests/default.hpp>
|
||||
|
||||
using namespace webserv;
|
||||
|
||||
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)
|
||||
{
|
||||
(void) 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);
|
||||
}
|
||||
|
||||
void Server::_setup(void) {
|
||||
@ -46,7 +98,8 @@ void Server::_setup(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void Server::_run(void) {
|
||||
void Server::_run(void)
|
||||
{
|
||||
struct pollfd fd;
|
||||
|
||||
fd.fd = _fd_server;
|
||||
@ -81,13 +134,13 @@ void Server::_run(void) {
|
||||
_client_fds.push_back(client_pollfd);
|
||||
++nbr_client;
|
||||
|
||||
// if (nbr_client ) TODO do we need a max client probably not :D
|
||||
// if (nbr_client) TODO do we need a max client probably not :D
|
||||
}
|
||||
for (int i = 1; i <= nbr_client; )
|
||||
{
|
||||
if (_client_fds[i].revents & POLLIN)
|
||||
{
|
||||
_handle_client(i);
|
||||
_handle_client(_client_fds[i].fd);
|
||||
close(_client_fds[i].fd);
|
||||
_client_fds[i] = _client_fds[nbr_client--];
|
||||
_client_fds.pop_back();
|
||||
|
Reference in New Issue
Block a user