echttps
- 1 Devlogs
- 7 Total hours
Cool HTTP server written in C++
Cool HTTP server written in C++
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!