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/client/Client.hpp>
#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/http/HttpHeaders.hpp> // for HttpHeaders
#include <webserv/log/Log.hpp> // for Log, LOCATION #include <webserv/log/Log.hpp> // for Log, LOCATION
#include <webserv/server/Server.hpp> // for Server #include <webserv/server/Server.hpp> // for Server
#include <webserv/socket/Socket.hpp> // for Socket #include <webserv/socket/Socket.hpp> // for Socket
#include <cstdint> // for uint8_t #include <cstdint> // for uint8_t
#include <functional> // for reference_wrapper, cref, ref #include <functional> // for ref, reference_wrapper
#include <map> // for map #include <map> // for map
#include <utility> // for pair, move #include <utility> // for pair, move
#include <sys/types.h> // for ssize_t #include <sys/types.h> // for ssize_t
class ServerConfig;
Client::Client(std::unique_ptr<Socket> socket, Server &server) Client::Client(std::unique_ptr<Socket> socket, Server &server)
: client_socket_(std::move(socket)), server_(std::ref(server)), httpRequest_(std::make_unique<HttpRequest>(this)) : client_socket_(std::move(socket)), server_(std::ref(server)), httpRequest_(std::make_unique<HttpRequest>(this))
{ {

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <webserv/config/ServerConfig.hpp> // for ServerConfig #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/http/HttpRequest.hpp> // for HttpRequest
#include <webserv/server/Server.hpp> #include <webserv/server/Server.hpp>
#include <webserv/socket/Socket.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 const ADirective *AConfig::getDirective(const std::string &name) const
{ {
for (const auto &directive : directives_) { for (const auto &directive : directives_)
if (directive->getName() == name) { {
if (directive->getName() == name)
{
return directive.get(); return directive.get();
} }
} }
@ -34,8 +36,10 @@ const ADirective *AConfig::getDirective(const std::string &name) const
bool AConfig::hasDirective(const std::string &name) const bool AConfig::hasDirective(const std::string &name) const
{ {
for (const auto &directive : directives_) { for (const auto &directive : directives_)
if (directive->getName() == name) { {
if (directive->getName() == name)
{
return true; return true;
} }
} }

View File

@ -5,7 +5,8 @@
#include <map> // for map #include <map> // for map
#include <memory> // for unique_ptr #include <memory> // for unique_ptr
#include <string> // for basic_string, string #include <string> // for string
#include <vector> // for vector
class AConfig class AConfig
{ {

View File

@ -1,4 +1,3 @@
#include "webserv/config/ServerConfig.hpp"
#include <webserv/config/ConfigManager.hpp> #include <webserv/config/ConfigManager.hpp>
#include <webserv/config/GlobalConfig.hpp> // for GlobalConfig #include <webserv/config/GlobalConfig.hpp> // for GlobalConfig
#include <webserv/config/utils.hpp> // for removeComments #include <webserv/config/utils.hpp> // for removeComments
@ -7,7 +6,9 @@
#include <fstream> // for basic_ifstream, basic_filebuf, basic_ostream::operator<<, ifstream, stringstream #include <fstream> // for basic_ifstream, basic_filebuf, basic_ostream::operator<<, ifstream, stringstream
#include <sstream> // for basic_stringstream #include <sstream> // for basic_stringstream
#include <stdexcept> // for runtime_error #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) {} ConfigManager::ConfigManager() : initialized_(false) {}

View File

@ -32,7 +32,8 @@ void ServerConfig::parseBlock(const std::string &block)
directives += block.substr(pos); directives += block.substr(pos);
break; 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 // Add global declarations before this server block
directives += block.substr(pos, locationPos - pos); directives += block.substr(pos, locationPos - pos);
size_t closeBrace = utils::findCorrespondingClosingBrace(block, bracePos); size_t closeBrace = utils::findCorrespondingClosingBrace(block, bracePos);

View File

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

View File

@ -1,12 +1,14 @@
#include "webserv/config/ConfigManager.hpp" #include <webserv/config/AConfig.hpp> // for AConfig
#include <webserv/config/AConfig.hpp> #include <webserv/config/ConfigManager.hpp> // for ConfigManager
#include <webserv/handler/ErrorHandler.hpp> #include <webserv/config/GlobalConfig.hpp> // for GlobalConfig
#include <webserv/http/HttpConstants.hpp> // for StatusCode #include <webserv/handler/ErrorHandler.hpp> // for ErrorHandler
#include <webserv/log/Log.hpp> #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 <fstream> // for basic_ifstream, basic_filebuf, basic_ostream::operator<<, ifstream, stringstream
#include <sstream> // for basic_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 #include <string_view> // for string_view
std::string ErrorHandler::getErrorResponse(int statusCode, AConfig *config) 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::to_string(statusCode) + " ";
response += std::string(getStatusMessage(statusCode)) + std::string(Http::Protocol::CRLF); response += std::string(getStatusMessage(statusCode)) + std::string(Http::Protocol::CRLF);
response += "Content-Type: text/html" + 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; return response;
} }

View File

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

View File

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

View File

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

View File

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