Added the new response style and 404 page
This commit is contained in:
27
src/HTTP.cpp
27
src/HTTP.cpp
@@ -1,5 +1,6 @@
|
||||
#include "Helpers.hpp"
|
||||
#include "Main.hpp"
|
||||
#include <exception>
|
||||
|
||||
void HTTPrequest::start() {
|
||||
// Possible Logging here
|
||||
@@ -7,29 +8,11 @@ void HTTPrequest::start() {
|
||||
}
|
||||
|
||||
void HTTPrequest::processRequest() {
|
||||
uint64_t pathHash = Helpers::Pathhash(requestPath);
|
||||
|
||||
// This is very much temp
|
||||
if (requestType == "POST") {
|
||||
switch (pathHash) {
|
||||
case "/upload"_hash:
|
||||
writeData(Helpers::GenerateResponse("501 Not Implemented", "text/text",
|
||||
bodyContent));
|
||||
return;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// This can be further refactored to just "File send"
|
||||
switch (pathHash) {
|
||||
case "/"_hash:
|
||||
writeData(Helpers::GenerateResponse("200 OK", "text/html",
|
||||
Helpers::ReadFile("www/index.html")));
|
||||
return;
|
||||
default:
|
||||
try {
|
||||
Webserver::responseMethods[requestType].at(requestPath)(*this);
|
||||
} catch (std::exception e) {
|
||||
writeData(Helpers::GenerateResponse("404 Not Found", "text/html",
|
||||
"Could not find that file!!"));
|
||||
return;
|
||||
Helpers::ReadFile("www/error.html")));
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,9 +1,5 @@
|
||||
// Big thanks to
|
||||
// https://medium.com/@ryan_forrester_/using-switch-statements-with-strings-in-c-a-complete-guide-efa12f64a59d
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace Helpers {
|
||||
std::string ReadFile(std::string Path);
|
||||
@@ -11,18 +7,4 @@ void getlineAndCount(std::basic_istream<char> &stream, uint64_t &count,
|
||||
std::string &string, char delimit = '\0');
|
||||
std::string GenerateResponse(std::string statusCode, std::string contentType,
|
||||
std::string content);
|
||||
// ===========
|
||||
// Hashing
|
||||
// ===========
|
||||
// This is called a polynomial rolling hash, prob going to collide
|
||||
constexpr uint64_t Pathhash(std::string_view s) {
|
||||
uint64_t res = 0;
|
||||
for (uint8_t c : s) {
|
||||
res = (res * 131) + c;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
} // namespace Helpers
|
||||
constexpr uint64_t operator""_hash(const char *string, std::size_t count) {
|
||||
return Helpers::Pathhash(std::string_view(string));
|
||||
}
|
||||
|
@@ -57,15 +57,16 @@ class Webserver : public std::enable_shared_from_this<Webserver> {
|
||||
public:
|
||||
Webserver(asio::io_context &context);
|
||||
|
||||
private:
|
||||
void begin();
|
||||
static void initResponses();
|
||||
|
||||
// Responses
|
||||
static std::unordered_map<
|
||||
std::string,
|
||||
std::unordered_map<std::string, std::function<void(HTTPrequest &self)>>>
|
||||
responseMethods;
|
||||
|
||||
private:
|
||||
void begin();
|
||||
static void initResponses();
|
||||
|
||||
asio::io_context &io;
|
||||
asio::ip::tcp::acceptor accept;
|
||||
};
|
||||
|
@@ -1,5 +1,6 @@
|
||||
#include "Main.hpp"
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
|
||||
// Webserver is defined at main.hpp, you probably do not want to code here
|
||||
Webserver::Webserver(asio::io_context &context)
|
||||
@@ -8,7 +9,7 @@ Webserver::Webserver(asio::io_context &context)
|
||||
asio::ip::tcp::endpoint(asio::ip::make_address(IP), PORT)) {
|
||||
std::cout << "Server is up!\n";
|
||||
initResponses();
|
||||
std::cout << "Path responses are set up!";
|
||||
std::cout << "Path responses are set up!" << std::endl;
|
||||
begin();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user