This commit is contained in:
whaffman 2025-09-30 10:44:40 +02:00
parent b7f8947440
commit ab2db513c0
13 changed files with 70 additions and 69 deletions

View File

@ -1,21 +1,19 @@
#include "webserv/config/ConfigManager.hpp"
#include "webserv/handler/ErrorHandler.hpp"
#include <webserv/client/Client.hpp>
#include <webserv/http/HttpHeaders.hpp> // for HttpHeaders
#include <webserv/log/Log.hpp> // for Log, LOCATION
#include <webserv/server/Server.hpp> // for Server
#include <webserv/socket/Socket.hpp> // for Socket
#include <webserv/config/ConfigManager.hpp> // for ConfigManager
#include <webserv/config/ServerConfig.hpp> // for ServerConfig
#include <webserv/handler/ErrorHandler.hpp> // for ErrorHandler
#include <webserv/http/HttpHeaders.hpp> // for HttpHeaders
#include <webserv/log/Log.hpp> // for Log, LOCATION
#include <webserv/server/Server.hpp> // for Server
#include <webserv/socket/Socket.hpp> // for Socket
#include <cstdint> // for uint8_t
#include <functional> // for reference_wrapper, cref, ref
#include <functional> // for ref, reference_wrapper
#include <map> // for map
#include <utility> // for pair, move
#include <sys/types.h> // for ssize_t
class ServerConfig;
Client::Client(std::unique_ptr<Socket> socket, Server &server)
: client_socket_(std::move(socket)), server_(std::ref(server)), httpRequest_(std::make_unique<HttpRequest>(this))
{

View File

@ -1,7 +1,7 @@
#pragma once
#include <webserv/config/ServerConfig.hpp> // for ServerConfig
#include <webserv/http/HttpConstants.hpp> // for StatusCode, HTTP
#include <webserv/http/HttpConstants.hpp> // for OK
#include <webserv/http/HttpRequest.hpp> // for HttpRequest
#include <webserv/server/Server.hpp>
#include <webserv/socket/Socket.hpp>

View File

@ -20,8 +20,10 @@ void AConfig::addDirective(const std::string &line)
const ADirective *AConfig::getDirective(const std::string &name) const
{
for (const auto &directive : directives_) {
if (directive->getName() == name) {
for (const auto &directive : directives_)
{
if (directive->getName() == name)
{
return directive.get();
}
}
@ -34,8 +36,10 @@ const ADirective *AConfig::getDirective(const std::string &name) const
bool AConfig::hasDirective(const std::string &name) const
{
for (const auto &directive : directives_) {
if (directive->getName() == name) {
for (const auto &directive : directives_)
{
if (directive->getName() == name)
{
return true;
}
}

View File

@ -5,7 +5,8 @@
#include <map> // for map
#include <memory> // for unique_ptr
#include <string> // for basic_string, string
#include <string> // for string
#include <vector> // for vector
class AConfig
{
@ -21,7 +22,7 @@ class AConfig
void addDirective(const std::string &line);
[[nodiscard]] const ADirective *getDirective(const std::string &name) const;
[[nodiscard]] std::string getErrorPage(int statusCode) const;
[[nodiscard]] std::string getErrorPage(int statusCode) const;
[[nodiscard]] bool hasDirective(const std::string &name) const;

View File

@ -1,4 +1,3 @@
#include "webserv/config/ServerConfig.hpp"
#include <webserv/config/ConfigManager.hpp>
#include <webserv/config/GlobalConfig.hpp> // for GlobalConfig
#include <webserv/config/utils.hpp> // for removeComments
@ -7,7 +6,9 @@
#include <fstream> // for basic_ifstream, basic_filebuf, basic_ostream::operator<<, ifstream, stringstream
#include <sstream> // for basic_stringstream
#include <stdexcept> // for runtime_error
#include <string> // for char_traits, operator+, basic_string, string
#include <string> // for basic_string, char_traits, operator+, string, to_string, operator==, stoi
#include <stddef.h> // for size_t
ConfigManager::ConfigManager() : initialized_(false) {}
@ -82,19 +83,19 @@ ServerConfig *ConfigManager::getMatchingServerConfig(const std::string &host, in
throw std::runtime_error("ConfigManager is not initialized.");
}
std::vector<ServerConfig *> serverConfigs = globalConfig_->getServerConfigs();
for (ServerConfig *serverConfig : serverConfigs)
{
auto serverName = serverConfig->getDirectiveValue<std::string>("server_name", "");
auto listenPorts = serverConfig->getDirectiveValue<int>("listen", 80);
for (ServerConfig *serverConfig : serverConfigs)
{
auto serverName = serverConfig->getDirectiveValue<std::string>("server_name", "");
auto listenPorts = serverConfig->getDirectiveValue<int>("listen", 80);
Log::debug("Checking server config: " + serverName + " on port " + std::to_string(listenPorts));
if ((serverName == host) && (listenPorts == port))
{
Log::info("Found matching server config for host: " + host + " and port: " + std::to_string(port));
return serverConfig;
}
}
if ((serverName == host) && (listenPorts == port))
{
Log::info("Found matching server config for host: " + host + " and port: " + std::to_string(port));
return serverConfig;
}
}
Log::warning("No matching server config found for host: " + host + " and port: " + std::to_string(port));
return nullptr;
return nullptr;
}
GlobalConfig *ConfigManager::getGlobalConfig() const

View File

@ -32,7 +32,8 @@ void ServerConfig::parseBlock(const std::string &block)
directives += block.substr(pos);
break;
}
std::string locationPath = utils::trim(block.substr(locationPos + 9, bracePos - (locationPos + 9))); //TODO magic numbers
std::string locationPath =
utils::trim(block.substr(locationPos + 9, bracePos - (locationPos + 9))); // TODO magic numbers
// Add global declarations before this server block
directives += block.substr(pos, locationPos - pos);
size_t closeBrace = utils::findCorrespondingClosingBrace(block, bracePos);

View File

@ -34,11 +34,11 @@ class ADirective
{
return getValue().get<T>();
}
return T(); //TODO: does this work for all types?
return T(); // TODO: does this work for all types?
}
protected:
std::string name_; //NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
std::string name_; // NOLINT(cppcoreguidelines-non-private-member-variables-in-classes)
};
// Non-member stream operator for ADirective

View File

@ -1,6 +1,7 @@
#pragma once
#include "webserv/config/AConfig.hpp"
#include <webserv/config/AConfig.hpp>
class ErrorHandler
{
public:
@ -11,5 +12,4 @@ class ErrorHandler
static std::string_view getStatusMessage(int statusCode);
// static bool isValidStatusCode(int statusCode);
static std::string getErrorPageFile(const std::string &path);
};

View File

@ -1,12 +1,14 @@
#include "webserv/config/ConfigManager.hpp"
#include <webserv/config/AConfig.hpp>
#include <webserv/handler/ErrorHandler.hpp>
#include <webserv/http/HttpConstants.hpp> // for StatusCode
#include <webserv/log/Log.hpp>
#include <webserv/config/AConfig.hpp> // for AConfig
#include <webserv/config/ConfigManager.hpp> // for ConfigManager
#include <webserv/config/GlobalConfig.hpp> // for GlobalConfig
#include <webserv/handler/ErrorHandler.hpp> // for ErrorHandler
#include <webserv/http/HttpConstants.hpp> // for StatusCodeInfo, CRLF, DOUBLE_CRLF, INTERNAL_SERVER_ERROR, statusCodeInfos
#include <webserv/log/Log.hpp> // for Log
#include <array> // for array
#include <fstream> // for basic_ifstream, basic_filebuf, basic_ostream::operator<<, ifstream, stringstream
#include <sstream> // for basic_stringstream
#include <string> // for basic_string, to_string, string
#include <string> // for basic_string, operator+, allocator, char_traits, string, to_string
#include <string_view> // for string_view
std::string ErrorHandler::getErrorResponse(int statusCode, AConfig *config)
@ -22,7 +24,8 @@ std::string ErrorHandler::generateErrorHeader(int statusCode, const std::string
response += std::to_string(statusCode) + " ";
response += std::string(getStatusMessage(statusCode)) + std::string(Http::Protocol::CRLF);
response += "Content-Type: text/html" + std::string(Http::Protocol::CRLF);
response += "Content-Length: " + std::to_string(body.size()) + std::string(Http::Protocol::DOUBLE_CRLF); // End of headers
response +=
"Content-Length: " + std::to_string(body.size()) + std::string(Http::Protocol::DOUBLE_CRLF); // End of headers
return response;
}
@ -32,7 +35,7 @@ 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));
Log::info("Checking for custom error page for status code: " + std::to_string(statusCode));
std::string customPage = config->getErrorPage(statusCode);
if (!customPage.empty())
{

View File

@ -1,8 +1,8 @@
#pragma once
#include <array>
#include <cstdint>
#include <string_view>
#include <array>
namespace Http
{
@ -49,20 +49,19 @@ struct StatusCodeInfo
std::string_view reason;
};
static constexpr std::array<StatusCodeInfo, 12> statusCodeInfos = {{
{ .code = StatusCode::OK, .reason = "OK" },
{ .code = StatusCode::CREATED, .reason = "Created" },
{ .code = StatusCode::NO_CONTENT, .reason = "No Content" },
{ .code = StatusCode::BAD_REQUEST, .reason = "Bad Request" },
{ .code = StatusCode::UNAUTHORIZED, .reason = "Unauthorized" },
{ .code = StatusCode::FORBIDDEN, .reason = "Forbidden" },
{ .code = StatusCode::NOT_FOUND, .reason = "Not Found" },
{ .code = StatusCode::METHOD_NOT_ALLOWED, .reason = "Method Not Allowed" },
{ .code = StatusCode::INTERNAL_SERVER_ERROR, .reason = "Internal Server Error" },
{ .code = StatusCode::NOT_IMPLEMENTED, .reason = "Not Implemented" },
{ .code = StatusCode::BAD_GATEWAY, .reason = "Bad Gateway" },
{ .code = StatusCode::SERVICE_UNAVAILABLE, .reason = "Service Unavailable" }
}};
static constexpr std::array<StatusCodeInfo, 12> statusCodeInfos = {
{{.code = StatusCode::OK, .reason = "OK"},
{.code = StatusCode::CREATED, .reason = "Created"},
{.code = StatusCode::NO_CONTENT, .reason = "No Content"},
{.code = StatusCode::BAD_REQUEST, .reason = "Bad Request"},
{.code = StatusCode::UNAUTHORIZED, .reason = "Unauthorized"},
{.code = StatusCode::FORBIDDEN, .reason = "Forbidden"},
{.code = StatusCode::NOT_FOUND, .reason = "Not Found"},
{.code = StatusCode::METHOD_NOT_ALLOWED, .reason = "Method Not Allowed"},
{.code = StatusCode::INTERNAL_SERVER_ERROR, .reason = "Internal Server Error"},
{.code = StatusCode::NOT_IMPLEMENTED, .reason = "Not Implemented"},
{.code = StatusCode::BAD_GATEWAY, .reason = "Bad Gateway"},
{.code = StatusCode::SERVICE_UNAVAILABLE, .reason = "Service Unavailable"}}};
// Header Names
namespace Header

View File

@ -1,9 +1,8 @@
#include <webserv/client/Client.hpp> // for Client
#include <webserv/config/utils.hpp> // for stoul
#include <webserv/http/HttpConstants.hpp> // for CRLF, DOUBLE_CRLF
#include <webserv/http/HttpConstants.hpp> // for CRLF, DOUBLE_CRLF, BAD_REQUEST
#include <webserv/http/HttpRequest.hpp>
#include <webserv/log/Log.hpp> // for Log, LOCATION
#include <webserv/client/Client.hpp> // for Client
#include <webserv/log/Log.hpp> // for Log, LOCATION
#include <map> // for map
#include <optional> // for optional
@ -11,8 +10,6 @@
#include <utility> // for pair
#include <vector> // for vector
class ServerConfig;
HttpRequest::HttpRequest(Client *client) : client_(client)
{
Log::trace(LOCATION);

View File

@ -5,6 +5,7 @@
#include <iostream> // for basic_ostream, operator<<, cerr, ios_base
#include <map> // for map
#include <string> // for basic_string, char_traits, allocator, operator+, operator<=>
#include <utility> // for pair
int main(int argc, char **argv)
{
@ -22,7 +23,6 @@ int main(int argc, char **argv)
ConfigManager &configManager = ConfigManager::getInstance();
Log::info("ConfigManager initialized successfully.");
// auto serverConfigs = configManager.getServerConfigs();
// auto *firstServer = serverConfigs[0];
// const auto *location = firstServer->getLocation("/");
@ -30,8 +30,6 @@ int main(int argc, char **argv)
// int listenPort = listenDirective->getValueAs<int>();
// Log::warning("Listen port for '/' location: " + std::to_string(listenPort));
Server server(configManager);
server.start();

View File

@ -112,8 +112,7 @@ void Server::handleConnection(struct epoll_event *event)
Socket &listener = getListener(event->data.fd);
std::unique_ptr<Socket> clientSocket = listener.accept();
addToEpoll(*clientSocket, EPOLLIN);
clients_.insert(
{clientSocket->getFd(), std::make_unique<Client>(std::move(clientSocket), *this)});
clients_.insert({clientSocket->getFd(), std::make_unique<Client>(std::move(clientSocket), *this)});
}
Socket &Server::getListener(int fd) const