format + README

This commit is contained in:
Nicholas Orlowsky 2025-02-04 11:44:34 -05:00
parent 71be773d49
commit 0ebdb34601
Signed by: nickorlow
GPG key ID: 838827D8C4611687
12 changed files with 370 additions and 345 deletions

View file

@ -5,6 +5,7 @@ project(anthracite)
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED True) set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS_RELEASE "-O3") set(CMAKE_CXX_FLAGS_RELEASE "-O3")
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
add_custom_target(build-version add_custom_target(build-version
COMMAND cd ../build_supp && ./version.sh COMMAND cd ../build_supp && ./version.sh

View file

@ -3,9 +3,9 @@ A simple web server written in C++. Supports HTTP 1.0 & 1.1.
## Developing ## Developing
To build/develop Anthracite, you must have C++20, Make, and Python3 installed. To build/develop Anthracite, you must have C++23, CMake, Make, and Python3 installed.
You can run Anthracite with: `make run` Create a `build/` directory, run `cmake ..`, and then `make` to build.
## Todo ## Todo
- [x] HTTP/1.0 - [x] HTTP/1.0
@ -14,14 +14,14 @@ You can run Anthracite with: `make run`
- [x] Add module-based backend system for handling requests - [x] Add module-based backend system for handling requests
- [x] Multithreading - [x] Multithreading
- [x] HTTP/1.1 - [x] HTTP/1.1
- [ ] Improve benchmarking infrastructure - [x] Enhance logging
- [-] Build out module-based backend system for handling requests
- [ ] Faster parsing - [ ] Faster parsing
- [ ] HTTP/2
- [ ] Improve benchmarking infrastructure
- [ ] Fix glaring security issues - [ ] Fix glaring security issues
- [ ] Proper error handling - [ ] Proper error handling
- [ ] User configuration - [ ] User configuration
- [ ] Build out module-based backend system for handling requests
- [ ] HTTP/2
- [ ] Enhance logging
- [ ] Cleanup (this one will never truly be done) - [ ] Cleanup (this one will never truly be done)
## Screenshots ## Screenshots

2
format.sh Executable file
View file

@ -0,0 +1,2 @@
git ls-files -- '*.cpp' '*.h' | xargs clang-format -i -style=file
git diff --exit-code --color

View file

@ -1,15 +1,15 @@
#include "backends/file_backend.hpp"
#include "./anthracite.hpp" #include "./anthracite.hpp"
#include "./log/log.hpp"
#include "./socket/socket.hpp"
#include "backends/file_backend.hpp"
#include <condition_variable> #include <condition_variable>
#include <iostream> #include <iostream>
#include <mutex> #include <mutex>
#include <netinet/in.h> #include <netinet/in.h>
#include <span>
#include <sys/socket.h> #include <sys/socket.h>
#include <thread> #include <thread>
#include <unistd.h> #include <unistd.h>
#include <span>
#include "./log/log.hpp"
#include "./socket/socket.hpp"
using namespace anthracite; using namespace anthracite;

View file

@ -1,13 +1,12 @@
#include "./file_backend.hpp" #include "./file_backend.hpp"
#include "../log/log.hpp" #include "../log/log.hpp"
#include <fstream>
#include <filesystem> #include <filesystem>
#include <fstream>
namespace anthracite::backends { namespace anthracite::backends {
std::unique_ptr<http::response> file_backend::handle_request_cache(http::request& req)
std::unique_ptr<http::response> file_backend::handle_request_cache(http::request& req) { {
std::string filename = req.path() == "/" ? "/index.html" : req.path(); std::string filename = req.path() == "/" ? "/index.html" : req.path();
filename = file_dir + filename; filename = file_dir + filename;
auto file_info = file_cache.find(filename); auto file_info = file_cache.find(filename);
@ -34,7 +33,8 @@ namespace anthracite::backends {
return resp; return resp;
} }
void file_backend::populate_cache_dir(std::string dir) { 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 cur = begin(std::filesystem::recursive_directory_iterator(dir));
std::filesystem::recursive_directory_iterator fin = end(std::filesystem::recursive_directory_iterator(dir)); std::filesystem::recursive_directory_iterator fin = end(std::filesystem::recursive_directory_iterator(dir));
@ -50,21 +50,25 @@ namespace anthracite::backends {
} }
} }
void file_backend::populate_cache() { void file_backend::populate_cache()
{
populate_cache_dir(file_dir); populate_cache_dir(file_dir);
populate_cache_dir("./error_pages/"); populate_cache_dir("./error_pages/");
} }
file_backend::file_backend(std::string dir) : file_dir(std::move(dir)) { file_backend::file_backend(std::string dir)
: file_dir(std::move(dir))
{
populate_cache(); populate_cache();
} }
std::unique_ptr<http::response> file_backend::handle_request(http::request& req)
std::unique_ptr<http::response> file_backend::handle_request(http::request& req) { {
return handle_request_cache(req); return handle_request_cache(req);
} }
std::unique_ptr<http::response> file_backend::handle_error(const http::status_codes& error) { std::unique_ptr<http::response> file_backend::handle_error(const http::status_codes& error)
{
std::string filename = "./error_pages/" + std::to_string(error) + ".html"; std::string filename = "./error_pages/" + std::to_string(error) + ".html";
auto file_info = file_cache.find(filename); auto file_info = file_cache.find(filename);

View file

@ -1,12 +1,13 @@
#include "request.hpp" #include "request.hpp"
#include "constants.hpp"
#include "../log/log.hpp" #include "../log/log.hpp"
#include "constants.hpp"
#include <stdio.h> #include <stdio.h>
namespace anthracite::http { namespace anthracite::http {
request::request(std::string& raw_data, const std::string& client_ip) request::request(std::string& raw_data, const std::string& client_ip)
: _path(""), _client_ipaddr(client_ip) : _path("")
, _client_ipaddr(client_ip)
{ {
parser_state state = METHOD; parser_state state = METHOD;
@ -121,16 +122,19 @@ namespace anthracite::http {
std::string request::client_ip() { return _client_ipaddr; } std::string request::client_ip() { return _client_ipaddr; }
version request::get_http_version() { version request::get_http_version()
{
return _http_version; return _http_version;
} }
bool request::is_supported_version() { bool request::is_supported_version()
{
// log::err << reverse_version_map.find(_http_version)->second << std::endl; // log::err << reverse_version_map.find(_http_version)->second << std::endl;
return _http_version == HTTP_1_1 || _http_version == HTTP_1_0; return _http_version == HTTP_1_1 || _http_version == HTTP_1_0;
} }
bool request::close_connection() { bool request::close_connection()
{
const auto& header = _headers.find("Connection"); const auto& header = _headers.find("Connection");
const bool found = header != _headers.end(); const bool found = header != _headers.end();

View file

@ -7,12 +7,14 @@ namespace anthracite::http {
int response::status_code() { return _status_code; } int response::status_code() { return _status_code; }
void response::add_body(const std::string body) { void response::add_body(const std::string body)
{
_content_noref = body; _content_noref = body;
_content = &_content_noref; _content = &_content_noref;
} }
void response::add_body_ref(std::string* body) { void response::add_body_ref(std::string* body)
{
_content = body; _content = body;
} }
@ -23,7 +25,8 @@ namespace anthracite::http {
} }
} }
void response::add_status(int code) { void response::add_status(int code)
{
_status_code = code; _status_code = code;
} }

View file

@ -5,13 +5,20 @@ namespace anthracite::log {
// TODO: implement logger as a singleton to prevent duplicates // TODO: implement logger as a singleton to prevent duplicates
Logger::Logger() = default; Logger::Logger() = default;
void Logger::initialize(enum LOG_LEVEL level) { void Logger::initialize(enum LOG_LEVEL level)
{
_level = level; _level = level;
} }
LogBuf::LogBuf(std::ostream& output_stream, const std::string& tag, enum LOG_LEVEL level) : _output_stream(output_stream), _tag(tag), _level(level) {} LogBuf::LogBuf(std::ostream& output_stream, const std::string& tag, enum LOG_LEVEL level)
: _output_stream(output_stream)
, _tag(tag)
, _level(level)
{
}
int LogBuf::sync() { int LogBuf::sync()
{
if (this->_level <= logger._level) { if (this->_level <= logger._level) {
std::cout << "[" << this->_tag << "] " << this->str(); std::cout << "[" << this->_tag << "] " << this->str();
std::cout.flush(); std::cout.flush();

View file

@ -1,3 +1,4 @@
#include "./socket.hpp"
#include <arpa/inet.h> #include <arpa/inet.h>
#include <array> #include <array>
#include <malloc.h> #include <malloc.h>
@ -7,11 +8,9 @@
#include <sys/time.h> #include <sys/time.h>
#include <unistd.h> #include <unistd.h>
#include <vector> #include <vector>
#include "./socket.hpp"
namespace anthracite::socket { namespace anthracite::socket {
anthracite_socket::anthracite_socket(int port, int max_queue) anthracite_socket::anthracite_socket(int port, int max_queue)
: server_socket(::socket(AF_INET, SOCK_STREAM, 0)) : server_socket(::socket(AF_INET, SOCK_STREAM, 0))
, client_ip("") , client_ip("")

View file

@ -17,9 +17,7 @@ private:
std::string client_ip; std::string client_ip;
struct sockaddr_in client_addr {}; struct sockaddr_in client_addr {};
socklen_t client_addr_len {}; socklen_t client_addr_len {};
static constexpr struct timeval timeout_tv { static const struct timeval timeout_tv;
.tv_sec = 5, .tv_usec = 0
};
public: public:
anthracite_socket(int port, int max_queue = MAX_QUEUE_LENGTH); anthracite_socket(int port, int max_queue = MAX_QUEUE_LENGTH);

View file

@ -1,12 +1,12 @@
#include "../lib/anthracite.hpp" #include "../lib/anthracite.hpp"
#include "../lib/backends/backend.hpp" #include "../lib/backends/backend.hpp"
#include "../lib/http/constants.hpp" #include "../lib/http/constants.hpp"
#include <iostream>
#include <memory> #include <memory>
#include <optional> #include <optional>
#include <sstream> #include <sstream>
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include <iostream>
using namespace anthracite; using namespace anthracite;
@ -15,16 +15,19 @@ class api_backend : public backends::backend {
class RouteNode { class RouteNode {
public: public:
std::optional<CallbackType> callback; std::optional<CallbackType> callback;
RouteNode() : callback(std::nullopt) {} RouteNode()
: callback(std::nullopt)
{
}
std::unordered_map<std::string, RouteNode> routes; std::unordered_map<std::string, RouteNode> routes;
}; };
RouteNode root; RouteNode root;
std::unique_ptr<http::response> default_route(http::request& req) { std::unique_ptr<http::response> default_route(http::request& req)
{
std::unique_ptr<http::response> resp = std::make_unique<http::response>(); std::unique_ptr<http::response> resp = std::make_unique<http::response>();
resp->add_body("Not Found"); resp->add_body("Not Found");
@ -34,7 +37,8 @@ class api_backend : public backends::backend {
return resp; return resp;
} }
std::unique_ptr<http::response> find_handler(http::request& req) { std::unique_ptr<http::response> find_handler(http::request& req)
{
std::string filename = req.path().substr(1); std::string filename = req.path().substr(1);
std::vector<std::string> result; std::vector<std::string> result;
std::stringstream ss(filename); std::stringstream ss(filename);
@ -58,21 +62,21 @@ class api_backend : public backends::backend {
} else { } else {
return default_route(req); return default_route(req);
} }
} }
std::unique_ptr<http::response> handle_request(http::request& req) override { std::unique_ptr<http::response> handle_request(http::request& req) override
{
return find_handler(req); return find_handler(req);
} }
public: public:
api_backend()
api_backend() { {
root.routes = std::unordered_map<std::string, RouteNode>(); root.routes = std::unordered_map<std::string, RouteNode>();
} }
void register_endpoint(std::string pathspec, CallbackType callback) { void register_endpoint(std::string pathspec, CallbackType callback)
{
std::vector<std::string> result; std::vector<std::string> result;
std::stringstream ss(pathspec); std::stringstream ss(pathspec);
std::string item; std::string item;
@ -87,7 +91,8 @@ class api_backend : public backends::backend {
} }
}; };
std::unique_ptr<http::response> handle_request(http::request& req) { std::unique_ptr<http::response> handle_request(http::request& req)
{
std::unique_ptr<http::response> resp = std::make_unique<http::response>(); std::unique_ptr<http::response> resp = std::make_unique<http::response>();
resp->add_body(R"({"user": "endpoint"}")"); resp->add_body(R"({"user": "endpoint"}")");
@ -97,7 +102,8 @@ std::unique_ptr<http::response> handle_request(http::request& req) {
return resp; return resp;
} }
int main(int argc, char** argv) { int main(int argc, char** argv)
{
auto args = std::span(argv, size_t(argc)); auto args = std::span(argv, size_t(argc));
api_backend ab; api_backend ab;
ab.register_endpoint("users/*", handle_request); ab.register_endpoint("users/*", handle_request);

View file

@ -3,7 +3,8 @@
using namespace anthracite; using namespace anthracite;
int main(int argc, char** argv) { int main(int argc, char** argv)
{
auto args = std::span(argv, size_t(argc)); auto args = std::span(argv, size_t(argc));
backends::file_backend fb(argc > 2 ? args[2] : "./www"); backends::file_backend fb(argc > 2 ? args[2] : "./www");
anthracite_main(argc, argv, fb); anthracite_main(argc, argv, fb);