From 8af6cfb3f7d621df4b8f3235fd9babc0ec6a6a77 Mon Sep 17 00:00:00 2001 From: anulax1225 Date: Wed, 13 Mar 2024 16:30:27 +0100 Subject: [PATCH] Working test serveur --- sandbox/commun.h | 12 +++++ sandbox/premake5.lua | 41 +++++++++++++++ sandbox/server/http/http_packet.cpp | 79 +++++++++++++++++++++++++++++ sandbox/server/http/http_packet.h | 44 ++++++++++++++++ sandbox/server/http/http_server.cpp | 38 ++++++++++++++ sandbox/server/http/http_server.h | 8 +++ sandbox/server/http/http_tools.cpp | 43 ++++++++++++++++ sandbox/server/http/http_tools.h | 9 ++++ sandbox/server/main.cpp | 11 ++++ 9 files changed, 285 insertions(+) create mode 100755 sandbox/commun.h create mode 100755 sandbox/premake5.lua create mode 100755 sandbox/server/http/http_packet.cpp create mode 100755 sandbox/server/http/http_packet.h create mode 100755 sandbox/server/http/http_server.cpp create mode 100755 sandbox/server/http/http_server.h create mode 100755 sandbox/server/http/http_tools.cpp create mode 100755 sandbox/server/http/http_tools.h create mode 100755 sandbox/server/main.cpp diff --git a/sandbox/commun.h b/sandbox/commun.h new file mode 100755 index 0000000..28323a1 --- /dev/null +++ b/sandbox/commun.h @@ -0,0 +1,12 @@ +#pragma once + +#include +#include +#include +#include +#include + +#define log(str) std::cout << str << "\n"; +#define input(ref) std::cin >> ref; + +#define PORT 8080 \ No newline at end of file diff --git a/sandbox/premake5.lua b/sandbox/premake5.lua new file mode 100755 index 0000000..cfdf8b4 --- /dev/null +++ b/sandbox/premake5.lua @@ -0,0 +1,41 @@ +project "server" + location "./server" + kind "ConsoleApp" + language "C++" + cppdialect "C++17" + systemversion "latest" + + targetdir("%{wks.location}/bin/" .. outputdir .. "/%{prj.name}") + objdir("%{wks.location}/bin-int/" .. outputdir .. "/%{prj.name}") + + includedirs + { + "%{IncludeDirs.bakanet}", + "./" + } + + files + { + "%{prj.location}/**.h", + "%{prj.location}/**.cpp", + "./commun.h" + } + + links + { + "bakanet" + } + + filter "system:linux" + defines + { + "BK_PLAFORM_LINUX" + } + + filter "system:windows" + defines + { + "BK_PLAFORM_WINDOWS" + } + + diff --git a/sandbox/server/http/http_packet.cpp b/sandbox/server/http/http_packet.cpp new file mode 100755 index 0000000..8532ba5 --- /dev/null +++ b/sandbox/server/http/http_packet.cpp @@ -0,0 +1,79 @@ +#include "http_packet.h" +#include + +HttpRequest::HttpRequest(std::string method, +std::string url, +std::string version, +HttpParams params, +std::string body) +: method(method), url(url), version(version), params(params), body(body) {} + +HttpRequest::HttpRequest(std::string data) +{ + auto lines = string_split(data, "\n"); + auto first_line = std::string(lines->at(0)); + auto req_data = string_split(first_line, " "); + method = req_data->at(0); + url = req_data->at(1); + version = req_data->at(2); + body = std::string(lines->at(lines->size() - 1)); + + lines->erase(lines->begin()); + lines->erase(lines->end()); + + for (auto line : *lines) + { + auto param = string_split(line, ":", 1); + if (param->size() >= 2) + { + string_trim(param->at(1)); + params.insert({param->at(0), param->at(1)}); + } + } +} + +std::string HttpRequest::to_string() +{ + std::string request = ""; + request += method + " " + url + " " + version + "\r\n"; + std::string param_order[] = + { + "Host", + "User-Agent", + "Accept", + "Accept-Language", + "Accept-Encoding", + "Connection", + "Upgrade-Insecure-Requests", + "Sec-Fetch-Dest", + "Sec-Fetch-Mode", + "Sec-Fetch-Site" + }; + if (params.size()) for ( const auto& param : param_order) if (params[param].length()) request += param + ": " + params[param] + "\r\n"; + request += "\r\n"; + if (body.length()) request += body; + return request; +} + +HttpReponse::HttpReponse(std::string status, +std::string version, +HttpParams params, +std::string body) +: status(status), version(version), params(params), body(body) {} + +HttpReponse::HttpReponse(std::string data) +{ + status = ""; + version = ""; + body = ""; +} + +std::string HttpReponse::to_string() +{ + std::string reponse = ""; + reponse = version + " " + status + " \r\n"; + if (params.size()) for ( const auto& pair : params) reponse += pair.first + ": " + pair.second + " \r\n"; + reponse += "\r\n"; + if (body.length()) reponse += body; + return reponse; +} \ No newline at end of file diff --git a/sandbox/server/http/http_packet.h b/sandbox/server/http/http_packet.h new file mode 100755 index 0000000..2f371ef --- /dev/null +++ b/sandbox/server/http/http_packet.h @@ -0,0 +1,44 @@ +#pragma once + +#include +#include "http_tools.h" + +using HttpParams = std::unordered_map; + +class HttpRequest +{ + public: + HttpRequest(std::string method, + std::string url, + std::string version, + HttpParams params, + std::string body); + + HttpRequest(std::string data); + + std::string to_string(); + + std::string method; + std::string url; + std::string version; + HttpParams params; + std::string body; +}; + +class HttpReponse +{ + public: + HttpReponse(std::string status, + std::string version, + HttpParams params, + std::string body); + + HttpReponse(std::string data); + + std::string to_string(); + + std::string status; + std::string version; + HttpParams params; + std::string body; +}; \ No newline at end of file diff --git a/sandbox/server/http/http_server.cpp b/sandbox/server/http/http_server.cpp new file mode 100755 index 0000000..0e92ff4 --- /dev/null +++ b/sandbox/server/http/http_server.cpp @@ -0,0 +1,38 @@ +#include "http_server.h" + +void http_server() +{ + IpAddress ip("127.0.0.1"); + Socket* sock = Socket::Create(ip, PORT, IpProtocol::TCP).release(); + bool running = sock->init() && sock->start(5); + char input = 'n'; + + do + { + Connection conn = sock->ack(); + if (conn >= 0) + { + log("New Conn") + std::string http_request(http_handler(sock, conn)); + + if (http_request == "") continue; + HttpRequest req(http_request); + log("to string") + log(req.to_string()) + close(conn); + } + log("Close?") + input(input); + } while (input != 'y'); + delete sock; +} + +std::string http_handler(Socket* sock, Connection conn) +{ + Packet req; + while(req.append_data(sock->obtain(conn, 4))); + int req_size = req.size(); + std::unique_ptr req_test = req.pull(req_size); + if (req_size) return std::string(req_test.release(), req_size); + return ""; +} \ No newline at end of file diff --git a/sandbox/server/http/http_server.h b/sandbox/server/http/http_server.h new file mode 100755 index 0000000..91a2357 --- /dev/null +++ b/sandbox/server/http/http_server.h @@ -0,0 +1,8 @@ +#pragma once + +#include +#include "http_packet.h" + +using namespace Bk::Net; +void http_server(); +std::string http_handler(Socket* sock, Connection conn); \ No newline at end of file diff --git a/sandbox/server/http/http_tools.cpp b/sandbox/server/http/http_tools.cpp new file mode 100755 index 0000000..64c1b79 --- /dev/null +++ b/sandbox/server/http/http_tools.cpp @@ -0,0 +1,43 @@ +#include "http_tools.h" + +std::string string_to_lower(std::string& str) +{ + for (int i = 0; i < str.length(); i++) + { + str[i] = std::tolower(str[i]); + } + return str; +} + +std::string string_to_upper(std::string& str) +{ + for (int i = 0; i < str.length(); i++) + { + str[i] = std::toupper(str[i]); + } + return str; +} + +std::unique_ptr> string_split(std::string& str, std::string delimiter, int cpt) +{ + std::string s(str); + std::unique_ptr> splits(new std::vector(0)); + size_t pos = 0; + while (((pos = s.find(delimiter)) != std::string::npos) && cpt-- != 0) + { + splits->push_back(s.substr(0, pos)); + s.erase(0, pos + delimiter.length()); + } + if (s.length()) splits->push_back(s); + return splits; +} + +void string_trim(std::string& str, const std::string& whitespace) +{ + const auto strBegin = str.find_first_not_of(whitespace); + const auto strEnd = str.find_last_not_of(whitespace); + if (strBegin != std::string::npos) + { + str.erase(0, strBegin); + } +} \ No newline at end of file diff --git a/sandbox/server/http/http_tools.h b/sandbox/server/http/http_tools.h new file mode 100755 index 0000000..aedba0e --- /dev/null +++ b/sandbox/server/http/http_tools.h @@ -0,0 +1,9 @@ +#pragma once + +#include +#include + +std::string string_to_lower(std::string& str); +std::string string_to_upper(std::string& str); +std::unique_ptr> string_split(std::string& str, std::string delimiter, int cpt = -1); +void string_trim(std::string& str, const std::string& whitespace = " \b\0"); \ No newline at end of file diff --git a/sandbox/server/main.cpp b/sandbox/server/main.cpp new file mode 100755 index 0000000..7ac4cf7 --- /dev/null +++ b/sandbox/server/main.cpp @@ -0,0 +1,11 @@ +#include +#include "http/http_server.h" + +int main() +{ + http_server(); + return 0; +} + + +