From b7f8947440f9143386e5c1cf5772c7a99bc70d09 Mon Sep 17 00:00:00 2001 From: whaffman Date: Tue, 30 Sep 2025 10:41:53 +0200 Subject: [PATCH] feat: add custom 400 error page and update configuration for error handling --- 400.html | 73 ++++++++++++++++++++++++++++++++ config/default.conf | 3 ++ webserv/config/ConfigManager.cpp | 9 ++++ webserv/handler/Errorhandler.cpp | 4 ++ 4 files changed, 89 insertions(+) create mode 100644 400.html diff --git a/400.html b/400.html new file mode 100644 index 0000000..1dd4625 --- /dev/null +++ b/400.html @@ -0,0 +1,73 @@ + + + + + 400 Bad Request + + + + +
+
400
+
Oops! Bad Request.
Your browser sent a request that this server could not understand.
+ Go Home +
+ + \ No newline at end of file diff --git a/config/default.conf b/config/default.conf index 93fd93b..25e0f23 100644 --- a/config/default.conf +++ b/config/default.conf @@ -1,4 +1,7 @@ autoindex on +error_page 400 ./400.html + + server { listen 8080; diff --git a/webserv/config/ConfigManager.cpp b/webserv/config/ConfigManager.cpp index 4c8beb9..9124fed 100644 --- a/webserv/config/ConfigManager.cpp +++ b/webserv/config/ConfigManager.cpp @@ -95,4 +95,13 @@ ServerConfig *ConfigManager::getMatchingServerConfig(const std::string &host, in } Log::warning("No matching server config found for host: " + host + " and port: " + std::to_string(port)); return nullptr; +} + +GlobalConfig *ConfigManager::getGlobalConfig() const +{ + if (!initialized_) + { + throw std::runtime_error("ConfigManager is not initialized."); + } + return globalConfig_.get(); } \ No newline at end of file diff --git a/webserv/handler/Errorhandler.cpp b/webserv/handler/Errorhandler.cpp index cbb0cae..c97ff61 100644 --- a/webserv/handler/Errorhandler.cpp +++ b/webserv/handler/Errorhandler.cpp @@ -1,3 +1,4 @@ +#include "webserv/config/ConfigManager.hpp" #include #include #include // for StatusCode @@ -30,11 +31,14 @@ std::string ErrorHandler::generateErrorPage(int statusCode, AConfig *config) if (config != nullptr) { + config = ConfigManager::getInstance().getGlobalConfig(); + Log::info("Checking for custom error page for status code: " + std::to_string(statusCode)); std::string customPage = config->getErrorPage(statusCode); if (!customPage.empty()) { return getErrorPageFile(customPage); } + Log::warning("No custom error page foundin config for status code: " + std::to_string(statusCode)); } return generateDefaultErrorPage(statusCode); }