polished up event loop changes
Some checks failed
Docker Build & Publish / build (push) Failing after 55m49s
Some checks failed
Docker Build & Publish / build (push) Failing after 55m49s
This commit is contained in:
parent
058c395095
commit
409024e04a
|
@ -1,6 +1,7 @@
|
||||||
# 0.3.0
|
# 0.3.0
|
||||||
- SSL support via OpenSSL
|
- SSL support via OpenSSL
|
||||||
- Switched from thread per connection to event driven threading model
|
- Added "Thread Manager" class to allow for multiple (or custom) threading models (process per thread, event loop)
|
||||||
|
- Defaul threading model is now event loop
|
||||||
- Rewrote request parser for readability and speed
|
- Rewrote request parser for readability and speed
|
||||||
- Added improved logging with different log levels
|
- Added improved logging with different log levels
|
||||||
- Separated anthracite into libanthracite and anthracite-bin to allow for other projects to implement anthracite (example in ./src/api_main.cpp)
|
- Separated anthracite into libanthracite and anthracite-bin to allow for other projects to implement anthracite (example in ./src/api_main.cpp)
|
||||||
|
|
|
@ -39,9 +39,6 @@ target_link_libraries(anthracite-bin anthracite)
|
||||||
add_dependencies(anthracite-bin build-supplemental)
|
add_dependencies(anthracite-bin build-supplemental)
|
||||||
add_dependencies(anthracite-bin anthracite)
|
add_dependencies(anthracite-bin anthracite)
|
||||||
|
|
||||||
add_executable(anthracite-api-bin src/api_main.cpp)
|
|
||||||
target_link_libraries(anthracite-api-bin anthracite)
|
|
||||||
|
|
||||||
include(FetchContent)
|
include(FetchContent)
|
||||||
FetchContent_Declare(
|
FetchContent_Declare(
|
||||||
googletest
|
googletest
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# Anthracite
|
# Anthracite
|
||||||
A simple web server written in C++. Supports HTTP 1.0 & 1.1.
|
|
||||||
|
Anthracite is an extensible, low-dependency, fast web server.
|
||||||
|
|
||||||
## Developing
|
## Developing
|
||||||
|
|
||||||
|
@ -16,7 +17,7 @@ Create a `build/` directory, run `cmake ..`, and then `make` to build.
|
||||||
- [x] HTTP/1.1
|
- [x] HTTP/1.1
|
||||||
- [x] Enhance logging
|
- [x] Enhance logging
|
||||||
- [x] Create library that can be used to implement custom backends (i.e. webapi, fileserver, etc)
|
- [x] Create library that can be used to implement custom backends (i.e. webapi, fileserver, etc)
|
||||||
- [ ] Faster parsing
|
- [x] Faster parsing
|
||||||
- [ ] HTTP/2
|
- [ ] HTTP/2
|
||||||
- [ ] Improve benchmarking infrastructure
|
- [ ] Improve benchmarking infrastructure
|
||||||
- [ ] Fix glaring security issues
|
- [ ] Fix glaring security issues
|
||||||
|
|
|
@ -1,44 +0,0 @@
|
||||||
#include "./anthracite.hpp"
|
|
||||||
#include "./log/log.hpp"
|
|
||||||
#include <iostream>
|
|
||||||
#include <netinet/in.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
#include <unistd.h>
|
|
||||||
#include "./config/config.hpp"
|
|
||||||
#include "./thread_mgr/event_loop.hpp"
|
|
||||||
#include <signal.h>
|
|
||||||
|
|
||||||
using namespace anthracite;
|
|
||||||
|
|
||||||
void log_request_and_response(http::request& req, std::unique_ptr<http::response>& resp, uint32_t micros);
|
|
||||||
|
|
||||||
constexpr int default_port = 80;
|
|
||||||
constexpr int max_worker_threads = 128;
|
|
||||||
|
|
||||||
thread_mgr::event_loop* elp = nullptr;
|
|
||||||
|
|
||||||
|
|
||||||
extern "C" void signalHandler(int signum) {
|
|
||||||
log::warn << "Caught signal #" << signum << ", exiting Anthracite" << std::endl;
|
|
||||||
elp->stop();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int anthracite_main(backends::backend& be, config::config& config)
|
|
||||||
{
|
|
||||||
signal(SIGTERM, signalHandler);
|
|
||||||
signal(SIGSEGV, signalHandler);
|
|
||||||
signal(SIGINT, signalHandler);
|
|
||||||
signal(SIGABRT, signalHandler);
|
|
||||||
|
|
||||||
log::logger.initialize(log::LOG_LEVEL_INFO);
|
|
||||||
thread_mgr::event_loop el(be, config);
|
|
||||||
elp = ⪙
|
|
||||||
el.start();
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
void log_request_and_response(http::request& req, std::unique_ptr<http::response>& resp, uint32_t micros)
|
|
||||||
{
|
|
||||||
log::info << "[" << resp->status_code() << " " + http::status_map.find(resp->status_code())->second + "] " + req.client_ip() + " " + http::reverse_method_map.find(req.get_method())->second + " " + req.path() << " in " << micros << " usecs" << std::endl;
|
|
||||||
}
|
|
|
@ -1,6 +0,0 @@
|
||||||
#include "backends/backend.hpp"
|
|
||||||
#include "config/config.hpp"
|
|
||||||
|
|
||||||
using namespace anthracite;
|
|
||||||
|
|
||||||
int anthracite_main(backends::backend& be, config::config& cfg);
|
|
|
@ -23,12 +23,13 @@ namespace anthracite::config {
|
||||||
};
|
};
|
||||||
|
|
||||||
class config {
|
class config {
|
||||||
uint16_t _worker_threads;
|
int _worker_threads;
|
||||||
|
int _max_clients;
|
||||||
std::optional<http_config> _http_config;
|
std::optional<http_config> _http_config;
|
||||||
std::optional<https_config> _https_config;
|
std::optional<https_config> _https_config;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
config(uint16_t worker_threads) : _worker_threads(worker_threads) {
|
config(int worker_threads, int max_clients) : _worker_threads(worker_threads), _max_clients(max_clients) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_http_config(http_config config) {
|
void add_http_config(http_config config) {
|
||||||
|
@ -39,10 +40,14 @@ namespace anthracite::config {
|
||||||
_https_config = config;
|
_https_config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t worker_threads() {
|
int worker_threads() {
|
||||||
return _worker_threads;
|
return _worker_threads;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int max_clients() {
|
||||||
|
return _max_clients;
|
||||||
|
}
|
||||||
|
|
||||||
std::optional<http_config>& http_cfg() {
|
std::optional<http_config>& http_cfg() {
|
||||||
return _http_config;
|
return _http_config;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,87 +1,91 @@
|
||||||
#include "request.hpp"
|
#include "request.hpp"
|
||||||
#include "../log/log.hpp"
|
#include "../log/log.hpp"
|
||||||
#include "constants.hpp"
|
#include "constants.hpp"
|
||||||
#include <map>
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <map>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
namespace anthracite::http {
|
namespace anthracite::http {
|
||||||
|
|
||||||
void request::parse_header(std::string& raw_line) {
|
void request::parse_header(std::string& raw_line)
|
||||||
|
{
|
||||||
auto delim_pos = raw_line.find_first_of(':');
|
auto delim_pos = raw_line.find_first_of(':');
|
||||||
auto value_pos = raw_line.find_first_not_of(' ', delim_pos+1);
|
auto value_pos = raw_line.find_first_not_of(' ', delim_pos + 1);
|
||||||
|
|
||||||
std::string header_name = raw_line.substr(0,delim_pos);
|
std::string header_name = raw_line.substr(0, delim_pos);
|
||||||
std::string header_val = raw_line.substr(value_pos);
|
std::string header_val = raw_line.substr(value_pos);
|
||||||
|
|
||||||
_headers[header_name] = header_val;
|
_headers[header_name] = header_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
void request::parse_query_param(std::string& raw_param) {
|
void request::parse_query_param(std::string& raw_param)
|
||||||
|
{
|
||||||
auto delim_pos = raw_param.find_first_of('=');
|
auto delim_pos = raw_param.find_first_of('=');
|
||||||
auto value_pos = delim_pos+1;
|
auto value_pos = delim_pos + 1;
|
||||||
|
|
||||||
std::string query_name = raw_param.substr(0,delim_pos);
|
std::string query_name = raw_param.substr(0, delim_pos);
|
||||||
std::string query_val = raw_param.substr(value_pos);
|
std::string query_val = raw_param.substr(value_pos);
|
||||||
|
|
||||||
_query_params[query_name] = query_val;
|
_query_params[query_name] = query_val;
|
||||||
}
|
}
|
||||||
|
|
||||||
void request::parse_path(char* raw_path) {
|
void request::parse_path(char* raw_path)
|
||||||
|
{
|
||||||
char* saveptr = nullptr;
|
char* saveptr = nullptr;
|
||||||
char* tok = strtok_r(raw_path, "?", &saveptr);
|
char* tok = strtok_r(raw_path, "?", &saveptr);
|
||||||
|
|
||||||
if (tok){
|
if (tok) {
|
||||||
_path = tok;
|
_path = tok;
|
||||||
}
|
}
|
||||||
|
|
||||||
tok = strtok_r(nullptr, "?", &saveptr);
|
tok = strtok_r(nullptr, "?", &saveptr);
|
||||||
while(tok) {
|
while (tok) {
|
||||||
std::string rtok(tok);
|
std::string rtok(tok);
|
||||||
parse_query_param(rtok);
|
parse_query_param(rtok);
|
||||||
tok = strtok_r(nullptr, "?", &saveptr);
|
tok = strtok_r(nullptr, "?", &saveptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void request::parse_request_line(char* raw_line) {
|
void request::parse_request_line(char* raw_line)
|
||||||
request_line_parser_state state = METHOD;
|
{
|
||||||
|
request_line_parser_state state = METHOD;
|
||||||
|
|
||||||
char* saveptr = nullptr;
|
char* saveptr = nullptr;
|
||||||
char* tok = strtok_r(raw_line, " \r", &saveptr);
|
char* tok = strtok_r(raw_line, " \r", &saveptr);
|
||||||
|
|
||||||
while(tok){
|
while (tok) {
|
||||||
switch(state) {
|
switch (state) {
|
||||||
case METHOD: {
|
case METHOD: {
|
||||||
auto search = method_map.find(tok);
|
auto search = method_map.find(tok);
|
||||||
if (search != method_map.end()) {
|
if (search != method_map.end()) {
|
||||||
_method = search->second;
|
_method = search->second;
|
||||||
} else {
|
} else {
|
||||||
_method = method::UNKNOWN;
|
_method = method::UNKNOWN;
|
||||||
}
|
|
||||||
|
|
||||||
state = PATH;
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
|
|
||||||
case PATH: {
|
|
||||||
std::string str_tok(tok);
|
|
||||||
parse_path(tok);
|
|
||||||
state = VERSION;
|
|
||||||
break;
|
|
||||||
};
|
|
||||||
|
|
||||||
case VERSION: {
|
|
||||||
auto search = version_map.find(tok);
|
|
||||||
if (search != version_map.end()) {
|
|
||||||
_http_version = search->second;
|
|
||||||
} else {
|
|
||||||
_http_version = version::HTTP_1_0;
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
tok = strtok_r(nullptr, " \r", &saveptr);
|
|
||||||
|
state = PATH;
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
|
case PATH: {
|
||||||
|
std::string str_tok(tok);
|
||||||
|
parse_path(tok);
|
||||||
|
state = VERSION;
|
||||||
|
break;
|
||||||
|
};
|
||||||
|
|
||||||
|
case VERSION: {
|
||||||
|
auto search = version_map.find(tok);
|
||||||
|
if (search != version_map.end()) {
|
||||||
|
_http_version = search->second;
|
||||||
|
} else {
|
||||||
|
_http_version = version::HTTP_1_0;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
tok = strtok_r(nullptr, " \r", &saveptr);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
request::request(std::string& raw_data, const std::string& client_ip)
|
request::request(std::string& raw_data, const std::string& client_ip)
|
||||||
|
@ -94,26 +98,27 @@ request::request(std::string& raw_data, const std::string& client_ip)
|
||||||
char* saveptr = nullptr;
|
char* saveptr = nullptr;
|
||||||
char* tok = strtok_r(raw_data.data(), "\r\n", &saveptr);
|
char* tok = strtok_r(raw_data.data(), "\r\n", &saveptr);
|
||||||
|
|
||||||
while(tok && state != BODY_CONTENT){
|
while (tok && state != BODY_CONTENT) {
|
||||||
switch(state) {
|
switch (state) {
|
||||||
case REQUEST_LINE: {
|
case REQUEST_LINE: {
|
||||||
parse_request_line(tok);
|
parse_request_line(tok);
|
||||||
state = HEADERS;
|
state = HEADERS;
|
||||||
tok = strtok_r(nullptr, "\n", &saveptr);
|
tok = strtok_r(nullptr, "\n", &saveptr);
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
case HEADERS: {
|
case HEADERS: {
|
||||||
if (tok[0] == '\r') {
|
if (tok[0] == '\r') {
|
||||||
state = BODY_CONTENT;
|
state = BODY_CONTENT;
|
||||||
} else {
|
} else {
|
||||||
std::string rtok(tok);
|
std::string rtok(tok);
|
||||||
rtok.pop_back();
|
rtok.pop_back();
|
||||||
parse_header(rtok);
|
parse_header(rtok);
|
||||||
tok = strtok_r(nullptr, "\n", &saveptr);
|
tok = strtok_r(nullptr, "\n", &saveptr);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
case BODY_CONTENT: break;
|
case BODY_CONTENT:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,9 +126,9 @@ request::request(std::string& raw_data, const std::string& client_ip)
|
||||||
if (tok) {
|
if (tok) {
|
||||||
_body_content = std::string(tok);
|
_body_content = std::string(tok);
|
||||||
}
|
}
|
||||||
//if (getline(line_stream, line, '\0')) {
|
// if (getline(line_stream, line, '\0')) {
|
||||||
// _body_content = line;
|
// _body_content = line;
|
||||||
//}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string request::path() { return _path; }
|
std::string request::path() { return _path; }
|
||||||
|
@ -147,11 +152,11 @@ 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();
|
||||||
|
|
||||||
if (found && header->second == "keep-alive") {
|
if (found && header->second == "close") {
|
||||||
return false;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string request::to_string()
|
std::string request::to_string()
|
||||||
|
|
|
@ -10,6 +10,11 @@ void Logger::initialize(enum LOG_LEVEL level)
|
||||||
_level = level;
|
_level = level;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Logger::log_request_and_response(http::request& req, std::unique_ptr<http::response>& resp, uint32_t micros)
|
||||||
|
{
|
||||||
|
log::info << "[" << resp->status_code() << " " + http::status_map.find(resp->status_code())->second + "] " + req.client_ip() + " " + http::reverse_method_map.find(req.get_method())->second + " " + req.path() << " in " << micros << " usecs" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
LogBuf::LogBuf(std::ostream& output_stream, const std::string& tag, enum LOG_LEVEL level)
|
LogBuf::LogBuf(std::ostream& output_stream, const std::string& tag, enum LOG_LEVEL level)
|
||||||
: _output_stream(output_stream)
|
: _output_stream(output_stream)
|
||||||
, _tag(tag)
|
, _tag(tag)
|
||||||
|
|
|
@ -3,6 +3,10 @@
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
|
#include <inttypes.h>
|
||||||
|
#include <memory>
|
||||||
|
#include "../http/request.hpp"
|
||||||
|
#include "../http/response.hpp"
|
||||||
|
|
||||||
namespace anthracite::log {
|
namespace anthracite::log {
|
||||||
enum LOG_LEVEL {
|
enum LOG_LEVEL {
|
||||||
|
@ -21,9 +25,9 @@ namespace anthracite::log {
|
||||||
|
|
||||||
Logger();
|
Logger();
|
||||||
void initialize(enum LOG_LEVEL level);
|
void initialize(enum LOG_LEVEL level);
|
||||||
|
void log_request_and_response(http::request& req, std::unique_ptr<http::response>& resp, uint32_t micros);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class LogBuf : public std::stringbuf
|
class LogBuf : public std::stringbuf
|
||||||
{
|
{
|
||||||
std::string _tag;
|
std::string _tag;
|
||||||
|
@ -50,4 +54,6 @@ namespace anthracite::log {
|
||||||
|
|
||||||
static class LogBuf debugBuf{std::cout, "DEBG", LOG_LEVEL_DEBUG};
|
static class LogBuf debugBuf{std::cout, "DEBG", LOG_LEVEL_DEBUG};
|
||||||
static std::ostream debug(&debugBuf);
|
static std::ostream debug(&debugBuf);
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
#include <exception>
|
|
||||||
#include <openssl/ssl.h>
|
|
||||||
#include <openssl/err.h>
|
|
||||||
#include "./openssl_socket.hpp"
|
#include "./openssl_socket.hpp"
|
||||||
|
#include "../log/log.hpp"
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <array>
|
#include <array>
|
||||||
|
#include <exception>
|
||||||
|
#include <iostream>
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
|
#include <openssl/err.h>
|
||||||
|
#include <openssl/ssl.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <iostream>
|
|
||||||
#include "../log/log.hpp"
|
|
||||||
|
|
||||||
namespace anthracite::socket {
|
namespace anthracite::socket {
|
||||||
|
|
||||||
|
@ -21,7 +21,7 @@ SSL_CTX* openssl_socket::_context = nullptr;
|
||||||
openssl_socket::openssl_socket(int port, int max_queue)
|
openssl_socket::openssl_socket(int port, int max_queue)
|
||||||
: anthracite_socket(port, max_queue)
|
: anthracite_socket(port, max_queue)
|
||||||
{
|
{
|
||||||
const SSL_METHOD *method = TLS_server_method();
|
const SSL_METHOD* method = TLS_server_method();
|
||||||
|
|
||||||
if (_context == nullptr) {
|
if (_context == nullptr) {
|
||||||
_context = SSL_CTX_new(method);
|
_context = SSL_CTX_new(method);
|
||||||
|
@ -37,7 +37,7 @@ openssl_socket::openssl_socket(int port, int max_queue)
|
||||||
throw std::exception();
|
throw std::exception();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SSL_CTX_use_PrivateKey_file(_context, "key.pem", SSL_FILETYPE_PEM) <= 0 ) {
|
if (SSL_CTX_use_PrivateKey_file(_context, "key.pem", SSL_FILETYPE_PEM) <= 0) {
|
||||||
log::err << "Unable to open key.pem" << std::endl;
|
log::err << "Unable to open key.pem" << std::endl;
|
||||||
throw std::exception();
|
throw std::exception();
|
||||||
}
|
}
|
||||||
|
@ -48,11 +48,11 @@ openssl_socket::~openssl_socket() = default;
|
||||||
bool openssl_socket::wait_for_conn()
|
bool openssl_socket::wait_for_conn()
|
||||||
{
|
{
|
||||||
client_ip = "";
|
client_ip = "";
|
||||||
struct timeval tv = {.tv_sec = 1, .tv_usec = 0};
|
struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
|
||||||
fd_set read_fd;
|
fd_set read_fd;
|
||||||
FD_ZERO(&read_fd);
|
FD_ZERO(&read_fd);
|
||||||
FD_SET(server_socket, &read_fd);
|
FD_SET(server_socket, &read_fd);
|
||||||
if (select(server_socket+1, &read_fd, NULL, NULL, &wait_timeout)) {
|
if (select(server_socket + 1, &read_fd, NULL, NULL, &wait_timeout)) {
|
||||||
client_socket = accept(server_socket, reinterpret_cast<struct sockaddr*>(&client_addr), &client_addr_len);
|
client_socket = accept(server_socket, reinterpret_cast<struct sockaddr*>(&client_addr), &client_addr_len);
|
||||||
std::array<char, INET_ADDRSTRLEN> ip_str { 0 };
|
std::array<char, INET_ADDRSTRLEN> ip_str { 0 };
|
||||||
inet_ntop(AF_INET, &client_addr.sin_addr, ip_str.data(), INET_ADDRSTRLEN);
|
inet_ntop(AF_INET, &client_addr.sin_addr, ip_str.data(), INET_ADDRSTRLEN);
|
||||||
|
@ -70,8 +70,6 @@ bool openssl_socket::wait_for_conn()
|
||||||
} else {
|
} else {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void openssl_socket::close_conn()
|
void openssl_socket::close_conn()
|
||||||
|
@ -98,7 +96,7 @@ std::string openssl_socket::recv_message(int buffer_size)
|
||||||
|
|
||||||
setsockopt(client_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout_tv, sizeof timeout_tv);
|
setsockopt(client_socket, SOL_SOCKET, SO_RCVTIMEO, &timeout_tv, sizeof timeout_tv);
|
||||||
std::vector<char> response(buffer_size + 1);
|
std::vector<char> response(buffer_size + 1);
|
||||||
ssize_t result = SSL_read(_ssl, response.data(), buffer_size+1);
|
ssize_t result = SSL_read(_ssl, response.data(), buffer_size + 1);
|
||||||
|
|
||||||
if (result < 1) {
|
if (result < 1) {
|
||||||
return "";
|
return "";
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
|
|
||||||
namespace anthracite::socket {
|
namespace anthracite::socket {
|
||||||
|
|
||||||
const struct timeval anthracite_socket::timeout_tv = { .tv_sec = 5, .tv_usec = 0};
|
const struct timeval anthracite_socket::timeout_tv = { .tv_sec = 5, .tv_usec = 0 };
|
||||||
|
|
||||||
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))
|
||||||
|
@ -32,11 +32,11 @@ anthracite_socket::anthracite_socket(int port, int max_queue)
|
||||||
bool anthracite_socket::wait_for_conn()
|
bool anthracite_socket::wait_for_conn()
|
||||||
{
|
{
|
||||||
client_ip = "";
|
client_ip = "";
|
||||||
struct timeval tv = {.tv_sec = 1, .tv_usec = 0};
|
struct timeval tv = { .tv_sec = 1, .tv_usec = 0 };
|
||||||
fd_set read_fd;
|
fd_set read_fd;
|
||||||
FD_ZERO(&read_fd);
|
FD_ZERO(&read_fd);
|
||||||
FD_SET(server_socket, &read_fd);
|
FD_SET(server_socket, &read_fd);
|
||||||
if (select(server_socket+1, &read_fd, NULL, NULL, &wait_timeout)) {
|
if (select(server_socket + 1, &read_fd, NULL, NULL, &wait_timeout)) {
|
||||||
client_socket = accept(server_socket, reinterpret_cast<struct sockaddr*>(&client_addr), &client_addr_len);
|
client_socket = accept(server_socket, reinterpret_cast<struct sockaddr*>(&client_addr), &client_addr_len);
|
||||||
std::array<char, INET_ADDRSTRLEN> ip_str { 0 };
|
std::array<char, INET_ADDRSTRLEN> ip_str { 0 };
|
||||||
inet_ntop(AF_INET, &client_addr.sin_addr, ip_str.data(), INET_ADDRSTRLEN);
|
inet_ntop(AF_INET, &client_addr.sin_addr, ip_str.data(), INET_ADDRSTRLEN);
|
||||||
|
|
|
@ -32,6 +32,8 @@ public:
|
||||||
virtual void close_conn();
|
virtual void close_conn();
|
||||||
virtual void send_message(std::string& msg);
|
virtual void send_message(std::string& msg);
|
||||||
virtual std::string recv_message(int buffer_size);
|
virtual std::string recv_message(int buffer_size);
|
||||||
|
|
||||||
|
int csock() { return client_socket; }
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,176 +1,217 @@
|
||||||
#include "./event_loop.hpp"
|
#include "./event_loop.hpp"
|
||||||
#include "../log/log.hpp"
|
#include "../log/log.hpp"
|
||||||
#include "../socket/openssl_socket.hpp"
|
#include "../socket/openssl_socket.hpp"
|
||||||
#include <mutex>
|
#include "assert.h"
|
||||||
|
#include "sys/epoll.h"
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <mutex>
|
||||||
#include <syncstream>
|
#include <syncstream>
|
||||||
|
|
||||||
using std::chrono::high_resolution_clock;
|
|
||||||
using std::chrono::duration_cast;
|
|
||||||
using std::chrono::duration;
|
using std::chrono::duration;
|
||||||
|
using std::chrono::duration_cast;
|
||||||
|
using std::chrono::high_resolution_clock;
|
||||||
using std::chrono::milliseconds;
|
using std::chrono::milliseconds;
|
||||||
|
|
||||||
namespace anthracite::thread_mgr {
|
namespace anthracite::thread_mgr {
|
||||||
event_loop::event::event(socket::anthracite_socket* socket) :
|
event_loop::event::event(socket::anthracite_socket* socket, std::chrono::time_point<std::chrono::high_resolution_clock> timestamp)
|
||||||
_socket(socket) {}
|
: _socket(socket)
|
||||||
|
, _ts(timestamp)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
socket::anthracite_socket* event_loop::event::socket() {
|
socket::anthracite_socket* event_loop::event::socket()
|
||||||
return _socket;
|
{
|
||||||
|
return _socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::chrono::time_point<std::chrono::high_resolution_clock>& event_loop::event::timestamp()
|
||||||
|
{
|
||||||
|
return _ts;
|
||||||
|
}
|
||||||
|
|
||||||
|
event_loop::event_loop(backends::backend& backend, config::config& config)
|
||||||
|
: thread_mgr(backend, config)
|
||||||
|
, _error_backend("./www")
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
bool event_loop::event_handler(event& event)
|
||||||
|
{
|
||||||
|
std::string raw_request = event.socket()->recv_message(http::HEADER_BYTES);
|
||||||
|
|
||||||
|
if (raw_request == "") {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
event_loop::event_loop(backends::backend& backend, config::config& config) : thread_mgr(backend, config), _error_backend("./www") {}
|
http::request req(raw_request, event.socket()->get_client_ip());
|
||||||
|
std::unique_ptr<http::response> resp = req.is_supported_version() ? _backend.handle_request(req) : _error_backend.handle_error(http::status_codes::HTTP_VERSION_NOT_SUPPORTED);
|
||||||
|
std::string header = resp->header_to_string();
|
||||||
|
event.socket()->send_message(header);
|
||||||
|
event.socket()->send_message(resp->content());
|
||||||
|
|
||||||
bool event_loop::event_handler(event& event) {
|
auto end = high_resolution_clock::now();
|
||||||
std::string raw_request = event.socket()->recv_message(http::HEADER_BYTES);
|
auto ms_int = duration_cast<std::chrono::microseconds>(end - event.timestamp());
|
||||||
|
log::logger.log_request_and_response(req, resp, ms_int.count());
|
||||||
|
|
||||||
// We're doing the start here even though it would ideally be done
|
resp.reset();
|
||||||
// before the first line since if we leave the connection open for
|
if (req.close_connection()) {
|
||||||
// HTTP 1.1, we can spend a bit of time waiting
|
return false;
|
||||||
auto start = high_resolution_clock::now();
|
|
||||||
|
|
||||||
if (raw_request == "") {
|
|
||||||
event.socket()->close_conn();
|
|
||||||
delete event.socket();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
http::request req(raw_request, event.socket()->get_client_ip());
|
|
||||||
std::unique_ptr<http::response> resp = req.is_supported_version() ? _backend.handle_request(req) : _error_backend.handle_error(http::status_codes::HTTP_VERSION_NOT_SUPPORTED);
|
|
||||||
std::string header = resp->header_to_string();
|
|
||||||
event.socket()->send_message(header);
|
|
||||||
event.socket()->send_message(resp->content());
|
|
||||||
|
|
||||||
auto end = high_resolution_clock::now();
|
|
||||||
auto ms_int = duration_cast<std::chrono::microseconds>(end-start);
|
|
||||||
//log_request_and_response(req, resp , ms_int.count());
|
|
||||||
|
|
||||||
resp.reset();
|
|
||||||
if (req.close_connection()) {
|
|
||||||
event.socket()->close_conn();
|
|
||||||
delete event.socket();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void event_loop::worker_thread_loop(int threadno) {
|
return true;
|
||||||
unsigned char buf[sizeof(class event)];
|
}
|
||||||
|
|
||||||
std::osyncstream(log::info) << "Starting worker thread " << threadno << std::endl;
|
void event_loop::worker_thread_loop(int threadno)
|
||||||
while(_run) {
|
{
|
||||||
// Get event from queue
|
unsigned char buf[sizeof(class event)];
|
||||||
std::unique_lock lock(_event_mtx);
|
|
||||||
|
|
||||||
|
std::osyncstream(log::info) << "Starting worker thread " << threadno << std::endl;
|
||||||
|
while (_run) {
|
||||||
|
// Get event from queue
|
||||||
|
std::unique_lock lock(_event_mtx);
|
||||||
|
|
||||||
event* ev = nullptr;
|
event* ev = nullptr;
|
||||||
|
|
||||||
if (_events.size() > 0) {
|
if (_events.size() > 0) {
|
||||||
ev = new (buf) event(_events.back());
|
if (!lock.owns_lock()) {
|
||||||
_events.pop();
|
lock.lock();
|
||||||
lock.unlock();
|
|
||||||
} else {
|
|
||||||
_event_cv.wait(lock, [this]{ return this->_events.size() > 0 || !_run; });
|
|
||||||
|
|
||||||
if (!_run) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ev = new (buf) event(_events.back());
|
|
||||||
_events.pop();
|
|
||||||
lock.unlock();
|
|
||||||
}
|
}
|
||||||
|
assert(lock.owns_lock());
|
||||||
|
ev = new (buf) event(_events.front());
|
||||||
// process
|
_events.pop();
|
||||||
bool requeue = event_handler(*ev);
|
lock.unlock();
|
||||||
|
|
||||||
// if necessary, requeue
|
|
||||||
if (requeue) {
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> lg(_event_mtx);
|
|
||||||
_events.push(*ev);
|
|
||||||
}
|
|
||||||
_event_cv.notify_one();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::osyncstream(log::info) << "Stopping worker thread " << threadno << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
void event_loop::listener_thread_loop(config::http_config& http_config) {
|
|
||||||
socket::anthracite_socket* socket;
|
|
||||||
|
|
||||||
config::http_config* http_ptr = &http_config;
|
|
||||||
config::https_config* https_ptr = dynamic_cast<config::https_config*>(http_ptr);
|
|
||||||
|
|
||||||
bool is_tls = https_ptr != nullptr;
|
|
||||||
|
|
||||||
if (is_tls){
|
|
||||||
socket = new socket::openssl_socket(https_ptr->port());
|
|
||||||
} else {
|
} else {
|
||||||
socket = new socket::anthracite_socket(http_ptr->port());
|
_event_cv.wait(lock, [this] { return this->_events.size() > 0 || !_run; });
|
||||||
}
|
|
||||||
|
|
||||||
std::osyncstream(log::info) << "Listening for " << (is_tls ? "HTTPS" : "HTTP" ) << " connections on port " << http_ptr->port() << std::endl;
|
if (!_run) {
|
||||||
|
break;
|
||||||
while (_run) {
|
|
||||||
if(socket->wait_for_conn()) {
|
|
||||||
socket::anthracite_socket* client_sock;
|
|
||||||
|
|
||||||
if (is_tls){
|
|
||||||
socket::openssl_socket* ssl_sock = dynamic_cast<socket::openssl_socket*>(socket);
|
|
||||||
client_sock = new socket::openssl_socket(*ssl_sock);
|
|
||||||
} else {
|
|
||||||
client_sock = new socket::anthracite_socket(*socket);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lg(_event_mtx);
|
|
||||||
_events.push(event(client_sock));
|
|
||||||
_event_cv.notify_one();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
assert(lock.owns_lock());
|
||||||
|
ev = new (buf) event(_events.front());
|
||||||
|
_events.pop();
|
||||||
|
lock.unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::osyncstream(log::info) << "Stopping listening for " << (is_tls ? "HTTPS" : "HTTP") << " connections on port " << http_ptr->port() << std::endl;
|
if (event_handler(*ev)) {
|
||||||
|
struct epoll_event event;
|
||||||
delete socket;
|
event.events = EPOLLIN;
|
||||||
}
|
event.data.ptr = ev->socket();
|
||||||
|
epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, ev->socket()->csock(), &event);
|
||||||
void event_loop::start() {
|
} else {
|
||||||
log::info << "Starting event_loop Thread Manager" << std::endl;
|
ev->socket()->close_conn();
|
||||||
|
delete ev->socket();
|
||||||
_run = true;
|
|
||||||
|
|
||||||
std::vector<std::thread> listener_threads;
|
|
||||||
std::vector<std::thread> worker_threads;
|
|
||||||
|
|
||||||
for(int i = 0; i < _config.worker_threads(); i++) {
|
|
||||||
auto thread = std::thread(&event_loop::worker_thread_loop, this, i);
|
|
||||||
worker_threads.push_back(std::move(thread));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_config.http_cfg().has_value()) {
|
|
||||||
auto thread = std::thread(&event_loop::listener_thread_loop, this, std::ref(_config.http_cfg().value()));
|
|
||||||
listener_threads.push_back(std::move(thread));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (_config.https_cfg().has_value()) {
|
|
||||||
auto thread = std::thread(&event_loop::listener_thread_loop, this, std::ref(_config.https_cfg().value()));
|
|
||||||
listener_threads.push_back(std::move(thread));
|
|
||||||
}
|
|
||||||
|
|
||||||
for(std::thread& t : worker_threads) {
|
|
||||||
t.join();
|
|
||||||
}
|
|
||||||
|
|
||||||
for(std::thread& t : listener_threads) {
|
|
||||||
t.join();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void event_loop::stop() {
|
std::osyncstream(log::info) << "Stopping worker thread " << threadno << std::endl;
|
||||||
_run = false;
|
}
|
||||||
std::lock_guard<std::mutex> lg(_event_mtx);
|
|
||||||
_event_cv.notify_all();
|
void event_loop::eventer_thread_loop()
|
||||||
|
{
|
||||||
|
struct epoll_event* events = new struct epoll_event[_config.max_clients()];
|
||||||
|
std::osyncstream(log::info) << "epoll() thread started" << std::endl;
|
||||||
|
while (_run) {
|
||||||
|
int ready_fds = epoll_wait(_epoll_fd, events, _config.max_clients(), 1000);
|
||||||
|
|
||||||
|
if (ready_fds > 0) {
|
||||||
|
std::lock_guard<std::mutex> lg(_event_mtx);
|
||||||
|
for (int i = 0; i < ready_fds; i++) {
|
||||||
|
socket::anthracite_socket* sockptr = reinterpret_cast<socket::anthracite_socket*>(events[i].data.ptr);
|
||||||
|
struct epoll_event ev;
|
||||||
|
epoll_ctl(_epoll_fd, EPOLL_CTL_DEL, sockptr->csock(), &events[i]);
|
||||||
|
_events.push(event(sockptr, std::chrono::high_resolution_clock::now()));
|
||||||
|
}
|
||||||
|
_event_cv.notify_one();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
delete[] events;
|
||||||
|
std::osyncstream(log::info) << "epoll() thread exited" << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
void event_loop::listener_thread_loop(config::http_config& http_config)
|
||||||
|
{
|
||||||
|
socket::anthracite_socket* socket;
|
||||||
|
|
||||||
|
config::http_config* http_ptr = &http_config;
|
||||||
|
config::https_config* https_ptr = dynamic_cast<config::https_config*>(http_ptr);
|
||||||
|
|
||||||
|
bool is_tls = https_ptr != nullptr;
|
||||||
|
|
||||||
|
if (is_tls) {
|
||||||
|
socket = new socket::openssl_socket(https_ptr->port());
|
||||||
|
} else {
|
||||||
|
socket = new socket::anthracite_socket(http_ptr->port());
|
||||||
|
}
|
||||||
|
|
||||||
|
std::osyncstream(log::info) << "Listening for " << (is_tls ? "HTTPS" : "HTTP") << " connections on port " << http_ptr->port() << std::endl;
|
||||||
|
|
||||||
|
while (_run) {
|
||||||
|
if (socket->wait_for_conn()) {
|
||||||
|
socket::anthracite_socket* client_sock;
|
||||||
|
|
||||||
|
if (is_tls) {
|
||||||
|
socket::openssl_socket* ssl_sock = dynamic_cast<socket::openssl_socket*>(socket);
|
||||||
|
client_sock = new socket::openssl_socket(*ssl_sock);
|
||||||
|
} else {
|
||||||
|
client_sock = new socket::anthracite_socket(*socket);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct epoll_event event;
|
||||||
|
event.events = EPOLLIN;
|
||||||
|
event.data.ptr = client_sock;
|
||||||
|
epoll_ctl(_epoll_fd, EPOLL_CTL_ADD, client_sock->csock(), &event);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::osyncstream(log::info) << "Stopping listening for " << (is_tls ? "HTTPS" : "HTTP") << " connections on port " << http_ptr->port() << std::endl;
|
||||||
|
|
||||||
|
delete socket;
|
||||||
|
}
|
||||||
|
|
||||||
|
void event_loop::start()
|
||||||
|
{
|
||||||
|
log::info << "Starting event_loop Thread Manager" << std::endl;
|
||||||
|
|
||||||
|
_epoll_fd = epoll_create(1);
|
||||||
|
_run = true;
|
||||||
|
|
||||||
|
std::vector<std::thread> listener_threads;
|
||||||
|
std::vector<std::thread> worker_threads;
|
||||||
|
|
||||||
|
for (int i = 0; i < _config.worker_threads(); i++) {
|
||||||
|
auto thread = std::thread(&event_loop::worker_thread_loop, this, i);
|
||||||
|
worker_threads.push_back(std::move(thread));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_config.http_cfg().has_value()) {
|
||||||
|
auto thread = std::thread(&event_loop::listener_thread_loop, this, std::ref(_config.http_cfg().value()));
|
||||||
|
listener_threads.push_back(std::move(thread));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_config.https_cfg().has_value()) {
|
||||||
|
auto thread = std::thread(&event_loop::listener_thread_loop, this, std::ref(_config.https_cfg().value()));
|
||||||
|
listener_threads.push_back(std::move(thread));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
auto thread = std::thread(&event_loop::eventer_thread_loop, this);
|
||||||
|
listener_threads.push_back(std::move(thread));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (std::thread& t : worker_threads) {
|
||||||
|
t.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
for (std::thread& t : listener_threads) {
|
||||||
|
t.join();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void event_loop::stop()
|
||||||
|
{
|
||||||
|
_run = false;
|
||||||
|
std::lock_guard<std::mutex> lg(_event_mtx);
|
||||||
|
_event_cv.notify_all();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
#include "./thread_mgr.hpp"
|
#include "./thread_mgr.hpp"
|
||||||
#include "../socket/socket.hpp"
|
#include "../socket/socket.hpp"
|
||||||
#include "../backends/file_backend.hpp"
|
#include "../backends/file_backend.hpp"
|
||||||
|
#include <chrono>
|
||||||
#include <condition_variable>
|
#include <condition_variable>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <queue>
|
#include <queue>
|
||||||
|
@ -9,11 +10,14 @@ namespace anthracite::thread_mgr {
|
||||||
class event_loop : public virtual thread_mgr {
|
class event_loop : public virtual thread_mgr {
|
||||||
class event {
|
class event {
|
||||||
socket::anthracite_socket* _socket;
|
socket::anthracite_socket* _socket;
|
||||||
|
std::chrono::time_point<std::chrono::high_resolution_clock> _ts;
|
||||||
public:
|
public:
|
||||||
event(socket::anthracite_socket* socket);
|
event(socket::anthracite_socket* socket, std::chrono::time_point<std::chrono::high_resolution_clock> timestamp);
|
||||||
socket::anthracite_socket* socket();
|
socket::anthracite_socket* socket();
|
||||||
|
std::chrono::time_point<std::chrono::high_resolution_clock>& timestamp();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
int _epoll_fd;
|
||||||
std::mutex _event_mtx;
|
std::mutex _event_mtx;
|
||||||
std::condition_variable _event_cv;
|
std::condition_variable _event_cv;
|
||||||
std::queue<event> _events;
|
std::queue<event> _events;
|
||||||
|
@ -21,6 +25,7 @@ namespace anthracite::thread_mgr {
|
||||||
|
|
||||||
void worker_thread_loop(int threadno);
|
void worker_thread_loop(int threadno);
|
||||||
void listener_thread_loop(config::http_config& http_config);
|
void listener_thread_loop(config::http_config& http_config);
|
||||||
|
void eventer_thread_loop();
|
||||||
bool event_handler(event& ev);
|
bool event_handler(event& ev);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
112
src/api_main.cpp
112
src/api_main.cpp
|
@ -1,112 +0,0 @@
|
||||||
#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 <span>
|
|
||||||
|
|
||||||
using namespace anthracite;
|
|
||||||
|
|
||||||
using CallbackType = std::unique_ptr<http::response> (*)(http::request&);
|
|
||||||
class api_backend : public backends::backend {
|
|
||||||
|
|
||||||
class RouteNode {
|
|
||||||
public:
|
|
||||||
std::optional<CallbackType> callback;
|
|
||||||
|
|
||||||
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> resp = std::make_unique<http::response>();
|
|
||||||
|
|
||||||
resp->add_body("Not Found");
|
|
||||||
resp->add_header(http::header("Content-Type", "application/json"));
|
|
||||||
resp->add_status(http::status_codes::NOT_FOUND);
|
|
||||||
|
|
||||||
return resp;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
std::string item;
|
|
||||||
|
|
||||||
RouteNode* cur = &root;
|
|
||||||
while (getline(ss, item, '/')) {
|
|
||||||
if (cur->routes.find(item) == cur->routes.end()) {
|
|
||||||
if (cur->routes.find("*") == cur->routes.end()) {
|
|
||||||
break;
|
|
||||||
} else {
|
|
||||||
cur = &cur->routes["*"];
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
cur = &cur->routes[item];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cur->callback.has_value()) {
|
|
||||||
return cur->callback.value()(req);
|
|
||||||
} else {
|
|
||||||
return default_route(req);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
std::unique_ptr<http::response> handle_request(http::request& req) override
|
|
||||||
{
|
|
||||||
return find_handler(req);
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
|
||||||
api_backend()
|
|
||||||
{
|
|
||||||
root.routes = std::unordered_map<std::string, RouteNode>();
|
|
||||||
}
|
|
||||||
|
|
||||||
void register_endpoint(std::string pathspec, CallbackType callback)
|
|
||||||
{
|
|
||||||
std::vector<std::string> result;
|
|
||||||
std::stringstream ss(pathspec);
|
|
||||||
std::string item;
|
|
||||||
|
|
||||||
RouteNode* cur = &root;
|
|
||||||
while (getline(ss, item, '/')) {
|
|
||||||
cur->routes[item] = RouteNode {};
|
|
||||||
cur = &cur->routes[item];
|
|
||||||
}
|
|
||||||
|
|
||||||
cur->callback = callback;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
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"}")");
|
|
||||||
resp->add_header(http::header("Content-Type", "application/json"));
|
|
||||||
resp->add_status(http::status_codes::OK);
|
|
||||||
|
|
||||||
return resp;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
|
||||||
{
|
|
||||||
auto args = std::span(argv, size_t(argc));
|
|
||||||
api_backend ab;
|
|
||||||
ab.register_endpoint("users/*", handle_request);
|
|
||||||
//anthracite_main(argc, argv, ab);
|
|
||||||
}
|
|
|
@ -1,15 +1,35 @@
|
||||||
#include "../lib/anthracite.hpp"
|
|
||||||
#include "../lib/backends/file_backend.hpp"
|
#include "../lib/backends/file_backend.hpp"
|
||||||
#include "../lib/config/config.hpp"
|
#include "../lib/config/config.hpp"
|
||||||
|
#include "../lib/log/log.hpp"
|
||||||
|
#include "../lib/thread_mgr/event_loop.hpp"
|
||||||
|
#include "signal.h"
|
||||||
|
#include "string.h"
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
using namespace anthracite;
|
std::shared_ptr<anthracite::thread_mgr::thread_mgr> server = nullptr;
|
||||||
|
|
||||||
|
extern "C" void signalHandler(int signum)
|
||||||
|
{
|
||||||
|
anthracite::log::warn << "Caught signal SIG" << sigabbrev_np(signum) << ", exiting Anthracite" << std::endl;
|
||||||
|
if (server != nullptr) {
|
||||||
|
server->stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
backends::file_backend fb("./www");
|
anthracite::log::logger.initialize(anthracite::log::LOG_LEVEL_INFO);
|
||||||
config::config cfg(5);
|
anthracite::log::info << "Starting Anthracite, a higher performance web server" << std::endl;
|
||||||
cfg.add_http_config(config::http_config(8080));
|
signal(SIGINT, signalHandler);
|
||||||
cfg.add_https_config(config::https_config(8081, "", ""));
|
|
||||||
|
|
||||||
anthracite_main(fb, cfg);
|
anthracite::backends::file_backend fb("./www");
|
||||||
|
anthracite::config::config cfg(5, 10000);
|
||||||
|
cfg.add_http_config(anthracite::config::http_config(8080));
|
||||||
|
// cfg.add_https_config(config::https_config(8081, "", ""));
|
||||||
|
|
||||||
|
server = std::make_shared<anthracite::thread_mgr::event_loop>(fb, cfg);
|
||||||
|
|
||||||
|
server->start();
|
||||||
|
|
||||||
|
anthracite::log::info << "Stopping Anthracite, a higher performance web server" << std::endl;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,21 @@
|
||||||
#include <gtest/gtest.h>
|
|
||||||
#include <fstream>
|
|
||||||
#include <chrono>
|
|
||||||
#include "../lib/http/request.hpp"
|
#include "../lib/http/request.hpp"
|
||||||
|
#include <chrono>
|
||||||
|
#include <fstream>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
#ifdef SPEEDTEST_COMPARE_BOOST
|
#ifdef SPEEDTEST_COMPARE_BOOST
|
||||||
#include <boost/beast.hpp>
|
#include <boost/beast.hpp>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
using std::chrono::high_resolution_clock;
|
|
||||||
using std::chrono::duration_cast;
|
|
||||||
using std::chrono::duration;
|
using std::chrono::duration;
|
||||||
|
using std::chrono::duration_cast;
|
||||||
|
using std::chrono::high_resolution_clock;
|
||||||
using std::chrono::milliseconds;
|
using std::chrono::milliseconds;
|
||||||
|
|
||||||
constexpr uint32_t num_requests = 10000000;
|
constexpr uint32_t num_requests = 10000000;
|
||||||
|
|
||||||
TEST(speed_tests, request_parse) {
|
TEST(speed_tests, request_parse)
|
||||||
|
{
|
||||||
std::ifstream t("./test_files/test_request.http");
|
std::ifstream t("./test_files/test_request.http");
|
||||||
std::stringstream buffer;
|
std::stringstream buffer;
|
||||||
buffer << t.rdbuf();
|
buffer << t.rdbuf();
|
||||||
|
@ -23,24 +23,25 @@ TEST(speed_tests, request_parse) {
|
||||||
|
|
||||||
auto start = high_resolution_clock::now();
|
auto start = high_resolution_clock::now();
|
||||||
|
|
||||||
for(int i = 0; i < num_requests; ++i) {
|
for (int i = 0; i < num_requests; ++i) {
|
||||||
volatile anthracite::http::request req (raw_req, "0.0.0.0");
|
volatile anthracite::http::request req(raw_req, "0.0.0.0");
|
||||||
}
|
}
|
||||||
|
|
||||||
auto end = high_resolution_clock::now();
|
auto end = high_resolution_clock::now();
|
||||||
|
|
||||||
auto ms_int = duration_cast<milliseconds>(end-start);
|
auto ms_int = duration_cast<milliseconds>(end - start);
|
||||||
|
|
||||||
double m_rps = ((1000.0 / ms_int.count()) * num_requests) / 1000000;
|
double m_rps = ((1000.0 / ms_int.count()) * num_requests) / 1000000;
|
||||||
|
|
||||||
std::cout << "Parsed " << (num_requests/1000000) << " Million requests in " << ms_int << " ms";
|
std::cout << "Parsed " << (num_requests / 1000000) << " Million requests in " << ms_int << " ms";
|
||||||
std::cout << " at " << m_rps << " Million RPS " << std::endl;
|
std::cout << " at " << m_rps << " Million RPS " << std::endl;
|
||||||
|
|
||||||
ASSERT_LT(ms_int.count(), 2000);
|
ASSERT_LT(ms_int.count(), 2000);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef SPEEDTEST_COMPARE_BOOST
|
#ifdef SPEEDTEST_COMPARE_BOOST
|
||||||
TEST(speed_tests, boost) {
|
TEST(speed_tests, boost)
|
||||||
|
{
|
||||||
std::ifstream t("./test_files/test_request.http");
|
std::ifstream t("./test_files/test_request.http");
|
||||||
std::stringstream buffer;
|
std::stringstream buffer;
|
||||||
buffer << t.rdbuf();
|
buffer << t.rdbuf();
|
||||||
|
@ -48,7 +49,7 @@ TEST(speed_tests, boost) {
|
||||||
|
|
||||||
auto start = high_resolution_clock::now();
|
auto start = high_resolution_clock::now();
|
||||||
|
|
||||||
for(int i = 0; i < num_requests; ++i) {
|
for (int i = 0; i < num_requests; ++i) {
|
||||||
boost::system::error_code ec;
|
boost::system::error_code ec;
|
||||||
boost::beast::http::request_parser<boost::beast::http::string_body> p;
|
boost::beast::http::request_parser<boost::beast::http::string_body> p;
|
||||||
p.put(boost::asio::buffer(raw_req), ec);
|
p.put(boost::asio::buffer(raw_req), ec);
|
||||||
|
@ -56,11 +57,11 @@ TEST(speed_tests, boost) {
|
||||||
}
|
}
|
||||||
|
|
||||||
auto end = high_resolution_clock::now();
|
auto end = high_resolution_clock::now();
|
||||||
auto ms_int = duration_cast<milliseconds>(end-start);
|
auto ms_int = duration_cast<milliseconds>(end - start);
|
||||||
|
|
||||||
double m_rps = ((1000.0 / ms_int.count()) * num_requests) / 1000000;
|
double m_rps = ((1000.0 / ms_int.count()) * num_requests) / 1000000;
|
||||||
|
|
||||||
std::cout << "Parsed " << (num_requests/1000000) << " Million requests in " << ms_int << " ms";
|
std::cout << "Parsed " << (num_requests / 1000000) << " Million requests in " << ms_int << " ms";
|
||||||
std::cout << " at " << m_rps << " Million RPS " << std::endl;
|
std::cout << " at " << m_rps << " Million RPS " << std::endl;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
#include <gtest/gtest.h>
|
|
||||||
#include <fstream>
|
|
||||||
#include "../lib/http/request.hpp"
|
#include "../lib/http/request.hpp"
|
||||||
#include <boost/beast.hpp>
|
#include <boost/beast.hpp>
|
||||||
|
#include <fstream>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
TEST(unit_tests, single_request_parse) {
|
TEST(unit_tests, single_request_parse)
|
||||||
|
{
|
||||||
std::ifstream t("./test_files/test_request.http");
|
std::ifstream t("./test_files/test_request.http");
|
||||||
std::stringstream buffer;
|
std::stringstream buffer;
|
||||||
buffer << t.rdbuf();
|
buffer << t.rdbuf();
|
||||||
|
@ -11,7 +12,7 @@ TEST(unit_tests, single_request_parse) {
|
||||||
std::string raw_req = buffer.str();
|
std::string raw_req = buffer.str();
|
||||||
std::string expected = buffer.str();
|
std::string expected = buffer.str();
|
||||||
|
|
||||||
anthracite::http::request req (raw_req, "0.0.0.0");
|
anthracite::http::request req(raw_req, "0.0.0.0");
|
||||||
|
|
||||||
ASSERT_EQ(expected, req.to_string());
|
ASSERT_EQ(expected, req.to_string());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue