feat: add autoindex location configuration and enhance URI handling for directory paths
This commit is contained in:
parent
0f9a1b0029
commit
5a2d801992
@ -59,6 +59,13 @@ server {
|
|||||||
allowed_methods GET POST;
|
allowed_methods GET POST;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
location /autoindex {
|
||||||
|
root ./htdocs/site-1;
|
||||||
|
autoindex on;
|
||||||
|
allowed_methods GET;
|
||||||
|
index index2.html;
|
||||||
|
}
|
||||||
|
|
||||||
# cgi_enabled yes;
|
# cgi_enabled yes;
|
||||||
# cgi_ext .php /usr/bin/php-cgi;
|
# cgi_ext .php /usr/bin/php-cgi;
|
||||||
}
|
}
|
||||||
|
|||||||
1
htdocs/site-1/folder1/testfile.txt
Normal file
1
htdocs/site-1/folder1/testfile.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
test
|
||||||
@ -74,7 +74,7 @@ void FileHandler::handleDirectory(const std::string &dirpath, ResourceType type)
|
|||||||
Log::debug("Requested path is a directory: " + dirpath);
|
Log::debug("Requested path is a directory: " + dirpath);
|
||||||
// ErrorHandler::createErrorResponse(Http::StatusCode::FORBIDDEN, response_, config_);
|
// 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)
|
// 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);
|
response_.setStatus(Http::StatusCode::OK);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -107,6 +107,7 @@ FileHandler::ResourceType FileHandler::getResourceType(const std::string &path)
|
|||||||
{
|
{
|
||||||
static_cast<void>(path);
|
static_cast<void>(path);
|
||||||
Log::trace(LOCATION);
|
Log::trace(LOCATION);
|
||||||
|
Log::debug("Determining resource type for path: " + path);
|
||||||
|
|
||||||
if (uri_.isFile())
|
if (uri_.isFile())
|
||||||
{
|
{
|
||||||
@ -114,10 +115,10 @@ FileHandler::ResourceType FileHandler::getResourceType(const std::string &path)
|
|||||||
}
|
}
|
||||||
if (uri_.isDirectory())
|
if (uri_.isDirectory())
|
||||||
{
|
{
|
||||||
if (config_->get<std::vector<std::string>>("index").has_value())
|
// if (config_->get<std::vector<std::string>>("index").has_value())
|
||||||
{
|
// {
|
||||||
return DIRECTORY_INDEX;
|
// return DIRECTORY_INDEX;
|
||||||
}
|
// }
|
||||||
if (config_->get<bool>("autoindex").value_or(false))
|
if (config_->get<bool>("autoindex").value_or(false))
|
||||||
{
|
{
|
||||||
return DIRECTORY_AUTOINDEX;
|
return DIRECTORY_AUTOINDEX;
|
||||||
|
|||||||
@ -22,9 +22,6 @@ URI::URI(const HttpRequest &request, const ServerConfig &serverConfig)
|
|||||||
parseFullpath();
|
parseFullpath();
|
||||||
|
|
||||||
authority_ = request.getHeaders().getHost().value();
|
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)
|
const AConfig *URI::matchConfig(const std::string &uri, const ServerConfig &serverConfig)
|
||||||
@ -217,6 +214,34 @@ std::string URI::getCgiPath() const
|
|||||||
return cgiPath;
|
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
|
const std::string &URI::getBaseName() const noexcept
|
||||||
{
|
{
|
||||||
return baseName_;
|
return baseName_;
|
||||||
|
|||||||
@ -26,6 +26,7 @@ class URI
|
|||||||
[[nodiscard]] std::string getExtension() const noexcept;
|
[[nodiscard]] std::string getExtension() const noexcept;
|
||||||
[[nodiscard]] std::string getCgiPath() const;
|
[[nodiscard]] std::string getCgiPath() const;
|
||||||
[[nodiscard]] std::pair<int, std::string> getRedirect() 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 AConfig *getConfig() const noexcept;
|
||||||
[[nodiscard]] const std::string &getBaseName() const noexcept;
|
[[nodiscard]] const std::string &getBaseName() const noexcept;
|
||||||
|
|||||||
@ -49,7 +49,7 @@ class Log
|
|||||||
|
|
||||||
void log(Level level, const std::string &message, const std::map<std::string, std::string> &context);
|
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 setFileChannel(const std::string &filename);
|
||||||
static void setStdoutChannel();
|
static void setStdoutChannel();
|
||||||
|
|||||||
@ -1,11 +1,13 @@
|
|||||||
|
#include "webserv/config/AConfig.hpp"
|
||||||
#include <webserv/utils/AutoIndex.hpp>
|
#include <webserv/utils/AutoIndex.hpp>
|
||||||
#include <webserv/utils/FileUtils.hpp>
|
#include <webserv/utils/FileUtils.hpp>
|
||||||
|
#include <webserv/handler/URI.hpp>
|
||||||
|
|
||||||
#include <format>
|
#include <format>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
std::string AutoIndex::generate(const std::string &dir)
|
std::string AutoIndex::generate(const std::string &dir, const URI &uri)
|
||||||
{
|
{
|
||||||
std::ostringstream html;
|
std::ostringstream html;
|
||||||
|
|
||||||
@ -37,7 +39,7 @@ std::string AutoIndex::generate(const std::string &dir)
|
|||||||
|
|
||||||
for (const auto &entry : entries)
|
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())
|
if (entry.is_directory())
|
||||||
{
|
{
|
||||||
href += "/";
|
href += "/";
|
||||||
|
|||||||
@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <webserv/handler/URI.hpp> // for URI
|
||||||
|
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@ -12,7 +14,7 @@ class AutoIndex
|
|||||||
AutoIndex(AutoIndex &&) = delete;
|
AutoIndex(AutoIndex &&) = delete;
|
||||||
AutoIndex &operator=(AutoIndex &&) = delete;
|
AutoIndex &operator=(AutoIndex &&) = delete;
|
||||||
~AutoIndex() = delete;
|
~AutoIndex() = delete;
|
||||||
static std::string generate(const std::string &dir);
|
static std::string generate(const std::string &dir, const URI &uri);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static std::string formatFileSize(uintmax_t size);
|
static std::string formatFileSize(uintmax_t size);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user