Skip to content

Commit

Permalink
last merge cleanups, better packet size checking
Browse files Browse the repository at this point in the history
  • Loading branch information
FreddyMSchubert committed Dec 16, 2024
1 parent 3bbe340 commit e851635
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 17 deletions.
4 changes: 2 additions & 2 deletions include/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Config
void parseServerName(const std::string & line);
void parseRoot(const std::string & line);
void parseIndex(const std::string & line);
void parsemaxPackageSize(const std::string & line);
void parseMaxPackageSize(const std::string & line);
void parseErrorPage(const std::string & line);
void parseClientTimeout(const std::string & line);

Expand Down Expand Up @@ -77,7 +77,7 @@ class Config
int getPort() const { return _port; }
std::string getRootDir() const { return _root_dir; }
FilePath getIndexFile() const { return *_index_file; }
unsigned int getmaxPackageSize() const { return _client_max_body_size; }
unsigned int getMaxPackageSize() const { return _client_max_body_size; }
std::map<int, FilePath> getErrorPages() const { return _error_pages; }
int getClientTimeout() const { return _client_timeout; }
std::vector<t_location> getLocations() const { return _locations; }
Expand Down
4 changes: 2 additions & 2 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Config::Config(std::string data)

// 2. parse each line, based on starting keyword
std::array<std::string, CONFIG_KEYWORD_COUNT> keywords = {"listen", "server_name", "root", "index", "client_max_body_size", "error_page", "location", "client_timeout"};
std::array<void (Config::*)(const std::string&), CONFIG_KEYWORD_COUNT> parsers = {&Config::parseListen, &Config::parseServerName, &Config::parseRoot, &Config::parseIndex, &Config::parsemaxPackageSize, &Config::parseErrorPage, &Config::parseLocation, &Config::parseClientTimeout};
std::array<void (Config::*)(const std::string&), CONFIG_KEYWORD_COUNT> parsers = {&Config::parseListen, &Config::parseServerName, &Config::parseRoot, &Config::parseIndex, &Config::parseMaxPackageSize, &Config::parseErrorPage, &Config::parseLocation, &Config::parseClientTimeout};
for (std::string &line : lines)
{
std::string keyword = line.substr(0, line.find(' '));
Expand Down Expand Up @@ -138,7 +138,7 @@ void Config::parseIndex(const std::string & line)
#endif
}

void Config::parsemaxPackageSize(const std::string & line)
void Config::parseMaxPackageSize(const std::string & line)
{
std::regex size_regex(R"(client_max_body_size\s+(\d+)\s*([KMG]?B);)", std::regex::icase);
std::smatch match;
Expand Down
21 changes: 9 additions & 12 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,15 @@ void Server::updatePoll()
bool Server::isDataComplete(t_socket_data &socket)
{
std::string data = socket.buffer.str();

// Step 1: Ensure buffer is not giant
if (data.size() > _config.getmaxPackageSize())
throw std::runtime_error("Client data size exceeded maximum body size");

size_t headerEnd = data.find("\r\n\r\n");
if (headerEnd == std::string::npos)
return false;

// Step 2: Check for complete content (chunked transfer encoding)
// Chunked transfer encoding
if (data.find("Transfer-Encoding: chunked") != std::string::npos)
return data.find("0\r\n\r\n") != std::string::npos;

// Step 3: Check for complete content (normal packets)
// Normal packet
if (data.find("Content-Length: ") != std::string::npos)
{
size_t headerEnd = data.find("\r\n\r\n");
Expand All @@ -79,11 +74,7 @@ bool Server::isDataComplete(t_socket_data &socket)
}
}

// Step 4: Check for stale packet
// ...
// TODO

return true; // Step 5: No body & header is done
return true;
}

void Server::acceptNewConnections()
Expand Down Expand Up @@ -123,6 +114,12 @@ void Server::handleExistingConnections()
try
{
_sockets[i].buffer << _sockets[i].socket.receiveData();
if (_sockets[i].buffer.str().size() > _config.getMaxPackageSize())
{
Logger::Log(LogLevel::ERROR, "Client data size exceeded maximum body size");
_sockets.erase(_sockets.begin() + i);
continue;
}
_sockets[i].last_activity = std::chrono::steady_clock::now();
}
catch(const std::runtime_error &e)
Expand Down
2 changes: 1 addition & 1 deletion src/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ void Socket::sendData(Response &response)
std::string Socket::receiveData()
{
std::string data;
int size = std::min(1024, static_cast<int>(_config.getmaxPackageSize() + 1));
int size = std::min(1024, static_cast<int>(_config.getMaxPackageSize() + 1));
char buffer[size];
ssize_t received;

Expand Down

0 comments on commit e851635

Please sign in to comment.