From 644fada095612ec1b2bac26187b9d96cb964e028 Mon Sep 17 00:00:00 2001 From: anulax1225 Date: Mon, 4 Mar 2024 11:34:13 +0100 Subject: [PATCH] Continuing client/server http --- sandbox/client/client.cpp | 9 ++++-- sandbox/commun.h | 4 ++- sandbox/server/server.cpp | 68 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 72 insertions(+), 9 deletions(-) diff --git a/sandbox/client/client.cpp b/sandbox/client/client.cpp index 5abef3d..661c6bc 100644 --- a/sandbox/client/client.cpp +++ b/sandbox/client/client.cpp @@ -15,15 +15,18 @@ void http_client() log(dns_lookup("edus2.rpn.ch", IpVersion::IPv4)[0]) IpAddress ip("127.0.0.1"); - Socket sock(ip, 80, IpProtocol::TCP); + Socket sock(ip, PORT, IpProtocol::TCP); - if(!sock.conn()) { + if(!sock.conn()) + { perror("Couldn't connect to the end point."); exit(1); } Packet packet; - std::string str("GET / HTTP/1.1\r\n\r\n"); + std::string str = "GET / HTTP/1.1\r\n" + "Host: 127.0.0.1:10001\r\n\r\n" + "Body jylkdkjlkjlkjlkjlkj"; packet.push(str.c_str(), str.length()); sock.send(packet.payload); diff --git a/sandbox/commun.h b/sandbox/commun.h index e837fb9..8843439 100644 --- a/sandbox/commun.h +++ b/sandbox/commun.h @@ -5,4 +5,6 @@ #include #include -#define log(str) std::cout << str << "\n"; \ No newline at end of file +#define log(str) std::cout << str << "\n"; + +#define PORT 10001 \ No newline at end of file diff --git a/sandbox/server/server.cpp b/sandbox/server/server.cpp index b346432..2edce3f 100644 --- a/sandbox/server/server.cpp +++ b/sandbox/server/server.cpp @@ -2,8 +2,20 @@ using namespace Bk::Net; +enum class HttpMethod +{ + NONE = 0, + GET = 1, + POST = 2, + PUT = 3, + DELETE = 4, +}; + void http_server(); -std::string http_handle(Socket& sock, Connection conn); +std::string http_handler(Socket& sock, Connection conn); +void http_parser(std::string req); +HttpMethod resolve_methode(std::string method); +std::unique_ptr> string_split(std::string s, std::string delimiter); int main() { @@ -14,22 +26,23 @@ int main() void http_server() { IpAddress ip("127.0.0.1"); - Socket sock(ip, 80, IpProtocol::TCP); + Socket sock(ip, PORT, IpProtocol::TCP); bool running = sock.init() && sock.start(5); while (running) { Connection conn = sock.ack(); if (conn >= 0) { - log(http_handle(sock, conn)); + std::string http_request(http_handler(sock, conn)); + http_parser(http_request); } } + } -std::string http_handle(Socket& sock, Connection conn) +std::string http_handler(Socket& sock, Connection conn) { Packet req; - bool reading = true; while(reading) { @@ -45,4 +58,49 @@ std::string http_handle(Socket& sock, Connection conn) if (req_size) return std::string(req_test.release(), req_size); return ""; +} + +void http_parser(std::string req) +{ + auto lines = string_split(req, "\r\n"); + for (auto line : *lines) + { + auto param = string_split(line, ":"); + + } + +} + + +HttpMethod resolve_methode(std::string method) +{ + if (method == "GET") return HttpMethod::GET; + else if (method == "POST") return HttpMethod::POST; + else if (method == "PUT") return HttpMethod::PUT; + else if (method == "DELETE") return HttpMethod::DELETE; + else return HttpMethod::NONE; +} + +std::unique_ptr> string_split(std::string s, std::string delimiter) +{ + std::unique_ptr> splits(new std::vector(0)); + size_t pos = 0; + while ((pos = s.find(delimiter)) != std::string::npos) { + splits->push_back(s.substr(0, pos)); + s.erase(0, pos + delimiter.length()); + } + splits->push_back(s); + return splits; +} + +std::string string_trim(const std::string& str, const std::string& whitespace = " \t") +{ + const auto strBegin = str.find_first_not_of(whitespace); + if (strBegin == std::string::npos) + return ""; // no content + + const auto strEnd = str.find_last_not_of(whitespace); + const auto strRange = strEnd - strBegin + 1; + + return str.substr(strBegin, strRange); } \ No newline at end of file