You are browsing as a guest. Sign up (or log in) to start making projects!

kurkiie

@kurkiie

Joined July 18th, 2026

  • 2Devlogs
  • 1Projects
  • 0Ships
  • 0Votes
Some person working on echttps
Open comments for this post

8h 8m 55s logged

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!

0
0
6
Open comments for this post

7h 14m 50s logged

Parsing HTTP requests

So as part of my HTTP server, the first thing I did is learning how HTTP requests are formulated and how to parse them. I created a header file, http_headers.hpp which stores various information about HTTP headers in a way that makes it easy to handle programatically - storing them in enums and creating various helper functions.

In the main function I utilize UNIX sockets to recieve HTTP requests. I read a helpful book about sockets which helped me get to the part of even being able to obtain HTTP requests.

The current HTTP server structure is very simple, it sets up everything that’s needed to start accepting connections, and then makes a call to accept() to recieve a request. Once that is done it runs fork() to create a child process with which it will handle the request, while the main thread continues running in a forever loop, again running accept(), and handling new connections.
​​
The parsing code works by tokenizing everything that the server recieved and then storing it in a std::unordered_map (a hash map). The key is an enum header, which holds all possible header types and the value is a std::string.

So if the client sent you something like this:
Host: example.com


In the map it would be stored as key: Host and value: example.com


I’ve also been working on a config file to let you specify which websites at which path and on which port you actually want to host. Right now the server only returns “Hiiii!!!” when you send a HTTP request to 127.0.0.1:80.

I’ve written up a more detailed architectural view here, though note that it doesn’t quite represent reality yet as this project is still a big WIP!! However, the architecture represents the general plan of the project.

Also no AI coding allowed on this project to maximize learning.

Thx for reading!

2
0
34

Followers

Loading…