1
0
forked from cat/WebBase

Added proper file reading

This commit is contained in:
2025-06-26 02:07:58 +03:00
parent ea8b8c884e
commit effa9ec9fa
3 changed files with 23 additions and 10 deletions

View File

@@ -47,25 +47,25 @@ void HTTPrequest::processRequest(
// //
// This is where we will process requests // This is where we will process requests
// //
std::stringstream responseStream;
std::string responseMeta = "\nContent-Type: text/html\nConnection: close\n\n";
responseStream << "HTTP/1.1 ";
// This is very much temp // This is very much temp
if (requestType != "GET") { if (requestType != "GET") {
responseStream << "403 Forbidden" << responseMeta << "No POST allowed"; writeData(Helpers::GenerateResponse("403 Forbidden", "text/text",
writeData(responseStream.str()); "POST requests are not allowed!"));
return; return;
} }
// This needs to read from a file instead of by hand // This can be further refactored to just "File send"
switch (Helpers::Pathhash(requestPath)) { switch (Helpers::Pathhash(requestPath)) {
case "/"_hash: case "/"_hash:
responseStream << "200 OK" << responseMeta writeData(Helpers::GenerateResponse("200 OK", "text/html",
<< Helpers::ReadFile("www/index.html"); Helpers::ReadFile("www/index.html")));
break; return;
default:
writeData(Helpers::GenerateResponse("404 Not Found", "text/html",
"Could not find that file!!"));
return;
} }
writeData(responseStream.str());
} }
void HTTPrequest::writeData(std::string data) { void HTTPrequest::writeData(std::string data) {

View File

@@ -1,6 +1,7 @@
#include "Helpers.hpp" #include "Helpers.hpp"
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
#include <string>
// Should add caching here // Should add caching here
std::string Helpers::ReadFile(std::string Path) { std::string Helpers::ReadFile(std::string Path) {
@@ -9,3 +10,13 @@ std::string Helpers::ReadFile(std::string Path) {
contents << file.rdbuf(); contents << file.rdbuf();
return contents.str(); return contents.str();
} }
std::string Helpers::GenerateResponse(std::string statusCode,
std::string contentType,
std::string content) {
std::stringstream output;
output << "HTTP/1.1 " << statusCode << "\n"
<< "Content-Type: " << contentType << "\nConnection: close\n\n"
<< content;
return output.str();
}

View File

@@ -8,6 +8,8 @@
namespace Helpers { namespace Helpers {
std::string ReadFile(std::string Path); std::string ReadFile(std::string Path);
std::string GenerateResponse(std::string statusCode, std::string contentType,
std::string content);
// =========== // ===========
// Hashing // Hashing
// =========== // ===========