Jump to content

Webserver in C


WarFox
 Share

Recommended Posts

So a few months ago, I heard a podcast where a person was talking about how helpful it is to do something as simple as implement an HTTP server in C. So, I decided to embark on this quest when school got a little more quieter for the summer (I am just taking a light load). I ended up doing this for a few reasons. First, as a learning experience. How better to get to know HTTP and how websites work than implementing a web server? Next was dog fooding my own code. Making something I can use. I could get the experience of writing it, but also the experiences of using my own code. And of course, implementing my own features natively in C. Lastly, I figured it would make an interesting resume project. Why C? Well, its a little closer to the metal and requires the user to get more intimate with the inner workings.

Here is the code on my github. Note, as of this posting, I still have some cleaning up to do. But it passes all memory checks on valgrind and seems to be running fine. It is single threaded and does not yet use non-blocking IO.

https://github.com/martintc/HttpServer

Website I currently have it deployed to for testing:

http://martintc.tech

Thing I plan to do and improve on (and lessons learned):

Implementing my own garbage collector to simplify the memory model.

handling PUT request methods.

Implement SSL/TLS

Implement multi-threaded and non-blocking IO

 

  • I Like This! 1
Link to comment
Share on other sites

This is really great! I started looking through this, but will need to look through the rest a bit later. My C is pretty rusty, so I doubt I could offer any suggestions, I'll mostly be looking out of interest and to brush up on knowledge. It looks like you've got it working on both netbsd and linux though, which is awesome!

I wrote an http server and client in C (and python) a long time ago (I'll try to find the src) and I remember that before doing so, actually simulating sending and recv a request via netcat according to the http spec was very helpful to my understanding. If you get stuck along the way with other enhancements you want to add, I might suggest going through that interactive "simulation" with netcat to wrap your head around the concepts.

Looking good! And awesome to see that you have it deployed. I went to the site and it loaded instantly in both Firefox and Brave. Great work.

  • I Like This! 1
Link to comment
Share on other sites

Being POSIX complaint makes it nice to get it working on both platforms. I only did the Linux compatibility mainly because I was originally using valgrind to solve memory issues, which is not ported to NetBSD. Then a NetBSD dev gave me some tips to use some sanitizers in gcc (NetBSD hasnt made the conversion to llvm yet and may not). So yea, linux compat was originally so I could run it through valgrind on my Debian laptop. Lesson learned though, POSIC compatibility goes only so far. So I learned that while the calls and parameters are the same format, enums for type definitions (in this case networking types) differ. NetBSD I believe IPPROTO_TCP on netbsd comes to 0 for an argument where as Linux needs a 6 (can't remember the name of the enum for it) when defining in the socket creation that it will be using TCP.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...