diff --git a/sandbox/server/http/http_parser.cpp b/sandbox/server/http/http_parser.cpp new file mode 100644 index 0000000..8554440 --- /dev/null +++ b/sandbox/server/http/http_parser.cpp @@ -0,0 +1,42 @@ +#include "http_parser.h" + +HttpRequest http_parser(std::string req) +{ + std::string url = "", method = "", body = ""; + std::unordered_map params; + + auto lines = string_split(req, "\n"); + + auto first_line = string_split(lines->at(0), " "); + method = string_to_upper(first_line->at(0)); + url = first_line->at(1); + body = lines->at(lines->size() - 1); + + lines->erase(lines->begin()); + lines->pop_back(); + + for (auto line : *lines) + { + auto param = string_split(line, ":", 1); + if (param->size() == 2) + { + params.insert({ param->at(0), string_trim(param->at(1))}); + } + } + return HttpRequest { + http_resolve_methode(method), + url, + params, + body + }; +} + + +HttpMethod http_resolve_methode(std::string method) +{ + if (method.compare("GET")) return HttpMethod::GET; + else if (method.compare("POST")) return HttpMethod::POST; + else if (method.compare("PUT")) return HttpMethod::PUT; + else if (method.compare("DELETE")) return HttpMethod::DELETE; + else return HttpMethod::NONE; +} \ No newline at end of file diff --git a/sandbox/server/http/http_parser.h b/sandbox/server/http/http_parser.h new file mode 100644 index 0000000..d725ebd --- /dev/null +++ b/sandbox/server/http/http_parser.h @@ -0,0 +1,26 @@ +#include + +#include + +#include "http_tools.h" + +enum class HttpMethod +{ + NONE = 0, + GET = 1, + POST = 2, + PUT = 3, + DELETE = 4, +}; + +class HttpRequest +{ + public: + HttpMethod method; + std::string url; + std::unordered_map params; + std::string body; +}; + +HttpRequest http_parser(std::string req); +HttpMethod http_resolve_methode(std::string method); \ 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 100644 index 0000000..49c1d0e --- /dev/null +++ b/sandbox/server/http/http_server.cpp @@ -0,0 +1,48 @@ +#include "http_server.h" + +void http_server() +{ + IpAddress ip("127.0.0.1"); + Socket sock(ip, PORT, IpProtocol::TCP); + bool running = sock.init() && sock.start(5); + char input = 'N'; + + do + { + Connection conn = sock.ack(); + if (conn >= 0) + { + std::string http_request(http_handler(sock, conn)); + + if (http_request == "") continue; + HttpRequest req = http_parser(http_request); + log("Http request"); + log("Method " << (int)req.method) + log("URL " << req.url) + log("Body " << req.body) + } + log("Close?") + input(input); + } while (input != 'y'); + +} + +std::string http_handler(Socket& sock, Connection conn) +{ + Packet req; + bool reading = true; + while(reading) + { + std::vector raw_data; + raw_data = sock.recv(conn, 4); + reading = req.append_data(raw_data); + } + + close(conn); + + 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 100644 index 0000000..f8c6e69 --- /dev/null +++ b/sandbox/server/http/http_server.h @@ -0,0 +1,7 @@ +#include + +#include "http_parser.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 100644 index 0000000..d192bf4 --- /dev/null +++ b/sandbox/server/http/http_tools.cpp @@ -0,0 +1,34 @@ +#include "http_tools.h" + +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 s, std::string delimiter, int cpt) +{ + 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()); + } + splits->push_back(s); + return splits; +} + +std::string string_trim(const std::string& str, const std::string& whitespace) +{ + 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 diff --git a/sandbox/server/http/http_tools.h b/sandbox/server/http/http_tools.h new file mode 100644 index 0000000..c555b7d --- /dev/null +++ b/sandbox/server/http/http_tools.h @@ -0,0 +1,7 @@ +#include + +#include + +std::string string_to_upper(std::string& str); +std::unique_ptr> string_split(std::string s, std::string delimiter, int cpt = -1); +std::string string_trim(const std::string& str, const std::string& whitespace = " "); \ No newline at end of file