Skip to content

Commit

Permalink
Broken time tracking fix, more config client timeout options
Browse files Browse the repository at this point in the history
  • Loading branch information
FreddyMSchubert committed Dec 12, 2024
1 parent 5432e56 commit 3609465
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 13 deletions.
6 changes: 3 additions & 3 deletions config/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ server {
index test.php noidea.html index.html;
client_max_body_size 10MB;
error_page 404 /error_pages/404.html;
client_timeout 10s;
client_timeout 60s;

location /{
allowed_methods GET POST DELETE;
Expand All @@ -31,7 +31,7 @@ server {
index test.php noidea.html index.html;
client_max_body_size 10mb;
error_page 404 /404/404.html;
client_timeout 10s;
client_timeout 60000ms;

location / {
allowed_methods GET POST DELETE;
Expand Down Expand Up @@ -60,7 +60,7 @@ server {
index test.php noidea.html index.html;
client_max_body_size 10mb;
error_page 404 404.html;
client_timeout 60s;
client_timeout 60000ms;

location / {
allowed_methods GET POST DELETE;
Expand Down
2 changes: 1 addition & 1 deletion include/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Config
std::optional<FilePath> _index_file; // optional but will always be present after constructor
unsigned int _client_max_body_size; // in bytes
std::map<int, FilePath> _error_pages;
int _client_timeout;
int _client_timeout; // in ms
std::vector<t_location> _locations;

// Line parsers
Expand Down
7 changes: 3 additions & 4 deletions include/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@ typedef struct s_socket_data
std::stringstream buffer;
std::chrono::time_point<std::chrono::steady_clock> last_activity;

s_socket_data(int fd, Socket socket,
std::chrono::time_point<std::chrono::steady_clock> last_activity) // Client socket constructor
: socket(std::move(socket)), fd(fd), buffer(), last_activity(last_activity) {}
s_socket_data(int fd, Socket socket) // Client socket constructor
: socket(std::move(socket)), fd(fd), buffer(), last_activity(std::chrono::steady_clock::now()) {}
s_socket_data(Config &config) // Listening socket constructor
: socket(config), fd(socket.getSocketFd()), buffer() {}
s_socket_data(const s_socket_data&) = delete;
s_socket_data& operator=(const s_socket_data&) = delete;

s_socket_data(s_socket_data&& other) noexcept
: socket(std::move(other.socket)), fd(other.fd), states(other.states), buffer(std::move(other.buffer)) {}
: socket(std::move(other.socket)), fd(other.fd), states(other.states), buffer(std::move(other.buffer)), last_activity(other.last_activity) {}
s_socket_data& operator=(s_socket_data&& other) noexcept
{
if (this == &other)
Expand Down
20 changes: 18 additions & 2 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,26 @@ void Config::parseErrorPage(const std::string & line)

void Config::parseClientTimeout(const std::string & line)
{
std::regex timeout_regex(R"(client_timeout\s+(\d+)s;)");
std::regex timeout_regex(R"(^\s*client_timeout\s+(\d+)\s*(ms|MS|s|S)\s*;?\s*$)");
std::smatch match;
if (std::regex_match(line, match, timeout_regex))
_client_timeout = std::stoi(match[1]);
{
int value = std::stoi(match[1]);
std::string unit = match[2];

if (unit == "ms" || unit == "MS")
{
_client_timeout = value;
}
else if (unit == "s" || unit == "S")
{
_client_timeout = value * 1000;
}
else
{
throw std::invalid_argument("Unsupported unit in client_timeout directive");
}
}
else
throw std::invalid_argument("Invalid client_timeout directive");

Expand Down
7 changes: 4 additions & 3 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void Server::acceptNewConnections()
if (client_fd >= 0)
{
Logger::Log(LogLevel::INFO, "New client connected");
_sockets.emplace_back(client_fd, Socket(_config, client_fd), std::chrono::steady_clock::now());
_sockets.emplace_back(client_fd, Socket(_config, client_fd));
}
else
break;
Expand All @@ -110,9 +110,10 @@ void Server::handleExistingConnections()
for (int i = (int)_sockets.size() - 1; i >= 0; i--)
{
std::chrono::time_point<std::chrono::steady_clock> now = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::seconds>(now - _sockets[i].last_activity).count() > _config.getClientTimeout())
std::chrono::time_point<std::chrono::steady_clock> last_activity = _sockets[i].last_activity;
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - last_activity).count() > _config.getClientTimeout())
{
Logger::Log(LogLevel::INFO, "Client timed out");
Logger::Log(LogLevel::INFO, "Client " + std::to_string(_sockets[i].fd) + " timed out");
_sockets.erase(_sockets.begin() + i);
continue;
}
Expand Down

0 comments on commit 3609465

Please sign in to comment.