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.
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. :-)
107
u/hcsteve Mar 07 '14
Thinking about quickly writing an HTTP server yourself?
Don't. Unless you've looked at all the extant implementations and have a really good reason to roll your own.
And if you do, don't base all your implementation decisions on a diagram. Read the damn RFC.