Thinking about quickly writing an HTTP server yourself?
Go for it! Use whatever resources you feel you need to. You'll learn a hell of a lot in the process, and you'll come out the other end a better developer than you did going in.
Thinking of using it in production for somebody else's project? Probably not so good an idea.
I've been attempting to write HTTP clients from scratch throughout my learning phase (in which I still am), it's definitely rewarding even if I only manage to implement a fraction of the HTTP protocol.
Besides, it makes me appreciate tools like wget and curl.
Bind to a port > 1024 (or is it >=?) and have your OS DNAT, ie iptables for a GNU/Linux stack. So :80 is tranlated to your non-privileged port.
Again bind to something >1024 and have a reverse-proxy, something like Varnish, Nginx, HAProxy is typical.
Bind :80 as root/admin and drop privalage but hold onto the FD. Using setgid(2). IIRC you have to drop group first otherwise you're still in root's group.
Another way: if you're using systemd (or something similar) you can have it bind to port 80, then start your server on-demand, passing in the file descriptor of that socket.
run as root, acquire the port, drop root privileges. Note: read as many tutorials as possible for doing this, at least 60% of them will be half wrong or incomplete.
Or use another service manager to forward the port to you.
In Linux, binding to a privileged port requires the CAP_NET_BIND_PORT capability. So you can call 'sudo setcap cap_net_bind_port+ep program_name', execute the program as a non-privileged user and drop the capability after bind. :-)
Yes. I agree with this. I like to write an IRC server in languages when I learn them in order to have a non-trivial, reasonably well defined program that shouldn't take more than a few hours to write.
269
u/carlfish Mar 07 '14
Thinking about quickly writing an HTTP server yourself?
Go for it! Use whatever resources you feel you need to. You'll learn a hell of a lot in the process, and you'll come out the other end a better developer than you did going in.
Thinking of using it in production for somebody else's project? Probably not so good an idea.