Added the new response style and 404 page
This commit is contained in:
@@ -9,9 +9,13 @@ set(CMAKE_CXX_FLAGS "-Wall -g -fsanitize=address")
|
|||||||
|
|
||||||
add_executable(
|
add_executable(
|
||||||
Webserver
|
Webserver
|
||||||
"src/Entry.cpp" "src/Webserver.cpp" "src/HTTP.cpp"
|
"src/Entry.cpp"
|
||||||
"libs/QuickDigest5/quickdigest5.cpp" "src/Helpers.cpp"
|
"src/Webserver.cpp"
|
||||||
"src/HTTPRequestProcess.cpp")
|
"src/HTTP.cpp"
|
||||||
|
"libs/QuickDigest5/quickdigest5.cpp"
|
||||||
|
"src/Helpers.cpp"
|
||||||
|
"src/HTTPRequestProcess.cpp"
|
||||||
|
"./src/HTTPMethods.cpp")
|
||||||
|
|
||||||
add_custom_command(
|
add_custom_command(
|
||||||
TARGET Webserver
|
TARGET Webserver
|
||||||
|
27
src/HTTP.cpp
27
src/HTTP.cpp
@@ -1,5 +1,6 @@
|
|||||||
#include "Helpers.hpp"
|
#include "Helpers.hpp"
|
||||||
#include "Main.hpp"
|
#include "Main.hpp"
|
||||||
|
#include <exception>
|
||||||
|
|
||||||
void HTTPrequest::start() {
|
void HTTPrequest::start() {
|
||||||
// Possible Logging here
|
// Possible Logging here
|
||||||
@@ -7,29 +8,11 @@ void HTTPrequest::start() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void HTTPrequest::processRequest() {
|
void HTTPrequest::processRequest() {
|
||||||
uint64_t pathHash = Helpers::Pathhash(requestPath);
|
try {
|
||||||
|
Webserver::responseMethods[requestType].at(requestPath)(*this);
|
||||||
// This is very much temp
|
} catch (std::exception e) {
|
||||||
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:
|
|
||||||
writeData(Helpers::GenerateResponse("404 Not Found", "text/html",
|
writeData(Helpers::GenerateResponse("404 Not Found", "text/html",
|
||||||
"Could not find that file!!"));
|
Helpers::ReadFile("www/error.html")));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -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 <cstdint>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <string_view>
|
|
||||||
|
|
||||||
namespace Helpers {
|
namespace Helpers {
|
||||||
std::string ReadFile(std::string Path);
|
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 &string, char delimit = '\0');
|
||||||
std::string GenerateResponse(std::string statusCode, std::string contentType,
|
std::string GenerateResponse(std::string statusCode, std::string contentType,
|
||||||
std::string content);
|
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
|
} // 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:
|
public:
|
||||||
Webserver(asio::io_context &context);
|
Webserver(asio::io_context &context);
|
||||||
|
|
||||||
private:
|
|
||||||
void begin();
|
|
||||||
static void initResponses();
|
|
||||||
|
|
||||||
// Responses
|
// Responses
|
||||||
static std::unordered_map<
|
static std::unordered_map<
|
||||||
std::string,
|
std::string,
|
||||||
std::unordered_map<std::string, std::function<void(HTTPrequest &self)>>>
|
std::unordered_map<std::string, std::function<void(HTTPrequest &self)>>>
|
||||||
responseMethods;
|
responseMethods;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void begin();
|
||||||
|
static void initResponses();
|
||||||
|
|
||||||
asio::io_context &io;
|
asio::io_context &io;
|
||||||
asio::ip::tcp::acceptor accept;
|
asio::ip::tcp::acceptor accept;
|
||||||
};
|
};
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
#include "Main.hpp"
|
#include "Main.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <ostream>
|
||||||
|
|
||||||
// Webserver is defined at main.hpp, you probably do not want to code here
|
// Webserver is defined at main.hpp, you probably do not want to code here
|
||||||
Webserver::Webserver(asio::io_context &context)
|
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)) {
|
asio::ip::tcp::endpoint(asio::ip::make_address(IP), PORT)) {
|
||||||
std::cout << "Server is up!\n";
|
std::cout << "Server is up!\n";
|
||||||
initResponses();
|
initResponses();
|
||||||
std::cout << "Path responses are set up!";
|
std::cout << "Path responses are set up!" << std::endl;
|
||||||
begin();
|
begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
15
www/error.html
Normal file
15
www/error.html
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||||
|
<title></title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<h1>404 NOT FOUND</h1>
|
||||||
|
<p>We could not find the requested page! Sorry ~_~ </p>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
Reference in New Issue
Block a user