Template
1
0

Compare commits

..

4 Commits

5 changed files with 42 additions and 56 deletions

View File

@@ -1,40 +1,31 @@
#include "../Helpers.hpp"
#include "../Main.hpp"
#include <asio/impl/read.hpp>
#include <asio/impl/read_until.hpp>
#include <asio/socket_base.hpp>
#include <iostream>
#include <istream>
#include <string>
// TODO: Remove boundary from the body. Do keep other stuff like
// file type and name
void HTTPrequest::processHTTPHeader() {
std::shared_ptr<HTTPrequest> 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<char> &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<char> &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;
}
}
}
@@ -83,7 +84,7 @@ void inline HTTPrequest::processBody() {
// This part needs to be sync since we want this specific instance
// execution to pause
while (octetCount < uploadSize) {
if (octetCount < uploadSize) {
octetCount += asio::read(sock, buffer.prepare(uploadSize));
}

View File

@@ -1,6 +1,7 @@
#include "Helpers.hpp"
#include "Main.hpp"
#include <sstream>
#include <cstdint>
#include <iostream>
// Idea is very simple this map has it so that stuff like
// responseMethods["GET","/"] have a method inside them
@@ -19,16 +20,16 @@ void Webserver::initResponses() {
};
responseMethods["POST"]["/upload"] = [](HTTPrequest *self) {
/*
auto result = Helpers::processFormData(self->bodyContent,
self->headers["Content-Type"]);
std::stringstream resp;
resp << "You have sent\n"
<< "filePassword = " << result["filePassword"].data << "\n"
<< "name = " << result["name"].data << "\n"
<< "numb = " << result["numb"].data << "\n"
<< "time = " << result["time"].data << "\n"
<< "uploadedFile = " << result["uploadedFile"].data << " as filetype "
<< result["uploadedFile"].headers["Content-Type"] << "\n";
self->sendResponse("200 OK", "text/text", resp.str());
for (char c : self->bodyContent) {
std::cout << ((uint32_t)c) << " ";
}
std::cout << "\n\n" << std::flush;
*/
self->sendResponse("200 OK", "text/text", self->bodyContent);
};
}

View File

@@ -1,4 +1,3 @@
#include <cstdint>
#include <string>
#include <unordered_map>
@@ -9,8 +8,6 @@ struct formData {
};
std::string ReadFile(std::string Path);
void getlineAndCount(std::basic_istream<char> &stream, uint64_t &count,
std::string &string, char delimit = '\0');
std::unordered_map<std::string, Helpers::formData>
processFormData(std::string data, std::string contentType);
namespace detail_processFormData {

View File

@@ -12,14 +12,3 @@ std::string Helpers::ReadFile(std::string Path) {
file.close();
return contents.str();
}
void Helpers::getlineAndCount(std::basic_istream<char> &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;
}

View File

@@ -14,7 +14,7 @@
#include <string>
#include <unordered_map>
#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<asocket>;
@@ -43,9 +43,7 @@ private:
// Breaking Header to in lines
void processHTTPHeader();
void inline processArgs();
void inline processHeaderValues(std::basic_istream<char> &stream,
std::size_t &octetCount,
std::size_t headerSize);
void inline processHeaderValues(std::basic_istream<char> &stream);
void inline processBody();
// Networking