From 5a2d801992427ba7ea3e6ba76bd6f872b98b1524 Mon Sep 17 00:00:00 2001 From: whaffman Date: Wed, 29 Oct 2025 11:58:06 +0100 Subject: [PATCH] feat: add autoindex location configuration and enhance URI handling for directory paths --- config/default.conf | 7 +++++++ htdocs/site-1/folder1/testfile.txt | 1 + webserv/handler/FileHandler.cpp | 11 ++++++----- webserv/handler/URI.cpp | 31 +++++++++++++++++++++++++++--- webserv/handler/URI.hpp | 1 + webserv/log/Log.hpp | 2 +- webserv/utils/AutoIndex.cpp | 6 ++++-- webserv/utils/AutoIndex.hpp | 4 +++- 8 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 htdocs/site-1/folder1/testfile.txt diff --git a/config/default.conf b/config/default.conf index b189ab0..7b71bb6 100644 --- a/config/default.conf +++ b/config/default.conf @@ -59,6 +59,13 @@ server { allowed_methods GET POST; } + location /autoindex { + root ./htdocs/site-1; + autoindex on; + allowed_methods GET; + index index2.html; + } + # cgi_enabled yes; # cgi_ext .php /usr/bin/php-cgi; } diff --git a/htdocs/site-1/folder1/testfile.txt b/htdocs/site-1/folder1/testfile.txt new file mode 100644 index 0000000..30d74d2 --- /dev/null +++ b/htdocs/site-1/folder1/testfile.txt @@ -0,0 +1 @@ +test \ No newline at end of file diff --git a/webserv/handler/FileHandler.cpp b/webserv/handler/FileHandler.cpp index 06a0245..1b17b3d 100644 --- a/webserv/handler/FileHandler.cpp +++ b/webserv/handler/FileHandler.cpp @@ -74,7 +74,7 @@ void FileHandler::handleDirectory(const std::string &dirpath, ResourceType type) Log::debug("Requested path is a directory: " + dirpath); // ErrorHandler::createErrorResponse(Http::StatusCode::FORBIDDEN, response_, config_); // TODO: This doesn't trigger for some reason :p (I Know why, i hacked the index in uri.cpp) - response_.setBody(AutoIndex::generate(dirpath)); + response_.setBody(AutoIndex::generate(dirpath, uri_)); response_.setStatus(Http::StatusCode::OK); return; } @@ -107,6 +107,7 @@ FileHandler::ResourceType FileHandler::getResourceType(const std::string &path) { static_cast(path); Log::trace(LOCATION); + Log::debug("Determining resource type for path: " + path); if (uri_.isFile()) { @@ -114,10 +115,10 @@ FileHandler::ResourceType FileHandler::getResourceType(const std::string &path) } if (uri_.isDirectory()) { - if (config_->get>("index").has_value()) - { - return DIRECTORY_INDEX; - } + // if (config_->get>("index").has_value()) + // { + // return DIRECTORY_INDEX; + // } if (config_->get("autoindex").value_or(false)) { return DIRECTORY_AUTOINDEX; diff --git a/webserv/handler/URI.cpp b/webserv/handler/URI.cpp index 85dfa82..10915ab 100644 --- a/webserv/handler/URI.cpp +++ b/webserv/handler/URI.cpp @@ -22,9 +22,6 @@ URI::URI(const HttpRequest &request, const ServerConfig &serverConfig) parseFullpath(); authority_ = request.getHeaders().getHost().value(); - authority_ += (serverConfig.get("listen") != 80) // NOFORMAT - ? ":" + std::to_string(serverConfig.get("listen").value()) - : ""; } const AConfig *URI::matchConfig(const std::string &uri, const ServerConfig &serverConfig) @@ -217,6 +214,34 @@ std::string URI::getCgiPath() const return cgiPath; } +std::string URI::getUriForPath(const std::string &path) const +{ + // TOPD not good yet zo even naar kijken + std::string trimmedPath = utils::trim(path, "/"); + std::string trimmedDir_; + + const LocationConfig *locConfig = dynamic_cast(config_); + if (locConfig != nullptr) + { + trimmedDir_ = utils::trim(locConfig->getPath(), "/"); + } + + + std::string trimmedRoot_ = utils::trim(config_->get("root").value_or(""), "/"); + + if (trimmedPath.starts_with(trimmedRoot_)) + { + trimmedPath = trimmedDir_.substr(trimmedRoot_.length()); + trimmedPath = utils::trim(trimmedPath, "/"); + } + + Log::debug("Generating URI for path", {{"path", path},{"trimmedDir", trimmedDir_}, {"trimmedPath", trimmedPath}, {"Authority", authority_}}); + std::string result = "http://" + authority_; + result = FileUtils::joinPath(result, trimmedDir_); + return FileUtils::joinPath(result, trimmedPath); +} + + const std::string &URI::getBaseName() const noexcept { return baseName_; diff --git a/webserv/handler/URI.hpp b/webserv/handler/URI.hpp index dcf02aa..c513ac1 100644 --- a/webserv/handler/URI.hpp +++ b/webserv/handler/URI.hpp @@ -26,6 +26,7 @@ class URI [[nodiscard]] std::string getExtension() const noexcept; [[nodiscard]] std::string getCgiPath() const; [[nodiscard]] std::pair getRedirect() const; + [[nodiscard]] std::string getUriForPath(const std::string &path) const; [[nodiscard]] const AConfig *getConfig() const noexcept; [[nodiscard]] const std::string &getBaseName() const noexcept; diff --git a/webserv/log/Log.hpp b/webserv/log/Log.hpp index edc1017..4d81217 100644 --- a/webserv/log/Log.hpp +++ b/webserv/log/Log.hpp @@ -49,7 +49,7 @@ class Log void log(Level level, const std::string &message, const std::map &context); - static constexpr Log::Level COMPILE_TIME_LOG_LEVEL = Log::Level::Info; + static constexpr Log::Level COMPILE_TIME_LOG_LEVEL = Log::Level::Trace; static void setFileChannel(const std::string &filename); static void setStdoutChannel(); diff --git a/webserv/utils/AutoIndex.cpp b/webserv/utils/AutoIndex.cpp index 455b4ea..3f21ebe 100644 --- a/webserv/utils/AutoIndex.cpp +++ b/webserv/utils/AutoIndex.cpp @@ -1,11 +1,13 @@ +#include "webserv/config/AConfig.hpp" #include #include +#include #include #include #include -std::string AutoIndex::generate(const std::string &dir) +std::string AutoIndex::generate(const std::string &dir, const URI &uri) { std::ostringstream html; @@ -37,7 +39,7 @@ std::string AutoIndex::generate(const std::string &dir) for (const auto &entry : entries) { - std::string href = entry.path().filename().string(); + std::string href = uri.getUriForPath(entry.path().filename().string()); if (entry.is_directory()) { href += "/"; diff --git a/webserv/utils/AutoIndex.hpp b/webserv/utils/AutoIndex.hpp index f2ffd76..9889b0f 100644 --- a/webserv/utils/AutoIndex.hpp +++ b/webserv/utils/AutoIndex.hpp @@ -1,5 +1,7 @@ #pragma once +#include // for URI + #include #include @@ -12,7 +14,7 @@ class AutoIndex AutoIndex(AutoIndex &&) = delete; AutoIndex &operator=(AutoIndex &&) = delete; ~AutoIndex() = delete; - static std::string generate(const std::string &dir); + static std::string generate(const std::string &dir, const URI &uri); private: static std::string formatFileSize(uintmax_t size);