forked from cat/WebBase
Added processFormData and reorganised the files
This commit is contained in:
@@ -9,13 +9,14 @@ set(CMAKE_CXX_FLAGS "-Wall -g -fsanitize=address")
|
|||||||
|
|
||||||
add_executable(
|
add_executable(
|
||||||
Webserver
|
Webserver
|
||||||
"src/Entry.cpp"
|
"src/Base/Entry.cpp"
|
||||||
"src/Webserver.cpp"
|
"src/Base/Webserver.cpp"
|
||||||
"src/HTTP.cpp"
|
"src/HTTP/HTTP.cpp"
|
||||||
"libs/QuickDigest5/quickdigest5.cpp"
|
"libs/QuickDigest5/quickdigest5.cpp"
|
||||||
"src/Helpers.cpp"
|
"src/Helpers/Helpers.cpp"
|
||||||
"src/HTTPRequestProcess.cpp"
|
"src/HTTP/HTTPRequestProcess.cpp"
|
||||||
"./src/HTTPMethods.cpp")
|
"src/HTTPMethods.cpp"
|
||||||
|
"src/Helpers/HelpersFormProcess.cpp")
|
||||||
|
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
TARGET Webserver
|
TARGET Webserver
|
||||||
|
@@ -1,4 +1,4 @@
|
|||||||
#include "Main.hpp"
|
#include "../Main.hpp"
|
||||||
#include <exception>
|
#include <exception>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
@@ -1,4 +1,4 @@
|
|||||||
#include "Main.hpp"
|
#include "../Main.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
|
|
@@ -1,5 +1,5 @@
|
|||||||
#include "Helpers.hpp"
|
#include "../Helpers.hpp"
|
||||||
#include "Main.hpp"
|
#include "../Main.hpp"
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
void HTTPrequest::start() {
|
void HTTPrequest::start() {
|
@@ -1,5 +1,5 @@
|
|||||||
#include "Helpers.hpp"
|
#include "../Helpers.hpp"
|
||||||
#include "Main.hpp"
|
#include "../Main.hpp"
|
||||||
#include <asio/socket_base.hpp>
|
#include <asio/socket_base.hpp>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <istream>
|
#include <istream>
|
@@ -1,5 +1,6 @@
|
|||||||
#include "Helpers.hpp"
|
#include "Helpers.hpp"
|
||||||
#include "Main.hpp"
|
#include "Main.hpp"
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
// 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,6 +19,16 @@ void Webserver::initResponses() {
|
|||||||
};
|
};
|
||||||
|
|
||||||
responseMethods["POST"]["/upload"] = [](HTTPrequest *self) {
|
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());
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
@@ -1,8 +1,20 @@
|
|||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
namespace Helpers {
|
namespace Helpers {
|
||||||
|
struct formData {
|
||||||
|
std::string data;
|
||||||
|
std::unordered_map<std::string, std::string> headers;
|
||||||
|
};
|
||||||
|
|
||||||
std::string ReadFile(std::string Path);
|
std::string ReadFile(std::string Path);
|
||||||
void getlineAndCount(std::basic_istream<char> &stream, uint64_t &count,
|
void getlineAndCount(std::basic_istream<char> &stream, uint64_t &count,
|
||||||
std::string &string, char delimit = '\0');
|
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
|
} // namespace Helpers
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
#include "Helpers.hpp"
|
#include "../Helpers.hpp"
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
70
src/Helpers/HelpersFormProcess.cpp
Normal file
70
src/Helpers/HelpersFormProcess.cpp
Normal 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);
|
||||||
|
}
|
@@ -11,7 +11,11 @@
|
|||||||
<h1>Welcome</h1>
|
<h1>Welcome</h1>
|
||||||
<h1>POST test</h1>
|
<h1>POST test</h1>
|
||||||
<form action="/upload" method="POST" enctype="multipart/form-data">
|
<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">
|
<input type="submit" value="Upload">
|
||||||
</form>
|
</form>
|
||||||
</body>
|
</body>
|
||||||
|
Reference in New Issue
Block a user