#include "Helpers.hpp" #include "main.hpp" #include void HTTPrequest::start() { // Possible Logging here readData(); } void HTTPrequest::readData() { // // Reading happens here // std::shared_ptr self(shared_from_this()); // TODO: Read until headers, headers contains the file size then allocate that // much memory on buffer, and THEN start reading the file sock.async_read_some( buffer.prepare(2048), [this, self](std::error_code error, std::size_t packageSize) { if (!error) { buffer.commit(packageSize); std::istream stream(&buffer); std::unordered_map request; // This is HTTP main request std::string rq, key, value, type, path; std::getline(stream, type, ' '); // HTTP request type std::getline(stream, path, ' '); // HTTP path std::getline(stream, rq); // Removes HTTP version part no need rn // Rest of the request vals while (buffer.size() > 0) { std::getline(stream, key, ':'); std::getline(stream, rq, ' '); // trim start // some gangster shit right here std::getline(stream, value); if (type == "GET") { request.insert({key, value}); } else { std::cout << key << " " << value << "\n"; } } processRequest(type, path, request); // This writes data too sock.close(); // Responded } }); } void HTTPrequest::processRequest( std::string requestType, std::string requestPath, std::unordered_map request) { // // This is where we will process requests // uint64_t pathHash = Helpers::Pathhash(requestPath); // This is very much temp if (requestType == "POST") { switch (pathHash) { case "/upload"_hash: writeData(Helpers::GenerateResponse("501 Not Implemented", "text/text", "This path is not implemented yet!")); return; } return; } // This can be further refactored to just "File send" switch (pathHash) { case "/"_hash: writeData(Helpers::GenerateResponse("200 OK", "text/html", Helpers::ReadFile("www/index.html"))); return; default: writeData(Helpers::GenerateResponse("404 Not Found", "text/html", "Could not find that file!!")); return; } } void HTTPrequest::writeData(std::string data) { // // Response here // asio::async_write(sock, asio::buffer(data), [](std::error_code, std::size_t) {}); } // ================= CLASS HANDLING SPECIFIC ================= HTTPrequest::HTTPrequest(asio::io_context &context) : sock(context) {} asio::ip::tcp::socket &HTTPrequest::socket() { return sock; } HTTPrequest::HTTPrequest_ptr HTTPrequest::create(asio::io_context &context) { return HTTPrequest_ptr(new HTTPrequest(context)); }