Refactor variable naming for consistency: update member variables in Client, ConfigManager, LocationConfig, ServerConfig, and Socket classes to use a consistent naming convention with trailing underscores.

This commit is contained in:
whaffman 2025-09-21 00:12:05 +02:00
parent 6e9725bb53
commit 4f47c0c1ee
10 changed files with 81 additions and 80 deletions

View File

@ -6,14 +6,14 @@
#include <iostream> #include <iostream>
Client::Client(std::unique_ptr<Socket> socket, Server &server, const ServerConfig &server_config) Client::Client(std::unique_ptr<Socket> socket, Server &server, const ServerConfig &server_config)
: client_socket_(std::move(socket)), server(std::ref(server)), server_config(std::cref(server_config)) : client_socket_(std::move(socket)), server_(std::ref(server)), server_config_(std::cref(server_config))
{ {
} }
Client::~Client() Client::~Client()
{ {
LOG_INFO("Client destructor called for fd: " + std::to_string(client_socket_->getFd())); LOG_INFO("Client destructor called for fd: " + std::to_string(client_socket_->getFd()));
server.removeFromEpoll(*client_socket_); server_.removeFromEpoll(*client_socket_);
}; };
int Client::parseHeaderforContentLength(const std::string &request) // NOLINT int Client::parseHeaderforContentLength(const std::string &request) // NOLINT
@ -62,7 +62,7 @@ void Client::request()
if (contentLength_ == -1) if (contentLength_ == -1)
{ {
LOG_INFO("Received complete request:\n" + requestBuffer_ + "\n=== HEADER FINISHED\n"); LOG_INFO("Received complete request:\n" + requestBuffer_ + "\n=== HEADER FINISHED\n");
server.responseReady(client_socket_->getFd()); server_.responseReady(client_socket_->getFd());
} }
requestBuffer_.erase(0, headerEnd + 4); requestBuffer_.erase(0, headerEnd + 4);
return; return;
@ -74,7 +74,7 @@ void Client::request()
if (content_.size() >= contentLength_) if (content_.size() >= contentLength_)
{ {
LOG_INFO("Received complete request:\n" + header_ + content_ + "\n=== FULL REQUEST FINISHED\n"); LOG_INFO("Received complete request:\n" + header_ + content_ + "\n=== FULL REQUEST FINISHED\n");
server.responseReady(client_socket_->getFd()); server_.responseReady(client_socket_->getFd());
requestBuffer_.clear(); requestBuffer_.clear();
contentLength_ = -1; contentLength_ = -1;
} }
@ -84,7 +84,7 @@ void Client::request()
std::string Client::getResponse() const std::string Client::getResponse() const
{ {
std::string response = "HTTP/1.1 200 OK\r\nContent-Length: 32\r\n\r\nHello, World!"; std::string response = "HTTP/1.1 200 OK\r\nContent-Length: 32\r\n\r\nHello, World!";
response += " Server port " + std::to_string(server_config.getPort()) + "\r\n"; response += " Server port " + std::to_string(server_config_.getPort()) + "\r\n";
LOG_DEBUG("Sending response:\n" + response); LOG_DEBUG("Sending response:\n" + response);
return response; return response;
} }

View File

@ -31,6 +31,6 @@ class Client
std::string header_; std::string header_;
std::string content_; std::string content_;
std::unique_ptr<Socket> client_socket_; std::unique_ptr<Socket> client_socket_;
const Server &server; const Server &server_;
const ServerConfig &server_config; const ServerConfig &server_config_;
}; };

View File

@ -8,7 +8,7 @@
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
ConfigManager::ConfigManager() : _initialized(false) {} ConfigManager::ConfigManager() : initialized_(false) {}
ConfigManager::~ConfigManager() {} ConfigManager::~ConfigManager() {}
@ -20,14 +20,14 @@ ConfigManager &ConfigManager::getInstance()
void ConfigManager::init(const std::string &filePath) void ConfigManager::init(const std::string &filePath)
{ {
if (_initialized) if (initialized_)
{ {
LOG_WARN("ConfigManager is already initialized."); LOG_WARN("ConfigManager is already initialized.");
throw std::runtime_error("ConfigManager is already initialized."); throw std::runtime_error("ConfigManager is already initialized.");
} }
LOG_INFO("Initializing ConfigManager with file: " + filePath); LOG_INFO("Initializing ConfigManager with file: " + filePath);
parseConfigFile(filePath); parseConfigFile(filePath);
_initialized = true; initialized_ = true;
} }
void removeEmptyLines(std::string &str) void removeEmptyLines(std::string &str)
@ -103,7 +103,7 @@ void ConfigManager::parseConfigFile(const std::string &filePath)
} }
// Optionally parse the server block here // Optionally parse the server block here
std::string serverBlock = content.substr(bracePos + 1, closeBrace - bracePos - 1); std::string serverBlock = content.substr(bracePos + 1, closeBrace - bracePos - 1);
serverConfigs.emplace_back(serverBlock); serverConfigs_.emplace_back(serverBlock);
pos = closeBrace + 1; pos = closeBrace + 1;
} }

