HTTP Server
An HTTP server in C is a program that listens on a socket, accepts connections, reads a request, and writes a response. Production servers add TLS, multiplexing, and a routing table. The language still only gives you bytes.
Detailed Explanation
The skeleton is POSIX: socket, bind to 0.0.0.0:8080 or a Unix path, listen, loop on accept. Each accepted fd is one client (HTTP/1.x). You read until you have headers, parse the request line, dispatch, write HTTP/1.1 200 OK plus headers plus body, then close or keep the fd for the next request.
A blocking read per client on a single thread is a toy. The next designs are: fork after accept (classic inetd/CGI style, simple isolation, heavy), a thread per client (easy sharing, scaling limits), or an event loop (poll / epoll) that never blocks on one client (the C10k shape). C servers that matter (nginx, HAProxy, parts of Apache) are event loops plus careful buffer ownership.
Safety is the job. Bound the header size. Reject Content-Length that does not fit. Do not sprintf user strings into headers. Serve files through a path that cannot escape the document root (../). If you add TLS, it wraps the same accept loop.
You can avoid writing this. Embed a library (libmicrohttpd, civetweb) or sit behind a reverse proxy and speak FastCGI or a simple framing protocol. Writing a server is the right learning project and the wrong place to invent a new HTTP parser for the internet.
Logging and metrics are still C: write to a FILE * or a socket, watch EINTR, do not hold a mutex across a slow write if the accept loop needs it.
Code Examples
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
int main(void)
{
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
return 1;
}
int on = 1;
setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof on);
struct sockaddr_in addr;
memset(&addr, 0, sizeof addr);
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
addr.sin_port = htons(0); /* ephemeral; real servers pick a port */
if (bind(fd, (struct sockaddr *)&addr, sizeof addr) != 0) {
close(fd);
return 1;
}
listen(fd, 1);
printf("listening\n");
close(fd);
return 0;
}#include <stdio.h>
#include <string.h>
static void write_ok(char *out, size_t n, const char *body)
{
snprintf(out, n,
"HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n"
"Content-Length: %zu\r\n\r\n%s",
strlen(body), body);
}
int main(void)
{
char resp[128];
write_ok(resp, sizeof resp, "ok");
fputs(resp, stdout);
return 0;
}Common Gotchas
One-thread-per-client with a blocking read will not survive slowloris-style clients
bound read timeouts.
`SO_REUSEADDR` is not `SO_REUSEPORT`
know which problem you are solving.
HTTP/1.1 keep-alive means the next request is on the same fd
a parser that `close`s after one response is HTTP/1.0.
A path from the request concatenated onto a document root without normalization
A path from the request concatenated onto a document root without normalization is directory traversal