Live data from Hacker News

Ask HN: Resources for Building a Webserver in C?

news.ycombinator.com

31–40 of 67 posts

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

#32
post #24

Earlier quoted context omitted.

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.

I'm not sure I follow - you want a smart, small webserver that takes in a http request and updates an analytic store based on headers? Sounds like something nginx might handle via lua scripting? Otherwise, maybe you want: https://github.com/lammertb/libhttp Or you might want to look at either of: https://www.acme.com/software/thttpd/ https://github.com/openbsd/src/tree/master/usr.sbin/httpd If you really want to buil…

I want to consume HTTP headers at the lowest possible level. The webserver would output a custom log file for direct consumption.

There are certainly off the shelf options, but I enjoy reinventing the wheel and dislike dependencies. I'd like to use C because I have interest in OS level webservers.

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

#33
post #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.

https://kernel.dk/io_uring.pdf ?

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

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

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

#37

"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 H…

This sounds like a ton of fun. I would have enjoyed this educational path.

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

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

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

#40
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

Post reply on HN