From 75ecb8e33c58cb9e43f5c1f956d30d46ea952dd3 Mon Sep 17 00:00:00 2001 From: anulax1225 Date: Thu, 14 Mar 2024 09:54:26 +0100 Subject: [PATCH] Deleted http tools from sandbox --- sandbox/commun.h | 12 ----- sandbox/premake5.lua | 1 - 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 | 27 ++++++++-- 9 files changed, 24 insertions(+), 237 deletions(-) delete mode 100755 sandbox/commun.h delete mode 100755 sandbox/server/http/http_packet.cpp delete mode 100755 sandbox/server/http/http_packet.h delete mode 100755 sandbox/server/http/http_server.cpp delete mode 100755 sandbox/server/http/http_server.h delete mode 100755 sandbox/server/http/http_tools.cpp delete mode 100755 sandbox/server/http/http_tools.h diff --git a/sandbox/commun.h b/sandbox/commun.h deleted file mode 100755 index 28323a1..0000000 --- a/sandbox/commun.h +++ /dev/null @@ -1,12 +0,0 @@ -#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 index cfdf8b4..9d87fbc 100755 --- a/sandbox/premake5.lua +++ b/sandbox/premake5.lua @@ -11,7 +11,6 @@ project "server" includedirs { "%{IncludeDirs.bakanet}", - "./" } files diff --git a/sandbox/server/http/http_packet.cpp b/sandbox/server/http/http_packet.cpp deleted file mode 100755 index 8532ba5..0000000 --- a/sandbox/server/http/http_packet.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#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 deleted file mode 100755 index 2f371ef..0000000 --- a/sandbox/server/http/http_packet.h +++ /dev/null @@ -1,44 +0,0 @@ -#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 deleted file mode 100755 index 0e92ff4..0000000 --- a/sandbox/server/http/http_server.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#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 deleted file mode 100755 index 91a2357..0000000 --- a/sandbox/server/http/http_server.h +++ /dev/null @@ -1,8 +0,0 @@ -#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 deleted file mode 100755 index 64c1b79..0000000 --- a/sandbox/server/http/http_tools.cpp +++ /dev/null @@ -1,43 +0,0 @@ -#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 deleted file mode 100755 index aedba0e..0000000 --- a/sandbox/server/http/http_tools.h +++ /dev/null @@ -1,9 +0,0 @@ -#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 index 7ac4cf7..6b64d49 100755 --- a/sandbox/server/main.cpp +++ b/sandbox/server/main.cpp @@ -1,9 +1,30 @@ -#include -#include "http/http_server.h" +#include + +#define PORT 8000 + +using namespace Bk::Net; int main() { - http_server(); + IpAddress ip; + HttpServer server(ip, PORT); + server.get("/", [](HttpRequest req) + { + log("Path /") + HttpReponse res(HTTP_RES_200, req.version); + res.body = "

Hello World! " + req.url + "

"; + return res; + }); + + server.get("/home/", [](HttpRequest req) + { + log("Path /home/") + HttpReponse res(HTTP_RES_200, req.version); + res.body = "

Hello World! " + req.url + "

"; + return res; + }); + + server.start(); return 0; }