mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-05-10 23:48:46 +02:00
「✨」 feat: added redirection handling
This commit is contained in:
38
includes/requests/RedirectResp.hpp
Normal file
38
includes/requests/RedirectResp.hpp
Normal 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
|
@ -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);
|
||||||
|
@ -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());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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,17 +107,18 @@ 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 "
|
||||||
@ -122,7 +127,8 @@ void Client::answer(void) {
|
|||||||
_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();
|
||||||
|
Reference in New Issue
Block a user