」 feat: added redirection handling

This commit is contained in:
2025-05-04 13:37:49 +02:00
parent 1537cf59ac
commit 4f93e0dcf5
4 changed files with 64 additions and 21 deletions

View File

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* RedirectResp.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/05/04 13:12:04 by adjoly #+# #+# */
/* Updated: 2025/05/04 13:33:36 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#pragma once
#include <requests/Response.hpp>
namespace webserv {
namespace http {
class Redirect : public Response {
public:
Redirect(std::string redir_path) : _path(redir_path) {
setProtocol("HTTP/1.1");
setStatusCode(308);
addHeader("Location", _path);
addHeader("Content-lengh", "0");
}
void setPath(const std::string path) { _path = path; }
std::string getPath(void) const { return _path; }
~Redirect(void) {}
protected:
private:
std::string _path;
};
} // namespace http
} // namespace webserv

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:21:20 by mmoussou #+# #+# */ /* Created: 2025/02/03 17:21:20 by mmoussou #+# #+# */
/* Updated: 2025/04/30 09:33:47 by adjoly ### ########.fr */ /* Updated: 2025/05/04 13:12:53 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,7 @@ typedef unsigned int uint;
namespace webserv { namespace webserv {
namespace http { namespace http {
class Response : public http::IMessage { class Response : public IMessage {
public: public:
Response(void); Response(void);
~Response(void); ~Response(void);

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */ /* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/03 17:28:31 by mmoussou #+# #+# */ /* Created: 2025/02/03 17:28:31 by mmoussou #+# #+# */
/* Updated: 2025/04/30 09:47:50 by adjoly ### ########.fr */ /* Updated: 2025/05/04 13:31:10 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -44,7 +44,7 @@ std::string http::Response::str(void) const
response << "\r\n"; response << "\r\n";
response << this->_body; response << this->_body;
//std::cout << "------------ RESPONSE -------------" << std::endl << response.str(); std::cout << "------------ RESPONSE -------------" << std::endl << response.str() << std::endl;
return (response.str()); return (response.str());
} }

View File

@ -6,11 +6,12 @@
/* 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/02 14:26:32 by mmoussou ### ########.fr */ /* Updated: 2025/05/04 13:37:21 by adjoly ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "cppeleven.hpp" #include "cppeleven.hpp"
#include "requests/RedirectResp.hpp"
#include "requests/default.hpp" #include "requests/default.hpp"
#include <log.hpp> #include <log.hpp>
#include <server/Client.hpp> #include <server/Client.hpp>
@ -56,14 +57,17 @@ void Client::parse(void) {
return; return;
} }
if ((this->_request->getMethod() == "GET" && !_route->getMethods()[0]) || if (_route->getRedirect() == true) {
(this->_request->getMethod() == "POST" && !_route->getMethods()[1]) || } else if ((this->_request->getMethod() == "GET" &&
(this->_request->getMethod() == "DELETE" && !_route->getMethods()[2])) !_route->getMethods()[0]) ||
(this->_request->getMethod() == "POST" &&
!_route->getMethods()[1]) ||
(this->_request->getMethod() == "DELETE" &&
!_route->getMethods()[2]))
this->_request->setMethod("405"); this->_request->setMethod("405");
if (received_data.length() > (unsigned long)(_route->getMaxBody())) if (received_data.length() > (unsigned long)(_route->getMaxBody()))
this->_request->setMethod("413"); this->_request->setMethod("413");
} }
bool Client::requestParsed(void) { bool Client::requestParsed(void) {
@ -103,26 +107,28 @@ void Client::answer(void) {
return; return;
} }
if (_response_str.empty()) if (_route->getRedirect() == true) {
{ http::Redirect redir(_route->getRootDir());
_response = redir;
_response_str = _response.str();
_bytes_sent = 0;
} else 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(); _response_str = _response.str();
} } else {
else
{
this->_response.setStatusCode(501); this->_response.setStatusCode(501);
_response_str = "HTTP/1.1 501 Not Implemented\r\nContent-Type: " _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>";
} }
_bytes_sent = 0; _bytes_sent = 0;
} }
ssize_t sent = send(_pfd->fd, _response_str.c_str() + _bytes_sent, _response_str.length() - _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 (sent == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) if (errno == EAGAIN || errno == EWOULDBLOCK)
@ -144,7 +150,6 @@ void Client::answer(void) {
_log->info(str.str()); _log->info(str.str());
} }
/*std::stringstream str; /*std::stringstream str;
str << "response sent, for page : "; str << "response sent, for page : ";
str << _request->getTarget(); str << _request->getTarget();
@ -159,5 +164,5 @@ Client::~Client(void) {
} }
bool Client::isReadyToClose() const { bool Client::isReadyToClose() const {
return _response_done; // Check if all response data has been sent return _response_done; // Check if all response data has been sent
} }