Live data from Hacker News

Ask HN: Resources for Building a Webserver in C?

news.ycombinator.com

61–67 of 67 posts

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

#61
post #24

Earlier quoted context omitted.

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.

I would probably go for rust over c if the only reason for c is "os level". But either way, this is for requests from "real" http clients, eg: web browsers? Presumably you're already "trapped" at a pretty high level, like tls, http/2 etc?

Otoh, if low level is what you want, maybe look at:

https://2ton.com.au/rwasa/

and/or the library it is built on.

On the other hand, if you just want to log headers to a file, caddy can do that in a few lines:

https://caddyserver.com/docs/caddyfile/directives/log

AFAIK it can't log request bodies out of the box (like the content of a POST).

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

#62

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.

In that case you might be better off looking at writing a plugin for your existing webserver. nginx, apache, etc, all allow writing modules that can be invoked at various parts of the HTTP-communication.

I don't technically run a webserver. I've built around Azure App Services which runs gunicorn behind the scenes.

Also, I rather own the majority of my stack even though its slower, harder, and more problematic.

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

#63

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

Where did you go to school? And how come every college educated (mostly in the US) developer I've met has never done that before...

As students c.a 2004 we were tasked with writing a MTA.

Actually, many MTAs.

Divided in small teams (1-3 people), each MTA would ultimately be pointed to the next MTA, and the teacher would use telnet to send a message over SMTP to the first one, the goal being that the message had to reach the last one in the chain.

Grades were attributed based on whether a team's implementation failed, crashed, mangled the message and whatnot, and also as a whole to promote team spirit all in good faith and sports.

It was quite fun.

(pretty confident we did HTTP things too although I have no actual recollection of it)

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

#64

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…

This sounds really interesting. Thanks for the share.

Thanks for your kind thanks.

I'm currently working on a generalised approach to structuring multiple multithreaded components that run kernel threads and lightweight threads similar to golang.

My 1:M:N userspace scheduler multiplexes N lightweight threads onto M kernel threads but it's a different repository. It has one kernel thread and preempts loops by setting them to their limit.

Ideally they should be merged into one codebase.

https://GitHub.com/samsquire/preemptible-thread

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

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

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

#67

For embedded: https://github.com/cesanta/mongoose/

this is commercial now? its open source forked version is civetweb

all my work is open source, so i don't mind the licence, it's dual licensed if i remember correctly, GPL or commercial

thanks for letting me know about civetweb, looks like it is based on a very old version?

Post reply on HN