2023-10-16 18:36:17 +00:00
|
|
|
#include "backends/file_backend.cpp"
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <exception>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <mutex>
|
|
|
|
#include <netinet/in.h>
|
2023-10-20 04:27:35 +00:00
|
|
|
#include <span>
|
2023-10-16 18:36:17 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <thread>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <unordered_map>
|
|
|
|
|
2023-10-18 18:09:26 +00:00
|
|
|
void log_request_and_response(http_request& req, std::unique_ptr<http_response>& resp);
|
2023-10-16 18:36:17 +00:00
|
|
|
|
|
|
|
constexpr int default_port = 80;
|
2023-10-17 17:16:31 +00:00
|
|
|
constexpr int max_worker_threads = 128;
|
2023-10-16 18:36:17 +00:00
|
|
|
|
2023-10-20 04:27:35 +00:00
|
|
|
void handle_client(anthracite_socket s, backend& b, std::mutex& thread_wait_mutex, std::condition_variable& thread_wait_condvar, int& active_threads)
|
2023-10-16 18:36:17 +00:00
|
|
|
{
|
|
|
|
http_request req(s);
|
2023-10-20 04:27:35 +00:00
|
|
|
std::unique_ptr<http_response> resp = b.handle_request(req);
|
2023-10-16 18:36:17 +00:00
|
|
|
log_request_and_response(req, resp);
|
2023-10-18 18:09:26 +00:00
|
|
|
std::string header = resp->header_to_string();
|
2023-10-17 17:16:31 +00:00
|
|
|
s.send_message(header);
|
|
|
|
s.send_message(resp->content());
|
2023-10-16 18:36:17 +00:00
|
|
|
resp.reset();
|
|
|
|
s.close_conn();
|
|
|
|
{
|
2023-10-20 04:27:35 +00:00
|
|
|
std::lock_guard<std::mutex> lock(thread_wait_mutex);
|
2023-10-16 18:36:17 +00:00
|
|
|
active_threads--;
|
|
|
|
}
|
2023-10-20 04:27:35 +00:00
|
|
|
thread_wait_condvar.notify_one();
|
2023-10-16 18:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2023-10-20 04:27:35 +00:00
|
|
|
auto args = std::span(argv, size_t(argc));
|
2023-10-16 18:36:17 +00:00
|
|
|
int port_number = default_port;
|
|
|
|
|
|
|
|
if (argc > 1) {
|
2023-10-20 04:27:35 +00:00
|
|
|
port_number = atoi(args[1]);
|
2023-10-16 18:36:17 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 18:09:26 +00:00
|
|
|
std::cout << "Initializing Anthracite" << std::endl;
|
2023-10-16 18:36:17 +00:00
|
|
|
anthracite_socket s(port_number);
|
2023-10-20 04:27:35 +00:00
|
|
|
file_backend fb(argc > 2 ? args[2] : "./www");
|
2023-10-18 18:09:26 +00:00
|
|
|
std::cout << "Initialization Complete" << std::endl;
|
|
|
|
std::cout << "Listening for HTTP connections on port " << port_number << std::endl;
|
2023-10-16 18:36:17 +00:00
|
|
|
|
2023-10-20 04:27:35 +00:00
|
|
|
int active_threads = 0;
|
|
|
|
std::mutex thread_wait_mutex;
|
|
|
|
std::condition_variable thread_wait_condvar;
|
|
|
|
|
2023-10-17 17:16:31 +00:00
|
|
|
while (true) {
|
2023-10-16 18:36:17 +00:00
|
|
|
s.wait_for_conn();
|
2023-10-20 04:27:35 +00:00
|
|
|
std::unique_lock<std::mutex> lock(thread_wait_mutex);
|
|
|
|
thread_wait_condvar.wait(lock, [active_threads] { return active_threads < max_worker_threads; });
|
2023-10-16 18:36:17 +00:00
|
|
|
active_threads++;
|
2023-10-20 04:27:35 +00:00
|
|
|
std::thread(handle_client, s, std::ref(fb), std::ref(thread_wait_mutex), std::ref(thread_wait_condvar), std::ref(active_threads)).detach();
|
2023-10-16 18:36:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2023-10-18 18:09:26 +00:00
|
|
|
void log_request_and_response(http_request& req, std::unique_ptr<http_response>& resp)
|
2023-10-16 18:36:17 +00:00
|
|
|
{
|
2023-10-20 04:27:35 +00:00
|
|
|
std::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() << std::endl;
|
2023-10-16 18:36:17 +00:00
|
|
|
}
|