mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-10 23:08:47 +02:00
「🏗️」 wip: loop nearly working
This commit is contained in:
@ -2,7 +2,7 @@ log_file = "test.log"
|
|||||||
|
|
||||||
[server]
|
[server]
|
||||||
server_names = { "localhost", "2B5.local" }
|
server_names = { "localhost", "2B5.local" }
|
||||||
host = "localhost"
|
host = "0.0.0.0"
|
||||||
port = 8080
|
port = 8080
|
||||||
|
|
||||||
[server.error_pages]
|
[server.error_pages]
|
||||||
|
@ -6,85 +6,74 @@
|
|||||||
/* 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/22 12:01:16 by mmoussou ### ########.fr */
|
/* Updated: 2025/04/23 12:42:41 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <server/Client.hpp>
|
|
||||||
#include <log.hpp>
|
#include <log.hpp>
|
||||||
|
#include <server/Client.hpp>
|
||||||
|
|
||||||
using namespace server;
|
using namespace server;
|
||||||
|
|
||||||
Client::Client(int fd, sockaddr_in socket, config::Config *conf): _fd(fd), _client_addr(socket)
|
Client::Client(int fd, sockaddr_in socket, config::Config *conf)
|
||||||
{
|
: _fd(fd), _client_addr(socket) {
|
||||||
std::string received_data;
|
std::string received_data;
|
||||||
char buffer[BUFFER_SIZE];
|
char buffer[BUFFER_SIZE];
|
||||||
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(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");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
received_data += std::string(buffer, bytes_received);
|
received_data += std::string(buffer, bytes_received);
|
||||||
}
|
} while (buffer[bytes_received]);
|
||||||
while (buffer[bytes_received]);
|
|
||||||
|
|
||||||
|
|
||||||
this->_getRequest(received_data);
|
this->_getRequest(received_data);
|
||||||
|
|
||||||
this->_conf = conf->getServer(this->_request->getHeaders()["Host"]);
|
this->_conf = conf->getServer(this->_request->getHeaders()["Host"]);
|
||||||
|
|
||||||
|
// if (received_data.length > (get max_body_size from Route corresponding) )
|
||||||
//if (received_data.length > (get max_body_size from Route corresponding) )
|
|
||||||
// throw error
|
// throw error
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::_getRequest(std::string request_str)
|
void Client::_getRequest(std::string request_str) {
|
||||||
{
|
std::string method = request_str.substr(
|
||||||
std::string method = request_str.substr(0, request_str.substr(0, 4).find_last_not_of(" ") + 1);
|
0, request_str.substr(0, 4).find_last_not_of(" ") + 1);
|
||||||
|
|
||||||
if (method == "GET")
|
if (method == "GET") {
|
||||||
{
|
|
||||||
_log->info("get request received");
|
_log->info("get request received");
|
||||||
this->_request = new http::Get(request_str);
|
this->_request = new http::Get(request_str);
|
||||||
}
|
} else if (method == "DELETE") {
|
||||||
else if (method == "DELETE")
|
|
||||||
{
|
|
||||||
_log->info("delete request received");
|
_log->info("delete request received");
|
||||||
this->_request = new http::Delete(request_str);
|
this->_request = new http::Delete(request_str);
|
||||||
}
|
} else if (method == "POST") {
|
||||||
else if (method == "POST")
|
|
||||||
{
|
|
||||||
_log->info("post request received");
|
_log->info("post request received");
|
||||||
this->_request = new http::Post(request_str);
|
this->_request = new http::Post(request_str);
|
||||||
}
|
} else {
|
||||||
else
|
|
||||||
{
|
|
||||||
_log->info("unsupported request received");
|
_log->info("unsupported request received");
|
||||||
this->_request = new http::Get();
|
this->_request = new http::Get();
|
||||||
this->_request->setMethod("501");
|
this->_request->setMethod("501");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Client::answer(void)
|
void Client::answer(void) {
|
||||||
{
|
|
||||||
std::string response;
|
std::string response;
|
||||||
(void) _client_addr;
|
(void)_client_addr;
|
||||||
|
|
||||||
if (this->_request->getMethod() == "GET" || this->_request->getMethod() == "DELETE" || this->_request->getMethod() == "POST")
|
if (this->_request->getMethod() == "GET" ||
|
||||||
|
this->_request->getMethod() == "DELETE" ||
|
||||||
|
this->_request->getMethod() == "POST")
|
||||||
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, response.c_str(), response.length(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Client::~Client(void) {
|
||||||
Client::~Client(void)
|
|
||||||
{
|
|
||||||
delete (http::Get *)(this->_request);
|
delete (http::Get *)(this->_request);
|
||||||
delete this->_response;
|
delete this->_response;
|
||||||
}
|
}
|
||||||
|
@ -6,12 +6,13 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
|
/* Created: 2025/04/11 16:11:40 by adjoly #+# #+# */
|
||||||
/* Updated: 2025/04/22 16:37:31 by adjoly ### ########.fr */
|
/* Updated: 2025/04/23 12:38:15 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include <cerrno>
|
#include <cerrno>
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
|
#include <cstring>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <iterator>
|
#include <iterator>
|
||||||
@ -71,12 +72,8 @@ void Server::_setup(void) {
|
|||||||
|
|
||||||
auto itH = hosts.begin();
|
auto itH = hosts.begin();
|
||||||
for (auto it = range(ports), itH++) {
|
for (auto it = range(ports), itH++) {
|
||||||
try {
|
|
||||||
int fd = _createSocket(*itH, *it);
|
int fd = _createSocket(*itH, *it);
|
||||||
_fds_server.push_back(fd);
|
_fds_server.push_back(fd);
|
||||||
} catch (std::exception &e) {
|
|
||||||
throw e;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,11 +123,18 @@ void Server::_run(void) {
|
|||||||
pfd.fd = client_fd;
|
pfd.fd = client_fd;
|
||||||
pfd.events = POLLIN | POLLOUT;
|
pfd.events = POLLIN | POLLOUT;
|
||||||
_client_fds.push_back(pfd);
|
_client_fds.push_back(pfd);
|
||||||
_client_data.push_back(new sockaddr_in(client_addr));
|
struct sockaddr_in* new_client_sock = new sockaddr_in();
|
||||||
|
std::memmove(new_client_sock, &client_addr, sizeof(struct sockaddr_in));
|
||||||
|
std::cout << "tamere ==== " << new_client_sock << std::endl;
|
||||||
|
_client_data.push_back(new_client_sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (size_t i = 0; i < _client_fds.size(); ++i) {
|
for (size_t i = _fds_server.size(); i < _client_fds.size(); ++i) {
|
||||||
|
std::cout << i << std::endl;
|
||||||
if (_client_fds[i].revents & POLLIN) {
|
if (_client_fds[i].revents & POLLIN) {
|
||||||
|
std::cout << _client_fds.size() << " " << _client_data.size() << std::endl;
|
||||||
|
std::cout << "tamere ==== " << _client_data[i] << std::endl;
|
||||||
|
std::cout << i << std::endl;
|
||||||
if (!_handle_client(_client_fds[i], _client_data[i])) {
|
if (!_handle_client(_client_fds[i], _client_data[i])) {
|
||||||
close(_client_fds[i].fd);
|
close(_client_fds[i].fd);
|
||||||
_client_fds.erase(_client_fds.begin() + i);
|
_client_fds.erase(_client_fds.begin() + i);
|
||||||
@ -149,7 +153,7 @@ Server::Server(config::Config *conf) : _conf(conf) {
|
|||||||
try {
|
try {
|
||||||
_setup();
|
_setup();
|
||||||
_run();
|
_run();
|
||||||
} catch (std::runtime_error &e) {
|
} catch (std::exception &e) {
|
||||||
_log->error(e.what());
|
_log->error(e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */
|
/* Created: 2025/04/17 11:58:42 by adjoly #+# #+# */
|
||||||
/* Updated: 2025/04/22 16:32:45 by adjoly ### ########.fr */
|
/* Updated: 2025/04/23 12:33:47 by adjoly ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
@ -14,6 +14,8 @@
|
|||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <server/default.hpp>
|
#include <server/default.hpp>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <string>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
|
||||||
using namespace webserv::server;
|
using namespace webserv::server;
|
||||||
@ -84,17 +86,14 @@ int Server::_createSocket(std::string host, int port) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Server::_handle_client(struct pollfd &pollfd, sockaddr_in *sock_data) {
|
bool Server::_handle_client(struct pollfd &pollfd, sockaddr_in *sock_data) {
|
||||||
Client *client;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
client = new Client(pollfd.fd, *sock_data, _conf);
|
std::cout << "tamere ==== " << sock_data << std::endl;
|
||||||
client->answer();
|
Client client(pollfd.fd, *sock_data, _conf);
|
||||||
} catch (std::exception &e) {
|
client.answer();
|
||||||
|
} catch (std::runtime_error &e) {
|
||||||
_log->error(e.what());
|
_log->error(e.what());
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
delete client;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user