A lightweight, multithreaded HTTP/1.1 server written in pure C with threads. For learning some network.
Multithreaded - Handles concurrent connections using pthreads
Directory serving - Serve entire directory trees
Single file mode - Serve a specific file
logs - Request logs in standard format
Security - Path traversal protection
MIME type detection - Auto-detects content types (HTML, CSS, JS, images, etc.)
gcc -pthread src/main.c src/netlib.c -I src -o serverServe a directory:
./server -d ./publicServe a single file:
./server -f index.htmlCustom port with logging:
./server -d ./website -p 8080 -l access.logDisplay help:
./server -h| Option | Description | Default |
|---|---|---|
-d <directory> |
Serve files from directory | Current directory |
-f <file> |
Serve single file to all requests | - |
-p <port> |
Port number | 4221 |
-l <logfile> |
Log file path | stdout only |
-h |
Display help message | - |
http://localhost:4221/→ servesindex.htmlhttp://localhost:4221/style.css→ servesstyle.csshttp://localhost:4221/js/app.js→ servesjs/app.jshttp://localhost:4221/images/logo.png→ servesimages/logo.png
./server -f mypage.html- Must access:
http://localhost:4221/mypage.html - Will 404:
http://localhost:4221/
The server logs requests in Apache Common Log Format:
127.0.0.1 - - [10/Nov/2025:14:32:20 +0100] "GET /index.html HTTP/1.1" 200 1234
This section summarizes the results of a wrk benchmark used to evaluate the server's concurrency handling and throughput.
- Tool:
wrk - Concurrency: 3,000 simultaneous connections
- Threads: 12
- Duration: 30 seconds
Example command used:
w rk -t12 -c3000 -d30s http://your-server.example/
(Replace http://your-server.example/ with the endpoint you tested.)
| Metric | Result |
|---|---|
| Throughput | 20,101.81 Requests/sec |
| Transfer Rate | 243.45 MB/sec |
| Avg Latency | 34.84 ms |
| Total Requests | 603,950 |
- Run the same
wrkcommand above to reproduce tests. - Ensure the test environment (network, server CPU, background load) is stable for consistent results.
- For higher-confidence results, run multiple iterations and report averages and standard deviations.
.
├── src/
│ ├── main.c # Server initialization and main loop
│ ├── netlib.c # HTTP handling and file serving
│ └── netlib.h # Header file with declarations
├── server # Compiled binary
└── README.md
- Path traversal protection (
..blocked) - Input validation
- Protocol: HTTP/1.1
- Concurrency: One thread per connection (pthread)
- Socket: TCP (AF_INET, SOCK_STREAM)
- Buffer Size: 4KB request buffer
- Response: Supports Content-Type and Content-Length headers
Start the server:
./server -d . -l server.logTest with curl:
curl http://localhost:4221/README.md
curl -I http://localhost:4221/README.md # Headers onlyTest with browser (port 4221 is the default):
http://localhost:4221/
- Only GET method supported
- No HTTPS/TLS support
- No compression (gzip)
- No keep-alive connections
- No caching headers
MIT License - Feel free to use and modify!
Built as a learning project to understand HTTP servers and network programming in C.
⭐ Star this repo if you found it helpful!