Template
1
0

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
//
std::stringstream responseStream;
std::string responseMeta = "\nContent-Type: text/html\nConnection: close\n\n";
responseStream << "HTTP/1.1 ";
// This is very much temp
if (requestType != "GET") {
responseStream << "403 Forbidden" << responseMeta << "No POST allowed";
writeData(responseStream.str());
writeData(Helpers::GenerateResponse("403 Forbidden", "text/text",
"POST requests are not allowed!"));
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)) {
case "/"_hash:
responseStream << "200 OK" << responseMeta
<< Helpers::ReadFile("www/index.html");
break;
writeData(Helpers::GenerateResponse("200 OK", "text/html",
Helpers::ReadFile("www/index.html")));
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) {

View File

@@ -1,6 +1,7 @@
#include "Helpers.hpp"
#include <fstream>
#include <sstream>
#include <string>
// Should add caching here
std::string Helpers::ReadFile(std::string Path) {
@@ -9,3 +10,13 @@ std::string Helpers::ReadFile(std::string Path) {
contents << file.rdbuf();
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 {
std::string ReadFile(std::string Path);
std::string GenerateResponse(std::string statusCode, std::string contentType,
std::string content);
// ===========
// Hashing
// ===========