feat: add autoindex location configuration and enhance URI handling for directory paths

This commit is contained in:
whaffman 2025-10-29 11:58:06 +01:00
parent 0f9a1b0029
commit 5a2d801992
8 changed files with 51 additions and 12 deletions

View File

@ -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;
}

View File

@ -0,0 +1 @@
test

View File

@ -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<void>(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<std::vector<std::string>>("index").has_value())
{
return DIRECTORY_INDEX;
}
// if (config_->get<std::vector<std::string>>("index").has_value())
// {
// return DIRECTORY_INDEX;
// }
if (config_->get<bool>("autoindex").value_or(false))
{
return DIRECTORY_AUTOINDEX;

View File

@ -22,9 +22,6 @@ URI::URI(const HttpRequest &request, const ServerConfig &serverConfig)
parseFullpath();
authority_ = request.getHeaders().getHost().value();
authority_ += (serverConfig.get<int>("listen") != 80) // NOFORMAT
? ":" + std::to_string(serverConfig.get<int>("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<const LocationConfig *>(config_);
if (locConfig != nullptr)
{
trimmedDir_ = utils::trim(locConfig->getPath(), "/");
}
std::string trimmedRoot_ = utils::trim(config_->get<std::string>("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_;

View File

@ -26,6 +26,7 @@ class URI
[[nodiscard]] std::string getExtension() const noexcept;
[[nodiscard]] std::string getCgiPath() const;
[[nodiscard]] std::pair<int, std::string> getRedirect() const;
[[nodiscard]] std::string getUriForPath(const std::string &path) const;
[[nodiscard]] const AConfig *getConfig() const noexcept;
[[nodiscard]] const std::string &getBaseName() const noexcept;

View File

@ -49,7 +49,7 @@ class Log
void log(Level level, const std::string &message, const std::map<std::string, std::string> &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();

View File

@ -1,11 +1,13 @@
#include "webserv/config/AConfig.hpp"
#include <webserv/utils/AutoIndex.hpp>
#include <webserv/utils/FileUtils.hpp>
#include <webserv/handler/URI.hpp>
#include <format>
#include <string>
#include <vector>
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 += "/";

View File

@ -1,5 +1,7 @@
#pragma once
#include <webserv/handler/URI.hpp> // for URI
#include <cstdint>
#include <string>
@ -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);