2023-09-07 23:33:22 +00:00
|
|
|
#include "backends/file_backend.cpp"
|
2023-09-06 20:44:29 +00:00
|
|
|
#include <exception>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <sstream>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2023-09-07 23:33:22 +00:00
|
|
|
void log_request_an_response(http_request req, http_response resp);
|
2023-09-06 20:44:29 +00:00
|
|
|
|
2023-09-07 23:33:22 +00:00
|
|
|
constexpr int default_port = 80;
|
2023-09-06 20:44:29 +00:00
|
|
|
|
2023-09-07 23:33:22 +00:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
int port_number = default_port;
|
2023-09-06 20:44:29 +00:00
|
|
|
|
2023-09-07 23:33:22 +00:00
|
|
|
if (argc > 1) {
|
|
|
|
port_number = atoi(argv[1]);
|
2023-09-06 20:44:29 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 23:33:22 +00:00
|
|
|
cout << "Initializing Anthracite\n";
|
|
|
|
anthracite_socket s(port_number);
|
|
|
|
cout << "Initialization Complete\n";
|
|
|
|
cout << "Listening for HTTP connections on port " << port_number << "\n";
|
|
|
|
file_backend fb;
|
|
|
|
|
|
|
|
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();
|
2023-09-06 20:44:29 +00:00
|
|
|
}
|
|
|
|
|
2023-09-07 23:33:22 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2023-09-06 20:44:29 +00:00
|
|
|
|
2023-09-07 23:33:22 +00:00
|
|
|
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";
|
2023-09-06 20:44:29 +00:00
|
|
|
}
|