」 feat: added error page implementation

This commit is contained in:
2025-05-28 11:46:20 +02:00
parent 570ade97b4
commit 887b2c5446
10 changed files with 90 additions and 57 deletions

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */
/* Updated: 2025/04/29 17:28:36 by adjoly ### ########.fr */
/* Updated: 2025/05/28 11:32:32 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */

View File

@ -1,3 +1,4 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
@ -6,7 +7,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/16 17:51:46 by mmoussou #+# #+# */
/* Updated: 2025/04/30 14:30:00 by mmoussou ### ########.fr */
/* Updated: 2025/05/28 11:26:01 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -22,8 +23,8 @@ namespace http {
class Errors {
public:
static std::string getResponseBody(int error_code);
static void setEntries(const std::map<int, std::string>);
static std::string getResponseBody(int error_code, std::string err_file);
// static void setEntries(const std::map<int, std::string>);
static std::map<int, std::string> message;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:30:15 by adjoly #+# #+# */
/* Updated: 2025/05/28 09:55:33 by adjoly ### ########.fr */
/* Updated: 2025/05/28 11:29:38 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -57,7 +57,7 @@ class Post : public ARequest {
class Delete : public ARequest {
public:
Delete(void) {}
Delete(std::string &data);
Delete(std::string &data, config::Server *srv);
void parse(std::string const &data);

13
not_found.html Normal file
View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Not found D:</title>
</head>
<body>
<h1>The page you requested was not found. Here is an image of Kanel instead</h1>
<img src= "https://send.kanel.ovh/hotlink/xA0pJbJY6vuwjvGjpXHde6Y7WSVs7VF0qWp8y9YS.png" width="500" height="500">
</body>
</html>

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */
/* Updated: 2025/05/09 11:47:48 by adjoly ### ########.fr */
/* Updated: 2025/05/28 11:42:25 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -15,6 +15,7 @@
#include "log.hpp"
#include <config/default.hpp>
#include <cstddef>
#include <sstream>
#include <utility>
#include <webserv.hpp>
@ -103,16 +104,15 @@ Server::~Server(void) {
std::map<int, std::string> *
Server::_parseErrPages(std::map<std::string, toml::ANode *> *table) {
std::map<int, std::string> *errPages = new std::map<int, std::string>;
void *val;
int nb;
for (std::map<std::string, toml::ANode *>::iterator it = table->begin();
it != table->end(); it++) {
val = accessValue(it->first, toml::STRING, _table, _log);
if (val != not_nullptr) {
if (it->second->type() == toml::STRING) {
nb = std::atoi(it->first.c_str());
if (nb >= 400 && nb <= 599)
(*errPages)[nb] = *static_cast<std::string *>(val);
(*errPages)[nb] =
*static_cast<std::string *>(it->second->getValue());
else
_log->warn("error page - " + it->first + " is not valid :(");
}
@ -133,11 +133,10 @@ Route *Server::whatRoute(const URL &url) {
std::map<URL, Route *>::iterator ret = _routes->end();
int i = 0;
if (_routes == not_nullptr)
return not_nullptr;
for (auto it = prange(_routes)) {
if (i < it->first.countMatchingSegments(url)) {
ret = it;

View File

@ -6,39 +6,55 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/17 14:08:12 by mmoussou #+# #+# */
/* Updated: 2025/04/30 14:35:48 by mmoussou ### ########.fr */
/* Updated: 2025/05/28 11:33:43 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
#include <fstream>
#include <iterator>
#include <log.hpp>
#include <requests/Errors.hpp>
#include <sstream>
#include <unistd.h>
using namespace webserv;
using namespace http;
void Errors::setEntries(const std::map<int, std::string> error_pages)
{
for (std::map<int, std::string>::const_iterator it = error_pages.begin(); it != error_pages.end(); ++it)
{
if (Errors::set_error_pages.find(it->first) == Errors::set_error_pages.end()) // only insert if key doesn't exist
Errors::set_error_pages[it->first] = it->second;
}
// void Errors::setEntries(const std::map<int, std::string> error_pages)
// {
// for (std::map<int, std::string>::const_iterator it = error_pages.begin(); it
// != error_pages.end(); ++it)
// {
// if (Errors::set_error_pages.find(it->first) ==
// Errors::set_error_pages.end()) // only insert if key doesn't exist
// Errors::set_error_pages[it->first] = it->second;
// }
// }
std::string Errors::getResponseBody(int error_code, std::string err_file) {
std::string body;
if (err_file == "") {
_log->warn("no error file going default");
return ("<html><body><h1>" + Errors::message[error_code] +
"</h1></body></html>");
}
if (access(err_file.c_str(), R_OK) != -1) {
std::ifstream file(err_file.c_str(), std::ios::binary);
std::stringstream buf;
buf << file.rdbuf();
return buf.str();
} else {
_log->error("could not read file");
return ("<html><body><h1>" + Errors::message[error_code] +
"</h1></body></html>");
}
}
std::string Errors::getResponseBody(int error_code)
{
std::string body;
std::map<int, std::string> Errors::message = Errors::populateMessages();
std::map<int, std::string> Errors::set_error_pages;
if (Errors::set_error_pages.find(error_code) != Errors::set_error_pages.end())
return(Errors::set_error_pages[error_code]);
else
return("<html><body><h1>" + Errors::message[error_code] + "</h1></body></html>");
}
std::map<int, std::string> Errors::message = Errors::populateMessages();
std::map<int, std::string> Errors::set_error_pages;
std::map<int, std::string> Errors::populateMessages()
{
std::map<int, std::string> Errors::populateMessages() {
std::map<int, std::string> m;
m[100] = "Continue";

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:42:18 by adjoly #+# #+# */
/* Updated: 2025/05/28 09:55:54 by adjoly ### ########.fr */
/* Updated: 2025/05/28 11:29:55 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -20,9 +20,9 @@
using namespace webserv::http;
Delete::Delete(std::string &data) {
Delete::Delete(std::string &data, config::Server *srv) {
_url = not_nullptr;
_srv = not_nullptr;
_srv = srv;
this->parse(data);
}
@ -90,8 +90,9 @@ Response Delete::execute(void) {
response.setProtocol(this->_protocol);
response.setStatusCode(404);
response.addHeader("Content-Type", "text/html");
response.setBody(
http::Errors::getResponseBody(response.getStatusCode()));
response.setBody(http::Errors::getResponseBody(
response.getStatusCode(),
_srv->getErrorPage(response.getStatusCode())));
}
return (response);

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:40:16 by adjoly #+# #+# */
/* Updated: 2025/05/28 10:00:45 by adjoly ### ########.fr */
/* Updated: 2025/05/28 11:28:01 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -158,8 +158,9 @@ Response Get::execute(void) {
if (_method == "500") {
response.setStatusCode(500);
response.addHeader("Content-Type", "text/html");
response.setBody(
http::Errors::getResponseBody(response.getStatusCode()));
response.setBody(http::Errors::getResponseBody(
response.getStatusCode(),
_srv->getErrorPage(response.getStatusCode())));
server::PfdManager::remove(_cgi->getId());
server::ResourceManager::remove(_cgi->getId());
_cgi = not_nullptr;
@ -277,8 +278,9 @@ body {\n\
response.setProtocol(this->_protocol);
response.setStatusCode(404);
response.addHeader("Content-Type", "text/html");
response.setBody(
http::Errors::getResponseBody(response.getStatusCode()));
response.setBody(http::Errors::getResponseBody(
response.getStatusCode(),
_srv->getErrorPage(response.getStatusCode())));
}
delete _url;

View File

@ -6,7 +6,7 @@
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/30 09:50:20 by adjoly #+# #+# */
/* Updated: 2025/05/28 09:55:37 by adjoly ### ########.fr */
/* Updated: 2025/05/28 11:28:35 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -17,8 +17,8 @@
#include <vector>
#include <config/URL.hpp>
#include <requests/default.hpp>
#include <log.hpp>
#include <requests/default.hpp>
using namespace webserv::http;
@ -79,7 +79,8 @@ std::string Post::extractFilename(const std::string &header) {
return this->_route->getUpRoot() + header.substr(start, end - start);
}
void Post::handleMultipartData(const std::string &body, const std::string &boundary) {
void Post::handleMultipartData(const std::string &body,
const std::string &boundary) {
size_t i = 0;
std::string delim = "--" + boundary;
delim.erase(delim.size() - 1);
@ -109,7 +110,7 @@ void Post::handleMultipartData(const std::string &body, const std::string &bound
Response Post::execute(void) {
http::Response response;
try {
handleMultipartData(
this->_body,
@ -121,15 +122,15 @@ Response Post::execute(void) {
response.setProtocol(this->_protocol);
response.setStatusCode(200);
response.addHeader("Content-Type", "text/html");
response.setBody(
http::Errors::getResponseBody(response.getStatusCode()));
response.setBody(http::Errors::getResponseBody(
response.getStatusCode(),
_srv->getErrorPage(response.getStatusCode())));
} catch (...) {
response.setProtocol(this->_protocol);
response.setStatusCode(500);
response.addHeader("Content-Type", "text/html");
response.setBody(
http::Errors::getResponseBody(response.getStatusCode()));
response.setBody(http::Errors::getResponseBody(
response.getStatusCode(),
_srv->getErrorPage(response.getStatusCode())));
}
return (response);
}

View File

@ -6,7 +6,7 @@
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/17 11:12:41 by mmoussou #+# #+# */
/* Updated: 2025/05/28 10:58:11 by adjoly ### ########.fr */
/* Updated: 2025/05/28 11:30:11 by adjoly ### ########.fr */
/* */
/* ************************************************************************** */
@ -106,7 +106,7 @@ void Client::_getRequest(std::string request_str) {
str << _request->getTarget();
_log->info(str.str());
} else if (method == "DELETE") {
_request = new http::Delete(request_str);
_request = new http::Delete(request_str, _conf);
_log->info("delete request received");
} else if (method == "POST") {
_request = new http::Post(request_str, _conf);