A lightweight, multi-threaded HTTP server implementation built purely in Rust without using high-level web frameworks like Actix, Axum, or Rocket.
This project focuses on understanding the low-level details of the HTTP/1.1 protocol, TCP handling, and concurrent connection management.
- TCP Connection Handling: Binds to a port and listens for incoming TCP streams.
- Request Parsing:
- Extracts URL paths and HTTP methods.
- Reads and parses HTTP headers.
- Reads request bodies.
- Response Generation:
- Constructs valid HTTP/1.1 status lines and headers.
- Sends text and file bodies.
- File Server: capable of serving static files from a directory.
- Concurrency: Handles multiple client connections simultaneously using a thread pool/threading.
- Extract URL path
- Respond with body
- Read header
- Concurrent connections
- Return a file
- Read request body
- HTTP Compression (Gzip)
- Persistent Connections (Keep-Alive)
- Ensure you have Rust installed.
- Clone the repository.
- Run the server:
# Defaults to localhost:4221 (or your configured port) cargo run - Test with curl:
curl -v [http://127.0.0.1:4221/](http://127.0.0.1:4221/)- Language: Rust (2021 Edition)
- Architecture: Multi-threaded, Blocking I/O
- std::net: Raw TCP socket management (TcpListener, TcpStream).
- std::io: Buffered reading/writing (BufReader) for efficient protocol parsing.
- std::sync: Atomic Reference Counting (Arc) for safe state sharing across threads.
- std::thread: Spawning threads for concurrent connection handling.
- flate2: Implements the Gzip compression algorithm (DEFLATE).