Added proper file reading
This commit is contained in:
20
src/HTTP.cpp
20
src/HTTP.cpp
@@ -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) {
|
||||
|
@@ -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();
|
||||
}
|
||||
|
@@ -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
|
||||
// ===========
|
||||
|
Reference in New Issue
Block a user