Enhance code style and organization: update .clang-format for include sorting, enable parallel compilation in CMake, and clean up includes across multiple files for improved readability and consistency.

This commit is contained in:
whaffman 2025-09-20 23:21:23 +02:00
parent 94cd396ac1
commit df97596321
23 changed files with 122 additions and 89 deletions

View File

@ -1 +1,38 @@
BasedOnStyle: Microsoft BasedOnStyle: Microsoft
# Include ordering
SortIncludes: CaseSensitive
IncludeBlocks: Regroup
IncludeCategories:
# Main header (current file's header)
- Regex: '^".*\.hpp"$'
Priority: 1
SortPriority: 1
CaseSensitive: true
# Project headers
- Regex: '^<webserv/.*\.hpp>$'
Priority: 2
SortPriority: 2
CaseSensitive: true
# System C++ headers
- Regex: '^<[a-z_]+>$'
Priority: 3
SortPriority: 3
CaseSensitive: true
# System C headers
- Regex: '^<.*\.h>$'
Priority: 4
SortPriority: 4
CaseSensitive: true
# Third-party libraries
- Regex: '^<.*>$'
Priority: 5
SortPriority: 5
CaseSensitive: true
# Allow simple functions and if statements on one line
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: WithoutElse
AllowShortLoopsOnASingleLine: true
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: true

3
.gitignore vendored
View File

@ -2,7 +2,4 @@
*.a *.a
build build
.cache .cache
webserv.log webserv.log

View File

