mirror of
https://github.com/KeyZox71/webserv.git
synced 2025-06-25 09:33:36 +02:00
「✨」 feat: added error page implementation
This commit is contained in:
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/20 09:28:27 by adjoly #+# #+# */
|
/* 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 */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
@ -6,7 +7,7 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/16 17:51:46 by mmoussou #+# #+# */
|
/* 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 {
|
class Errors {
|
||||||
public:
|
public:
|
||||||
static std::string getResponseBody(int error_code);
|
static std::string getResponseBody(int error_code, std::string err_file);
|
||||||
static void setEntries(const std::map<int, std::string>);
|
// static void setEntries(const std::map<int, std::string>);
|
||||||
|
|
||||||
static std::map<int, std::string> message;
|
static std::map<int, std::string> message;
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/30 09:30:15 by adjoly #+# #+# */
|
/* 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 {
|
class Delete : public ARequest {
|
||||||
public:
|
public:
|
||||||
Delete(void) {}
|
Delete(void) {}
|
||||||
Delete(std::string &data);
|
Delete(std::string &data, config::Server *srv);
|
||||||
|
|
||||||
void parse(std::string const &data);
|
void parse(std::string const &data);
|
||||||
|
|
||||||
|
13
not_found.html
Normal file
13
not_found.html
Normal 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>
|
||||||
|
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/24 15:10:07 by adjoly #+# #+# */
|
/* 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 "log.hpp"
|
||||||
#include <config/default.hpp>
|
#include <config/default.hpp>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
|
#include <sstream>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <webserv.hpp>
|
#include <webserv.hpp>
|
||||||
|
|
||||||
@ -103,16 +104,15 @@ Server::~Server(void) {
|
|||||||
std::map<int, std::string> *
|
std::map<int, std::string> *
|
||||||
Server::_parseErrPages(std::map<std::string, toml::ANode *> *table) {
|
Server::_parseErrPages(std::map<std::string, toml::ANode *> *table) {
|
||||||
std::map<int, std::string> *errPages = new std::map<int, std::string>;
|
std::map<int, std::string> *errPages = new std::map<int, std::string>;
|
||||||
void *val;
|
|
||||||
int nb;
|
int nb;
|
||||||
|
|
||||||
for (std::map<std::string, toml::ANode *>::iterator it = table->begin();
|
for (std::map<std::string, toml::ANode *>::iterator it = table->begin();
|
||||||
it != table->end(); it++) {
|
it != table->end(); it++) {
|
||||||
val = accessValue(it->first, toml::STRING, _table, _log);
|
if (it->second->type() == toml::STRING) {
|
||||||
if (val != not_nullptr) {
|
|
||||||
nb = std::atoi(it->first.c_str());
|
nb = std::atoi(it->first.c_str());
|
||||||
if (nb >= 400 && nb <= 599)
|
if (nb >= 400 && nb <= 599)
|
||||||
(*errPages)[nb] = *static_cast<std::string *>(val);
|
(*errPages)[nb] =
|
||||||
|
*static_cast<std::string *>(it->second->getValue());
|
||||||
else
|
else
|
||||||
_log->warn("error page - " + it->first + " is not valid :(");
|
_log->warn("error page - " + it->first + " is not valid :(");
|
||||||
}
|
}
|
||||||
@ -137,7 +137,6 @@ Route *Server::whatRoute(const URL &url) {
|
|||||||
if (_routes == not_nullptr)
|
if (_routes == not_nullptr)
|
||||||
return not_nullptr;
|
return not_nullptr;
|
||||||
|
|
||||||
|
|
||||||
for (auto it = prange(_routes)) {
|
for (auto it = prange(_routes)) {
|
||||||
if (i < it->first.countMatchingSegments(url)) {
|
if (i < it->first.countMatchingSegments(url)) {
|
||||||
ret = it;
|
ret = it;
|
||||||
|
@ -6,39 +6,55 @@
|
|||||||
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
/* By: mmoussou <mmoussou@student.42angouleme.fr +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/03/17 14:08:12 by mmoussou #+# #+# */
|
/* 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 <requests/Errors.hpp>
|
||||||
|
#include <sstream>
|
||||||
|
#include <unistd.h>
|
||||||
|
|
||||||
using namespace webserv;
|
using namespace webserv;
|
||||||
using namespace http;
|
using namespace http;
|
||||||
|
|
||||||
void Errors::setEntries(const std::map<int, std::string> error_pages)
|
// 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)
|
// 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;
|
// 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::map<int, std::string> Errors::message = Errors::populateMessages();
|
||||||
{
|
std::map<int, std::string> Errors::set_error_pages;
|
||||||
std::string body;
|
|
||||||
|
|
||||||
if (Errors::set_error_pages.find(error_code) != Errors::set_error_pages.end())
|
std::map<int, std::string> Errors::populateMessages() {
|
||||||
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> m;
|
std::map<int, std::string> m;
|
||||||
|
|
||||||
m[100] = "Continue";
|
m[100] = "Continue";
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/30 09:42:18 by adjoly #+# #+# */
|
/* 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;
|
using namespace webserv::http;
|
||||||
|
|
||||||
Delete::Delete(std::string &data) {
|
Delete::Delete(std::string &data, config::Server *srv) {
|
||||||
_url = not_nullptr;
|
_url = not_nullptr;
|
||||||
_srv = not_nullptr;
|
_srv = srv;
|
||||||
this->parse(data);
|
this->parse(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,8 +90,9 @@ Response Delete::execute(void) {
|
|||||||
response.setProtocol(this->_protocol);
|
response.setProtocol(this->_protocol);
|
||||||
response.setStatusCode(404);
|
response.setStatusCode(404);
|
||||||
response.addHeader("Content-Type", "text/html");
|
response.addHeader("Content-Type", "text/html");
|
||||||
response.setBody(
|
response.setBody(http::Errors::getResponseBody(
|
||||||
http::Errors::getResponseBody(response.getStatusCode()));
|
response.getStatusCode(),
|
||||||
|
_srv->getErrorPage(response.getStatusCode())));
|
||||||
}
|
}
|
||||||
|
|
||||||
return (response);
|
return (response);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/30 09:40:16 by adjoly #+# #+# */
|
/* 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") {
|
if (_method == "500") {
|
||||||
response.setStatusCode(500);
|
response.setStatusCode(500);
|
||||||
response.addHeader("Content-Type", "text/html");
|
response.addHeader("Content-Type", "text/html");
|
||||||
response.setBody(
|
response.setBody(http::Errors::getResponseBody(
|
||||||
http::Errors::getResponseBody(response.getStatusCode()));
|
response.getStatusCode(),
|
||||||
|
_srv->getErrorPage(response.getStatusCode())));
|
||||||
server::PfdManager::remove(_cgi->getId());
|
server::PfdManager::remove(_cgi->getId());
|
||||||
server::ResourceManager::remove(_cgi->getId());
|
server::ResourceManager::remove(_cgi->getId());
|
||||||
_cgi = not_nullptr;
|
_cgi = not_nullptr;
|
||||||
@ -277,8 +278,9 @@ body {\n\
|
|||||||
response.setProtocol(this->_protocol);
|
response.setProtocol(this->_protocol);
|
||||||
response.setStatusCode(404);
|
response.setStatusCode(404);
|
||||||
response.addHeader("Content-Type", "text/html");
|
response.addHeader("Content-Type", "text/html");
|
||||||
response.setBody(
|
response.setBody(http::Errors::getResponseBody(
|
||||||
http::Errors::getResponseBody(response.getStatusCode()));
|
response.getStatusCode(),
|
||||||
|
_srv->getErrorPage(response.getStatusCode())));
|
||||||
}
|
}
|
||||||
|
|
||||||
delete _url;
|
delete _url;
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/04/30 09:50:20 by adjoly #+# #+# */
|
/* 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 <vector>
|
||||||
|
|
||||||
#include <config/URL.hpp>
|
#include <config/URL.hpp>
|
||||||
#include <requests/default.hpp>
|
|
||||||
#include <log.hpp>
|
#include <log.hpp>
|
||||||
|
#include <requests/default.hpp>
|
||||||
|
|
||||||
using namespace webserv::http;
|
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);
|
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;
|
size_t i = 0;
|
||||||
std::string delim = "--" + boundary;
|
std::string delim = "--" + boundary;
|
||||||
delim.erase(delim.size() - 1);
|
delim.erase(delim.size() - 1);
|
||||||
@ -121,15 +122,15 @@ Response Post::execute(void) {
|
|||||||
response.setProtocol(this->_protocol);
|
response.setProtocol(this->_protocol);
|
||||||
response.setStatusCode(200);
|
response.setStatusCode(200);
|
||||||
response.addHeader("Content-Type", "text/html");
|
response.addHeader("Content-Type", "text/html");
|
||||||
response.setBody(
|
response.setBody(http::Errors::getResponseBody(
|
||||||
http::Errors::getResponseBody(response.getStatusCode()));
|
response.getStatusCode(),
|
||||||
|
_srv->getErrorPage(response.getStatusCode())));
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
response.setProtocol(this->_protocol);
|
response.setProtocol(this->_protocol);
|
||||||
response.setStatusCode(500);
|
response.setStatusCode(500);
|
||||||
response.addHeader("Content-Type", "text/html");
|
response.setBody(http::Errors::getResponseBody(
|
||||||
response.setBody(
|
response.getStatusCode(),
|
||||||
http::Errors::getResponseBody(response.getStatusCode()));
|
_srv->getErrorPage(response.getStatusCode())));
|
||||||
}
|
}
|
||||||
return (response);
|
return (response);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* 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/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();
|
str << _request->getTarget();
|
||||||
_log->info(str.str());
|
_log->info(str.str());
|
||||||
} else if (method == "DELETE") {
|
} else if (method == "DELETE") {
|
||||||
_request = new http::Delete(request_str);
|
_request = new http::Delete(request_str, _conf);
|
||||||
_log->info("delete request received");
|
_log->info("delete request received");
|
||||||
} else if (method == "POST") {
|
} else if (method == "POST") {
|
||||||
_request = new http::Post(request_str, _conf);
|
_request = new http::Post(request_str, _conf);
|
||||||
|
Reference in New Issue
Block a user