forked from cat/WebBase
Start of map responses
This commit is contained in:
@@ -14,7 +14,7 @@ void HTTPrequest::processRequest() {
|
|||||||
switch (pathHash) {
|
switch (pathHash) {
|
||||||
case "/upload"_hash:
|
case "/upload"_hash:
|
||||||
writeData(Helpers::GenerateResponse("501 Not Implemented", "text/text",
|
writeData(Helpers::GenerateResponse("501 Not Implemented", "text/text",
|
||||||
"This path is not implemented yet!"));
|
bodyContent));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
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")));
|
||||||
|
};
|
||||||
|
}
|
20
src/Main.hpp
20
src/Main.hpp
@@ -10,6 +10,7 @@
|
|||||||
#include <asio/placeholders.hpp>
|
#include <asio/placeholders.hpp>
|
||||||
#include <asio/streambuf.hpp>
|
#include <asio/streambuf.hpp>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
@@ -27,11 +28,16 @@ public:
|
|||||||
static HTTPrequest_ptr create(asio::io_context &context);
|
static HTTPrequest_ptr create(asio::io_context &context);
|
||||||
|
|
||||||
void start();
|
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:
|
private:
|
||||||
HTTPrequest(asio::io_context &context);
|
HTTPrequest(asio::io_context &context);
|
||||||
void processRequest();
|
void processRequest();
|
||||||
void writeData(std::string data);
|
|
||||||
|
|
||||||
// Breaking Header to in lines
|
// Breaking Header to in lines
|
||||||
void processHTTPHeader();
|
void processHTTPHeader();
|
||||||
@@ -41,11 +47,6 @@ private:
|
|||||||
std::size_t headerSize);
|
std::size_t headerSize);
|
||||||
void inline processBody();
|
void inline processBody();
|
||||||
|
|
||||||
// Request itself
|
|
||||||
std::string requestType, requestPath;
|
|
||||||
std::unordered_map<std::string, std::string> headers, args;
|
|
||||||
std::string bodyContent;
|
|
||||||
|
|
||||||
// Networking
|
// Networking
|
||||||
asio::ip::tcp::socket sock;
|
asio::ip::tcp::socket sock;
|
||||||
asio::streambuf buffer;
|
asio::streambuf buffer;
|
||||||
@@ -58,6 +59,13 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
void begin();
|
void begin();
|
||||||
|
static void initResponses();
|
||||||
|
|
||||||
|
// Responses
|
||||||
|
static std::unordered_map<
|
||||||
|
std::string,
|
||||||
|
std::unordered_map<std::string, std::function<void(HTTPrequest &self)>>>
|
||||||
|
responseMethods;
|
||||||
asio::io_context &io;
|
asio::io_context &io;
|
||||||
asio::ip::tcp::acceptor accept;
|
asio::ip::tcp::acceptor accept;
|
||||||
};
|
};
|
||||||
|
@@ -7,6 +7,8 @@ Webserver::Webserver(asio::io_context &context)
|
|||||||
accept(context,
|
accept(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();
|
||||||
|
std::cout << "Path responses are set up!";
|
||||||
begin();
|
begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user