Added QuickDigest5 for MD5 Hashing, added bare bones responding, added Helper namespace, and minor redesign of methods
This commit is contained in:
46
src/HTTP.cpp
46
src/HTTP.cpp
@@ -1,4 +1,9 @@
|
||||
#include "helper.hpp"
|
||||
#include "main.hpp"
|
||||
#include <asio/impl/write.hpp>
|
||||
#include <cstddef>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
|
||||
void HTTPrequest::start() {
|
||||
// Possible Logging here
|
||||
@@ -11,31 +16,60 @@ void HTTPrequest::readData() {
|
||||
//
|
||||
std::shared_ptr<HTTPrequest> self(shared_from_this());
|
||||
sock.async_read_some(
|
||||
buffer.prepare(1024),
|
||||
buffer.prepare(2048),
|
||||
[this, self](std::error_code error, std::size_t packageSize) {
|
||||
if (!error) {
|
||||
buffer.commit(packageSize);
|
||||
std::istream stream(&buffer);
|
||||
std::string request(packageSize, '\0');
|
||||
stream.read(request.data(), packageSize);
|
||||
std::unordered_map<std::string, std::string> request;
|
||||
|
||||
std::cout << request << std::endl;
|
||||
// This is HTTP main request
|
||||
std::string rq, key, value, type, path;
|
||||
std::getline(stream, type, ' '); // HTTP request type
|
||||
std::getline(stream, path, ' '); // HTTP path
|
||||
std::getline(stream, rq); // Removes HTTP version part no need rn
|
||||
|
||||
readData();
|
||||
// Rest of the request vals
|
||||
while (buffer.size() > 0) {
|
||||
std::getline(stream, key, ':');
|
||||
std::getline(stream, rq, ' '); // trim start
|
||||
// some gangster shit right here
|
||||
std::getline(stream, value);
|
||||
|
||||
request.insert({key, value});
|
||||
}
|
||||
|
||||
processRequest(type, path, request); // This writes data too
|
||||
readData(); // Loop
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void HTTPrequest::processRequest(std::string request) {
|
||||
void HTTPrequest::processRequest(
|
||||
std::string requestType, std::string requestPath,
|
||||
std::unordered_map<std::string, std::string> request) {
|
||||
//
|
||||
// This is where we will process requests
|
||||
//
|
||||
if (requestType != "GET") {
|
||||
return;
|
||||
}
|
||||
switch (Helpers::Pathhash(requestPath)) {
|
||||
case "/"_hash:
|
||||
writeData("<html><head><title>Hello "
|
||||
"you!</title></head><body><h1>Test</h1><p>pretty "
|
||||
"cool</p></body></html>");
|
||||
sock.close(); // Responded
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void HTTPrequest::writeData(std::string data) {
|
||||
//
|
||||
// Response here
|
||||
//
|
||||
asio::async_write(sock, asio::buffer(data),
|
||||
[](std::error_code, std::size_t) {});
|
||||
}
|
||||
|
||||
// ================= CLASS HANDLING SPECIFIC =================
|
||||
|
19
src/helper.hpp
Normal file
19
src/helper.hpp
Normal file
@@ -0,0 +1,19 @@
|
||||
// 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_view>
|
||||
|
||||
// This is called a polynomial rolling hash, prob going to collide
|
||||
namespace Helpers {
|
||||
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));
|
||||
}
|
@@ -15,6 +15,7 @@
|
||||
#include <istream>
|
||||
#include <string>
|
||||
#include <system_error>
|
||||
#include <unordered_map>
|
||||
|
||||
#define IP "127.0.0.1"
|
||||
#define PORT 8000
|
||||
@@ -34,7 +35,8 @@ public:
|
||||
private:
|
||||
HTTPrequest(asio::io_context &context);
|
||||
void readData();
|
||||
void processRequest(std::string request);
|
||||
void processRequest(std::string requestType, std::string requestPath,
|
||||
std::unordered_map<std::string, std::string> request);
|
||||
void writeData(std::string data);
|
||||
|
||||
asio::ip::tcp::socket sock;
|
||||
|
Reference in New Issue
Block a user