split into src and lib and socket changes
* libanthracite files are now all in lib/ * anthracite-bin + anthracite-api-bin files are now all in src/ * socket split into header and source properly
This commit is contained in:
parent
fba87f3fbb
commit
71be773d49
24 changed files with 49 additions and 121 deletions
20
lib/backends/backend.hpp
Normal file
20
lib/backends/backend.hpp
Normal file
|
@ -0,0 +1,20 @@
|
|||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "../http/request.hpp"
|
||||
#include "../http/response.hpp"
|
||||
|
||||
namespace anthracite::backends {
|
||||
|
||||
class backend {
|
||||
public:
|
||||
backend() = default;
|
||||
virtual ~backend() = default;
|
||||
backend(backend const&) = delete;
|
||||
backend& operator = (backend const&) = delete;
|
||||
backend(backend&&) = delete;
|
||||
backend& operator=(backend&&) = delete;
|
||||
virtual std::unique_ptr<http::response> handle_request(http::request& req) = 0;
|
||||
};
|
||||
|
||||
};
|
87
lib/backends/file_backend.cpp
Normal file
87
lib/backends/file_backend.cpp
Normal file
|
@ -0,0 +1,87 @@
|
|||
#include "./file_backend.hpp"
|
||||
#include "../log/log.hpp"
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
|
||||
|
||||
namespace anthracite::backends {
|
||||
|
||||
|
||||
std::unique_ptr<http::response> file_backend::handle_request_cache(http::request& req) {
|
||||
std::string filename = req.path() == "/" ? "/index.html" : req.path();
|
||||
filename = file_dir + filename;
|
||||
auto file_info = file_cache.find(filename);
|
||||
|
||||
int status = http::status_codes::OK;
|
||||
if (file_info == file_cache.end()) {
|
||||
return handle_error(http::status_codes::NOT_FOUND);
|
||||
}
|
||||
|
||||
std::unique_ptr<http::response> resp = std::make_unique<http::response>();
|
||||
|
||||
std::string file_extension = file_info->first.substr(file_info->first.rfind('.') + 1);
|
||||
std::string content_type = "text/html";
|
||||
|
||||
auto mime_type = http::mime_types.find(file_extension);
|
||||
if (mime_type != http::mime_types.end()) {
|
||||
content_type = mime_type->second;
|
||||
}
|
||||
|
||||
resp->add_body_ref(&file_info->second);
|
||||
resp->add_status(http::status_codes::OK);
|
||||
resp->add_header(http::header("Content-Type", content_type), false);
|
||||
|
||||
return resp;
|
||||
}
|
||||
|
||||
void file_backend::populate_cache_dir(std::string dir) {
|
||||
std::filesystem::recursive_directory_iterator cur = begin(std::filesystem::recursive_directory_iterator(dir));
|
||||
std::filesystem::recursive_directory_iterator fin = end(std::filesystem::recursive_directory_iterator(dir));
|
||||
|
||||
while (cur != fin) {
|
||||
auto p = cur->path();
|
||||
std::string filename = p.string();
|
||||
std::stringstream buffer;
|
||||
std::ifstream stream(filename);
|
||||
buffer << stream.rdbuf();
|
||||
file_cache[filename] = buffer.str();
|
||||
log::verbose << "File at " << filename << " cached (" << file_cache[filename].size() << " bytes)" << std::endl;
|
||||
++cur;
|
||||
}
|
||||
}
|
||||
|
||||
void file_backend::populate_cache() {
|
||||
populate_cache_dir(file_dir);
|
||||
populate_cache_dir("./error_pages/");
|
||||
}
|
||||
|
||||
file_backend::file_backend(std::string dir) : file_dir(std::move(dir)) {
|
||||
populate_cache();
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<http::response> file_backend::handle_request(http::request& req) {
|
||||
return handle_request_cache(req);
|
||||
}
|
||||
|
||||
std::unique_ptr<http::response> file_backend::handle_error(const http::status_codes& error) {
|
||||
std::string filename = "./error_pages/" + std::to_string(error) + ".html";
|
||||
auto file_info = file_cache.find(filename);
|
||||
|
||||
http::status_codes status = error;
|
||||
if (file_info == file_cache.end()) {
|
||||
status = http::status_codes::NOT_FOUND;
|
||||
filename = "./error_pages/404.html";
|
||||
file_info = file_cache.find(filename);
|
||||
}
|
||||
|
||||
std::unique_ptr<http::response> resp = std::make_unique<http::response>();
|
||||
|
||||
resp->add_body_ref(&file_info->second);
|
||||
resp->add_status(error);
|
||||
resp->add_header(http::header("Content-Type", "text/html"), false);
|
||||
|
||||
return resp;
|
||||
}
|
||||
|
||||
};
|
22
lib/backends/file_backend.hpp
Normal file
22
lib/backends/file_backend.hpp
Normal file
|
@ -0,0 +1,22 @@
|
|||
#pragma once
|
||||
|
||||
#include "backend.hpp"
|
||||
|
||||
namespace anthracite::backends {
|
||||
|
||||
class file_backend : public backend {
|
||||
private:
|
||||
std::unordered_map<std::string,std::string> file_cache;
|
||||
std::string file_dir;
|
||||
|
||||
std::unique_ptr<http::response> handle_request_cache(http::request& req);
|
||||
void populate_cache_dir(std::string dir);
|
||||
void populate_cache();
|
||||
public:
|
||||
file_backend(std::string dir = "./www") ;
|
||||
|
||||
std::unique_ptr<http::response> handle_request(http::request& req) override;
|
||||
std::unique_ptr<http::response> handle_error(const http::status_codes& error);
|
||||
};
|
||||
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue