Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
FreddyMSchubert committed Dec 12, 2024
2 parents ddefb49 + 5110fca commit c5ad19e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 8 deletions.
6 changes: 3 additions & 3 deletions include/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ typedef struct s_location
std::variant<Path, FilePath> path; // path in config
std::string root_dir; // folder to get data from. not a path as it might not be in config root. If empty, use config root
std::array<bool, 3> allowed_methods; // saved in order of Method enum in Enums.hpp
bool directory_listing;
bool directory_listing = false;
std::vector<std::string> cgi_extensions;
std::map<int, Path> redirections;
Path upload_dir;
Expand All @@ -39,9 +39,9 @@ class Config
int _port;
std::string _root_dir;
std::optional<FilePath> _index_file; // optional but will always be present after constructor
unsigned int _client_max_body_size; // in bytes
unsigned int _client_max_body_size = 1048576; // 1 MB in bytes
std::map<int, FilePath> _error_pages;
int _client_timeout; // in ms
int _client_timeout = 30000; // 30 seconds in ms
std::vector<t_location> _locations;

// Line parsers
Expand Down
11 changes: 7 additions & 4 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ Config::Config(std::string data)
if (!foundMatch)
throw std::invalid_argument("Invalid directive: \"" + keyword + "\"");
}
if (_host.empty()) throw std::invalid_argument("No listen directive found");
if (_port == 0) throw std::invalid_argument("No listen directive found");
if (_root_dir.empty()) throw std::invalid_argument("No root directive found");
}

/* ------------------------ */
Expand Down Expand Up @@ -153,11 +156,11 @@ void Config::parseClientMaxBodySize(const std::string & line)
else if (unit == "GB" || unit == "gb")
_client_max_body_size = static_cast<unsigned int>(size) * 1024 * 1024 * 1024;
else
throw std::invalid_argument("Unsupported unit in client_max_body_size");
Logger::Log(LogLevel::WARNING, "Invalid unit in client_max_body_size directive. Using default value (1MB).");
}
else
{
throw std::invalid_argument("Invalid client_max_body_size directive");
Logger::Log(LogLevel::WARNING, "Invalid unit in client_max_body_size directive. Using default value (1MB).");
}

#if LOG_CONFIG_PARSING
Expand Down Expand Up @@ -219,7 +222,7 @@ void Config::parseClientTimeout(const std::string & line)
}
}
else
throw std::invalid_argument("Invalid client_timeout directive");
Logger::Log(LogLevel::WARNING, "Invalid client_timeout directive. Using default value (30s).");

#if LOG_CONFIG_PARSING
Logger::Log(LogLevel::INFO, "Client timeout: " + std::to_string(_client_timeout));
Expand Down Expand Up @@ -343,7 +346,7 @@ void Config::parseLocationAutoindex(const std::string & line, t_location & loc)
loc.directory_listing = false;
}
else
throw std::invalid_argument("Invalid location autoindex (directory_listing) directive: \"" + line + "\"");
Logger::Log(LogLevel::WARNING, "Invalid location autoindex directive. Using default value (off).");
}

void Config::parseLocationCgiExtensions(const std::string & line, t_location & loc)
Expand Down
3 changes: 2 additions & 1 deletion src/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ void Socket::sendData(Response &response)
std::string Socket::receiveData()
{
std::string data;
char buffer[_config.getClientMaxBodySize() + 1];
int size = std::min(1024, static_cast<int>(_config.getClientMaxBodySize() + 1));
char buffer[size];
ssize_t received;

received = recv(_socket_fd, buffer, sizeof(buffer), 0);
Expand Down

0 comments on commit c5ad19e

Please sign in to comment.