HTTP Server in C
COMS 3157 Advanced Programming · private repo · Live
Overview
This project was the final homework assignment for COMS3157 Advanced Programming (AP) at Columbia. It was the cumulation of everything we learned about the C language and required us to use work from all of our previous homeworks.
The main goal of the assignment was to create a webserver that used HTTP/1.0 and could take on a single connection, serve a static HTML + CSS page and allow for querying of a "database" we had made on an earlier homework assignment. To build this we used the socket API handling each Bind, Listen, and Accept manually, this allowed us to see how all modern internet connections and libraries are built.
Because this is apart of a homework assignment that is used each semester the code for it must remain private to prevent cheating; However, to show you how it works I am actually serving this webpage with the explanation via the server itself with the logs printed to the side. If you are interested in the code as well fell free to reach out and I can share.
Architecture
This project was set up to be a super simple example of an end to end 3-tiered architecture that we fully built out in C. A simplified diagram of our setup can be seen below, the client in this situation is you the reader. The server is the Web server that was the main portion of this project and acts as the middleman between the client and the database. Finally the database was a simple in memory file that we defined as a 5 character key and 25 character value.
Although this implementation is a large simplification to the real world, it provides a good foundation for understanding how web servers work and allowed us to build each part from scratch in one semester.
Error Handling
Error handling is one of the most critical aspects of the web server. Along with feeding the user the correct information when they send you a valid request, the server must also handle errors gracefully and provide informative error messages.
The server handles the basic HTML error codes: malformed requests get a 400 error, a file that cannot be found gets a 404 error, and a file that cannot be read gets a 403 error. This validation acts as a layer of security and ensures only safe requests make it to the server.<\p>The database
The database that is accessed by the web server was apart of an earlier homework assignment in the class. In order to be able to easily create our own database we defined it as a regular binary file where the first 5 characters are the key for that entry and the next 25 are the message. Our database supports inserting elements and querying elements by key allowing it to do all simple processes that a database needs.
Originally the database held names and messages from people in the class that everyone entered but for privacy reasons I changed the content to be a list of projects and publications I have participated in. You can query the database yourself below by entering a key.
Hosting
In order for people like you to actually be able to view this I needed a way to safely allow people from the internet to access my server. Fot this I hosted the code on a Raspberry Pi V that I have in the corner of my apartment and to deal with DNS and other security concerns it is hidden behind a Cloudflare tunnel.
For further security the server and database itself is hosted within a sandbox on my Pi so that even if there is a security breach they can only access the code related to the server and not disrupt anything else I host on there.