A simple HTTP server implementation for educational purposes demonstrating:
- Basic socket programming
- HTTP protocol handling
- Concurrent connection management using forking
- Signal handling
- Handles multiple concurrent connections using process forking
- Responds to GET requests with basic HTML page
- Displays requested path and method
- Proper signal handling for zombie processes
- Port reuse capability (SO_REUSEADDR)
- C compiler (gcc recommended)
- POSIX-compliant operating system (Linux/macOS)
- Basic understanding of networking concepts
make
./http_server
Proper HTTP Header Parsing ✅
- Parse full request headers into key-value pairs
- Handle
Host,User-Agent, andContent-Lengthheaders - Example structure:
typedefstruct { charmethod[8]; charpath[1024]; charheaders[32][2][256]; // [header_count][key/value]intheader_count; } HTTPRequest;
HTTP Error Handling ✅
- Implement 400 (Bad Request), 404 (Not Found), 501 (Not Implemented)
- Create error template HTML responses
- Handle malformed requests gracefully
HTTP/1.1 Keep-Alive Support ✅
- Add
Connection: keep-aliveheader - Implement request pipelining
- Use content-length for proper message delimitation
- Add
Thread Pool Implementation ✅
- Replace
fork()with POSIX threads (pthread) - Create worker thread pool with task queue
- Use mutexes and condition variables for synchronization
- Replace
Event-Driven Architecture
- Implement using
epoll(Linux) orkqueue(BSD) - Non-blocking I/O with state machines
- Compare performance against process/thread models
- Implement using
Zero-Copy File Transfer ✅ Implement using
sendfile()system call:intfile_fd=open(filename, O_RDONLY); off_toffset=0; sendfile(client_socket, file_fd, &offset, file_size);
Buffer Management
- Create ring buffer for request/response handling ✅
- Implement dynamic buffer resizing ✅
- Add read/write timeout protection
HTTP Compression ✅
- Add gzip compression using zlib
- Handle
Accept-Encodingheader - Compress responses on-the-fly
TLS Support
- Integrate OpenSSL library
- Implement HTTPS on port 443
- Handle SSL handshake and encryption
Request Validation
- Add maximum request size limit
- Sanitize file paths to prevent directory traversal
- Implement basic rate limiting
Static File Serving
- Serve files from document root directory
- Handle MIME types (Content-Type header)
- Add directory listing support
CGI Support
- Execute external programs via fork/exec
- Handle environment variables:
setenv("REQUEST_METHOD", method, 1); setenv("QUERY_STRING", query, 1);
Reverse Proxy
- Forward requests to backend servers
- Handle load balancing between multiple backends
- Add caching layer
Configuration System
- Read config file (JSON/INI format)
- Support virtual hosts
- Hot reload without restart
Logging & Monitoring
- Implement access logs (Common Log Format)
- Add metrics collection (requests/sec, latency)
- Create health check endpoint
Benchmarking
- Compare with nginx using
wrk:
wrk -t12 -c400 -d30s http://localhost:8080/
- Profile with
perf/valgrind
- Compare with nginx using
Networking Foundations
- Complete Phases 1-3 → Understand HTTP/TCP stack
Systems Programming
- Complete Phase 2 & 5 → Master concurrency/I/O
Production Engineering
- Complete Phase 6 → Learn deployment concerns