From 4f47c0c1eea44c9544db3c33b70d07fec8afed3a Mon Sep 17 00:00:00 2001 From: whaffman Date: Sun, 21 Sep 2025 00:12:05 +0200 Subject: [PATCH] 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. --- webserv/client/Client.cpp | 10 ++++---- webserv/client/Client.hpp | 4 +-- webserv/config/ConfigManager.cpp | 8 +++--- webserv/config/ConfigManager.hpp | 7 +++--- webserv/config/LocationConfig.cpp | 12 ++++----- webserv/config/LocationConfig.hpp | 8 +++--- webserv/config/ServerConfig.cpp | 42 +++++++++++++++---------------- webserv/config/ServerConfig.hpp | 36 +++++++++++++------------- webserv/socket/Socket.cpp | 32 +++++++++++------------ webserv/socket/Socket.hpp | 2 +- 10 files changed, 81 insertions(+), 80 deletions(-) diff --git a/webserv/client/Client.cpp b/webserv/client/Client.cpp index 6d5efb9..546e993 100644 --- a/webserv/client/Client.cpp +++ b/webserv/client/Client.cpp @@ -6,14 +6,14 @@ #include Client::Client(std::unique_ptr 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() { 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 @@ -62,7 +62,7 @@ void Client::request() if (contentLength_ == -1) { 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); return; @@ -74,7 +74,7 @@ void Client::request() if (content_.size() >= contentLength_) { 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(); contentLength_ = -1; } @@ -84,7 +84,7 @@ void Client::request() std::string Client::getResponse() const { 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); return response; } \ No newline at end of file diff --git a/webserv/client/Client.hpp b/webserv/client/Client.hpp index ca6fe40..8c6db95 100644 --- a/webserv/client/Client.hpp +++ b/webserv/client/Client.hpp @@ -31,6 +31,6 @@ class Client std::string header_; std::string content_; std::unique_ptr client_socket_; - const Server &server; - const ServerConfig &server_config; + const Server &server_; + const ServerConfig &server_config_; }; \ No newline at end of file diff --git a/webserv/config/ConfigManager.cpp b/webserv/config/ConfigManager.cpp index f19ba36..5bf5a92 100644 --- a/webserv/config/ConfigManager.cpp +++ b/webserv/config/ConfigManager.cpp @@ -8,7 +8,7 @@ #include #include -ConfigManager::ConfigManager() : _initialized(false) {} +ConfigManager::ConfigManager() : initialized_(false) {} ConfigManager::~ConfigManager() {} @@ -20,14 +20,14 @@ ConfigManager &ConfigManager::getInstance() void ConfigManager::init(const std::string &filePath) { - if (_initialized) + if (initialized_) { LOG_WARN("ConfigManager is already initialized."); throw std::runtime_error("ConfigManager is already initialized."); } LOG_INFO("Initializing ConfigManager with file: " + filePath); parseConfigFile(filePath); - _initialized = true; + initialized_ = true; } void removeEmptyLines(std::string &str) @@ -103,7 +103,7 @@ void ConfigManager::parseConfigFile(const std::string &filePath) } // Optionally parse the server block here std::string serverBlock = content.substr(bracePos + 1, closeBrace - bracePos - 1); - serverConfigs.emplace_back(serverBlock); + serverConfigs_.emplace_back(serverBlock); pos = closeBrace + 1; } diff --git a/webserv/config/ConfigManager.hpp b/webserv/config/ConfigManager.hpp index fb2af78..3e8cacf 100644 --- a/webserv/config/ConfigManager.hpp +++ b/webserv/config/ConfigManager.hpp @@ -5,6 +5,7 @@ #include #include + class ConfigManager { public: @@ -15,13 +16,13 @@ class ConfigManager void init(const std::string &filePath); static ConfigManager &getInstance(); - const std::vector &getServerConfigs() const { return serverConfigs; } + const std::vector &getServerConfigs() const { return serverConfigs_; } private: - bool _initialized; + bool initialized_; ConfigManager(); ~ConfigManager(); - std::vector serverConfigs; + std::vector serverConfigs_; void parseConfigFile(const std::string &filePath); // void parseGlobalDeclarations(const std::string &declarations); diff --git a/webserv/config/LocationConfig.cpp b/webserv/config/LocationConfig.cpp index 669688f..b6529b2 100644 --- a/webserv/config/LocationConfig.cpp +++ b/webserv/config/LocationConfig.cpp @@ -5,7 +5,7 @@ #include #include -LocationConfig::LocationConfig(const std::string &locationBlock) : autoIndex(false) +LocationConfig::LocationConfig(const std::string &locationBlock) : autoIndex_(false) { parseLocationBlock(locationBlock); } @@ -29,17 +29,17 @@ void LocationConfig::parseDirectives(const std::string &declarations) lineStream >> value; if (directive == "autoindex") { - autoIndex = (value == "on"); - LOG_INFO("Set autoindex to " + std::string(autoIndex ? "on" : "off")); + autoIndex_ = (value == "on"); + LOG_INFO("Set autoindex to " + std::string(autoIndex_ ? "on" : "off")); } else if (directive == "index") { - indexFile = value; - LOG_INFO("Set index file to " + indexFile); + indexFile_ = value; + LOG_INFO("Set index file to " + indexFile_); } else { - directives[directive] = value; + directives_[directive] = value; } } } diff --git a/webserv/config/LocationConfig.hpp b/webserv/config/LocationConfig.hpp index ac32485..f839e77 100644 --- a/webserv/config/LocationConfig.hpp +++ b/webserv/config/LocationConfig.hpp @@ -9,10 +9,10 @@ class LocationConfig LocationConfig(const std::string &locationBlock); private: - std::string path; - bool autoIndex; - std::string indexFile; - std::map directives; + std::string path_; + bool autoIndex_; + std::string indexFile_; + std::map directives_; void parseLocationBlock(const std::string &block); void parseDirectives(const std::string &declarations); diff --git a/webserv/config/ServerConfig.cpp b/webserv/config/ServerConfig.cpp index 2323588..4e874de 100644 --- a/webserv/config/ServerConfig.cpp +++ b/webserv/config/ServerConfig.cpp @@ -7,7 +7,7 @@ #include #include -ServerConfig::ServerConfig(std::string const &serverBlock) : port(80) +ServerConfig::ServerConfig(std::string const &serverBlock) : port_(80) { parseServerBlock(serverBlock); } @@ -44,7 +44,7 @@ void ServerConfig::parseServerBlock(const std::string &block) // Optionally parse the server block here std::string locationBlock = block.substr(bracePos + 1, closeBrace - bracePos - 1); LOG_INFO("Added location: " + locationPath); - locations.emplace(locationPath, locationBlock); + locations_.emplace(locationPath, locationBlock); pos = closeBrace + 1; } @@ -69,40 +69,40 @@ void ServerConfig::parseDirectives(const std::string &declarations) if (directive == "listen") { - port = std::stoi(value); - if (port < 1 || port > 65535) + port_ = std::stoi(value); + 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") { - root = value; - LOG_INFO("Set root to " + root); + root_ = value; + LOG_INFO("Set root to " + root_); } else if (directive == "host") { - host = value; - LOG_INFO("Set host to " + host); + host_ = value; + LOG_INFO("Set host to " + host_); } else if (directive == "cgi_pass") { - cgi_pass = value; - LOG_INFO("Set cgi_pass to " + cgi_pass); + cgi_pass_ = value; + LOG_INFO("Set cgi_pass to " + cgi_pass_); } else if (directive == "cgi_ext") { - cgi_ext = value; - LOG_INFO("Set cgi_ext to " + cgi_ext); + cgi_ext_ = value; + LOG_INFO("Set cgi_ext to " + cgi_ext_); } else if (directive == "index") { - index_files.clear(); + index_files_.clear(); std::string indexFile; while (lineStream >> indexFile) { - index_files.push_back(indexFile); + index_files_.push_back(indexFile); LOG_INFO("Added index file: " + indexFile); } } @@ -111,7 +111,7 @@ void ServerConfig::parseDirectives(const std::string &declarations) int statusCode = std::stoi(value); std::string errorPagePath; lineStream >> errorPagePath; - error_page[statusCode] = errorPagePath; + error_page_[statusCode] = errorPagePath; LOG_INFO("Set error_page for status " + std::to_string(statusCode) + " to " + errorPagePath); } else @@ -124,9 +124,9 @@ void ServerConfig::parseDirectives(const std::string &declarations) 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); throw std::runtime_error("Location not found: " + path); @@ -135,8 +135,8 @@ const LocationConfig &ServerConfig::getLocation(const std::string &path) const std::vector ServerConfig::getLocationPaths() const { std::vector paths; - paths.reserve(locations.size()); - for (const auto &pair : locations) + paths.reserve(locations_.size()); + for (const auto &pair : locations_) { paths.push_back(pair.first); } diff --git a/webserv/config/ServerConfig.hpp b/webserv/config/ServerConfig.hpp index c584e5d..a87deee 100644 --- a/webserv/config/ServerConfig.hpp +++ b/webserv/config/ServerConfig.hpp @@ -11,29 +11,29 @@ class ServerConfig public: ServerConfig(const std::string &serverBlock); - [[nodiscard]] const std::string &getHost() const { return host; } - [[nodiscard]] int getPort() const { return port; } - [[nodiscard]] const std::string &getRoot() const { return root; } - [[nodiscard]] const std::string &getCgiPass() const { return cgi_pass; } - [[nodiscard]] const std::string &getCgiExt() const { return cgi_ext; } - [[nodiscard]] const std::map &getErrorPages() const { return error_page; } - [[nodiscard]] const std::vector &getIndexFiles() const { return index_files; } + [[nodiscard]] const std::string &getHost() const { return host_; } + [[nodiscard]] int getPort() const { return port_; } + [[nodiscard]] const std::string &getRoot() const { return root_; } + [[nodiscard]] const std::string &getCgiPass() const { return cgi_pass_; } + [[nodiscard]] const std::string &getCgiExt() const { return cgi_ext_; } + [[nodiscard]] const std::map &getErrorPages() const { return error_page_; } + [[nodiscard]] const std::vector &getIndexFiles() const { return index_files_; } [[nodiscard]] const LocationConfig &getLocation(const std::string &path) const; [[nodiscard]] std::vector getLocationPaths() const; - void setServerFD(int fd) { server_fd = fd; } - [[nodiscard]] int getServerFD() const { return server_fd; } + void setServerFD(int fd) { server_fd_ = fd; } + [[nodiscard]] int getServerFD() const { return server_fd_; } private: - std::string host; - int port; - int server_fd; - std::string root; - std::string cgi_pass; - std::string cgi_ext; - std::map error_page; - std::vector index_files; - std::map locations; + std::string host_; + int port_; + int server_fd_; + std::string root_; + std::string cgi_pass_; + std::string cgi_ext_; + std::map error_page_; + std::vector index_files_; + std::map locations_; void parseServerBlock(const std::string &block); void parseDirectives(const std::string &declarations); diff --git a/webserv/socket/Socket.cpp b/webserv/socket/Socket.cpp index baca4ac..53afa72 100644 --- a/webserv/socket/Socket.cpp +++ b/webserv/socket/Socket.cpp @@ -10,27 +10,27 @@ #include #include // 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"); throw std::runtime_error("Socket creation failed"); } 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); - _fd = -1; + close(fd_); + fd_ = -1; LOG_ERROR("setsockopt failed"); throw std::runtime_error("setsockopt failed"); } 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"); throw std::runtime_error("Invalid file descriptor"); @@ -40,15 +40,15 @@ Socket::Socket(int fd) : _fd(fd) // NOLINT(readability-identifier-naming) Socket::~Socket() { - if (_fd != -1) + if (fd_ != -1) { - close(_fd); + close(fd_); } } void Socket::listen(int backlog) const { - if (::listen(_fd, backlog) < 0) + if (::listen(fd_, backlog) < 0) { LOG_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_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"); } @@ -70,7 +70,7 @@ void Socket::bind(const std::string &host, const int port) const std::unique_ptr Socket::accept() const { - int client_fd = ::accept(_fd, nullptr, nullptr); + int client_fd = ::accept(fd_, nullptr, nullptr); if (client_fd < 0) { LOG_ERROR("Accept failed"); @@ -81,17 +81,17 @@ std::unique_ptr Socket::accept() 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 { - return ::send(_fd, buf, len, 0); + return ::send(fd_, buf, len, 0); } 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"); throw std::runtime_error("Failed to set non-blocking mode"); @@ -100,5 +100,5 @@ void Socket::setNonBlocking() const int Socket::getFd() const { - return _fd; + return fd_; } \ No newline at end of file diff --git a/webserv/socket/Socket.hpp b/webserv/socket/Socket.hpp index ef6e2ed..1ce39bc 100644 --- a/webserv/socket/Socket.hpp +++ b/webserv/socket/Socket.hpp @@ -27,5 +27,5 @@ class Socket [[nodiscard]] int getFd() const; private: - int _fd; + int fd_; }; \ No newline at end of file