diff --git a/includes/log.hpp b/includes/log.hpp index 6d44ca4..118b9ee 100644 --- a/includes/log.hpp +++ b/includes/log.hpp @@ -6,7 +6,7 @@ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/includes/requests/Errors.hpp b/includes/requests/Errors.hpp index 049343c..c404e09 100644 --- a/includes/requests/Errors.hpp +++ b/includes/requests/Errors.hpp @@ -1,3 +1,4 @@ + /* ************************************************************************** */ /* */ /* ::: :::::::: */ @@ -6,7 +7,7 @@ /* By: mmoussou ); + static std::string getResponseBody(int error_code, std::string err_file); + // static void setEntries(const std::map); static std::map message; diff --git a/includes/requests/RequestImplement.hpp b/includes/requests/RequestImplement.hpp index f304b58..e652224 100644 --- a/includes/requests/RequestImplement.hpp +++ b/includes/requests/RequestImplement.hpp @@ -6,7 +6,7 @@ /* 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 { public: Delete(void) {} - Delete(std::string &data); + Delete(std::string &data, config::Server *srv); void parse(std::string const &data); diff --git a/not_found.html b/not_found.html new file mode 100644 index 0000000..6911a4a --- /dev/null +++ b/not_found.html @@ -0,0 +1,13 @@ + + + + + + Not found D: + + +

The page you requested was not found. Here is an image of Kanel instead

+ + + + diff --git a/src/config/Server.cpp b/src/config/Server.cpp index ca3d87e..26883ac 100644 --- a/src/config/Server.cpp +++ b/src/config/Server.cpp @@ -6,7 +6,7 @@ /* 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 #include +#include #include #include @@ -103,16 +104,15 @@ Server::~Server(void) { std::map * Server::_parseErrPages(std::map *table) { std::map *errPages = new std::map; - void *val; int nb; for (std::map::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(val); + (*errPages)[nb] = + *static_cast(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::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; diff --git a/src/requests_handling/Errors.cpp b/src/requests_handling/Errors.cpp index f61c1aa..f7e20f0 100644 --- a/src/requests_handling/Errors.cpp +++ b/src/requests_handling/Errors.cpp @@ -6,39 +6,55 @@ /* By: mmoussou +#include +#include #include +#include +#include using namespace webserv; using namespace http; -void Errors::setEntries(const std::map error_pages) -{ - for (std::map::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 error_pages) +// { +// for (std::map::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 ("

" + Errors::message[error_code] + + "

"); + } + 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 ("

" + Errors::message[error_code] + + "

"); + } } -std::string Errors::getResponseBody(int error_code) -{ - std::string body; +std::map Errors::message = Errors::populateMessages(); +std::map 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("

" + Errors::message[error_code] + "

"); -} - -std::map Errors::message = Errors::populateMessages(); -std::map Errors::set_error_pages; - -std::map Errors::populateMessages() -{ +std::map Errors::populateMessages() { std::map m; m[100] = "Continue"; diff --git a/src/requests_handling/requestImplementation/Delete.cpp b/src/requests_handling/requestImplementation/Delete.cpp index 2e25981..312e9a5 100644 --- a/src/requests_handling/requestImplementation/Delete.cpp +++ b/src/requests_handling/requestImplementation/Delete.cpp @@ -6,7 +6,7 @@ /* 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; -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); diff --git a/src/requests_handling/requestImplementation/Get.cpp b/src/requests_handling/requestImplementation/Get.cpp index 647d874..82ef408 100644 --- a/src/requests_handling/requestImplementation/Get.cpp +++ b/src/requests_handling/requestImplementation/Get.cpp @@ -6,7 +6,7 @@ /* 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") { 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; diff --git a/src/requests_handling/requestImplementation/Post.cpp b/src/requests_handling/requestImplementation/Post.cpp index 0068b32..ecf32a1 100644 --- a/src/requests_handling/requestImplementation/Post.cpp +++ b/src/requests_handling/requestImplementation/Post.cpp @@ -6,7 +6,7 @@ /* 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 #include -#include #include +#include 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); } - diff --git a/src/server/Client.cpp b/src/server/Client.cpp index eb3a859..fc433b8 100644 --- a/src/server/Client.cpp +++ b/src/server/Client.cpp @@ -6,7 +6,7 @@ /* By: mmoussou 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);