Added sending css file. Broke up some pieces of code, so now the ServerUtil is a bit more clean now.

This commit is contained in:
Rasmus Rasmussen 2025-05-06 23:11:20 +02:00
parent 94ffa8784d
commit fc5af2468f
8 changed files with 287 additions and 104 deletions

57
content/style.css Normal file
View File

@ -0,0 +1,57 @@
.flex {
display: flex;
}
.flex-col {
flex-direction: column;
}
.min-h-screen {
min-height: 100vh;
}
.justify-center {
justify-content: center;
}
.grow {
flex-grow: 1;
}
.pt-10 {
padding-top: 2.5rem /* 40px */;
}
.px-4 {
padding-left: 1rem /* 16px */;
padding-right: 1rem /* 16px */;
}
.text-2xl {
font-size: 1.5rem /* 24px */;
line-height: 2rem /* 32px */;
}
.place-items-center {
place-items: center;
}
.w-full {
width: 100%;
}
.max-w-screen-lg {
max-width: 1024px;
}
.mt-10 {
margin-top: 2.5rem /* 40px */;
}
.pb-2 {
padding-bottom: 0.5rem /* 8px */;
}
.underline {
text-decoration-line: underline;
}

10
include/DataType.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef DATATYPE_H
#define DATATYPE_H
enum data_type {
HTML,
CSS,
IMAGE
};
#endif

View File

@ -8,7 +8,6 @@ using namespace std;
struct FileUtils { struct FileUtils {
static bool fileExists(const char *path); static bool fileExists(const char *path);
static char* getFiles(const char *path);
}; };
#endif #endif

View File

@ -12,12 +12,71 @@
#include <chrono> #include <chrono>
#include <unordered_map> #include <unordered_map>
#include <map> #include <map>
#include "../include/DataType.h"
using namespace std; using namespace std;
struct ServerUtils { class ServerUtils {
static void serve(map<string, map<string, int>> wordwordFrequencies); public:
static void process_request(int client_fd, map<string, map<string, int>> wordwordFrequencies); static void serve();
private:
static void process_request(int client_fd);
static void send_header(int client_fd, data_type type);
static void send_chunked_html(int client_fd, unsigned int hash);
static void send_chunked_css(int client_fd);
static void send_data(int client_fd, string data);
}; };
const string HTML_RESPONSE_HEADER =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=utf-8\r\n"
"Transfer-Encoding: chunked\r\n"
"Cache-control: max-age=12000\r\n"
"Connection: close\r\n\r\n";
const string CSS_RESPONSE_HEADER =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/css; charset=utf-8\r\n"
"Transfer-Encoding: chunked\r\n"
"Cache-control: max-age=12000\r\n"
"Connection: close\r\n\r\n";
const string PNG_RESPONSE_HEADER =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/png; charset=utf-8\r\n"
"Transfer-Encoding: chunked\r\n"
"Cache-control: max-age=12000\r\n"
"Connection: close\r\n\r\n";
const string HTML_BEGINNING =
"<!DOCTYPE html>\n<html><head>\r\n"
"<title>Drip</title>\r\n"
"<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\r\n"
"<link rel=\"icon\" href=\"favicon.png\"\r\n"
"</head><body>\r\n";
const string HTML_END = "</body></html>\r\n";
const string HTML_NAV =
"<div class=\"flex flex-col min-h-screen bg-var_background\">\r\n"
"<header class=\"flex justify-center\">\r\n"
"<nav class=\"pt-10 text-2xl flex place-items-center\">\r\n"
"<a class=\"underline\" href=\"/\">Home</a>\r\n"
"</nav>\r\n"
"</header>\r\n";
const string HTML_MAIN_1 =
"<main class=\"pt-10 flex justify-center grow\">\r\n"
"<div class=\"w-full max-w-screen-lg px-4\">\r\n";
const string HTML_MAIN_2 =
"</div>\r\n"
"</main>\r\n";
const string HTML_FOOTER =
"<footer class=\"flex justify-center pt-10 mt-10 pb-2\">\r\n"
"<h5>Author: <a class=\"underline\" href=\"https://proxy.rbwr.dk/\">Dr. Lol Man 365</a></h5>\r\n"
"</footer>\r\n"
"</div>\r\n";
#endif #endif

View File

