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:
parent
6e9725bb53
commit
4f47c0c1ee
@ -6,14 +6,14 @@
|
||||
#include <iostream>
|
||||
|
||||
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()
|
||||
{
|
||||
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;
|
||||
}
|
||||
@ -31,6 +31,6 @@ class Client
|
||||
std::string header_;
|
||||
std::string content_;
|
||||
std::unique_ptr<Socket> client_socket_;
|
||||
const Server &server;
|
||||
const ServerConfig &server_config;
|
||||
const Server &server_;
|
||||
const ServerConfig &server_config_;
|
||||
};
|
||||
@ -8,7 +8,7 @@
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@ -5,6 +5,7 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
|
||||
class ConfigManager
|
||||
{
|
||||
public:
|
||||
@ -15,13 +16,13 @@ class ConfigManager
|
||||
|
||||
void init(const std::string &filePath);
|
||||
static ConfigManager &getInstance();
|
||||
const std::vector<ServerConfig> &getServerConfigs() const { return serverConfigs; }
|
||||
const std::vector<ServerConfig> &getServerConfigs() const { return serverConfigs_; }
|
||||
|
||||
private:
|
||||
bool _initialized;
|
||||
bool initialized_;
|
||||
ConfigManager();
|
||||
~ConfigManager();
|
||||
std::vector<ServerConfig> serverConfigs;
|
||||
std::vector<ServerConfig> serverConfigs_;
|
||||
|
||||
void parseConfigFile(const std::string &filePath);
|
||||
// void parseGlobalDeclarations(const std::string &declarations);
|
||||
|
||||
@ -5,7 +5,7 @@
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,10 +9,10 @@ class LocationConfig
|
||||
LocationConfig(const std::string &locationBlock);
|
||||
|
||||
private:
|
||||
std::string path;
|
||||
bool autoIndex;
|
||||
std::string indexFile;
|
||||
std::map<std::string, std::string> directives;
|
||||
std::string path_;
|
||||
bool autoIndex_;
|
||||
std::string indexFile_;
|
||||
std::map<std::string, std::string> directives_;
|
||||
|
||||
void parseLocationBlock(const std::string &block);
|
||||
void parseDirectives(const std::string &declarations);
|
||||
|
||||
@ -7,7 +7,7 @@
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
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<std::string> ServerConfig::getLocationPaths() const
|
||||
{
|
||||
std::vector<std::string> paths;
|
||||
paths.reserve(locations.size());
|
||||
for (const auto &pair : locations)
|
||||
paths.reserve(locations_.size());
|
||||
for (const auto &pair : locations_)
|
||||
{
|
||||
paths.push_back(pair.first);
|
||||
}
|
||||
|
||||
@ -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<int, std::string> &getErrorPages() const { return error_page; }
|
||||
[[nodiscard]] const std::vector<std::string> &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<int, std::string> &getErrorPages() const { return error_page_; }
|
||||
[[nodiscard]] const std::vector<std::string> &getIndexFiles() const { return index_files_; }
|
||||
[[nodiscard]] const LocationConfig &getLocation(const std::string &path) const;
|
||||
[[nodiscard]] std::vector<std::string> 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<int, std::string> error_page;
|
||||
std::vector<std::string> index_files;
|
||||
std::map<std::string, LocationConfig> locations;
|
||||
std::string host_;
|
||||
int port_;
|
||||
int server_fd_;
|
||||
std::string root_;
|
||||
std::string cgi_pass_;
|
||||
std::string cgi_ext_;
|
||||
std::map<int, std::string> error_page_;
|
||||
std::vector<std::string> index_files_;
|
||||
std::map<std::string, LocationConfig> locations_;
|
||||
|
||||
void parseServerBlock(const std::string &block);
|
||||
void parseDirectives(const std::string &declarations);
|
||||
|
||||
@ -10,27 +10,27 @@
|
||||
#include <sys/socket.h>
|
||||
#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");
|
||||
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> 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> 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_;
|
||||
}
|
||||
@ -27,5 +27,5 @@ class Socket
|
||||
[[nodiscard]] int getFd() const;
|
||||
|
||||
private:
|
||||
int _fd;
|
||||
int fd_;
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user