Start of the body processing
This commit is contained in:
82
src/HTTP.cpp
82
src/HTTP.cpp
@@ -1,6 +1,13 @@
|
|||||||
#include "Helpers.hpp"
|
#include "Helpers.hpp"
|
||||||
#include "main.hpp"
|
#include "main.hpp"
|
||||||
|
#include <asio/completion_condition.hpp>
|
||||||
|
#include <asio/impl/read_until.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <istream>
|
||||||
|
#include <ostream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <string>
|
||||||
|
#include <system_error>
|
||||||
|
|
||||||
void HTTPrequest::start() {
|
void HTTPrequest::start() {
|
||||||
// Possible Logging here
|
// Possible Logging here
|
||||||
@@ -15,44 +22,79 @@ void HTTPrequest::readData() {
|
|||||||
|
|
||||||
// TODO: Read until headers, headers contains the file size then allocate that
|
// TODO: Read until headers, headers contains the file size then allocate that
|
||||||
// much memory on buffer, and THEN start reading the file
|
// much memory on buffer, and THEN start reading the file
|
||||||
sock.async_read_some(
|
|
||||||
buffer.prepare(2048),
|
// HEADER read
|
||||||
|
asio::async_read_until(
|
||||||
|
sock, buffer, "\r\n\r\n", // its not the other way bud
|
||||||
[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);
|
buffer.commit(packageSize);
|
||||||
std::istream stream(&buffer);
|
|
||||||
std::unordered_map<std::string, std::string> request;
|
std::unordered_map<std::string, std::string> request;
|
||||||
|
std::unordered_map<std::string, std::string> args;
|
||||||
|
std::istream stream(&buffer);
|
||||||
|
uint64_t bitCount = 0;
|
||||||
|
|
||||||
// This is HTTP main request
|
// This is HTTP main request
|
||||||
std::string rq, key, value, type, path;
|
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
|
Helpers::getlineAndCount(stream, bitCount, type,
|
||||||
while (buffer.size() > 0) {
|
' '); // HTTP request type
|
||||||
std::getline(stream, key, ':');
|
Helpers::getlineAndCount(stream, bitCount, path, ' '); // HTTP path
|
||||||
std::getline(stream, rq, ' '); // trim start
|
Helpers::getlineAndCount(stream, bitCount,
|
||||||
// some gangster shit right here
|
rq); // Version, omitting for now
|
||||||
std::getline(stream, value);
|
|
||||||
|
|
||||||
if (type == "GET") {
|
// Quick stop to process arguments
|
||||||
|
std::stringstream pathStream(path);
|
||||||
|
std::getline(pathStream, path, '?'); // Keeping the path pure
|
||||||
|
|
||||||
request.insert({key, value});
|
while (!pathStream.eof()) {
|
||||||
} else {
|
std::getline(pathStream, key, '=');
|
||||||
std::cout << key << " " << value << "\n";
|
std::getline(pathStream, value, '&');
|
||||||
}
|
args.insert({key, value});
|
||||||
|
std::cout << key << " " << value << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
processRequest(type, path, request); // This writes data too
|
// Rest of the request vals
|
||||||
sock.close(); // Responded
|
while (bitCount <
|
||||||
|
packageSize - 2) { // -2 is due to last two \r\n\r\n combo
|
||||||
|
Helpers::getlineAndCount(stream, bitCount, key, ':');
|
||||||
|
Helpers::getlineAndCount(
|
||||||
|
stream, bitCount, rq,
|
||||||
|
' '); // trim start some gangster shit right here
|
||||||
|
Helpers::getlineAndCount(stream, bitCount, value);
|
||||||
|
request.insert({key, value});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================
|
||||||
|
// BODY OF THE REQUEST
|
||||||
|
// ===================
|
||||||
|
std::size_t uploadSize =
|
||||||
|
std::stoull(request.at("Content-Length")) - buffer.size();
|
||||||
|
asio::async_read(
|
||||||
|
sock, buffer.prepare(uploadSize),
|
||||||
|
asio::transfer_exactly(uploadSize),
|
||||||
|
[self, this](std::error_code error, std::size_t packageSize) {
|
||||||
|
buffer.commit(packageSize);
|
||||||
|
std::istream bodyStream(&buffer);
|
||||||
|
std::string body;
|
||||||
|
body.resize(buffer.size());
|
||||||
|
bodyStream.read(body.data(), buffer.size());
|
||||||
|
|
||||||
|
std::cout << body << "\ntest" << std::endl;
|
||||||
|
buffer.consume(buffer.size());
|
||||||
|
sock.close();
|
||||||
|
});
|
||||||
|
|
||||||
|
processRequest(type, path, request, args); // This writes data too
|
||||||
|
sock.close();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
void HTTPrequest::processRequest(
|
void HTTPrequest::processRequest(
|
||||||
std::string requestType, std::string requestPath,
|
std::string requestType, std::string requestPath,
|
||||||
std::unordered_map<std::string, std::string> request) {
|
std::unordered_map<std::string, std::string> request,
|
||||||
|
std::unordered_map<std::string, std::string> args) {
|
||||||
//
|
//
|
||||||
// This is where we will process requests
|
// This is where we will process requests
|
||||||
//
|
//
|
||||||
|
@@ -12,6 +12,16 @@ std::string Helpers::ReadFile(std::string Path) {
|
|||||||
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;
|
||||||
|
}
|
||||||
std::string Helpers::GenerateResponse(std::string statusCode,
|
std::string Helpers::GenerateResponse(std::string statusCode,
|
||||||
std::string contentType,
|
std::string contentType,
|
||||||
std::string content) {
|
std::string content) {
|
||||||
|
@@ -7,7 +7,8 @@
|
|||||||
|
|
||||||
namespace Helpers {
|
namespace Helpers {
|
||||||
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::string GenerateResponse(std::string statusCode, std::string contentType,
|
std::string GenerateResponse(std::string statusCode, std::string contentType,
|
||||||
std::string content);
|
std::string content);
|
||||||
// ===========
|
// ===========
|
||||||
|
@@ -32,7 +32,8 @@ private:
|
|||||||
HTTPrequest(asio::io_context &context);
|
HTTPrequest(asio::io_context &context);
|
||||||
void readData();
|
void readData();
|
||||||
void processRequest(std::string requestType, std::string requestPath,
|
void processRequest(std::string requestType, std::string requestPath,
|
||||||
std::unordered_map<std::string, std::string> request);
|
std::unordered_map<std::string, std::string> request,
|
||||||
|
std::unordered_map<std::string, std::string> args);
|
||||||
void writeData(std::string data);
|
void writeData(std::string data);
|
||||||
|
|
||||||
asio::ip::tcp::socket sock;
|
asio::ip::tcp::socket sock;
|
||||||
|
Reference in New Issue
Block a user