@ -5,6 +5,13 @@ project(webserv)
set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Enable parallel compilation
include(ProcessorCount)
ProcessorCount(N)
if(NOT N EQUAL 0)
set(CMAKE_BUILD_PARALLEL_LEVEL ${N})
endif()
# Add source files # Add source files
file(GLOB_RECURSE SOURCES file(GLOB_RECURSE SOURCES
"${PROJECT_SOURCE_DIR}/webserv/*.cpp" "${PROJECT_SOURCE_DIR}/webserv/*.cpp"

View File

@ -1,8 +1,10 @@
#include "webserv/socket/Socket.hpp" #include "webserv/socket/Socket.hpp"
#include <iostream>
#include <webserv/client/Client.hpp> #include <webserv/client/Client.hpp>
#include <webserv/log/Log.hpp> #include <webserv/log/Log.hpp>
#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))
{ {
@ -14,7 +16,7 @@ Client::~Client()
server.removeFromEpoll(*client_socket_); server.removeFromEpoll(*client_socket_);
}; };
int Client::parseHeaderforContentLength(const std::string &request) //NOLINT int Client::parseHeaderforContentLength(const std::string &request) // NOLINT
{ {
std::string header = "Content-Length: "; std::string header = "Content-Length: ";
size_t pos = request.find(header); size_t pos = request.find(header);

View File

@ -1,10 +1,12 @@
#pragma once #pragma once
#include "webserv/socket/Socket.hpp" #include "webserv/socket/Socket.hpp"
#include <memory>
#include <webserv/config/ServerConfig.hpp> #include <webserv/config/ServerConfig.hpp>
#include <webserv/server/Server.hpp> #include <webserv/server/Server.hpp>
#include <memory>
class Server; class Server;
class Client class Client

View File

@ -8,13 +8,9 @@
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
ConfigManager::ConfigManager() : _initialized(false) ConfigManager::ConfigManager() : _initialized(false) {}
{
}
ConfigManager::~ConfigManager() ConfigManager::~ConfigManager() {}
{
}
ConfigManager &ConfigManager::getInstance() ConfigManager &ConfigManager::getInstance()
{ {

View File

@ -1,10 +1,10 @@
#pragma once #pragma once
#include <webserv/config/ServerConfig.hpp>
#include <string> #include <string>
#include <vector> #include <vector>
#include <webserv/config/ServerConfig.hpp>
class ConfigManager class ConfigManager
{ {
public: public:

View File

@ -2,7 +2,6 @@
#include <webserv/config/utils.hpp> #include <webserv/config/utils.hpp>
#include <webserv/log/Log.hpp> #include <webserv/log/Log.hpp>
#include <iostream>
#include <sstream> #include <sstream>
#include <string> #include <string>

View File

@ -1,8 +1,7 @@
#pragma once #pragma once
#include <string>
#include <map> #include <map>
#include <string>
class LocationConfig class LocationConfig
{ {

View File

@ -1,9 +1,9 @@
#include "ServerConfig.hpp"
#include <webserv/config/LocationConfig.hpp> #include <webserv/config/LocationConfig.hpp>
#include <webserv/config/ServerConfig.hpp>
#include <webserv/config/utils.hpp> #include <webserv/config/utils.hpp>
#include <webserv/log/Log.hpp> #include <webserv/log/Log.hpp>
#include <iostream>
#include <sstream> #include <sstream>
#include <string> #include <string>
@ -54,7 +54,7 @@ void ServerConfig::parseServerBlock(const std::string &block)
void ServerConfig::parseDirectives(const std::string &declarations) void ServerConfig::parseDirectives(const std::string &declarations)
{ {
LOG_INFO("Parsing server directives" ); LOG_INFO("Parsing server directives");
std::string line; std::string line;
std::istringstream stream(declarations); std::istringstream stream(declarations);
while (std::getline(stream, line)) while (std::getline(stream, line))

View File

@ -1,11 +1,11 @@
#pragma once #pragma once
#include <webserv/config/LocationConfig.hpp>
#include <map> #include <map>
#include <string> #include <string>
#include <vector> #include <vector>
#include <webserv/config/LocationConfig.hpp>
class ServerConfig class ServerConfig
{ {
public: public:
@ -20,6 +20,7 @@ class ServerConfig
[[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; }

View File

@ -1,10 +1,11 @@
#pragma once #pragma once
#include <string>
#include <map>
#include <webserv/log/Log.hpp> #include <webserv/log/Log.hpp>
#include <webserv/log/LogLevel.hpp> #include <webserv/log/LogLevel.hpp>
#include <map>
#include <string>
class Channel class Channel
{ {
public: public:

View File

@ -1,13 +1,11 @@
#include "webserv/log/LogLevel.hpp" #include "webserv/log/LogLevel.hpp"
#include <webserv/log/FileChannel.hpp> #include <webserv/log/FileChannel.hpp>
#include <chrono> #include <chrono>
#include <iostream> #include <iostream>
FileChannel::FileChannel(const std::string &filename) : filename_(filename), fileStream_(filename, std::ios::trunc)
FileChannel::FileChannel(const std::string &filename)
: filename_(filename),
fileStream_(filename, std::ios::trunc)
{ {
if (!fileStream_.is_open()) if (!fileStream_.is_open())
{ {
@ -23,8 +21,7 @@ FileChannel::~FileChannel()
} }
} }
void FileChannel::log(LogLevel &logLevel, const std::string &message, void FileChannel::log(LogLevel &logLevel, const std::string &message, const std::map<std::string, std::string> &context)
const std::map<std::string, std::string> &context)
{ {
if (!fileStream_.is_open()) if (!fileStream_.is_open())
{ {
@ -52,4 +49,3 @@ void FileChannel::log(LogLevel &logLevel, const std::string &message,
} }
fileStream_ << std::flush; fileStream_ << std::flush;
} }

View File

@ -3,9 +3,9 @@
#include <webserv/log/Channel.hpp> #include <webserv/log/Channel.hpp>
#include <webserv/log/LogLevel.hpp> #include <webserv/log/LogLevel.hpp>
#include <string>
#include <fstream> #include <fstream>
#include <map> #include <map>
#include <string>
class FileChannel : public Channel class FileChannel : public Channel
{ {

View File

@ -1,10 +1,11 @@
#include <chrono>
#include <filesystem>
#include <iostream>
#include <webserv/log/FileChannel.hpp> #include <webserv/log/FileChannel.hpp>
#include <webserv/log/Log.hpp> #include <webserv/log/Log.hpp>
#include <webserv/log/StdoutChannel.hpp> #include <webserv/log/StdoutChannel.hpp>
#include <chrono>
#include <filesystem>
#include <iostream>
Log::Log() Log::Log()
{ {
// get start time // get start time
@ -52,6 +53,7 @@ void Log::log(LogLevel level, const std::string &message, const std::string &fil
if (it != channels_.end()) if (it != channels_.end())
{ {
std::string extendedMessage; std::string extendedMessage;
extendedMessage += message + " | ";
if (!file.empty()) if (!file.empty())
{ {
extendedMessage += std::filesystem::path(file).filename().string(); extendedMessage += std::filesystem::path(file).filename().string();
@ -64,7 +66,7 @@ void Log::log(LogLevel level, const std::string &message, const std::string &fil
{ {
extendedMessage += " (" + function + ")"; extendedMessage += " (" + function + ")";
} }
extendedMessage += " | " + message; // extendedMessage += " | " + message;
it->second->log(level, extendedMessage, context); it->second->log(level, extendedMessage, context);
} }
} }

View File

@ -1,12 +1,13 @@
#pragma once #pragma once
#include <webserv/log/Channel.hpp>
#include <webserv/log/LogLevel.hpp>
#include <chrono> #include <chrono>
#include <map> #include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <unordered_map> #include <unordered_map>
#include <webserv/log/Channel.hpp>
#include <webserv/log/LogLevel.hpp>
#define LOG(level, message) Log::static_log((level), (message), __FILE__, __LINE__, __FUNCTION__, "file", {}) #define LOG(level, message) Log::static_log((level), (message), __FILE__, __LINE__, __FUNCTION__, "file", {})

View File

@ -29,18 +29,12 @@ inline std::string logLevelToString(LogLevel level)
{ {
switch (level) switch (level)
{ {
case LogLevel::LOGLVL_TRACE: case LogLevel::LOGLVL_TRACE: return "TRACE";
return "TRACE"; case LogLevel::LOGLVL_DEBUG: return "DEBUG";
case LogLevel::LOGLVL_DEBUG: case LogLevel::LOGLVL_INFO: return "INFO";
return "DEBUG"; case LogLevel::LOGLVL_WARN: return "WARN";
case LogLevel::LOGLVL_INFO: case LogLevel::LOGLVL_ERROR: return "ERROR";
return "INFO"; case LogLevel::LOGLVL_FATAL: return "FATAL";
case LogLevel::LOGLVL_WARN:
return "WARN";
case LogLevel::LOGLVL_ERROR:
return "ERROR";
case LogLevel::LOGLVL_FATAL:
return "FATAL";
} }
return "UNKNOWN"; return "UNKNOWN";
} }
@ -49,18 +43,12 @@ inline const char *logLevelToColor(LogLevel level)
{ {
switch (level) switch (level)
{ {
case LogLevel::LOGLVL_TRACE: case LogLevel::LOGLVL_TRACE: return LogColors::TRACE_COLOR;
return LogColors::TRACE_COLOR; case LogLevel::LOGLVL_DEBUG: return LogColors::DEBUG_COLOR;
case LogLevel::LOGLVL_DEBUG: case LogLevel::LOGLVL_INFO: return LogColors::INFO_COLOR;
return LogColors::DEBUG_COLOR; case LogLevel::LOGLVL_WARN: return LogColors::WARN_COLOR;
case LogLevel::LOGLVL_INFO: case LogLevel::LOGLVL_ERROR: return LogColors::ERROR_COLOR;
return LogColors::INFO_COLOR; case LogLevel::LOGLVL_FATAL: return LogColors::FATAL_COLOR;
case LogLevel::LOGLVL_WARN:
return LogColors::WARN_COLOR;
case LogLevel::LOGLVL_ERROR:
return LogColors::ERROR_COLOR;
case LogLevel::LOGLVL_FATAL:
return LogColors::FATAL_COLOR;
} }
return LogColors::RESET; return LogColors::RESET;
} }

View File

@ -1,7 +1,8 @@
#include <webserv/log/StdoutChannel.hpp>
#include <iomanip>
#include <iostream> #include <iostream>
#include <map> #include <map>
#include <webserv/log/StdoutChannel.hpp>
#include <iomanip>
void StdoutChannel::log(LogLevel &logLevel, const std::string &message, void StdoutChannel::log(LogLevel &logLevel, const std::string &message,
const std::map<std::string, std::string> &context) const std::map<std::string, std::string> &context)

View File

@ -1,4 +1,5 @@
#include "webserv/log/Log.hpp" #include "webserv/log/Log.hpp"
#include <webserv/config/ConfigManager.hpp> #include <webserv/config/ConfigManager.hpp>
#include <webserv/config/LocationConfig.hpp> #include <webserv/config/LocationConfig.hpp>
#include <webserv/config/ServerConfig.hpp> #include <webserv/config/ServerConfig.hpp>

View File

@ -1,17 +1,18 @@
#include "webserv/socket/Socket.hpp" #include "webserv/socket/Socket.hpp"
#include <arpa/inet.h> // For inet_addr
#include <cstdlib> // For exit() #include <webserv/log/Log.hpp>
#include <webserv/server/Server.hpp>
#include <cstring> // For memset #include <cstring> // For memset
#include <fcntl.h> // For fcntl()"
#include <iostream>
#include <memory> #include <memory>
#include <vector>
#include <arpa/inet.h> // For inet_addr
#include <fcntl.h> // For fcntl()
#include <netinet/in.h> // For sockaddr_in #include <netinet/in.h> // For sockaddr_in
#include <sys/epoll.h> #include <sys/epoll.h>
#include <sys/socket.h> // For socket functions #include <sys/socket.h> // For socket functions
#include <unistd.h> // For close() #include <unistd.h> // For close()
#include <vector>
#include <webserv/server/Server.hpp>
#include <webserv/log/Log.hpp>
Server::Server(const ConfigManager &configManager) : epoll_fd_(epoll_create1(0)), configManager_(configManager) Server::Server(const ConfigManager &configManager) : epoll_fd_(epoll_create1(0)), configManager_(configManager)
{ {
@ -207,8 +208,8 @@ void Server::eventLoop()
ssize_t bytesSent = send(event.data.fd, httpResponse, strlen(httpResponse), 0); ssize_t bytesSent = send(event.data.fd, httpResponse, strlen(httpResponse), 0);
if (bytesSent < 0) if (bytesSent < 0)
{ {
LOG_ERROR("Send failed for fd: " + std::to_string(event.data.fd) + " with error: " + std::strerror(errno)); LOG_ERROR("Send failed for fd: " + std::to_string(event.data.fd) +
" with error: " + std::strerror(errno));
} }
else else
{ {

View File

@ -1,13 +1,15 @@
#pragma once #pragma once
#include "webserv/config/ServerConfig.hpp" #include "webserv/config/ServerConfig.hpp"
#include <functional>
#include <memory>
#include <unordered_map>
#include <webserv/client/Client.hpp> #include <webserv/client/Client.hpp>
#include <webserv/config/ConfigManager.hpp> #include <webserv/config/ConfigManager.hpp>
#include <webserv/socket/Socket.hpp> #include <webserv/socket/Socket.hpp>
#include <functional>
#include <memory>
#include <unordered_map>
class Client; class Client;
class Server class Server
@ -28,7 +30,7 @@ class Server
void start(); void start();
void addToEpoll(const Socket &socket, uint32_t events) const; void addToEpoll(const Socket &socket, uint32_t events) const;
void removeFromEpoll(const Socket &socket) const ; void removeFromEpoll(const Socket &socket) const;
void setupServerSocket(const ServerConfig &config); void setupServerSocket(const ServerConfig &config);
void handleConnection(struct epoll_event *event); void handleConnection(struct epoll_event *event);
void handleRequest(struct epoll_event *event) const; void handleRequest(struct epoll_event *event) const;
@ -39,7 +41,6 @@ class Server
const ServerConfig &getConfig(int fd) const; const ServerConfig &getConfig(int fd) const;
const ServerConfig &getConfig(const Socket &socket) const; const ServerConfig &getConfig(const Socket &socket) const;
private: private:
int epoll_fd_; int epoll_fd_;
const ConfigManager &configManager_; const ConfigManager &configManager_;

View File

@ -1,14 +1,14 @@
#include <memory>
#include <webserv/socket/Socket.hpp>
#include <webserv/log/Log.hpp> #include <webserv/log/Log.hpp>
#include <webserv/socket/Socket.hpp>
#include <memory>
#include <stdexcept> #include <stdexcept>
#include <unistd.h> // For close()
#include <arpa/inet.h> // For inet_addr #include <arpa/inet.h> // For inet_addr
#include <fcntl.h> // For fcntl()" #include <fcntl.h> // For fcntl()"
#include <netinet/in.h> // For sockaddr_in #include <netinet/in.h> // For sockaddr_in
#include <sys/socket.h> #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))
{ {

View File

@ -2,6 +2,7 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <sys/types.h> #include <sys/types.h>
class Socket class Socket