This project is a Rust implementation of an HTTP/1.1 server, built as part of the "Build Your Own HTTP server" Challenge by CodeCrafters.
HTTP is the protocol that powers the web. This server is capable of handling multiple clients concurrently and understanding basic HTTP requests.
Note: If you're viewing this repo on GitHub, head over to codecrafters.io to try the challenge.
This HTTP server supports the following features:
- TCP Server: Establishes a TCP listener to accept client connections.
- Concurrent Client Handling 🧑🤝🧑: Uses Tokio's asynchronous capabilities to manage multiple client connections simultaneously.
- HTTP Request Parsing: Parses incoming HTTP/1.1 requests, including headers and body.
- Route Handling:
/: Responds with a 200 OK./echo/{str}🌬️: Responds with the{str}part of the path. Supportsgzipcompression if requested by theAccept-Encodingheader./user-agent🕵️: Responds with the value of theUser-Agentheader from the request./files/{filename}:GET📄: Serves the content of{filename}from a specified directory.POST💾: Saves the request body to{filename}in a specified directory.
- Response Generation: Constructs appropriate HTTP responses, including status codes, headers (like
Content-Type,Content-Length,Content-Encoding), and body. - File System Interaction: Reads files from and writes files to the local file system.
- Keep-Alive Connections 🔄: Supports persistent connections based on the
Connectionheader. - Configurable Directory: Allows specifying the directory to serve files from via a command-line argument.
- Timeout Handling ⏳: Implements a read timeout for client connections to prevent server hangs.
- Ensure you have
cargo(Rust's package manager and build tool) installed. - Clone the repository.
- Run the server using:
Replace
cargo run [-- --directory /path/to/your/files]
/path/to/your/fileswith the actual directory you want to serve files from. If no directory is specified, it defaults to the current directory. - The server will start on
127.0.0.1:4221by default.