HACKER Q&A
📣 shanebellone

Resources for Building a Webserver in C?


I'm starting to work with C and would like to build a webserver. I've found a few interesting writeups but would love to hear from the community.

What's your favorite book/post/lecture/whatever? I'd also be interested in opinionated writeups/positions.


  👤 st_goliath Accepted Answer ✓
"Write a tiny web server in C" was a standard exercise when I was in high school, and later again in university. I think I did that at least 3 times as an assignment so far.

I reused the final version for a different university exercise instead of bothering with Tomcat as I was required, and back in 2015, dumped the end result here: https://github.com/AgentD/websrv where I kept adding to it for a while for fun.

The HTTP 1.x protocol itself is pretty dead simple (if you ignore chunked requests/responses and such) and the more interesting part was actually the socket code.

Back in school, our teacher had us write a simple forking server and consult the corresponding man pages (man 2 socket, man 7 socket, man 7 tcp). It was an acceptable minimal solution to not even look at the client request, respond with a static string containing a dummy response header and HTML page, which absolutely works. Bonus points for parsing the request path and sending a file back, more extra points if the path was a directory and your server sends back an "index.html" file, further points if it instead generates an HTML directory listing on the fly.

When I visited our former teacher a couple years ago, he had modified the exercise somewhat. He brought a Beagle Bone Black with him to class, with a bunch of I2C sensors attached to it, and the extended exercises now revolve around reporting temperature/humidity/... to the browser.

So, for "resource" I recommend the HTTP example on Wikipedia, as well as "Beej's guide to network programming" (as have others), as well as the man pages for quick reference.


👤 mobilio

👤 jart
Here's a 2000 line web server I wrote in C (with a focus on readability) which runs https://ipv4.games/ You can read the code at https://github.com/jart/cosmopolitan/blob/master/net/turfwar... This web server runs on a single VM. It was handling about 300 write requests per second earlier today. You can monitor its metrics here: https://ipv4.games/statusz It uses *NSYNC as its multi-threaded locking primitives, which was designed by the guy who created Google's lock server. This service is frequently targeted by hackers so you can learn a lot about things like DDOS protection by reading the source.

👤 rzzzt
Beej's guide to network programming: https://beej.us/guide/bgnet/

👤 qntty
Unix Network Programming by W. Richard Stevens and parts of The Linux Programming Interface by Michael Kerrisk

👤 AlexeyBrin
Hands-On Network Programming with C by Lewis Van Winkle is a really good intro book for network programming with C.

👤 cinntaile
Someone on HN made this [0] a couple of weeks or months ago. You code a web server to solve each of the different problems. It's language agnostic but it looks like a great way to learn.

[0] https://protohackers.com/


👤 samsquire
You might find my epollserver interesting.

It multiplexes multiple clients (sockets) over a thread, so you can write an event loop in each thread and serve far more requests per thread than you could if it was one thread per client or one process per client. (epollserver_threaded.c)

It uses a thread safe multiconsumer multiproducer ringbuffer to communicate between threads.

https://github.com/samsquire/epoll-server

I use Alexander Krizhanovsky's of Tempesta technologies Lock-Free Multi-Producer Multi-Consumer Queue on Ring Buffer

https://www.linuxjournal.com/content/lock-free-multi-produce...


👤 User23
Unix Network Programming by Stevens and TCP/IP Illustrated by Fall and Stevens are must reads. There are many subtleties and pitfalls in writing low level network code and those books comprehensively cover everything.

Edit: I’ve heard that the first edition of TCP/IP illustrated has considerably more readable prose, so if you’re not doing IPv6 maybe get it instead. Apparently the 2nd edition with Fall is more of a rewrite with a considerable number if technical errors so I can’t recommend it having not read it.


👤 rwmj
Here's a fun webserver I wrote 20 years ago. It uses a pool allocator to make allocations easy to track (gives you a lot of the benefits of garbage collection, but from pure C), and it also uses an interesting "inversion of control" (similar to "green threads" or coroutines) allowing the server to be written as straight-line code but actually being implemented using poll.

It actually ran a production site at an old company I worked at until fairly recently.

http://git.annexia.org/?p=c2lib.git;a=tree http://git.annexia.org/?p=pthrlib.git;a=tree http://git.annexia.org/?p=rws.git;a=tree


👤 nam17887
I've written a fully functioning web server using a managed language, with SSL, multi-threading, Keep-Alive and chunked content encoding.

I suggest to start with understanding the HTTP protocol by reading the RFC. The most important part is how to start and end the HTTP message (by closing connection, by Content-Length, or by chunked encoding).

Then next important bit is how to receive the HTTP message. Network APIs will ask for number of bytes to receive, so you'll need to know exactly how the message ends.

Stick with the RFC, programming language doesn't matter because socket APIs are very much the same.


👤 rnmmrnm
https://tools.suckless.org/quark/ is a honorable mention, very elegant code.

👤 jjice
If you want to do something simple, read Beej's Guide to Networking (many other have recommended it in this thread) to learn networking fundamentals and C APIs. Next, learn about HTTP - MDN is a good resource, but may go too in depth for this project at times. HTTP 1.1 is super simple. The best thing about HTTP? It's all just plain text and newlines. Parsing it is super easy. This is a good learning exercise - good luck on it.

👤 bjourne
> I'd also be interested in opinionated writeups/positions.

Ok then! Writing a web server in C is bloody stupid. A web server's performance is mostly limited by concurrency issues and various IO latencies, not by how fast the machine code runs. Thus, there are zero advantages to writing a web server in C and mountains of disadvantages. Save your time and write your web server in a good language, Java, Go, Python, Nim, but not C.


👤 coolandsmartrr
I remember using this tutorial to build a web server that handles sockets.

Short source code, excludes edge cases, but useful to understand the gist of web servers.

https://www.ibm.com/support/pages/how-does-webserver-actuall...


👤 rqmedes
If you are looking at performance I found pulling this code apart a great learning experience.

https://github.com/fredrikwidlund/libreactor


👤 locriacyber
Complete OpenBSD + Sqlite + C + httpd toolkit for building websites.

http://bsd.lv/

I recommend swapping out C for a safer&more productive language.


👤 vetelko
Althttpd source is great resource

https://sqlite.org/althttpd/file/althttpd.c


👤 Kukumber

👤 LargoLasskhyfv

👤 mmmmwhat

👤 082349872349872
If you wish, use the source. (Almost?) all early webservers were written in C.

👤 barbarbar
Maybe redbean made by jart?

👤 badrabbit
Read the mongoose source code.