Skip to content

Commit

Permalink
Merge pull request #50 from FreddyMSchubert/47-loop-per-ticks-for-dat…
Browse files Browse the repository at this point in the history
…atransfer

error pages 🗿 and renamed client max body size to max package size
  • Loading branch information
NoelSabia authored Dec 16, 2024
2 parents 7e8dd8e + 4ba2be3 commit 600d6e9
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 14 deletions.
6 changes: 3 additions & 3 deletions config/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ server {
server_name test.wooo yourmum.gay kindergartenhumore.true clicker.com;
root /www/clicker/;
index test.php noidea.html index.html;
client_max_body_size 10MB;
max_package_size 10MB;
error_page 404 /error_pages/404.html;
client_timeout 60s;

Expand All @@ -29,7 +29,7 @@ server {
server_name kindergartenhumore.false platformer.com testing.yourmum;
root /www/platformer/;
index test.php noidea.html index.html;
client_max_body_size 10mb;
max_package_size 10mb;
error_page 404 /404/404.html;
client_timeout 60000ms;

Expand Down Expand Up @@ -58,7 +58,7 @@ server {
server_name tetris.com kindergartenhumore.true;
root /www/tetris/;
index test.php noidea.html index.html;
client_max_body_size 10mb;
max_package_size 10mb;
error_page 404 404.html;
client_timeout 60000ms;

Expand Down
2 changes: 1 addition & 1 deletion include/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ 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 = 1048576; // 1 MB in bytes
unsigned int _max_package_size = 1048576; // 1 MB in bytes
std::map<int, FilePath> _error_pages;
int _client_timeout = 30000; // 30 seconds in ms
std::vector<t_location> _locations;
Expand Down
16 changes: 8 additions & 8 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,31 +140,31 @@ void Config::parseIndex(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::regex size_regex(R"(max_package_size\s+(\d+)\s*([KMG]?B);)", std::regex::icase);
std::smatch match;
if (std::regex_match(line, match, size_regex))
{
unsigned int size = std::stoi(match[1]);
std::string unit = match[2];

if (unit == "B" || unit == "b")
_client_max_body_size = size;
_max_package_size = size;
else if (unit == "KB" || unit == "kb")
_client_max_body_size = static_cast<unsigned int>(size) * 1024;
_max_package_size = static_cast<unsigned int>(size) * 1024;
else if (unit == "MB" || unit == "mb")
_client_max_body_size = static_cast<unsigned int>(size) * 1024 * 1024;
_max_package_size = static_cast<unsigned int>(size) * 1024 * 1024;
else if (unit == "GB" || unit == "gb")
_client_max_body_size = static_cast<unsigned int>(size) * 1024 * 1024 * 1024;
_max_package_size = static_cast<unsigned int>(size) * 1024 * 1024 * 1024;
else
Logger::Log(LogLevel::WARNING, "Invalid unit in client_max_body_size directive. Using default value (1MB).");
Logger::Log(LogLevel::WARNING, "Invalid unit in max_package_size directive. Using default value (1MB).");
}
else
{
Logger::Log(LogLevel::WARNING, "Invalid unit in client_max_body_size directive. Using default value (1MB).");
Logger::Log(LogLevel::WARNING, "Invalid unit in max_package_size directive. Using default value (1MB).");
}

#if LOG_CONFIG_PARSING
Logger::Log(LogLevel::INFO, "Client max body size: " + std::to_string(_client_max_body_size));
Logger::Log(LogLevel::INFO, "Client max body size: " + std::to_string(_max_package_size));
#endif
}

Expand Down
1 change: 1 addition & 0 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ void Server::handleExistingConnections()
Request req(_sockets[i].buffer.str());
Response res(req, _config);
_sockets[i].socket.sendData(res);
// _sockets[i].socket.redirectToError(501); //testing for error pages
_sockets.erase(_sockets.begin() + i);
continue;
}
Expand Down
27 changes: 27 additions & 0 deletions src/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,24 +142,48 @@ void Socket::redirectToError(int error_code)
{
// 1. Noel's memery
bool noel = true;
std::string title;
std::string image;
std::string text;
std::string positioning;
std::string status_line;
switch (error_code)
{
case 302:
title = "302 - Found";
image = "https://i.imgflip.com/9dx1g6.jpg";
text = "302 - Found. But u know what we didnt find? Your father.";
positioning = "";
status_line = "HTTP/1.1 302 Found\r\n";
break;
case 400:
title = "400 - Bad Request";
image = "https://i.imgflip.com/9dx3dr.jpg";
text = "400 - Bad Request. Listen to your mother and stop making bad requests!";
positioning = "";
status_line = "HTTP/1.1 400 Bad Request\r\n";
break;
case 404:
title = "404 - Not Found";
image = "https://img.sparknews.funkemedien.de/214885251/214885251_1532012732_v16_9_1600.webp";
text = "404 - Not Found";
positioning = "position: absolute; left: 0%;";
status_line = "HTTP/1.1 404 Not Found\r\n";
break;
case 413:
title = "413 - Payload Too Large";
image = "https://i0.wp.com/worleygig.com/wp-content/uploads/2014/05/p1030261-e1401153271349.jpg";
text = "413 - Payload Too Large. Thats what she said!";
status_line = "HTTP/1.1 413 Payload Too Large\r\n";
positioning = "";
break;
case 501:
title = "501 - Not Implemented";
image = "https://i.imgflip.com/9dx6ze.jpg";
text = "501 - Not Implemented. You know what else is not implemented? Your father.";
positioning = "";
status_line = "HTTP/1.1 501 Not Implemented\r\n";
break;
default:
noel = false;
break;
Expand All @@ -168,6 +192,9 @@ void Socket::redirectToError(int error_code)
if (noel)
{
std::string template_data = getFileData("templates/error_page.html");
size_t title_pos = template_data.find("{title}");
if (title_pos != std::string::npos)
template_data.replace(title_pos, 7, title);
size_t img_pos = template_data.find("{img}");
if (img_pos != std::string::npos)
template_data.replace(img_pos, 5, image);
Expand Down
6 changes: 4 additions & 2 deletions templates/error_page.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>404 Not Found</title>
<title>{title}</title>
<style>
body {
display: flex;
Expand All @@ -13,7 +13,9 @@
margin: 0;
font-family: Arial, sans-serif;
background: url('{img}') no-repeat center center fixed;
background-size: cover;
background-size: contain;
background-repeat: no-repeat;
background-position: center;
}
.blink {
font-size: 50px;
Expand Down

0 comments on commit 600d6e9

Please sign in to comment.