A simple HTTP server built from scratch in Rust, capable of handling basic GET and POST requests, serving files, and managing concurrent connections. This project was undertaken as part of the "Build your own HTTP server" challenge.
- Responds to basic HTTP GET requests with
200 OK. - Handles
404 Not Foundfor unknown paths. - Supports concurrent client connections using threads.
- Path Routing:
GET /: Returns a200 OKresponse.GET /echo/{string}: Echoes the provided string in the response body withContent-Type: text/plain.GET /user-agent: Returns the client's User-Agent header in the response body withContent-Type: text/plain.GET /files/{filename}:- Serves the content of
{filename}from a specified directory. - Responds with
Content-Type: application/octet-stream. - Returns
404 Not Foundif the file does not exist or the directory is not specified.
- Serves the content of
POST /files/{filename}:- Creates a new file named
{filename}in the specified directory with the content from the request body. - Responds with
201 Created.
- Creates a new file named
- Accepts a
--directory <path>command-line argument to specify the root directory for file operations.
This project uses the following Rust crates (as defined in Cargo.toml):
anyhow = "1.0.68": For flexible error handling.bytes = "1.3.0": For working with byte buffers.thiserror = "1.0.38": For creating custom error types.
[package]
name = "http-server"
version = "0.1.0"
edition = "2021"
rust-version = "1.80"
[dependencies]
anyhow = "1.0.68"
bytes = "1.3.0"
thiserror = "1.0.38"-
Clone the repository (if you've pushed it to GitHub):
git clone <your-repository-url> cd http-server
-
Build the project:
cargo build
-
Run the server: To serve files, you need to provide the
--directoryargument../target/debug/http-server --directory /path/to/your/files
Replace
/path/to/your/fileswith the actual path to the directory you want to serve files from.If you don't need to serve files from a specific directory for testing other endpoints, you can run it without the
--directoryflag, but file-related operations will result in a 404 if the directory isn't implicitly handled or an error occurs.
- Root path:
curl -v http://localhost:4221/
- Echo endpoint:
curl -v http://localhost:4221/echo/hello_world
- User-Agent endpoint:
curl -v http://localhost:4221/user-agent
- Get a file (assuming
your_file.txtexists in your specified--directory):curl -v http://localhost:4221/files/your_file.txt
- Create a file:
curl -v --data "This is the content of the new file." -H "Content-Type: application/octet-stream" http://localhost:4221/files/new_file.txt
I plan to extend this HTTP server with more advanced features in the future, including:
- Compression:
- Support for
Accept-EncodingandContent-Encodingheaders. - Implementation of Gzip compression.
- Handling multiple compression schemes.
- Support for
- Persistent Connections (Keep-Alive):
- Allowing multiple HTTP requests over a single TCP connection.
- Managing multiple persistent connections.
- Graceful connection closure.
- Improved Error Handling: Moving away from
.unwrap()for more robust error management. - Security Enhancements: Addressing potential vulnerabilities like path traversal more comprehensively.
- More HTTP Methods and Headers: Expanding support for other common HTTP features.
This project is licensed under the MIT License.