This project implements a complete HTTP server using Rust, featuring request parsing, response generation, and static file serving capabilities.
This server is a comprehensive learning project that demonstrates:
- Full HTTP server implementation in Rust
- Complete HTTP request/response cycle
- Static file serving with security features
- Directory traversal attack prevention
- Rust's ownership model and error handling
- Modular code organization with trait-based architecture
The project consists of the following files:
main.rs: Entry point of the applicationserver.rs: Contains theServerstruct andHandlertraitwebsite_handler.rs: Implements static file serving with security featureshttp/mod.rs: Module declarations for HTTP-related structshttp/method.rs: Defines theMethodenum for HTTP methodshttp/request.rs: Implements theRequeststruct for parsing HTTP requestshttp/response.rs: Implements theResponsestruct for generating HTTP responseshttp/status_code.rs: Defines HTTP status codeshttp/query_string.rs: Handles URL query string parsing
- Complete HTTP Server: Full request-response cycle implementation
- Static File Serving: Serves HTML, CSS, JS, and other static files
- Security: Directory traversal attack prevention
- Multiple HTTP Methods: Support for GET, POST, and other HTTP methods
- Query String Parsing: Handles URL parameters
- Error Handling: Comprehensive error handling with proper HTTP status codes
- Modular Architecture: Clean separation of concerns with trait-based design
- Ensure you have Rust installed on your system
- Clone this repository
- Navigate to the project directory
- Create a
publicdirectory and add your HTML files (e.g.,index.html,hello.html) - Run the server:
cargo run
The server will start and listen on 127.0.0.1:8080.
Basic GET request:
curl http://127.0.0.1:8080Request specific pages:
curl http://127.0.0.1:8080/hello
curl http://127.0.0.1:8080/index.htmlTest with verbose output:
curl -v http://127.0.0.1:8080POST request:
curl -X POST http://127.0.0.1:8080 -d "test data"Invoke-WebRequest -Uri "http://127.0.0.1:8080" -Method GET
Invoke-WebRequest -Uri "http://127.0.0.1:8080/hello" -Method GETSimply navigate to:
http://127.0.0.1:8080- Serves index.htmlhttp://127.0.0.1:8080/hello- Serves hello.htmlhttp://127.0.0.1:8080/any-file.html- Serves any file from the public directory
telnet 127.0.0.1 8080Then type:
GET / HTTP/1.1
Host: 127.0.0.1:8080
(Press Enter twice after the Host line)
Serverstruct: Manages the TCP listener and incoming connectionsHandlertrait: Defines the interface for request handlingWebsiteHandler: Implements static file serving with security features
- Request Parsing: HTTP requests are parsed into
Requestobjects - Response Generation:
Responseobjects generate proper HTTP responses - Method Support: The
Methodenum represents different HTTP methods - Status Codes: Proper HTTP status code handling
- Directory Traversal Prevention: Prevents access to files outside the public directory
- Path Canonicalization: Ensures secure file path resolution
- Comprehensive error handling using Rust's
Resulttype - Proper HTTP status codes for different error conditions
- Graceful handling of malformed requests
project-root/
├── src/
│ ├── main.rs
│ ├── server.rs
│ ├── website_handler.rs
│ └── http/
│ ├── mod.rs
│ ├── method.rs
│ ├── request.rs
│ ├── response.rs
│ ├── status_code.rs
│ └── query_string.rs
├── public/
│ ├── index.html
│ ├── hello.html
│ └── style.css
└── Cargo.toml
This project demonstrates several key Rust concepts:
- Trait Implementations:
TryFrom,FromStr, customHandlertrait - Error Handling: Comprehensive use of
Resultand custom error types - String Manipulation: Working with HTTP headers and body content
- File System Operations: Safe file reading with security considerations
- Network Programming: TCP listener and stream handling
- Modular Design: Clean module organization and separation of concerns
✅ Complete HTTP request parsing
✅ HTTP response generation
✅ Static file serving
✅ Security against directory traversal attacks
✅ Multiple HTTP method support
✅ Query string parsing
✅ Proper error handling with HTTP status codes
- Concurrent connection handling with threading
- Enhanced logging and metrics
- Configuration file support
- HTTPS/TLS support
- Request routing with pattern matching
- Middleware support
- Template engine integration
The server responds to various requests:
- GET /: Returns
index.html(200 OK) - GET /hello: Returns
hello.html(200 OK) - GET /nonexistent.html: Returns 404 Not Found
- POST /: Returns 404 Not Found (only GET supported for files)
- GET /../etc/passwd: Blocked with security warning (403 Forbidden)
As this is a learning project, contributions that enhance its educational value are welcome. Areas for contribution:
- Additional HTTP method implementations
- Performance improvements
- Security enhancements
- Documentation improvements
- Test coverage
Please feel free to submit a Pull Request or open an Issue for discussion.
This project is based on the Udemy course "Learn Rust by Building Real Applications". Special thanks to the course instructor for the guidance and structure provided. The implementation has been extended beyond the course material to include complete HTTP response handling and security features.
This project is open source and available under the MIT License.