Live data from Hacker News

Ask HN: Resources for Building a Webserver in C?

news.ycombinator.com

11–20 of 67 posts

Re: Ask HN: Resources for Building a Webserver in C?

#14

Earlier quoted context omitted.

Being largely unfamiliar with C, I would prefer conceptual resources over source code. The why is more important than the how. Hopefully that makes sense?

conceptually things are pretty limited - this is really simple if you are just playing around, and are just doing http 1 o bind a socket o manage incoming connections o parse http headers o 'routing' - demultiplex handlers based on URL o encode response unless its a little embedded thing one usually uses some kind of select/epoll machinery to multiplex the work on each channel. I guess thread-per-request is another m…

A little context:

I'm specifically interested in compressing my analytics stack. I'm currently interacting with HTTP Messages at a higher level. The problem, as I see it, is the webserver is already doing this work but without useful output. I'd like to implement my analytics paradigm natively at the webserver level.

Re: Ask HN: Resources for Building a Webserver in C?

#17
post #5

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

This guide is great for introducing the bsd socket api but this is really not the API you want to use if you want to create a production quality web server in C in 2022. You need the uring io API.

Re: Ask HN: Resources for Building a Webserver in C?

#18
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.

Re: Ask HN: Resources for Building a Webserver in C?

#19

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

Being largely unfamiliar with C, I would prefer conceptual resources over source code. The why is more important than the how. Hopefully that makes sense?

C is the native language of Unix, so in a way it really is the language for formally discussing Unix concepts.

Re: Ask HN: Resources for Building a Webserver in C?

#20
"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.

Post reply on HN