Handling requests and serving sites in my HTTP server
So since my last devlog I’ve implemented serving websites alongside a config parser. The HTTP server parses all config files in /etc/echttps/sites/. Each file is a different config and corresponds to a different ‘server’. Each server has a name, it’s own port(s) that it listens to, and a root directory from which to serve files.
The main loop is super simple:
int main() {
signal(SIGINT, &signal_handler);
File::create_config_dir(File::DEFAULT_CONFIG_DIR);
auto files = File::get_configs();
if(files.empty()){
std::cerr << "No config files found in /etc/echttps/sites. Make one!\n";
return 1;
}
for(std::string f : files){
auto server = Config::parse_config(f);
if(!server){
std::cerr << "Server at " << f << " wasn't initialized due to errors\n";
continue;
}
Socket::setup_server_socket(server.value());
}
std::cerr << "Fully initialized all servers\n";
for(;;);
}
We just parse the configs and setup the servers (aka bind ports and setup sockets…). Under the hood most of the work goes into actually parsing HTTP requests and sending data through sockets.
Anyway, now that all of that is done, we can finally serve files and be a real HTTP server :D. Now all that is left for me is to polish everything up, maybe add a few features (I’ll try to see how hard it would be to add SSL support) and then we can ship!
Thx for reading!