Template
1
0

Added processFormData and reorganised the files

This commit is contained in:
2025-07-07 03:11:41 +03:00
parent 4962bb5a2d
commit 481b4b410a
10 changed files with 114 additions and 15 deletions

View File

@@ -9,13 +9,14 @@ set(CMAKE_CXX_FLAGS "-Wall -g -fsanitize=address")
add_executable(
Webserver
"src/Entry.cpp"
"src/Webserver.cpp"
"src/HTTP.cpp"
"src/Base/Entry.cpp"
"src/Base/Webserver.cpp"
"src/HTTP/HTTP.cpp"
"libs/QuickDigest5/quickdigest5.cpp"
"src/Helpers.cpp"
"src/HTTPRequestProcess.cpp"
"./src/HTTPMethods.cpp")
"src/Helpers/Helpers.cpp"
"src/HTTP/HTTPRequestProcess.cpp"
"src/HTTPMethods.cpp"
"src/Helpers/HelpersFormProcess.cpp")
add_custom_command(
TARGET Webserver

View File

@@ -1,4 +1,4 @@
#include "Main.hpp"
#include "../Main.hpp"
#include <exception>
#include <iostream>

View File

@@ -1,4 +1,4 @@
#include "Main.hpp"
#include "../Main.hpp"
#include <iostream>
#include <ostream>

View File

@@ -1,5 +1,5 @@
#include "Helpers.hpp"
#include "Main.hpp"
#include "../Helpers.hpp"
#include "../Main.hpp"
#include <stdexcept>
void HTTPrequest::start() {

View File

@@ -1,5 +1,5 @@
#include "Helpers.hpp"
#include "Main.hpp"
#include "../Helpers.hpp"
#include "../Main.hpp"
#include <asio/socket_base.hpp>
#include <iostream>
#include <istream>

View File

@@ -1,5 +1,6 @@
#include "Helpers.hpp"
#include "Main.hpp"
#include <sstream>
// Idea is very simple this map has it so that stuff like
// responseMethods["GET","/"] have a method inside them
@@ -18,6 +19,16 @@ void Webserver::initResponses() {
};
responseMethods["POST"]["/upload"] = [](HTTPrequest *self) {
self->sendResponse("200 OK", "text/text", self->bodyContent);
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());
};
}

View File

@@ -1,8 +1,20 @@
#include <cstdint>
#include <string>
#include <unordered_map>
namespace Helpers {
struct formData {
std::string data;
std::unordered_map<std::string, std::string> headers;
};
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 {
std::string inline getFieldName(const std::string &dataBlock);
std::string inline getBodyData(const std::string &dataBlock);
} // namespace detail_processFormData
} // namespace Helpers

View File

@@ -1,5 +1,6 @@
#include "Helpers.hpp"
#include "../Helpers.hpp"
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

View File

@@ -0,0 +1,70 @@
#include "../Helpers.hpp"
// FORM PROCESSOR
std::unordered_map<std::string, Helpers::formData>
Helpers::processFormData(std::string data, std::string contentType) {
std::unordered_map<std::string, Helpers::formData> formDataMap;
std::string boundary =
"--" +
contentType.substr(contentType.find('=') +
1); // Boundary for some fucking reason misses --
std::size_t currentPos = 0;
std::size_t nextBoundaryPos = 0;
while (nextBoundaryPos != std::string::npos) {
Helpers::formData fieldData;
// Find the current data block
std::size_t blockStart = currentPos + boundary.size() + 1; // +1 due to \n
nextBoundaryPos = data.find(boundary, blockStart);
std::size_t blockEnd =
(nextBoundaryPos != std::string::npos)
? nextBoundaryPos
: data.size() - boundary.size() - 3; // - 3 for final "--\n"
std::string dataBlock = data.substr(blockStart, blockEnd - blockStart);
std::string fieldName = detail_processFormData::getFieldName(dataBlock);
// Process headers
std::size_t headersEnd = dataBlock.find("\r\n\r\n"), headerPos = 0;
while (headerPos < headersEnd) {
std::size_t colonPos = dataBlock.find(":", headerPos),
lineEnd = dataBlock.find("\r\n", colonPos);
if (colonPos == std::string::npos || lineEnd == std::string::npos)
break;
fieldData.headers.insert(
{dataBlock.substr(headerPos, colonPos - headerPos),
dataBlock.substr(colonPos + 2,
lineEnd - colonPos - 2)}); // -/+ 2's are for \r\n
headerPos = lineEnd + 2; // Same as above
}
fieldData.data = detail_processFormData::getBodyData(dataBlock);
formDataMap.insert({fieldName, fieldData});
currentPos = nextBoundaryPos;
}
return formDataMap;
}
// ===================
// Inline Helpers
// ===================
std::string inline Helpers::detail_processFormData::getFieldName(
const std::string &dataBlock) {
size_t nameStart = dataBlock.find("name=") + 6; // Skip 'name="'
size_t nameEnd = dataBlock.find("\"", nameStart);
return dataBlock.substr(nameStart, nameEnd - nameStart);
}
std::string inline Helpers::detail_processFormData::getBodyData(
const std::string &dataBlock) {
size_t bodyStart = dataBlock.find("\r\n\r\n") + 4; // Skip "\r\n\r\n"
size_t bodyLength =
dataBlock.size() - bodyStart - 2; // Remove trailing "\r\n"
return dataBlock.substr(bodyStart, bodyLength);
}

View File

@@ -11,7 +11,11 @@
<h1>Welcome</h1>
<h1>POST test</h1>
<form action="/upload" method="POST" enctype="multipart/form-data">
<input type="file" name="uploaded_file">
<input type="text" name="filePassword">
<input type="text" name="name">
<input type="number" name="numb">
<input type="date" name="time">
<input type="file" name="uploadedFile">
<input type="submit" value="Upload">
</form>
</body>