Project: Building a Multi-Threaded HTTP Server in C from Scratch

Project: Building a Multi-Threaded HTTP Server in C from Scratch
Phase 4 Capstone. You've mastered TCP sockets, POSIX threads, and file I/O. Now you'll combine them to build a real HTTP/1.1 server: a program that accepts browser connections, parses HTTP requests, reads HTML/CSS files from disk, and sends proper HTTP responses. This is exactly how the early web worked — and still the foundation of Apache, Nginx, and every C-based web server today.
Table of Contents
- HTTP/1.1 Protocol Primer
- Server Architecture: Thread Pool Model
- Step 1: Create and Configure the Listening Socket
- Step 2: Thread Pool Implementation
- Step 3: HTTP Request Parsing
- Step 4: MIME Type Detection
- Step 5: File Serving with sendfile
- Step 6: HTTP Response Building
- Step 7: Graceful Shutdown with SIGTERM
- Complete Server: Putting It Together
- Testing Your Server
- Extension Challenges
- Phase 4 Reflection
HTTP/1.1 Protocol Primer
HTTP is a text-based request-response protocol over TCP. Every browser interaction follows the same pattern:
Request (browser → server):
Response (server → browser):
Key rules: headers end with \r\n, the header section ends with \r\n\r\n (blank line), then the body follows.
Server Architecture: Thread Pool Model
Step 1: Create and Configure the Listening Socket
Step 2: Thread Pool Implementation
Step 3: HTTP Request Parsing
Step 4: MIME Type Detection
Step 5: File Serving with sendfile
Step 6: HTTP Response Building
Step 7: Graceful Shutdown with SIGTERM
Testing Your Server
Extension Challenges
- Keep-Alive: Support
Connection: keep-alive— reuse the TCP connection for multiple requests, eliminating per-request TCP handshake overhead. - Range requests: Support
Range: bytes=0-4095headers for video streaming (seek without downloading the entire file). - epoll-based event loop: Replace the thread-per-request model with a single-thread epoll loop — handle 10,000+ concurrent connections with no context switching overhead.
- Gzip compression: Compress text files before sending using zlib — reduce bandwidth by 60-80% for HTML/CSS/JS.
- Virtual hosting: Parse the
Host:header; serve from differentwww/root directories based on domain name.
Phase 4 Reflection
You've just built a significant piece of infrastructure — a real web server that can serve content to a browser. By combining sockets, threads, file I/O, and signal handling, you've touched every layer of the systems stack.
The fundamentals here — listen/accept loop, thread pool, HTTP parsing, file serving — are exactly how Apache httpd was architected in the late 1990s. Nginx replaced the thread model with an event loop (epoll), achieving 10× better concurrency — the extension challenge above is your path to that same transformation.
Read next: C23 Modern Evolution & C Security Hardening →
Part of the C Mastery Course — 30 modules from C basics to production systems engineering.
