mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-10 13:58:46 +02:00
「🔨」 fix(Server): fixed main loop
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
}
|
||||
|
||||
bool isReadyToClose() const;
|
||||
|
||||
private:
|
||||
void _getRequest(std::string);
|
||||
|
||||
@ -58,6 +60,9 @@ class Client {
|
||||
http::Response _response;
|
||||
config::Server *_conf;
|
||||
config::Route *_route;
|
||||
size_t _bytes_sent;
|
||||
std::string _response_str;
|
||||
bool _response_done;
|
||||
};
|
||||
|
||||
} // namespace server
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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) {
|
||||
_request = not_nullptr;
|
||||
log("➕", "Client", "constructor called");
|
||||
_response_done = false;
|
||||
}
|
||||
|
||||
Client::Client(const Client &cpy) {
|
||||
@ -101,31 +102,65 @@ void Client::_getRequest(std::string request_str) {
|
||||
}
|
||||
|
||||
void Client::answer(void) {
|
||||
std::string response;
|
||||
|
||||
if (_request == not_nullptr) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->_request->getMethod() == "GET" ||
|
||||
this->_request->getMethod() == "DELETE" ||
|
||||
this->_request->getMethod() == "POST")
|
||||
_response = this->_request->execute();
|
||||
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 = _response.str();
|
||||
send(_pfd->fd, response.c_str(), response.length(), 0);
|
||||
std::stringstream str;
|
||||
if (_response_str.empty())
|
||||
{
|
||||
if (this->_request->getMethod() == "GET" ||
|
||||
this->_request->getMethod() == "DELETE" ||
|
||||
this->_request->getMethod() == "POST")
|
||||
{
|
||||
_response = this->_request->execute();
|
||||
_response_str = _response.str();
|
||||
}
|
||||
else
|
||||
{
|
||||
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 "
|
||||
"Implemented</h1></body></html>";
|
||||
}
|
||||
_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;
|
||||
str << "response sent, for page : ";
|
||||
str << _request->getTarget();
|
||||
str << " with response code : ";
|
||||
str << _response.getStatusCode();
|
||||
_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());
|
||||
_log->info(str.str());*/
|
||||
}
|
||||
|
||||
Client::~Client(void) {
|
||||
log("➖", "Client", "destructor called");
|
||||
delete (http::Get *)(this->_request);
|
||||
}
|
||||
|
||||
bool Client::isReadyToClose() const {
|
||||
return _response_done; // Check if all response data has been sent
|
||||
}
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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,21 +187,25 @@ void Server::_run(void) {
|
||||
_log->error("client does not exist");
|
||||
continue;
|
||||
}
|
||||
if (client->requestParsed() == true) {
|
||||
if (client->requestParsed() == true && !client->isReadyToClose()) {
|
||||
client->answer();
|
||||
continue;
|
||||
}
|
||||
_client_data.erase(std::find(_client_data.begin(),
|
||||
|
||||
if (client->isReadyToClose()) {
|
||||
_client_data.erase(std::find(_client_data.begin(),
|
||||
_client_data.end(), client));
|
||||
delete client;
|
||||
for (auto it = range(_client_fds)) {
|
||||
if (_client_fds[i].fd == (*it).fd) {
|
||||
_log->debug("client fds erased");
|
||||
close(it.base()->fd);
|
||||
_client_fds.erase(it);
|
||||
break;
|
||||
delete client;
|
||||
for (auto it = range(_client_fds)) {
|
||||
if (_client_fds[i].fd == (*it).fd) {
|
||||
_log->debug("client fds erased");
|
||||
close(it.base()->fd);
|
||||
_client_fds.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
i--;
|
||||
}
|
||||
i--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user