A high-performance HTTP/1.1 server built from the ground up in C. This project handles concurrent connections using threads and features a custom-built compression engine that implements LZ77 and Huffman Coding to mimic GZIP.
You will need gcc and the pthread library (standard on Linux/macOS):
gcc -o server main.c -lpthreadSpecify the directory where you want to store or serve files:
./server --directory .The server will start listening on port 4221.
You can test the raw HTTP interaction by using telnet. This allows you to see exactly how the server parses headers and sends responses.
Connect to the server:
telnet localhost 4221Then paste the following and press Enter twice:
GET /echo/hello_world HTTP/1.1
Host: localhost:4221
Connect to the server:
telnet localhost 4221Then paste the following and press Enter:
POST /files/note.txt HTTP/1.1
Host: localhost:4221
Content-Length: 13
Hello from C!
| Endpoint | Method | Result |
|---|---|---|
/ |
GET | Returns a simple 200 OK. |
/echo/{str} |
GET | Returns {str} in the response body. |
/user-agent |
GET | Returns the client's User-Agent string. |
/files/{name} |
GET | Downloads the specified file from the directory. |
/files/{name} |
POST | Saves the request body as a new file. |
The server uses pthread_create for every incoming connection. This ensures that a large file download from one user does not block smaller or faster requests from other clients.
If a request includes the Accept-Encoding: gzip header, the server passes the response data through two compression stages:
-
LZ77 A sliding window algorithm that replaces repeated substrings with distance/length pairs.
-
Huffman Coding A frequency-based encoding system using a Min-Heap to build a prefix tree for bit-level compression.
The server respects the Connection: close header. If it is not present, the server attempts to keep the socket open for subsequent requests until a 15-second timeout is reached.
- GCC compiler
- POSIX-compliant OS (Linux, Unix, or macOS)
pthreadlibrary
Created as a deep-dive into systems programming and network protocols.