diff --git a/src/HTTP/HTTPRequestProcess.cpp b/src/HTTP/HTTPRequestProcess.cpp index c237b9b..0af65ac 100644 --- a/src/HTTP/HTTPRequestProcess.cpp +++ b/src/HTTP/HTTPRequestProcess.cpp @@ -10,31 +10,22 @@ void HTTPrequest::processHTTPHeader() { std::shared_ptr self(shared_from_this()); - // HEADER read - // asio::async_read_until( - sock, buffer, "\r\n\r\n", // its not the other way bud + sock, buffer, "\r\n\r\n", [this, self](std::error_code error, std::size_t packageSize) { if (!error) { - buffer.commit(packageSize); std::istream stream(&buffer); - std::size_t octetCount = 0; - // This is HTTP main request + // Process request line std::string rq; - Helpers::getlineAndCount(stream, octetCount, requestType, - ' '); // HTTP request type - Helpers::getlineAndCount(stream, octetCount, requestPath, - ' '); // HTTP path - Helpers::getlineAndCount(stream, octetCount, - rq); // Version, omitting for now + std::getline(stream, requestType, ' '); // HTTP request type + std::getline(stream, requestPath, ' '); // HTTP path + std::getline(stream, rq); // Version, omitting for now - // Get arguments and other header stuff out of the way processArgs(); - processHeaderValues(stream, octetCount, packageSize); + processHeaderValues(stream); processBody(); - // RESPOND processRequest(); } }); @@ -58,16 +49,26 @@ void inline HTTPrequest::processArgs() { } } -void inline HTTPrequest::processHeaderValues(std::basic_istream &stream, - std::size_t &octetCount, - std::size_t headerSize) { - std::string key, value, empty; - while (octetCount < headerSize - 2) { // -2 is due to last two \r\n\r\n combo - Helpers::getlineAndCount(stream, octetCount, key, ':'); - Helpers::getlineAndCount(stream, octetCount, empty, - ' '); // trim start some gangster shit right here - Helpers::getlineAndCount(stream, octetCount, value); - headers.insert({key, value}); +// No lie AI generated this and I just adapted it +void inline HTTPrequest::processHeaderValues(std::basic_istream &stream) { + std::string line; + while (std::getline(stream, line) && line != "\r") { + if (line.back() == '\r') { + + line.pop_back(); + } + + size_t colonPos = line.find(':'); + if (colonPos != std::string::npos) { + std::string key = line.substr(0, colonPos); + std::string value = line.substr(colonPos + 1); + + // Trim whitespace + value.erase(0, value.find_first_not_of(" \t")); + value.erase(value.find_last_not_of(" \t") + 1); + + headers[key] = value; + } } } diff --git a/src/HTTPMethods.cpp b/src/HTTPMethods.cpp index f1080c3..eb52690 100644 --- a/src/HTTPMethods.cpp +++ b/src/HTTPMethods.cpp @@ -1,5 +1,7 @@ #include "Helpers.hpp" #include "Main.hpp" +#include +#include // Idea is very simple this map has it so that stuff like // responseMethods["GET","/"] have a method inside them @@ -18,8 +20,16 @@ void Webserver::initResponses() { }; responseMethods["POST"]["/upload"] = [](HTTPrequest *self) { + /* auto result = Helpers::processFormData(self->bodyContent, self->headers["Content-Type"]); + + for (char c : self->bodyContent) { + std::cout << ((uint32_t)c) << " "; + } + std::cout << "\n\n" << std::flush; + + */ self->sendResponse("200 OK", "text/text", self->bodyContent); }; } diff --git a/src/Helpers.hpp b/src/Helpers.hpp index b1b9c23..aba9d54 100644 --- a/src/Helpers.hpp +++ b/src/Helpers.hpp @@ -1,4 +1,3 @@ -#include #include #include @@ -9,8 +8,6 @@ struct formData { }; std::string ReadFile(std::string Path); -void getlineAndCount(std::basic_istream &stream, uint64_t &count, - std::string &string, char delimit = '\0'); std::unordered_map processFormData(std::string data, std::string contentType); namespace detail_processFormData { diff --git a/src/Helpers/Helpers.cpp b/src/Helpers/Helpers.cpp index e13a1a4..5fdb7e5 100644 --- a/src/Helpers/Helpers.cpp +++ b/src/Helpers/Helpers.cpp @@ -12,14 +12,3 @@ std::string Helpers::ReadFile(std::string Path) { file.close(); return contents.str(); } - -void Helpers::getlineAndCount(std::basic_istream &stream, uint64_t &count, - std::string &string, char delimit) { - if (delimit == '\0') { - std::getline(stream, string); - } else { - std::getline(stream, string, delimit); - } - count += string.size() + 1; // Delimiter - return; -} diff --git a/src/Main.hpp b/src/Main.hpp index aec1bc9..5bf1c4a 100644 --- a/src/Main.hpp +++ b/src/Main.hpp @@ -14,7 +14,7 @@ #include #include -#define IP "127.0.0.1" +#define IP "0.0.0.0" #define PORT 8000 using asocket = asio::ip::tcp::socket; using asocket_ptr = std::shared_ptr; @@ -43,9 +43,7 @@ private: // Breaking Header to in lines void processHTTPHeader(); void inline processArgs(); - void inline processHeaderValues(std::basic_istream &stream, - std::size_t &octetCount, - std::size_t headerSize); + void inline processHeaderValues(std::basic_istream &stream); void inline processBody(); // Networking