Skip to content

Commit

Permalink
more benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
osotnoc-corp committed Oct 14, 2023
1 parent ef78691 commit 89a6d9a
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 26 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
A simple web server written in C++

## 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
backends could be implemented:

Expand Down
2 changes: 1 addition & 1 deletion backends/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@

class backend {
public:
virtual http_response handle_request(http_request req) {};
virtual http_response handle_request(http_request req) = 0;
};
57 changes: 54 additions & 3 deletions backends/file_backend.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#include "backend.cpp"
#include <filesystem>

class file_backend : public backend {
public:
http_response handle_request(http_request req) {
private:
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();
filename = "./www/" + filename;
ifstream stream(filename);
Expand All @@ -16,6 +20,53 @@ class file_backend : public backend {

stringstream buffer;
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);
}
};
15 changes: 7 additions & 8 deletions http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ static unordered_map<int, string> const http_status_map = {

class name_value {
private:
string _name;
string _value;
string _name;
string _value;

protected:
name_value() {}
name_value() {}

public:
name_value(string name, string value)
Expand All @@ -155,11 +155,10 @@ class name_value {
{
}
virtual ~name_value() = default;
name_value(const name_value &) = default;
name_value& operator =(name_value const&) = default;
name_value(const name_value&) = default;
name_value& operator=(name_value const&) = default;
name_value(name_value&&) = default;
name_value& operator = (name_value&&) = default;

name_value& operator=(name_value&&) = default;

string name() { return _name; }
string value() { return _value; }
Expand Down
30 changes: 18 additions & 12 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <netinet/in.h>
#include <sstream>
#include <sys/socket.h>
#include <thread>
#include <unistd.h>
#include <unordered_map>

Expand All @@ -14,6 +15,15 @@ void log_request_an_response(http_request req, http_response resp);

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 port_number = default_port;
Expand All @@ -22,25 +32,21 @@ int main(int argc, char** argv)
port_number = atoi(argv[1]);
}

cout << "Initializing Anthracite\n";
cout << "Initializing Anthracite" << endl;
anthracite_socket s(port_number);
cout << "Initialization Complete\n";
cout << "Listening for HTTP connections on port " << port_number << "\n";
file_backend fb;
file_backend fb(false);
cout << "Initialization Complete" << endl;
cout << "Listening for HTTP connections on port " << port_number << endl;

while (true) {
while(true) {
s.wait_for_conn();
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();
thread(handle_client, s, ref(fb)).detach();
}

return 0;
exit(0);
}

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;
}
2 changes: 1 addition & 1 deletion socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class anthracite_socket {
socklen_t client_addr_len {};

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))
, client_ip("")
{
Expand Down

0 comments on commit 89a6d9a

Please sign in to comment.