@ -6,16 +6,18 @@
#include <unordered_map> #include <unordered_map>
#include <map> #include <map>
#include <iostream> #include <iostream>
#include "../include/DataType.h"
using namespace std; using namespace std;
struct WordUtils { struct WordUtils {
static map<string, map<string, int>> load_data(const char *path); static map<string, map<string, int>> load_data(const string& path);
static vector<string> load_css(const string& path);
static vector<string> predict_next_word(const string& input, const map<string, map<string, int>>& word_frequencies, size_t count); static vector<string> predict_next_word(const string& input, const map<string, map<string, int>>& word_frequencies, size_t count);
static string load_file(const char *path); static string load_file(const string& path);
static vector<string> split_string(const string& input, const char *delimiters); static vector<string> split_string(const string& input, data_type type);
static string extract_url(const string& input); static string extract_url(const string& input);
static vector<string> create_tag(const map<string, map<string, int>>& word_frequencies, unsigned int hash); static string create_tag(const map<string, map<string, int>>& word_frequencies, const char& hash);
static unsigned int hash_url(const string& input); static unsigned int hash_url(const string& input);
}; };

View File

@ -1,5 +1,6 @@
#include "../include/ServerUtils.h" #include "../include/ServerUtils.h"
#include "../include/WordUtils.h" #include "../include/WordUtils.h"
#include "../include/DataType.h"
#include <string> #include <string>
#include <vector> #include <vector>
@ -11,9 +12,13 @@
#include <thread> #include <thread>
#include <chrono> #include <chrono>
using namespace std; vector<string> _css;
map<string, map<string, int>> _word_frequencies;
void ServerUtils::serve() {
_word_frequencies = WordUtils::load_data("/home/skingging/Documents/Projects/CPP/AI-Tarpit-Reimagined/wordlist/wordlist-Food.txt");
_css = WordUtils::load_css("/home/skingging/Documents/Projects/CPP/AI-Tarpit-Reimagined/content/style.css");
void ServerUtils::serve(map<string, map<string, int>> word_frequencies) {
// server_fd is a file descriptor. // server_fd is a file descriptor.
int server_fd = socket(AF_INET, SOCK_STREAM, 0); int server_fd = socket(AF_INET, SOCK_STREAM, 0);
@ -30,15 +35,14 @@ void ServerUtils::serve(map<string, map<string, int>> word_frequencies) {
while (true) { while (true) {
int client_fd = accept(server_fd, nullptr, nullptr); int client_fd = accept(server_fd, nullptr, nullptr);
thread(process_request, client_fd, word_frequencies).detach(); thread(process_request, client_fd).detach();
} }
close(server_fd); close(server_fd);
} }
void ServerUtils::process_request(int client_fd, map<string, map<string, int>> word_frequencies) { void ServerUtils::process_request(int client_fd) {
char buffer[1024]; char buffer[1024];
int bytes_received = recv(client_fd, buffer, sizeof(buffer) - 1, 0); int bytes_received = recv(client_fd, buffer, sizeof(buffer) - 1, 0);
string url; string url;
@ -52,25 +56,21 @@ void ServerUtils::process_request(int client_fd, map<string, map<string, int>> w
cerr << "AAAA \n"; cerr << "AAAA \n";
} }
unsigned int hash = WordUtils::hash_url(url); if (url == "/style.css") {
// This sends the header, that instructs how the browser should interpret the data.
string headers = send_header(client_fd, data_type::CSS);
"HTTP/1.1 200 OK\r\n" send_chunked_css(client_fd);
"Content-Type: text/html; charset=utf-8\r\n" }
"Transfer-Encoding: chunked\r\n"
"Connection: close\r\n\r\n"; else if (url == "/favicon.png") {
// This sends the header, that instructs how the browser should interpret the data.
send(client_fd, headers.c_str(), headers.size(), 0); //send_header(client_fd, data_type::IMAGE);
}
const vector<string> html_chunks = WordUtils::create_tag(word_frequencies, hash);
else {
for (const auto& chunk : html_chunks) { unsigned int hash = WordUtils::hash_url(url);
ostringstream oss; send_header(client_fd, data_type::HTML);
oss << hex << chunk.size() << "\r\n" << chunk << "\r\n"; send_chunked_html(client_fd, hash);
string to_send = oss.str();
send(client_fd, to_send.c_str(), to_send.size(), 0);
this_thread::sleep_for(chrono::milliseconds(5));
} }
// Send final zero-length chunk to end the response // Send final zero-length chunk to end the response
@ -78,3 +78,68 @@ void ServerUtils::process_request(int client_fd, map<string, map<string, int>> w
close(client_fd); close(client_fd);
} }
void ServerUtils::send_header(int client_fd, data_type type) {
if (type == data_type::HTML) {
send(client_fd, HTML_RESPONSE_HEADER.c_str(), HTML_RESPONSE_HEADER.size(), 0);
}
else if (type == data_type::CSS) {
send(client_fd, CSS_RESPONSE_HEADER.c_str(), CSS_RESPONSE_HEADER.size(), 0);
}
else {
send(client_fd, PNG_RESPONSE_HEADER.c_str(), PNG_RESPONSE_HEADER.size(), 0);
}
}
void ServerUtils::send_chunked_html(int client_fd, unsigned int hash) {
string chunk;
const string hashes = to_string(hash);
unsigned short itr = 0;
unsigned short end = hashes.size();
send_data(client_fd, HTML_BEGINNING);
send_data(client_fd, HTML_NAV);
send_data(client_fd, HTML_MAIN_1);
while (itr < end) {
ostringstream oss;
chunk = WordUtils::create_tag(_word_frequencies, hashes[itr]);
oss << hex << chunk.size() << "\r\n" << chunk << "\r\n";
string to_send = oss.str();
send(client_fd, to_send.c_str(), to_send.size(), 0);
//this_thread::sleep_for(chrono::milliseconds(0));
itr++;
}
send_data(client_fd, HTML_MAIN_2);
send_data(client_fd, HTML_FOOTER);
send_data(client_fd, HTML_END);
}
void ServerUtils::send_chunked_css(int client_fd) {
for (size_t i = 0; i < _css.size(); i++) {
ostringstream oss;
oss << hex << _css[i].size() << "\r\n" << _css[i] << "\r\n";
string to_send = oss.str();
send(client_fd, to_send.c_str(), to_send.size(), 0);
}
}
void ServerUtils::send_data(int client_fd, string data) {
ostringstream oss;
oss << hex << data.size() << "\r\n" << data << "\r\n";
send(client_fd, oss.str().c_str(), oss.str().size(), 0);
}

View File

@ -1,6 +1,6 @@
#include "../include/WordUtils.h"
#include <string> #include <string>
#include <fstream> #include <fstream>
#include "../include/WordUtils.h"
#include <iostream> #include <iostream>
#include <algorithm> #include <algorithm>
#include <cctype> #include <cctype>
@ -8,10 +8,8 @@
#include <cstdint> #include <cstdint>
#include <random> #include <random>
map<string, map<string, int>> WordUtils::load_data(const char *path) { map<string, map<string, int>> WordUtils::load_data(const string& path) {
const char del[] = {' ', '.', ',', '!', '?', ';', ':', '(', ')', '\n', '\r', '\t'}; vector<string> data = split_string(load_file(path), data_type::HTML);
vector<string> data = split_string(load_file(path), del);
map<string, map<string, int>> word_frequencies = {}; map<string, map<string, int>> word_frequencies = {};
@ -24,6 +22,10 @@ map<string, map<string, int>> WordUtils::load_data(const char *path) {
return word_frequencies; return word_frequencies;
} }
vector<string> WordUtils::load_css(const string& path) {
return split_string(load_file(path), data_type::CSS);
}
vector<string> WordUtils::predict_next_word(const string& input, const map<string, map<string, int>>& word_frequencies, size_t count) { vector<string> WordUtils::predict_next_word(const string& input, const map<string, map<string, int>>& word_frequencies, size_t count) {
auto it = word_frequencies.find(input); auto it = word_frequencies.find(input);
@ -40,7 +42,7 @@ vector<string> WordUtils::predict_next_word(const string& input, const map<strin
vector<string> results; vector<string> results;
// Take up to `count` most common words // Take up to "count" most common words
for (size_t i = 0; i < min(count, sortedWords.size()); ++i) { for (size_t i = 0; i < min(count, sortedWords.size()); ++i) {
results.push_back(sortedWords[i].first); results.push_back(sortedWords[i].first);
} }
@ -48,79 +50,65 @@ vector<string> WordUtils::predict_next_word(const string& input, const map<strin
return results; return results;
} }
vector<string> WordUtils::create_tag(const map<string, map<string, int>>& word_frequencies, unsigned int hash) { string WordUtils::create_tag(const map<string, map<string, int>>& word_frequencies, const char& hash) {
const vector<string> beginning_sequences = {"the", "but", "with"}; unsigned char predict_num = 5;
const string start = "<!DOCTYPE html>\n<html><head><title>Drip</title></head><body>\n";
const string end = "</body></html>\n"; const string start_words[3] = {"the", "but", "with"};
vector<string> tags; vector<string> tags;
const string hashes = to_string(hash); minstd_rand generator(hash);
tags.push_back(start); uniform_int_distribution<unsigned short> outer_distribution(0, start_words->length() - 1);
int predict_num = 5;
// Tags
for (int i = 0; i < hashes.size(); i++)
{
vector<string> inner_tags;
minstd_rand generator((int)hashes[i]);
uniform_int_distribution<int> outer_distribution(0, beginning_sequences.size() - 1);
vector<string> temp_words = predict_next_word(beginning_sequences[outer_distribution(generator)], word_frequencies, predict_num);
uniform_int_distribution<int> outer_2_distribution(0, temp_words.size() - 1);
inner_tags.push_back(temp_words[outer_2_distribution(generator)]);
// Words per tag
for (int j = 0; j < 25; j++)
{
temp_words = predict_next_word(inner_tags[j], word_frequencies, predict_num);
uniform_int_distribution<int> inner_distribution(0, temp_words.size() - 1);
if (temp_words.size() != 0)
{
temp_words = predict_next_word(inner_tags[j], word_frequencies, predict_num);
uniform_int_distribution<int> inner_2_distribution(0, temp_words.size() - 1);
inner_tags.push_back(temp_words[inner_2_distribution(generator)]);
} else {
temp_words = predict_next_word(beginning_sequences[outer_distribution(generator)], word_frequencies, predict_num);
uniform_int_distribution<int> inner_3_distribution(0, temp_words.size() - 1);
inner_tags.push_back(temp_words[inner_3_distribution(generator)]);
}
}
string temp_string = "<p>";
for (int l = 0; l < inner_tags.size(); l++)
{
temp_string += inner_tags[l] + " ";
}
temp_string += ".</p>";
tags.push_back(temp_string);
}
vector<string> temp_words = predict_next_word(start_words[outer_distribution(generator)], word_frequencies, predict_num);
return tags; uniform_int_distribution<unsigned short> outer_2_distribution(0, temp_words.size() - 1);
tags.push_back(temp_words[outer_2_distribution(generator)]);
// Words inside the <p> tag
for (unsigned short j = 0; j < 25; j++)
{
temp_words = predict_next_word(tags[j], word_frequencies, predict_num);
uniform_int_distribution<unsigned short> inner_distribution(0, temp_words.size() - 1);
tags.push_back(temp_words[inner_distribution(generator)]);
}
string temp_string = "<p>";
for (unsigned short l = 0; l < tags.size(); l++) {
temp_string += tags[l];
temp_string += " ";
}
temp_string += ".</p>\n";
return temp_string;
} }
vector<string> WordUtils::split_string(const string& input, const char *delimiters) { vector<string> WordUtils::split_string(const string& input, data_type type) {
vector<string> data; vector<string> data;
size_t start = 0; size_t start = 0;
size_t end = 0; size_t end = 0;
// Create a string from the delimiters array // Create a string from the delimiters array
string delimiter_string = " .,!?;:()\n\r\t"; string delimiter_string;
if (type == data_type::HTML)
{
delimiter_string = " .,!?;:()\n\r\t";
}
else if (type == data_type::CSS)
{
delimiter_string = "}\n\r\t";
}
while ((end = input.find_first_of(delimiter_string, start)) != string::npos) { while ((end = input.find_first_of(delimiter_string, start)) != string::npos) {
if (end > start) { if (end > start) {
data.push_back(input.substr(start, end - start)); data.push_back(input.substr(start, end - start));
@ -137,7 +125,7 @@ vector<string> WordUtils::split_string(const string& input, const char *delimite
return data; return data;
} }
string WordUtils::load_file(const char *path) { string WordUtils::load_file(const string& path) {
ifstream file(path); ifstream file(path);
string temp; string temp;
string data; string data;
@ -152,15 +140,15 @@ string WordUtils::load_file(const char *path) {
} }
string WordUtils::extract_url(const string& input) { string WordUtils::extract_url(const string& input) {
int first_line_end = input.find("\n"); unsigned short first_line_end = input.find("\n");
if (first_line_end == string::npos) return ""; if (first_line_end == string::npos) return "";
string first_line = input.substr(0, first_line_end); string first_line = input.substr(0, first_line_end);
int method_end = first_line.find(' '); unsigned short method_end = first_line.find(' ');
if (method_end == string::npos) return ""; if (method_end == string::npos) return "";
int path_end = first_line.find(' ', method_end + 1); unsigned short path_end = first_line.find(' ', method_end + 1);
if (path_end == string::npos) return ""; if (path_end == string::npos) return "";
return first_line.substr(method_end + 1, path_end - method_end - 1); return first_line.substr(method_end + 1, path_end - method_end - 1);
@ -168,8 +156,10 @@ string WordUtils::extract_url(const string& input) {
unsigned int WordUtils::hash_url(const string& input) { unsigned int WordUtils::hash_url(const string& input) {
unsigned int hash = 0; unsigned int hash = 0;
for (char c : input) { for (char c : input) {
hash += static_cast<unsigned int>(c); hash += static_cast<unsigned int>(c) * 597301;
} }
return hash; return hash;
} }

View File

@ -4,7 +4,7 @@
using namespace std; using namespace std;
int main(int argc, char* argv[]) { int main(int argc, const char* argv[]) {
/*if (argc > 2) /*if (argc > 2)
{ {
cout << "Too many arguments"; cout << "Too many arguments";
@ -36,7 +36,8 @@ int main(int argc, char* argv[]) {
//argv[1] //argv[1]
ServerUtils::serve(WordUtils::load_data("/home/skingging/Documents/Projects/CPP/AI-Tarpit-Reimagined/wordlist/wordlist-Food.txt")); ServerUtils server;
server.serve();
return 0; return 0;
} }