This project is an HTTP server written in Rust, created as part of the Codecrafters "Build your own HTTP server" challenge. The server supports basic HTTP functionalities such as handling GET and POST requests, serving static files, and echoing request bodies.
Caution
This project is for my personal learning purposes.
- GET /echo/:message: Echoes the message provided in the URL.
- GET /user-agent: Returns the
User-Agent
header from the request. - GET /files/:filename: Serves static files from a specified directory.
- POST /files/:filename: Saves the request body as a file in the specified directory.
- Gzip Compression: Supports gzip compression for responses if requested by the client.
- Thread Pool: Handles concurrent connections using a fixed-size thread pool for improved performance under load.
src/config.rs
: Configuration handling for the server.src/errors.rs
: Custom error types for the server.src/handlers.rs
: Request handlers for different routes.src/http/mod.rs
: HTTP types and re-exports.src/http/request.rs
: HTTP request parsing.src/http/response.rs
: HTTP response generation.src/main.rs
: Entry point of the application.src/router.rs
: Request routing logic.src/server/app_server.rs
: Server setup and connection handling.src/server/thread_pool.rs
: Thread pool implementation for handling concurrent connections.
- Rust (latest stable version)
- Cargo (Rust package manager)
-
Fork and clone the repository:
git clone https://github.com/yourusername/http-server-rust.git cd http-server-rust
-
Build the project:
cargo build
To run the server, use the following command:
cargo run -- [-t | --target_dir=TARGET_DIR] [-a | --address=ADDRESS]
TARGET_DIR
: Directory to serve and save files (default/root:/tmp
).ADDRESS
: Address to bind the server to (default:127.0.0.1:4221
).
Example:
cargo run -- --target_dir=/path/to/dir --address=127.0.0.1:8080
The project includes unit tests for a few components. To run the tests, use the following command:
cargo test
This will execute all the tests and display the results.
-
GET /echo/:message
Echoes the message provided in the URL.
curl http://127.0.0.1:4221/echo/hello
-
GET /user-agent
Returns the
User-Agent
header from the request.curl -H "User-Agent: MyTestAgent" http://127.0.0.1:4221/user-agent
-
GET /files/:filename
Serves static files from the specified directory.
curl http://127.0.0.1:4221/files/test.txt
-
POST /files/:filename
Saves the request body as a file in the specified directory.
curl -X POST -d "File content" http://127.0.0.1:4221/files/test.txt