format + README
This commit is contained in:
parent
71be773d49
commit
0ebdb34601
|
@ -5,6 +5,7 @@ project(anthracite)
|
|||
set(CMAKE_CXX_STANDARD 23)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED True)
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "-O3")
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
|
||||
add_custom_target(build-version
|
||||
COMMAND cd ../build_supp && ./version.sh
|
||||
|
|
12
README.md
12
README.md
|
@ -3,9 +3,9 @@ A simple web server written in C++. Supports HTTP 1.0 & 1.1.
|
|||
|
||||
## 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
|
||||
- [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] Multithreading
|
||||
- [x] HTTP/1.1
|
||||
- [ ] Improve benchmarking infrastructure
|
||||
- [x] Enhance logging
|
||||
- [-] Build out module-based backend system for handling requests
|
||||
- [ ] Faster parsing
|
||||
- [ ] HTTP/2
|
||||
- [ ] Improve benchmarking infrastructure
|
||||
- [ ] Fix glaring security issues
|
||||
- [ ] Proper error handling
|
||||
- [ ] User configuration
|
||||
- [ ] Build out module-based backend system for handling requests
|
||||
- [ ] HTTP/2
|
||||
- [ ] Enhance logging
|
||||
- [ ] Cleanup (this one will never truly be done)
|
||||
|
||||
## Screenshots
|
||||
|
|
2
format.sh
Executable file
2
format.sh
Executable file
|
@ -0,0 +1,2 @@
|
|||
git ls-files -- '*.cpp' '*.h' | xargs clang-format -i -style=file
|
||||
git diff --exit-code --color
|
|
@ -1,15 +1,15 @@
|
|||
#include "backends/file_backend.hpp"
|
||||
#include "./anthracite.hpp"
|
||||
#include "./log/log.hpp"
|
||||
#include "./socket/socket.hpp"
|
||||
#include "backends/file_backend.hpp"
|
||||
#include <condition_variable>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <netinet/in.h>
|
||||
#include <span>
|
||||
#include <sys/socket.h>
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
#include <span>
|
||||
#include "./log/log.hpp"
|
||||
#include "./socket/socket.hpp"
|
||||
|
||||
using namespace anthracite;
|
||||
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
#include "./file_backend.hpp"
|
||||
#include "../log/log.hpp"
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
|
||||
#include <fstream>
|
||||
|
||||
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();
|
||||
filename = file_dir + filename;
|
||||
auto file_info = file_cache.find(filename);
|
||||
|
@ -34,7 +33,8 @@ namespace anthracite::backends {
|
|||
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 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("./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();
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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";
|
||||
auto file_info = file_cache.find(filename);
|
||||
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
#include "request.hpp"
|
||||
#include "constants.hpp"
|
||||
#include "../log/log.hpp"
|
||||
#include "constants.hpp"
|
||||
#include <stdio.h>
|
||||
|
||||
namespace anthracite::http {
|
||||
|
||||
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;
|
||||
|
@ -121,16 +122,19 @@ namespace anthracite::http {
|
|||
|
||||
std::string request::client_ip() { return _client_ipaddr; }
|
||||
|
||||
version request::get_http_version() {
|
||||
version request::get_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;
|
||||
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 bool found = header != _headers.end();
|
||||
|
||||
|
|
|
@ -7,12 +7,14 @@ namespace anthracite::http {
|
|||
|
||||
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 = &_content_noref;
|
||||
}
|
||||
|
||||
void response::add_body_ref(std::string* body) {
|
||||
void response::add_body_ref(std::string* 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;
|
||||
}
|
||||
|
||||
|
|
|
@ -5,13 +5,20 @@ namespace anthracite::log {
|
|||
|
||||
// TODO: implement logger as a singleton to prevent duplicates
|
||||
Logger::Logger() = default;
|
||||
void Logger::initialize(enum LOG_LEVEL level) {
|
||||
void Logger::initialize(enum LOG_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) {
|
||||
std::cout << "[" << this->_tag << "] " << this->str();
|
||||
std::cout.flush();
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
#include "./socket.hpp"
|
||||
#include <arpa/inet.h>
|
||||
#include <array>
|
||||
#include <malloc.h>
|
||||
|
@ -7,11 +8,9 @@
|
|||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
#include "./socket.hpp"
|
||||
|
||||
namespace anthracite::socket {
|
||||
|
||||
|
||||
anthracite_socket::anthracite_socket(int port, int max_queue)
|
||||
: server_socket(::socket(AF_INET, SOCK_STREAM, 0))
|
||||
, client_ip("")
|
||||
|
|
|
@ -17,9 +17,7 @@ private:
|
|||
std::string client_ip;
|
||||
struct sockaddr_in client_addr {};
|
||||
socklen_t client_addr_len {};
|
||||
static constexpr struct timeval timeout_tv {
|
||||
.tv_sec = 5, .tv_usec = 0
|
||||
};
|
||||
static const struct timeval timeout_tv;
|
||||
|
||||
public:
|
||||
anthracite_socket(int port, int max_queue = MAX_QUEUE_LENGTH);
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
#include "../lib/anthracite.hpp"
|
||||
#include "../lib/backends/backend.hpp"
|
||||
#include "../lib/http/constants.hpp"
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <sstream>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <iostream>
|
||||
|
||||
using namespace anthracite;
|
||||
|
||||
|
@ -15,16 +15,19 @@ class api_backend : public backends::backend {
|
|||
|
||||
class RouteNode {
|
||||
public:
|
||||
|
||||
std::optional<CallbackType> callback;
|
||||
|
||||
RouteNode() : callback(std::nullopt) {}
|
||||
RouteNode()
|
||||
: callback(std::nullopt)
|
||||
{
|
||||
}
|
||||
std::unordered_map<std::string, RouteNode> routes;
|
||||
};
|
||||
|
||||
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>();
|
||||
|
||||
resp->add_body("Not Found");
|
||||
|
@ -34,7 +37,8 @@ class api_backend : public backends::backend {
|
|||
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::vector<std::string> result;
|
||||
std::stringstream ss(filename);
|
||||
|
@ -58,21 +62,21 @@ class api_backend : public backends::backend {
|
|||
} else {
|
||||
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);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
api_backend() {
|
||||
api_backend()
|
||||
{
|
||||
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::stringstream ss(pathspec);
|
||||
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>();
|
||||
|
||||
resp->add_body(R"({"user": "endpoint"}")");
|
||||
|
@ -97,7 +102,8 @@ std::unique_ptr<http::response> handle_request(http::request& req) {
|
|||
return resp;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
auto args = std::span(argv, size_t(argc));
|
||||
api_backend ab;
|
||||
ab.register_endpoint("users/*", handle_request);
|
||||
|
|
|
@ -3,7 +3,8 @@
|
|||
|
||||
using namespace anthracite;
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
auto args = std::span(argv, size_t(argc));
|
||||
backends::file_backend fb(argc > 2 ? args[2] : "./www");
|
||||
anthracite_main(argc, argv, fb);
|
||||
|
|
Loading…
Reference in a new issue