Schnell is a toy HTTP server library written in Rust from scratch. It provides a simple, lightweight HTTP server implementation with basic routing capabilities.
Note: This is a work in progress and is not suited for production use.
- β Basic HTTP/1.1 server implementation
- β HTTP response handling with proper status codes
- β Custom headers and cookies support
- β Multiple HTTP methods (GET, POST, PUT, DELETE, etc.)
- π§ Request parsing and routing (in development)
- π§ Middleware support (planned)
- π§ Static file serving (planned)
Add Schnell to your Cargo.toml:
[dependencies]
schnell = "0.1.0"Or use it as a library in your project:
cargo add schnelluse schnell::response::HttpResponse;
use std::io::prelude::*;
use std::net::{TcpListener, TcpStream};
fn main() {
let listener = TcpListener::bind("127.0.0.1:8080").unwrap();
println!("Server running on http://127.0.0.1:8080");
for stream in listener.incoming() {
let stream = stream.unwrap();
handle_connection(stream);
}
}
fn handle_connection(mut stream: TcpStream) {
let response = HttpResponse::new(200, "text/html",
"<h1>Hello from Schnell!</h1>".to_string());
stream.write(response.to_string().as_bytes()).unwrap();
stream.flush().unwrap();
}# Build the project
cargo build
# Run the server
cargo run
# Run with release optimizations
cargo run --releaseThe main response structure for handling HTTP responses.
HttpResponse::new(status_code: u16, content_type: &str, body: String) -> Selfto_string(&self) -> String- Converts the response to a proper HTTP response stringadd_header(&mut self, key: &str, value: &str)- Adds a custom headeradd_cookie(&mut self, cookie: &str)- Adds a Set-Cookie header
status_code: u16- HTTP status codecontent_type: String- Response content typebody: String- Response body contentheaders: HashMap<String, String>- Custom headerscookies: Vec<String>- Cookie values
Enumeration of supported HTTP methods:
pub enum HttpMethod {
GET,
POST,
PUT,
PATCH,
DELETE,
HEAD,
OPTIONS,
TRACE,
}Server structure for handling HTTP requests with routing:
pub struct Server {
// Implementation in progress
}use schnell::response::HttpResponse;
let mut response = HttpResponse::new(
200,
"application/json",
r#"{"message": "Hello, World!"}"#.to_string()
);
response.add_header("Access-Control-Allow-Origin", "*");
println!("{}", response.to_string());use schnell::response::HttpResponse;
let mut response = HttpResponse::new(
200,
"text/html",
"<html><body><h1>Welcome!</h1></body></html>".to_string()
);
response.add_cookie("session_id=abc123; HttpOnly; Secure");
response.add_header("X-Powered-By", "Schnell");use schnell::response::HttpResponse;
let response = HttpResponse::new(
404,
"text/plain",
"Page not found".to_string()
);Schnell supports all standard HTTP status codes:
- 1xx: Informational responses (100-103)
- 2xx: Successful responses (200-226)
- 3xx: Redirection messages (300-308)
- 4xx: Client error responses (400-451)
- 5xx: Server error responses (500-511)
src/
βββ lib.rs # Library entry point
βββ main.rs # Binary entry point
βββ common.rs # Common types and enums
βββ constants.rs # HTTP constants
βββ request.rs # HTTP request handling (WIP)
βββ response.rs # HTTP response handling
βββ router.rs # Request routing (WIP)
βββ server.rs # Server implementation (WIP)
βββ utils/
βββ mod.rs # Utils module
βββ http.rs # HTTP parsing utilities
# Clone the repository
git clone https://github.com/yourusername/schnell.git
cd schnell
# Build the project
cargo build
# Run the example server
cargo runRun the test suite:
# Run all tests
cargo test
# Run tests with output
cargo test -- --nocapture
# Run specific test
cargo test test_new_response_creationCurrent test coverage includes:
- β HTTP response creation and formatting
- β Status code handling
- β Header management
- β Cookie handling
- π§ Request parsing (in development)
- π§ Routing (in development)
Contributions are welcome! This is a learning project, so feel free to:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
- Follow Rust naming conventions
- Add tests for new features
- Update documentation for API changes
- Keep commits focused and descriptive
This project is licensed under the MIT License - see the LICENSE file for details.
- Complete request parsing implementation
- Implement routing system
- Add middleware support
- Static file serving
- WebSocket support
- Performance optimizations
- Documentation improvements
Schnell means "fast" in German π