From 4f93e0dcf5aadd325db18d73a2afb0be79d9bcb6 Mon Sep 17 00:00:00 2001 From: adjoly Date: Sun, 4 May 2025 13:37:49 +0200 Subject: [PATCH] =?UTF-8?q?=E3=80=8C=E2=9C=A8=E3=80=8D=20feat:=20added=20r?= =?UTF-8?q?edirection=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/requests/RedirectResp.hpp | 38 +++++++++++++++++++++++++++++ includes/requests/Response.hpp | 4 +-- src/requests_handling/Response.cpp | 4 +-- src/server/Client.cpp | 39 +++++++++++++++++------------- 4 files changed, 64 insertions(+), 21 deletions(-) create mode 100644 includes/requests/RedirectResp.hpp diff --git a/includes/requests/RedirectResp.hpp b/includes/requests/RedirectResp.hpp new file mode 100644 index 0000000..5ef6000 --- /dev/null +++ b/includes/requests/RedirectResp.hpp @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* RedirectResp.hpp :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: adjoly +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/05/04 13:12:04 by adjoly #+# #+# */ +/* Updated: 2025/05/04 13:33:36 by adjoly ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#pragma once + +#include + +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 diff --git a/includes/requests/Response.hpp b/includes/requests/Response.hpp index b6bc37a..0e20ddf 100644 --- a/includes/requests/Response.hpp +++ b/includes/requests/Response.hpp @@ -6,7 +6,7 @@ /* By: mmoussou _body; - //std::cout << "------------ RESPONSE -------------" << std::endl << response.str(); + std::cout << "------------ RESPONSE -------------" << std::endl << response.str() << std::endl; return (response.str()); } diff --git a/src/server/Client.cpp b/src/server/Client.cpp index b3e2a53..56d584d 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,11 +6,12 @@ /* By: mmoussou #include @@ -56,14 +57,17 @@ void Client::parse(void) { return; } - if ((this->_request->getMethod() == "GET" && !_route->getMethods()[0]) || - (this->_request->getMethod() == "POST" && !_route->getMethods()[1]) || - (this->_request->getMethod() == "DELETE" && !_route->getMethods()[2])) + if (_route->getRedirect() == true) { + } else if ((this->_request->getMethod() == "GET" && + !_route->getMethods()[0]) || + (this->_request->getMethod() == "POST" && + !_route->getMethods()[1]) || + (this->_request->getMethod() == "DELETE" && + !_route->getMethods()[2])) this->_request->setMethod("405"); if (received_data.length() > (unsigned long)(_route->getMaxBody())) this->_request->setMethod("413"); - } bool Client::requestParsed(void) { @@ -103,26 +107,28 @@ void Client::answer(void) { 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" || this->_request->getMethod() == "DELETE" || - this->_request->getMethod() == "POST") - { + this->_request->getMethod() == "POST") { _response = this->_request->execute(); _response_str = _response.str(); - } - else - { + } else { this->_response.setStatusCode(501); _response_str = "HTTP/1.1 501 Not Implemented\r\nContent-Type: " - "text/html\r\n\r\n

501 Not " - "Implemented

"; + "text/html\r\n\r\n

501 Not " + "Implemented

"; } _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 (errno == EAGAIN || errno == EWOULDBLOCK) @@ -143,7 +149,6 @@ void Client::answer(void) { str << _response.getStatusCode(); _log->info(str.str()); } - /*std::stringstream str; str << "response sent, for page : "; @@ -159,5 +164,5 @@ Client::~Client(void) { } 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 }