From 8c3661889f662f2af4854dc427ddf15482710585 Mon Sep 17 00:00:00 2001 From: Luna Date: Fri, 2 May 2025 05:58:01 +0200 Subject: [PATCH] initial --- .gitmodules | 3 + CMakeLists.txt | 10 +++ main.cpp | 79 ++++++++++++++++++++++ netlib | 1 + test.html | 180 +++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 273 insertions(+) create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 main.cpp create mode 160000 netlib create mode 100644 test.html diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..cf80379 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "netlib"] + path = netlib + url = https://github.com/Lluna4/netlib diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..11c1d3d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.10.0) +project(web_serv VERSION 0.1.0 LANGUAGES C CXX) +set(CMAKE_CXX_STANDARD 23) + +add_subdirectory(netlib) + +add_executable(web_serv main.cpp) + +target_link_libraries(web_serv PRIVATE netlib) + diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..7415d3b --- /dev/null +++ b/main.cpp @@ -0,0 +1,79 @@ +#include "netlib/src/netlib.h" +#include +#include +#include + +int atoi_newline(const char *data) +{ + int ret = 0; + + while (true) + { + ret += *data - '0'; + data++; + if (*data == '\n' || *data == '\0' || *data == '\r') + break; + ret *= 10; + } + return ret; +} + +int main() +{ + netlib::server_raw server; + server.open_server("0.0.0.0", 8080); + + while (true) + { + std::vector readable = server.get_readable(); + for (auto user: readable) + { + char *data = server.receive_everything(user); + if (data) + { + std::string_view dat(data); + if (dat.starts_with("GET") && dat.size() > 4) + { + dat.remove_prefix(4); + int ins_size = 0; + for (auto c: dat) + { + if (c == ' ') + break; + ins_size++; + } + dat.remove_suffix(dat.size() - ins_size); + std::println("{}", dat); + if (dat.compare("/") == 0) + { + netlib::send_packet(std::make_tuple(std::string("HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n")), user); + std::ifstream file("../test.html",std::ios::binary); + std::streampos size = file.tellg(); + file.seekg(0, std::ios::end); + size = file.tellg() - size; + file.close(); + int filefd = open("../test.html", O_RDONLY); + sendfile(user, filefd, 0, size); + } + server.disconnect_user(user); + } + if (dat.starts_with("POST")) + { + size_t pos = dat.find("Content-Length"); + dat.remove_prefix(pos); + dat.remove_prefix(strlen("Content-Length: ")); + int lenght = atoi_newline(dat.data()); + std::println("{} {}", dat, lenght); + pos = dat.find("image/png"); + dat.remove_prefix(pos); + dat.remove_prefix(strlen("image/png\r\n") + 2); + std::ofstream a("Test.png", std::ios::binary); + a.write(dat.data(), lenght); + netlib::send_packet(std::make_tuple(std::string("HTTP/1.1 201 CREATED")), user); + server.disconnect_user(user); + } + } + } + std::this_thread::sleep_for(std::chrono::milliseconds(50)); + } +} diff --git a/netlib b/netlib new file mode 160000 index 0000000..1dc4578 --- /dev/null +++ b/netlib @@ -0,0 +1 @@ +Subproject commit 1dc4578cf1c732065894d961b4ffcb81e12c090d diff --git a/test.html b/test.html new file mode 100644 index 0000000..544a663 --- /dev/null +++ b/test.html @@ -0,0 +1,180 @@ +//THIS IS NOT MY CODE, THIS IS AN AI GENERATED CODE TO TEST MY SERVER AND SEE HOW EVERYTHING WORKS +//I WILL CHANGE IT TO MY OWN WHEN MY SERVER WORKS PROPERLY + + + + + + + Drag and Drop File Upload + + + + +

Simple File Upload via Drag & Drop

+ +
+

Drag one or more files to this drop zone.

+
+ +
+ + + + + \ No newline at end of file