🔨」 fix(Server): fixed main loop

This commit is contained in:
y-syo
2025-05-02 13:42:24 +02:00
parent 86896945f9
commit 2352a865bf
3 changed files with 71 additions and 27 deletions

View File

@ -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/05/01 15:25:45 by adjoly ### ########.fr */ /* Updated: 2025/05/02 13:21:10 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -41,6 +41,8 @@ class Client {
return true; return true;
} }
bool isReadyToClose() const;
private: private:
void _getRequest(std::string); void _getRequest(std::string);
@ -58,6 +60,9 @@ class Client {
http::Response _response; http::Response _response;
config::Server *_conf; config::Server *_conf;
config::Route *_route; config::Route *_route;
size_t _bytes_sent;
std::string _response_str;
bool _response_done;
}; };
} // namespace server } // namespace server

View File

@ -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/05/01 15:27:00 by adjoly ### ########.fr */ /* Updated: 2025/05/02 13:33:07 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,6 +22,7 @@ Client::Client(struct pollfd *pfd, config::Server *conf)
: _pfd(pfd), _conf(conf) { : _pfd(pfd), _conf(conf) {
_request = not_nullptr; _request = not_nullptr;
log("", "Client", "constructor called"); log("", "Client", "constructor called");
_response_done = false;
} }
Client::Client(const Client &cpy) { Client::Client(const Client &cpy) {
@ -101,22 +102,43 @@ void Client::_getRequest(std::string request_str) {
} }
void Client::answer(void) { void Client::answer(void) {
std::string response;
if (_request == not_nullptr) { if (_request == not_nullptr) {
return; return;
} }
if (_response_str.empty())
{
if (this->_request->getMethod() == "GET" || if (this->_request->getMethod() == "GET" ||
this->_request->getMethod() == "DELETE" || this->_request->getMethod() == "DELETE" ||
this->_request->getMethod() == "POST") this->_request->getMethod() == "POST")
{
_response = this->_request->execute(); _response = this->_request->execute();
_response_str = _response.str();
}
else else
response = "HTTP/1.1 501 Not Implemented\r\nContent-Type: " {
this->_response.setStatusCode(501);
_response_str = "HTTP/1.1 501 Not Implemented\r\nContent-Type: "
"text/html\r\n\r\n<html><body><h1>501 Not " "text/html\r\n\r\n<html><body><h1>501 Not "
"Implemented</h1></body></html>"; "Implemented</h1></body></html>";
response = _response.str(); }
send(_pfd->fd, response.c_str(), response.length(), 0); _bytes_sent = 0;
}
ssize_t sent = send(_pfd->fd, _response_str.c_str() + _bytes_sent, _response_str.length() - _bytes_sent, 0);
if (sent == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
return;
_log->error("send failed: " + std::string(strerror(errno)));
_response_done = true;
return;
}
_bytes_sent += sent;
if (_bytes_sent >= _response_str.length()) {
_response_done = true;
std::stringstream str; std::stringstream str;
str << "response sent, for page : "; str << "response sent, for page : ";
str << _request->getTarget(); str << _request->getTarget();
@ -125,7 +147,20 @@ void Client::answer(void) {
_log->info(str.str()); _log->info(str.str());
} }
/*std::stringstream str;
str << "response sent, for page : ";
str << _request->getTarget();
str << " with response code : ";
str << _response.getStatusCode();
_log->info(str.str());*/
}
Client::~Client(void) { Client::~Client(void) {
log("", "Client", "destructor called"); log("", "Client", "destructor called");
delete (http::Get *)(this->_request); delete (http::Get *)(this->_request);
} }
bool Client::isReadyToClose() const {
return _response_done; // Check if all response data has been sent
}

View File

@ -6,7 +6,7 @@
/* 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/05/01 12:52:46 by adjoly ### ########.fr */ /* Updated: 2025/05/02 13:30:50 by mmoussou ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -187,9 +187,12 @@ void Server::_run(void) {
_log->error("client does not exist"); _log->error("client does not exist");
continue; continue;
} }
if (client->requestParsed() == true) { if (client->requestParsed() == true && !client->isReadyToClose()) {
client->answer(); client->answer();
continue;
} }
if (client->isReadyToClose()) {
_client_data.erase(std::find(_client_data.begin(), _client_data.erase(std::find(_client_data.begin(),
_client_data.end(), client)); _client_data.end(), client));
delete client; delete client;
@ -206,6 +209,7 @@ void Server::_run(void) {
} }
} }
} }
}
Server::Server(config::Config *conf) : _conf(conf) { Server::Server(config::Config *conf) : _conf(conf) {
log("", "Server::Server", "config constructor called"); log("", "Server::Server", "config constructor called");