Fixed webkit issue, removed getlineAndCount, default is 0.0.0.0 now, AI aided on header processing rewrite
This commit is contained in:
@@ -10,31 +10,22 @@
|
|||||||
void HTTPrequest::processHTTPHeader() {
|
void HTTPrequest::processHTTPHeader() {
|
||||||
std::shared_ptr<HTTPrequest> self(shared_from_this());
|
std::shared_ptr<HTTPrequest> self(shared_from_this());
|
||||||
|
|
||||||
// HEADER read
|
|
||||||
//
|
|
||||||
asio::async_read_until(
|
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) {
|
[this, self](std::error_code error, std::size_t packageSize) {
|
||||||
if (!error) {
|
if (!error) {
|
||||||
buffer.commit(packageSize);
|
|
||||||
std::istream stream(&buffer);
|
std::istream stream(&buffer);
|
||||||
std::size_t octetCount = 0;
|
|
||||||
|
|
||||||
// This is HTTP main request
|
// Process request line
|
||||||
std::string rq;
|
std::string rq;
|
||||||
Helpers::getlineAndCount(stream, octetCount, requestType,
|
std::getline(stream, requestType, ' '); // HTTP request type
|
||||||
' '); // HTTP request type
|
std::getline(stream, requestPath, ' '); // HTTP path
|
||||||
Helpers::getlineAndCount(stream, octetCount, requestPath,
|
std::getline(stream, rq); // Version, omitting for now
|
||||||
' '); // HTTP path
|
|
||||||
Helpers::getlineAndCount(stream, octetCount,
|
|
||||||
rq); // Version, omitting for now
|
|
||||||
|
|
||||||
// Get arguments and other header stuff out of the way
|
|
||||||
processArgs();
|
processArgs();
|
||||||
processHeaderValues(stream, octetCount, packageSize);
|
processHeaderValues(stream);
|
||||||
processBody();
|
processBody();
|
||||||
|
|
||||||
// RESPOND
|
|
||||||
processRequest();
|
processRequest();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -58,16 +49,26 @@ void inline HTTPrequest::processArgs() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void inline HTTPrequest::processHeaderValues(std::basic_istream<char> &stream,
|
// No lie AI generated this and I just adapted it
|
||||||
std::size_t &octetCount,
|
void inline HTTPrequest::processHeaderValues(std::basic_istream<char> &stream) {
|
||||||
std::size_t headerSize) {
|
std::string line;
|
||||||
std::string key, value, empty;
|
while (std::getline(stream, line) && line != "\r") {
|
||||||
while (octetCount < headerSize - 2) { // -2 is due to last two \r\n\r\n combo
|
if (line.back() == '\r') {
|
||||||
Helpers::getlineAndCount(stream, octetCount, key, ':');
|
|
||||||
Helpers::getlineAndCount(stream, octetCount, empty,
|
line.pop_back();
|
||||||
' '); // trim start some gangster shit right here
|
}
|
||||||
Helpers::getlineAndCount(stream, octetCount, value);
|
|
||||||
headers.insert({key, value});
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
#include "Helpers.hpp"
|
#include "Helpers.hpp"
|
||||||
#include "Main.hpp"
|
#include "Main.hpp"
|
||||||
|
#include <cstdint>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
// Idea is very simple this map has it so that stuff like
|
// Idea is very simple this map has it so that stuff like
|
||||||
// responseMethods["GET","/"] have a method inside them
|
// responseMethods["GET","/"] have a method inside them
|
||||||
@@ -18,8 +20,16 @@ void Webserver::initResponses() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
responseMethods["POST"]["/upload"] = [](HTTPrequest *self) {
|
responseMethods["POST"]["/upload"] = [](HTTPrequest *self) {
|
||||||
|
/*
|
||||||
auto result = Helpers::processFormData(self->bodyContent,
|
auto result = Helpers::processFormData(self->bodyContent,
|
||||||
self->headers["Content-Type"]);
|
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);
|
self->sendResponse("200 OK", "text/text", self->bodyContent);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@@ -1,4 +1,3 @@
|
|||||||
#include <cstdint>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
@@ -9,8 +8,6 @@ struct formData {
|
|||||||
};
|
};
|
||||||
|
|
||||||
std::string ReadFile(std::string Path);
|
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>
|
std::unordered_map<std::string, Helpers::formData>
|
||||||
processFormData(std::string data, std::string contentType);
|
processFormData(std::string data, std::string contentType);
|
||||||
namespace detail_processFormData {
|
namespace detail_processFormData {
|
||||||
|
@@ -12,14 +12,3 @@ std::string Helpers::ReadFile(std::string Path) {
|
|||||||
file.close();
|
file.close();
|
||||||
return contents.str();
|
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;
|
|
||||||
}
|
|
||||||
|
@@ -14,7 +14,7 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
#define IP "127.0.0.1"
|
#define IP "0.0.0.0"
|
||||||
#define PORT 8000
|
#define PORT 8000
|
||||||
using asocket = asio::ip::tcp::socket;
|
using asocket = asio::ip::tcp::socket;
|
||||||
using asocket_ptr = std::shared_ptr<asocket>;
|
using asocket_ptr = std::shared_ptr<asocket>;
|
||||||
@@ -43,9 +43,7 @@ private:
|
|||||||
// Breaking Header to in lines
|
// Breaking Header to in lines
|
||||||
void processHTTPHeader();
|
void processHTTPHeader();
|
||||||
void inline processArgs();
|
void inline processArgs();
|
||||||
void inline processHeaderValues(std::basic_istream<char> &stream,
|
void inline processHeaderValues(std::basic_istream<char> &stream);
|
||||||
std::size_t &octetCount,
|
|
||||||
std::size_t headerSize);
|
|
||||||
void inline processBody();
|
void inline processBody();
|
||||||
|
|
||||||
// Networking
|
// Networking
|
||||||
|
Reference in New Issue
Block a user