Added send link and image functionality. Sending images doesn't always work.

This commit is contained in:
2025-05-09 22:22:23 +02:00
parent fc5af2468f
commit 87d8afda4d
10 changed files with 277 additions and 92 deletions
+5
View File
@@ -7,4 +7,9 @@ enum data_type {
IMAGE
};
enum image_type {
PNG,
AVIF
};
#endif
+2
View File
@@ -8,6 +8,8 @@ using namespace std;
struct FileUtils {
static bool fileExists(const char *path);
static vector<unsigned char> open_image(const string& path);
static vector<string> get_image_list(const string& path);
};
#endif
+6 -19
View File
@@ -2,16 +2,7 @@
#define SERVERUTILS_H
#include <string>
#include <vector>
#include <iostream>
#include <sstream>
#include <string>
#include <unistd.h>
#include <netinet/in.h>
#include <thread>
#include <chrono>
#include <unordered_map>
#include <map>
#include "../include/DataType.h"
using namespace std;
@@ -22,9 +13,11 @@ class ServerUtils {
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_html(int client_fd, size_t hash);
static void send_chunked_css(int client_fd);
static void send_data(int client_fd, string data);
static void send_data(int client_fd, const string& data);
static void send_image(int client_fd, const string& path, image_type type);
static size_t send_all(int sockfd, const char* data, size_t length);
};
const string HTML_RESPONSE_HEADER =
@@ -41,18 +34,12 @@ const string CSS_RESPONSE_HEADER =
"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=\"preload\" href=\"style.css\" as=\"style\" />\r\n"
"<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\r\n"
"<link rel=\"icon\" href=\"favicon.png\"\r\n"
"<link rel=\"icon\" type=\"image/png\" href=\"favicon.png\"\r\n"
"</head><body>\r\n";
const string HTML_END = "</body></html>\r\n";
+9 -5
View File
@@ -4,21 +4,25 @@
#include <string>
#include <vector>
#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 string& path);
static unordered_map<string, unordered_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 unordered_map<string, unordered_map<string, int>>& word_frequencies, size_t count);
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 string create_tag(const map<string, map<string, int>>& word_frequencies, const char& hash);
static bool contains_image(const string& input);
static string extract_image_name(const string& input);
static string create_tag(const unordered_map<string, unordered_map<string, int>>& word_frequencies, const char& hash);
static string create_link(const unordered_map<string, unordered_map<string, int>>& word_frequencies, unsigned long hash);
static string create_image(const string& image);
static unsigned int hash_url(const string& input);
static unsigned long djb2Hash(const string& str);
static unsigned long fnv1aHash(const string& str);
};
#endif