View File

@ -5,6 +5,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
class ConfigManager class ConfigManager
{ {
public: public:
@ -15,13 +16,13 @@ class ConfigManager
void init(const std::string &filePath); void init(const std::string &filePath);
static ConfigManager &getInstance(); static ConfigManager &getInstance();
const std::vector<ServerConfig> &getServerConfigs() const { return serverConfigs; } const std::vector<ServerConfig> &getServerConfigs() const { return serverConfigs_; }
private: private:
bool _initialized; bool initialized_;
ConfigManager(); ConfigManager();
~ConfigManager(); ~ConfigManager();
std::vector<ServerConfig> serverConfigs; std::vector<ServerConfig> serverConfigs_;
void parseConfigFile(const std::string &filePath); void parseConfigFile(const std::string &filePath);
// void parseGlobalDeclarations(const std::string &declarations); // void parseGlobalDeclarations(const std::string &declarations);

View File

@ -5,7 +5,7 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
LocationConfig::LocationConfig(const std::string &locationBlock) : autoIndex(false) LocationConfig::LocationConfig(const std::string &locationBlock) : autoIndex_(false)
{ {
parseLocationBlock(locationBlock); parseLocationBlock(locationBlock);
} }
@ -29,17 +29,17 @@ void LocationConfig::parseDirectives(const std::string &declarations)
lineStream >> value; lineStream >> value;
if (directive == "autoindex") if (directive == "autoindex")
{ {
autoIndex = (value == "on"); autoIndex_ = (value == "on");
LOG_INFO("Set autoindex to " + std::string(autoIndex ? "on" : "off")); LOG_INFO("Set autoindex to " + std::string(autoIndex_ ? "on" : "off"));
} }
else if (directive == "index") else if (directive == "index")
{ {
indexFile = value; indexFile_ = value;
LOG_INFO("Set index file to " + indexFile); LOG_INFO("Set index file to " + indexFile_);
} }
else else
{ {
directives[directive] = value; directives_[directive] = value;
} }
} }
} }

View File

@ -9,10 +9,10 @@ class LocationConfig
LocationConfig(const std::string &locationBlock); LocationConfig(const std::string &locationBlock);
private: private:
std::string path; std::string path_;
bool autoIndex; bool autoIndex_;
std::string indexFile; std::string indexFile_;
std::map<std::string, std::string> directives; std::map<std::string, std::string> directives_;
void parseLocationBlock(const std::string &block); void parseLocationBlock(const std::string &block);
void parseDirectives(const std::string &declarations); void parseDirectives(const std::string &declarations);

View File

