forked from cat/WebBase
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() {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,5 +1,7 @@
|
||||
#include "Helpers.hpp"
|
||||
#include "Main.hpp"
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
|
||||
// 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);
|
||||
};
|
||||
}
|
||||
|
@@ -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 {
|
||||
|
@@ -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;
|
||||
}
|
||||
|
@@ -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
|
||||
|
Reference in New Issue
Block a user