Compare commits
2 Commits
c83064f8fc
...
0d87542196
Author | SHA1 | Date | |
---|---|---|---|
0d87542196 | |||
8097796af4 |
@@ -9,9 +9,13 @@ set(CMAKE_CXX_FLAGS "-Wall -g -fsanitize=address")
|
||||
|
||||
add_executable(
|
||||
Webserver
|
||||
"src/Entry.cpp" "src/Webserver.cpp" "src/HTTP.cpp"
|
||||
"libs/QuickDigest5/quickdigest5.cpp" "src/Helpers.cpp"
|
||||
"src/HTTPRequestProcess.cpp")
|
||||
"src/Entry.cpp"
|
||||
"src/Webserver.cpp"
|
||||
"src/HTTP.cpp"
|
||||
"libs/QuickDigest5/quickdigest5.cpp"
|
||||
"src/Helpers.cpp"
|
||||
"src/HTTPRequestProcess.cpp"
|
||||
"./src/HTTPMethods.cpp")
|
||||
|
||||
add_custom_command(
|
||||
TARGET Webserver
|
||||
|
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",
|
||||
"This path is not implemented yet!"));
|
||||
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")));
|
||||
}
|
||||
}
|
||||
|
||||
|
19
src/HTTPMethods.cpp
Normal file
19
src/HTTPMethods.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include "Helpers.hpp"
|
||||
#include "Main.hpp"
|
||||
|
||||
// Idea is very simple this map has it so that stuff like
|
||||
// responseMethods["GET","/"] have a method inside them
|
||||
// There is an invoking method inside each HTTPrequest
|
||||
//
|
||||
// So just add your logic here and relax ;)
|
||||
std::unordered_map<
|
||||
std::string,
|
||||
std::unordered_map<std::string, std::function<void(HTTPrequest &self)>>>
|
||||
Webserver::responseMethods;
|
||||
|
||||
void Webserver::initResponses() {
|
||||
responseMethods["GET"]["/"] = [](HTTPrequest &self) {
|
||||
self.writeData(Helpers::GenerateResponse(
|
||||
"200 OK", "text/html", Helpers::ReadFile("www/index.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));
|
||||
}
|
||||
|
21
src/Main.hpp
21
src/Main.hpp
@@ -10,6 +10,7 @@
|
||||
#include <asio/placeholders.hpp>
|
||||
#include <asio/streambuf.hpp>
|
||||
|
||||
#include <functional>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
@@ -27,11 +28,16 @@ public:
|
||||
static HTTPrequest_ptr create(asio::io_context &context);
|
||||
|
||||
void start();
|
||||
void writeData(std::string data);
|
||||
|
||||
// Request itself
|
||||
std::string requestType, requestPath;
|
||||
std::unordered_map<std::string, std::string> headers, args;
|
||||
std::string bodyContent;
|
||||
|
||||
private:
|
||||
HTTPrequest(asio::io_context &context);
|
||||
void processRequest();
|
||||
void writeData(std::string data);
|
||||
|
||||
// Breaking Header to in lines
|
||||
void processHTTPHeader();
|
||||
@@ -41,11 +47,6 @@ private:
|
||||
std::size_t headerSize);
|
||||
void inline processBody();
|
||||
|
||||
// Request itself
|
||||
std::string requestType, requestPath;
|
||||
std::unordered_map<std::string, std::string> headers, args;
|
||||
std::string bodyContent;
|
||||
|
||||
// Networking
|
||||
asio::ip::tcp::socket sock;
|
||||
asio::streambuf buffer;
|
||||
@@ -56,8 +57,16 @@ class Webserver : public std::enable_shared_from_this<Webserver> {
|
||||
public:
|
||||
Webserver(asio::io_context &context);
|
||||
|
||||
// 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)
|
||||
@@ -7,6 +8,8 @@ Webserver::Webserver(asio::io_context &context)
|
||||
accept(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::endl;
|
||||
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