@ -7,7 +7,7 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
ServerConfig::ServerConfig(std::string const &serverBlock) : port(80) ServerConfig::ServerConfig(std::string const &serverBlock) : port_(80)
{ {
parseServerBlock(serverBlock); parseServerBlock(serverBlock);
} }
@ -44,7 +44,7 @@ void ServerConfig::parseServerBlock(const std::string &block)
// Optionally parse the server block here // Optionally parse the server block here
std::string locationBlock = block.substr(bracePos + 1, closeBrace - bracePos - 1); std::string locationBlock = block.substr(bracePos + 1, closeBrace - bracePos - 1);
LOG_INFO("Added location: " + locationPath); LOG_INFO("Added location: " + locationPath);
locations.emplace(locationPath, locationBlock); locations_.emplace(locationPath, locationBlock);
pos = closeBrace + 1; pos = closeBrace + 1;
} }
@ -69,40 +69,40 @@ void ServerConfig::parseDirectives(const std::string &declarations)
if (directive == "listen") if (directive == "listen")
{ {
port = std::stoi(value); port_ = std::stoi(value);
if (port < 1 || port > 65535) if (port_ < 1 || port_ > 65535)
{ {
throw std::runtime_error("Invalid port number: " + std::to_string(port)); throw std::runtime_error("Invalid port number: " + std::to_string(port_));
} }
LOG_INFO("Set port to " + std::to_string(port)); LOG_INFO("Set port to " + std::to_string(port_));
} }
else if (directive == "root") else if (directive == "root")
{ {
root = value; root_ = value;
LOG_INFO("Set root to " + root); LOG_INFO("Set root to " + root_);
} }
else if (directive == "host") else if (directive == "host")
{ {
host = value; host_ = value;
LOG_INFO("Set host to " + host); LOG_INFO("Set host to " + host_);
} }
else if (directive == "cgi_pass") else if (directive == "cgi_pass")
{ {
cgi_pass = value; cgi_pass_ = value;
LOG_INFO("Set cgi_pass to " + cgi_pass); LOG_INFO("Set cgi_pass to " + cgi_pass_);
} }
else if (directive == "cgi_ext") else if (directive == "cgi_ext")
{ {
cgi_ext = value; cgi_ext_ = value;
LOG_INFO("Set cgi_ext to " + cgi_ext); LOG_INFO("Set cgi_ext to " + cgi_ext_);
} }
else if (directive == "index") else if (directive == "index")
{ {
index_files.clear(); index_files_.clear();
std::string indexFile; std::string indexFile;
while (lineStream >> indexFile) while (lineStream >> indexFile)
{ {
index_files.push_back(indexFile); index_files_.push_back(indexFile);
LOG_INFO("Added index file: " + indexFile); LOG_INFO("Added index file: " + indexFile);
} }
} }
@ -111,7 +111,7 @@ void ServerConfig::parseDirectives(const std::string &declarations)
int statusCode = std::stoi(value); int statusCode = std::stoi(value);
std::string errorPagePath; std::string errorPagePath;
lineStream >> errorPagePath; lineStream >> errorPagePath;
error_page[statusCode] = errorPagePath; error_page_[statusCode] = errorPagePath;
LOG_INFO("Set error_page for status " + std::to_string(statusCode) + " to " + errorPagePath); LOG_INFO("Set error_page for status " + std::to_string(statusCode) + " to " + errorPagePath);
} }
else else
@ -124,9 +124,9 @@ void ServerConfig::parseDirectives(const std::string &declarations)
const LocationConfig &ServerConfig::getLocation(const std::string &path) const const LocationConfig &ServerConfig::getLocation(const std::string &path) const
{ {
if (locations.count(path) > 0) // NOLINT if (locations_.count(path) > 0) // NOLINT
{ {
return locations.at(path); return locations_.at(path);
} }
LOG_ERROR("Location not found: " + path); LOG_ERROR("Location not found: " + path);
throw std::runtime_error("Location not found: " + path); throw std::runtime_error("Location not found: " + path);
@ -135,8 +135,8 @@ const LocationConfig &ServerConfig::getLocation(const std::string &path) const
std::vector<std::string> ServerConfig::getLocationPaths() const std::vector<std::string> ServerConfig::getLocationPaths() const
{ {
std::vector<std::string> paths; std::vector<std::string> paths;
paths.reserve(locations.size()); paths.reserve(locations_.size());
for (const auto &pair : locations) for (const auto &pair : locations_)
{ {
paths.push_back(pair.first); paths.push_back(pair.first);
} }

View File

@ -11,29 +11,29 @@ class ServerConfig
public: public:
ServerConfig(const std::string &serverBlock); ServerConfig(const std::string &serverBlock);
[[nodiscard]] const std::string &getHost() const { return host; } [[nodiscard]] const std::string &getHost() const { return host_; }
[[nodiscard]] int getPort() const { return port; } [[nodiscard]] int getPort() const { return port_; }
[[nodiscard]] const std::string &getRoot() const { return root; } [[nodiscard]] const std::string &getRoot() const { return root_; }
[[nodiscard]] const std::string &getCgiPass() const { return cgi_pass; } [[nodiscard]] const std::string &getCgiPass() const { return cgi_pass_; }
[[nodiscard]] const std::string &getCgiExt() const { return cgi_ext; } [[nodiscard]] const std::string &getCgiExt() const { return cgi_ext_; }
[[nodiscard]] const std::map<int, std::string> &getErrorPages() const { return error_page; } [[nodiscard]] const std::map<int, std::string> &getErrorPages() const { return error_page_; }
[[nodiscard]] const std::vector<std::string> &getIndexFiles() const { return index_files; } [[nodiscard]] const std::vector<std::string> &getIndexFiles() const { return index_files_; }
[[nodiscard]] const LocationConfig &getLocation(const std::string &path) const; [[nodiscard]] const LocationConfig &getLocation(const std::string &path) const;
[[nodiscard]] std::vector<std::string> getLocationPaths() const; [[nodiscard]] std::vector<std::string> getLocationPaths() const;
void setServerFD(int fd) { server_fd = fd; } void setServerFD(int fd) { server_fd_ = fd; }
[[nodiscard]] int getServerFD() const { return server_fd; } [[nodiscard]] int getServerFD() const { return server_fd_; }
private: private:
std::string host; std::string host_;
int port; int port_;
int server_fd; int server_fd_;
std::string root; std::string root_;
std::string cgi_pass; std::string cgi_pass_;
std::string cgi_ext; std::string cgi_ext_;
std::map<int, std::string> error_page; std::map<int, std::string> error_page_;
std::vector<std::string> index_files; std::vector<std::string> index_files_;
std::map<std::string, LocationConfig> locations; std::map<std::string, LocationConfig> locations_;
void parseServerBlock(const std::string &block); void parseServerBlock(const std::string &block);
void parseDirectives(const std::string &declarations); void parseDirectives(const std::string &declarations);

View File

@ -10,27 +10,27 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <unistd.h> // For close() #include <unistd.h> // For close()
Socket::Socket() : _fd(socket(AF_INET, SOCK_STREAM, 0)) Socket::Socket() : fd_(socket(AF_INET, SOCK_STREAM, 0))
{ {
if (_fd == -1) if (fd_ == -1)
{ {
LOG_ERROR("Socket creation failed"); LOG_ERROR("Socket creation failed");
throw std::runtime_error("Socket creation failed"); throw std::runtime_error("Socket creation failed");
} }
int opt = 1; int opt = 1;
if (setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0) if (setsockopt(fd_, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
{ {
close(_fd); close(fd_);
_fd = -1; fd_ = -1;
LOG_ERROR("setsockopt failed"); LOG_ERROR("setsockopt failed");
throw std::runtime_error("setsockopt failed"); throw std::runtime_error("setsockopt failed");
} }
setNonBlocking(); setNonBlocking();
} }
Socket::Socket(int fd) : _fd(fd) // NOLINT(readability-identifier-naming) Socket::Socket(int fd) : fd_(fd) // NOLINT(readability-identifier-naming)
{ {
if (_fd == -1) if (fd_ == -1)
{ {
LOG_ERROR("Invalid file descriptor"); LOG_ERROR("Invalid file descriptor");
throw std::runtime_error("Invalid file descriptor"); throw std::runtime_error("Invalid file descriptor");
@ -40,15 +40,15 @@ Socket::Socket(int fd) : _fd(fd) // NOLINT(readability-identifier-naming)
Socket::~Socket() Socket::~Socket()
{ {
if (_fd != -1) if (fd_ != -1)
{ {
close(_fd); close(fd_);
} }
} }
void Socket::listen(int backlog) const void Socket::listen(int backlog) const
{ {
if (::listen(_fd, backlog) < 0) if (::listen(fd_, backlog) < 0)
{ {
LOG_ERROR("Listen failed"); LOG_ERROR("Listen failed");
throw std::runtime_error("Listen failed"); throw std::runtime_error("Listen failed");
@ -62,7 +62,7 @@ void Socket::bind(const std::string &host, const int port) const
address.sin_addr.s_addr = inet_addr(host.c_str()); address.sin_addr.s_addr = inet_addr(host.c_str());
address.sin_port = htons(port); address.sin_port = htons(port);
if (::bind(_fd, (struct sockaddr *)&address, sizeof(address)) < 0) // NOLINT(cppcoreguidelines-pro-type-cstyle-cast if (::bind(fd_, (struct sockaddr *)&address, sizeof(address)) < 0) // NOLINT(cppcoreguidelines-pro-type-cstyle-cast
{ {
throw std::runtime_error("Bind failed"); throw std::runtime_error("Bind failed");
} }
@ -70,7 +70,7 @@ void Socket::bind(const std::string &host, const int port) const
std::unique_ptr<Socket> Socket::accept() const std::unique_ptr<Socket> Socket::accept() const
{ {
int client_fd = ::accept(_fd, nullptr, nullptr); int client_fd = ::accept(fd_, nullptr, nullptr);
if (client_fd < 0) if (client_fd < 0)
{ {
LOG_ERROR("Accept failed"); LOG_ERROR("Accept failed");
@ -81,17 +81,17 @@ std::unique_ptr<Socket> Socket::accept() const
ssize_t Socket::recv(void *buf, size_t len) const ssize_t Socket::recv(void *buf, size_t len) const
{ {
return ::recv(_fd, buf, len, 0); return ::recv(fd_, buf, len, 0);
} }
ssize_t Socket::send(const void *buf, size_t len) const ssize_t Socket::send(const void *buf, size_t len) const
{ {
return ::send(_fd, buf, len, 0); return ::send(fd_, buf, len, 0);
} }
void Socket::setNonBlocking() const void Socket::setNonBlocking() const
{ {
if (fcntl(_fd, F_SETFL, O_NONBLOCK) < 0) if (fcntl(fd_, F_SETFL, O_NONBLOCK) < 0)
{ {
LOG_ERROR("Failed to set non-blocking mode"); LOG_ERROR("Failed to set non-blocking mode");
throw std::runtime_error("Failed to set non-blocking mode"); throw std::runtime_error("Failed to set non-blocking mode");
@ -100,5 +100,5 @@ void Socket::setNonBlocking() const
int Socket::getFd() const int Socket::getFd() const
{ {
return _fd; return fd_;
} }

View File

@ -27,5 +27,5 @@ class Socket
[[nodiscard]] int getFd() const; [[nodiscard]] int getFd() const;
private: private:
int _fd; int fd_;
}; };