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:
parent
94ffa8784d
commit
fc5af2468f
57
content/style.css
Normal file
57
content/style.css
Normal 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
10
include/DataType.h
Normal file
@ -0,0 +1,10 @@
|
||||
#ifndef DATATYPE_H
|
||||
#define DATATYPE_H
|
||||
|
||||
enum data_type {
|
||||
HTML,
|
||||
CSS,
|
||||
IMAGE
|
||||
};
|
||||
|
||||
#endif
|
@ -8,7 +8,6 @@ using namespace std;
|
||||
|
||||
struct FileUtils {
|
||||
static bool fileExists(const char *path);
|
||||
static char* getFiles(const char *path);
|
||||
};
|
||||
|
||||
#endif
|
@ -12,12 +12,71 @@
|
||||
#include <chrono>
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
#include "../include/DataType.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct ServerUtils {
|
||||
static void serve(map<string, map<string, int>> wordwordFrequencies);
|
||||
static void process_request(int client_fd, map<string, map<string, int>> wordwordFrequencies);
|
||||
class ServerUtils {
|
||||
public:
|
||||
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
|
@ -6,16 +6,18 @@
|
||||
#include <unordered_map>
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
#include "../include/DataType.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
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 string load_file(const char *path);
|
||||
static vector<string> split_string(const string& input, const char *delimiters);
|
||||
static string load_file(const string& path);
|
||||
static vector<string> split_string(const string& input, data_type type);
|
||||
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);
|
||||
};
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include "../include/ServerUtils.h"
|
||||
#include "../include/WordUtils.h"
|
||||
#include "../include/DataType.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@ -11,9 +12,13 @@
|
||||
#include <thread>
|
||||
#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.
|
||||
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) {
|
||||
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);
|
||||
}
|
||||
|
||||
void ServerUtils::process_request(int client_fd, map<string, map<string, int>> word_frequencies) {
|
||||
void ServerUtils::process_request(int client_fd) {
|
||||
char buffer[1024];
|
||||
|
||||
int bytes_received = recv(client_fd, buffer, sizeof(buffer) - 1, 0);
|
||||
|
||||
string url;
|
||||
@ -52,25 +56,21 @@ void ServerUtils::process_request(int client_fd, map<string, map<string, int>> w
|
||||
cerr << "AAAA \n";
|
||||
}
|
||||
|
||||
unsigned int hash = WordUtils::hash_url(url);
|
||||
|
||||
string headers =
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Type: text/html; charset=utf-8\r\n"
|
||||
"Transfer-Encoding: chunked\r\n"
|
||||
"Connection: close\r\n\r\n";
|
||||
|
||||
send(client_fd, headers.c_str(), headers.size(), 0);
|
||||
|
||||
const vector<string> html_chunks = WordUtils::create_tag(word_frequencies, hash);
|
||||
|
||||
for (const auto& chunk : html_chunks) {
|
||||
ostringstream oss;
|
||||
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(5));
|
||||
if (url == "/style.css") {
|
||||
// This sends the header, that instructs how the browser should interpret the data.
|
||||
send_header(client_fd, data_type::CSS);
|
||||
send_chunked_css(client_fd);
|
||||
}
|
||||
|
||||
else if (url == "/favicon.png") {
|
||||
// This sends the header, that instructs how the browser should interpret the data.
|
||||
//send_header(client_fd, data_type::IMAGE);
|
||||
}
|
||||
|
||||
else {
|
||||
unsigned int hash = WordUtils::hash_url(url);
|
||||
send_header(client_fd, data_type::HTML);
|
||||
send_chunked_html(client_fd, hash);
|
||||
}
|
||||
|
||||
// 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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
#include "../include/WordUtils.h"
|
||||
#include <string>
|
||||
#include <fstream>
|
||||
#include "../include/WordUtils.h"
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
@ -8,10 +8,8 @@
|
||||
#include <cstdint>
|
||||
#include <random>
|
||||
|
||||
map<string, map<string, int>> WordUtils::load_data(const char *path) {
|
||||
const char del[] = {' ', '.', ',', '!', '?', ';', ':', '(', ')', '\n', '\r', '\t'};
|
||||
|
||||
vector<string> data = split_string(load_file(path), del);
|
||||
map<string, map<string, int>> WordUtils::load_data(const string& path) {
|
||||
vector<string> data = split_string(load_file(path), data_type::HTML);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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) {
|
||||
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;
|
||||
|
||||
// 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) {
|
||||
results.push_back(sortedWords[i].first);
|
||||
}
|
||||
@ -48,79 +50,65 @@ vector<string> WordUtils::predict_next_word(const string& input, const map<strin
|
||||
return results;
|
||||
}
|
||||
|
||||
vector<string> WordUtils::create_tag(const map<string, map<string, int>>& word_frequencies, unsigned int hash) {
|
||||
const vector<string> beginning_sequences = {"the", "but", "with"};
|
||||
const string start = "<!DOCTYPE html>\n<html><head><title>Drip</title></head><body>\n";
|
||||
const string end = "</body></html>\n";
|
||||
string WordUtils::create_tag(const map<string, map<string, int>>& word_frequencies, const char& hash) {
|
||||
unsigned char predict_num = 5;
|
||||
|
||||
const string start_words[3] = {"the", "but", "with"};
|
||||
|
||||
vector<string> tags;
|
||||
|
||||
const string hashes = to_string(hash);
|
||||
minstd_rand generator(hash);
|
||||
|
||||
tags.push_back(start);
|
||||
|
||||
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);
|
||||
}
|
||||
uniform_int_distribution<unsigned short> outer_distribution(0, start_words->length() - 1);
|
||||
|
||||
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;
|
||||
|
||||
size_t start = 0;
|
||||
size_t end = 0;
|
||||
|
||||
// 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) {
|
||||
if (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;
|
||||
}
|
||||
|
||||
string WordUtils::load_file(const char *path) {
|
||||
string WordUtils::load_file(const string& path) {
|
||||
ifstream file(path);
|
||||
string temp;
|
||||
string data;
|
||||
@ -152,15 +140,15 @@ string WordUtils::load_file(const char *path) {
|
||||
}
|
||||
|
||||
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 "";
|
||||
|
||||
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 "";
|
||||
|
||||
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 "";
|
||||
|
||||
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 hash = 0;
|
||||
|
||||
for (char c : input) {
|
||||
hash += static_cast<unsigned int>(c);
|
||||
hash += static_cast<unsigned int>(c) * 597301;
|
||||
}
|
||||
|
||||
return hash;
|
||||
}
|
@ -4,7 +4,7 @@
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
int main(int argc, const char* argv[]) {
|
||||
/*if (argc > 2)
|
||||
{
|
||||
cout << "Too many arguments";
|
||||
@ -36,7 +36,8 @@ int main(int argc, char* argv[]) {
|
||||
|
||||
|
||||
//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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user