more benchmarks
This commit is contained in:
parent
ef78691f97
commit
89a6d9a528
|
@ -2,7 +2,7 @@
|
||||||
A simple web server written in C++
|
A simple web server written in C++
|
||||||
|
|
||||||
## Module-Based Backends
|
## Module-Based Backends
|
||||||
Anthracite includes (read: will include) a system for allowing different "backend modules" to handle requests.
|
Anthracite includes (read: will include) system for allowing different "backend modules" to handle requests.
|
||||||
This allows for anthracite to be extended for additional use-cases. For example, the following
|
This allows for anthracite to be extended for additional use-cases. For example, the following
|
||||||
backends could be implemented:
|
backends could be implemented:
|
||||||
|
|
||||||
|
|
|
@ -2,5 +2,5 @@
|
||||||
|
|
||||||
class backend {
|
class backend {
|
||||||
public:
|
public:
|
||||||
virtual http_response handle_request(http_request req) {};
|
virtual http_response handle_request(http_request req) = 0;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
#include "backend.cpp"
|
#include "backend.cpp"
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
class file_backend : public backend {
|
class file_backend : public backend {
|
||||||
public:
|
private:
|
||||||
http_response handle_request(http_request req) {
|
unordered_map<string, string> file_cache;
|
||||||
|
bool cache_enabled;
|
||||||
|
|
||||||
|
http_response handle_request_nocache(http_request req) {
|
||||||
string filename = req.path() == "/" ? "index.html" : req.path();
|
string filename = req.path() == "/" ? "index.html" : req.path();
|
||||||
filename = "./www/" + filename;
|
filename = "./www/" + filename;
|
||||||
ifstream stream(filename);
|
ifstream stream(filename);
|
||||||
|
@ -16,6 +20,53 @@ public:
|
||||||
|
|
||||||
stringstream buffer;
|
stringstream buffer;
|
||||||
buffer << stream.rdbuf();
|
buffer << stream.rdbuf();
|
||||||
return http_response(buffer.str(), status);
|
return { buffer.str(), status };
|
||||||
|
}
|
||||||
|
|
||||||
|
http_response handle_request_cache(http_request req) {
|
||||||
|
string filename = req.path() == "/" ? "/index.html" : req.path();
|
||||||
|
filename = "./www" + filename;
|
||||||
|
auto file_info = file_cache.find(filename);
|
||||||
|
|
||||||
|
int status = 200;
|
||||||
|
if (file_info == file_cache.end()) {
|
||||||
|
status = 404;
|
||||||
|
filename = "./error_pages/404.html";
|
||||||
|
file_info = file_cache.find(filename);
|
||||||
|
}
|
||||||
|
|
||||||
|
return { file_info->second, status };
|
||||||
|
}
|
||||||
|
|
||||||
|
void populate_cache_dir(string dir) {
|
||||||
|
filesystem::recursive_directory_iterator cur = begin(filesystem::recursive_directory_iterator(dir));
|
||||||
|
filesystem::recursive_directory_iterator fin = end(filesystem::recursive_directory_iterator(dir));
|
||||||
|
|
||||||
|
while (cur != fin) {
|
||||||
|
auto p = cur->path();
|
||||||
|
string filename = p.string();
|
||||||
|
stringstream buffer;
|
||||||
|
ifstream stream(filename);
|
||||||
|
buffer << stream.rdbuf();
|
||||||
|
file_cache[filename] = buffer.str();
|
||||||
|
cout << "File at " << filename << " cached (" << file_cache[filename].size() << " bytes)" << endl;
|
||||||
|
++cur;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void populate_cache() {
|
||||||
|
populate_cache_dir("./www/");
|
||||||
|
populate_cache_dir("./error_pages/");
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
file_backend(bool enable_cache) : cache_enabled(enable_cache) {
|
||||||
|
if(cache_enabled) {
|
||||||
|
populate_cache();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
http_response handle_request(http_request req) override {
|
||||||
|
return cache_enabled ? handle_request_cache(req) : handle_request_nocache(req);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
7
http.cpp
7
http.cpp
|
@ -155,11 +155,10 @@ public:
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
virtual ~name_value() = default;
|
virtual ~name_value() = default;
|
||||||
name_value(const name_value &) = default;
|
name_value(const name_value&) = default;
|
||||||
name_value& operator =(name_value const&) = default;
|
name_value& operator=(name_value const&) = default;
|
||||||
name_value(name_value&&) = default;
|
name_value(name_value&&) = default;
|
||||||
name_value& operator = (name_value&&) = default;
|
name_value& operator=(name_value&&) = default;
|
||||||
|
|
||||||
|
|
||||||
string name() { return _name; }
|
string name() { return _name; }
|
||||||
string value() { return _value; }
|
string value() { return _value; }
|
||||||
|
|
30
main.cpp
30
main.cpp
|
@ -5,6 +5,7 @@
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
|
#include <thread>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
|
@ -14,6 +15,15 @@ void log_request_an_response(http_request req, http_response resp);
|
||||||
|
|
||||||
constexpr int default_port = 80;
|
constexpr int default_port = 80;
|
||||||
|
|
||||||
|
void handle_client(anthracite_socket s, file_backend fb)
|
||||||
|
{
|
||||||
|
http_request req(s);
|
||||||
|
http_response resp = fb.handle_request(req);
|
||||||
|
log_request_an_response(req, resp);
|
||||||
|
s.send_message(resp.to_string());
|
||||||
|
s.close_conn();
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
int port_number = default_port;
|
int port_number = default_port;
|
||||||
|
@ -22,25 +32,21 @@ int main(int argc, char** argv)
|
||||||
port_number = atoi(argv[1]);
|
port_number = atoi(argv[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
cout << "Initializing Anthracite\n";
|
cout << "Initializing Anthracite" << endl;
|
||||||
anthracite_socket s(port_number);
|
anthracite_socket s(port_number);
|
||||||
cout << "Initialization Complete\n";
|
file_backend fb(false);
|
||||||
cout << "Listening for HTTP connections on port " << port_number << "\n";
|
cout << "Initialization Complete" << endl;
|
||||||
file_backend fb;
|
cout << "Listening for HTTP connections on port " << port_number << endl;
|
||||||
|
|
||||||
while (true) {
|
while(true) {
|
||||||
s.wait_for_conn();
|
s.wait_for_conn();
|
||||||
http_request req(s);
|
thread(handle_client, s, ref(fb)).detach();
|
||||||
http_response resp = fb.handle_request(req);
|
|
||||||
log_request_an_response(req, resp);
|
|
||||||
s.send_message(resp.to_string());
|
|
||||||
s.close_conn();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void log_request_an_response(http_request req, http_response resp)
|
void log_request_an_response(http_request req, http_response resp)
|
||||||
{
|
{
|
||||||
cout << "[" << resp.status_code() << " " + http_status_map.find(resp.status_code())->second + "] " + req.client_ip() + " " + http_reverse_method_map.find(req.method())->second + " " + req.path() + "\n";
|
cout << "[" << resp.status_code() << " " + http_status_map.find(resp.status_code())->second + "] " + req.client_ip() + " " + http_reverse_method_map.find(req.method())->second + " " + req.path() << endl;
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,7 +20,7 @@ private:
|
||||||
socklen_t client_addr_len {};
|
socklen_t client_addr_len {};
|
||||||
|
|
||||||
public:
|
public:
|
||||||
anthracite_socket(int port, int max_queue = 10)
|
anthracite_socket(int port, int max_queue = 100)
|
||||||
: server_socket(socket(AF_INET, SOCK_STREAM, 0))
|
: server_socket(socket(AF_INET, SOCK_STREAM, 0))
|
||||||
, client_ip("")
|
, client_ip("")
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue