http_handle/server.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
// src/server.rs
//! Server module for handling HTTP requests and responses.
//!
//! This module defines the core functionality for a simple HTTP server. It includes
//! the `Server` struct, which represents the configuration of the server and manages
//! incoming client connections. The server can serve static files from a document root
//! and handle basic HTTP requests.
//!
//! The primary components of this module are:
//!
//! - `Server`: Represents the HTTP server and its configuration (address and document root).
//! - `handle_connection`: Manages a single client connection, processing HTTP requests and sending responses.
//! - `generate_response`: Generates an appropriate HTTP response based on the requested file or directory.
//! - `get_content_type`: Determines the content type of files based on their extension.
//!
//! This module also includes error handling using `ServerError`, which covers
//! I/O errors, invalid requests, file not found errors, and forbidden access.
//!
//! # Features
//!
//! - Handles HTTP GET requests and serves static files.
//! - Supports serving an `index.html` for directories.
//! - Returns a `404 Not Found` response for missing files.
//! - Provides security against directory traversal attacks by restricting access
//! to the document root.
//! - Serves appropriate content types based on file extensions (e.g., `.html`, `.css`, `.js`).
//!
use crate::error::ServerError;
use crate::request::Request;
use crate::response::Response;
use serde::{Deserialize, Serialize};
use std::fs;
use std::io;
use std::net::{TcpListener, TcpStream};
use std::path::{Path, PathBuf};
use std::thread;
/// Represents the Http Handle and its configuration.
#[derive(
Clone, Debug, PartialEq, Eq, Hash, Default, Serialize, Deserialize,
)]
pub struct Server {
address: String,
document_root: PathBuf,
}
impl Server {
/// Creates a new `Server` instance.
///
/// # Arguments
///
/// * `address` - A string slice that holds the IP address and port (e.g., "127.0.0.1:8080").
/// * `document_root` - A string slice that holds the path to the document root directory.
///
/// # Returns
///
/// A new `Server` instance.
pub fn new(address: &str, document_root: &str) -> Self {
Server {
address: address.to_string(),
document_root: PathBuf::from(document_root),
}
}
/// Starts the server and begins listening for incoming connections.
///
/// # Returns
///
/// A `Result` indicating success or an I/O error.
pub fn start(&self) -> io::Result<()> {
let listener = TcpListener::bind(&self.address)?;
println!("❯ Server is now running at http://{}", self.address);
println!(" Document root: {}", self.document_root.display());
println!(" Press Ctrl+C to stop the server.");
for stream in listener.incoming() {
match stream {
Ok(stream) => {
let document_root = self.document_root.clone();
let _ = thread::spawn(move || {
if let Err(e) =
handle_connection(stream, &document_root)
{
eprintln!(
"Error handling connection: {}",
e
);
}
});
}
Err(e) => eprintln!("Connection error: {}", e),
}
}
Ok(())
}
}
/// Handles a single client connection.
///
/// # Arguments
///
/// * `stream` - A `TcpStream` representing the client connection.
/// * `document_root` - A `PathBuf` representing the server's document root.
///
/// # Returns
///
/// A `Result` indicating success or a `ServerError`.
fn handle_connection(
mut stream: TcpStream,
document_root: &Path,
) -> Result<(), ServerError> {
let request = Request::from_stream(&stream)?;
let response = generate_response(&request, document_root)?;
response.send(&mut stream)?;
Ok(())
}
/// Generates an HTTP response based on the requested file.
///
/// # Arguments
///
/// * `request` - A `Request` instance representing the client's request.
/// * `document_root` - A `Path` representing the server's document root.
///
/// # Returns
///
/// A `Result` containing the `Response` or a `ServerError`.
fn generate_response(
request: &Request,
document_root: &Path,
) -> Result<Response, ServerError> {
let mut path = PathBuf::from(document_root);
let request_path = request.path().trim_start_matches('/');
if request_path.is_empty() {
// If the request is for the root, append "index.html"
path.push("index.html");
} else {
for component in request_path.split('/') {
if component == ".." {
let _ = path.pop();
} else {
path.push(component);
}
}
}
if !path.starts_with(document_root) {
return Err(ServerError::forbidden("Access denied"));
}
if path.is_file() {
let contents = fs::read(&path)?;
let content_type = get_content_type(&path);
let mut response = Response::new(200, "OK", contents);
response.add_header("Content-Type", content_type);
Ok(response)
} else if path.is_dir() {
// If it's a directory, try to serve index.html from that directory
path.push("index.html");
if path.is_file() {
let contents = fs::read(&path)?;
let content_type = get_content_type(&path);
let mut response = Response::new(200, "OK", contents);
response.add_header("Content-Type", content_type);
Ok(response)
} else {
generate_404_response(document_root)
}
} else {
generate_404_response(document_root)
}
}
/// Generates a 404 Not Found response.
///
/// # Arguments
///
/// * `document_root` - A `Path` representing the server's document root.
///
/// # Returns
///
/// A `Result` containing the `Response` or a `ServerError`.
fn generate_404_response(
document_root: &Path,
) -> Result<Response, ServerError> {
let not_found_path = document_root.join("404/index.html");
let contents = if not_found_path.is_file() {
fs::read(not_found_path)?
} else {
b"404 Not Found".to_vec()
};
let mut response = Response::new(404, "NOT FOUND", contents);
response.add_header("Content-Type", "text/html");
Ok(response)
}
/// Determines the content type based on the file extension.
///
/// # Arguments
///
/// * `path` - A `Path` representing the file path.
///
/// # Returns
///
/// A string slice representing the content type.
fn get_content_type(path: &Path) -> &'static str {
match path.extension().and_then(std::ffi::OsStr::to_str) {
Some("html") => "text/html",
Some("css") => "text/css",
Some("js") => "application/javascript",
Some("json") => "application/json",
Some("png") => "image/png",
Some("jpg") | Some("jpeg") => "image/jpeg",
Some("gif") => "image/gif",
Some("svg") => "image/svg+xml",
_ => "application/octet-stream",
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs::File;
use std::io::Write;
use tempfile::TempDir;
fn setup_test_directory() -> TempDir {
let temp_dir = TempDir::new().unwrap();
let root_path = temp_dir.path();
// Create index.html
let mut index_file =
File::create(root_path.join("index.html")).unwrap();
index_file
.write_all(b"<html><body>Hello, World!</body></html>")
.unwrap();
// Create 404/index.html
fs::create_dir(root_path.join("404")).unwrap();
let mut not_found_file =
File::create(root_path.join("404/index.html")).unwrap();
not_found_file
.write_all(b"<html><body>404 Not Found</body></html>")
.unwrap();
// Create a subdirectory with its own index.html
fs::create_dir(root_path.join("subdir")).unwrap();
let mut subdir_index_file =
File::create(root_path.join("subdir/index.html")).unwrap();
subdir_index_file
.write_all(b"<html><body>Subdirectory Index</body></html>")
.unwrap();
temp_dir
}
#[test]
fn test_server_creation() {
let server = Server::new("127.0.0.1:8080", "/var/www");
assert_eq!(server.address, "127.0.0.1:8080");
assert_eq!(server.document_root, PathBuf::from("/var/www"));
}
#[test]
fn test_get_content_type() {
assert_eq!(
get_content_type(Path::new("test.html")),
"text/html"
);
assert_eq!(
get_content_type(Path::new("style.css")),
"text/css"
);
assert_eq!(
get_content_type(Path::new("script.js")),
"application/javascript"
);
assert_eq!(
get_content_type(Path::new("data.json")),
"application/json"
);
assert_eq!(
get_content_type(Path::new("image.png")),
"image/png"
);
assert_eq!(
get_content_type(Path::new("photo.jpg")),
"image/jpeg"
);
assert_eq!(
get_content_type(Path::new("animation.gif")),
"image/gif"
);
assert_eq!(
get_content_type(Path::new("icon.svg")),
"image/svg+xml"
);
assert_eq!(
get_content_type(Path::new("unknown.xyz")),
"application/octet-stream"
);
}
#[test]
fn test_generate_response() {
let temp_dir = setup_test_directory();
let document_root = temp_dir.path();
// Test root request (should serve index.html)
let root_request = Request {
method: "GET".to_string(),
path: "/".to_string(),
version: "HTTP/1.1".to_string(),
};
let root_response =
generate_response(&root_request, document_root).unwrap();
assert_eq!(root_response.status_code, 200);
assert_eq!(root_response.status_text, "OK");
assert!(root_response
.body
.starts_with(b"<html><body>Hello, World!</body></html>"));
// Test specific file request
let file_request = Request {
method: "GET".to_string(),
path: "/index.html".to_string(),
version: "HTTP/1.1".to_string(),
};
let file_response =
generate_response(&file_request, document_root).unwrap();
assert_eq!(file_response.status_code, 200);
assert_eq!(file_response.status_text, "OK");
assert!(file_response
.body
.starts_with(b"<html><body>Hello, World!</body></html>"));
// Test subdirectory index request
let subdir_request = Request {
method: "GET".to_string(),
path: "/subdir/".to_string(),
version: "HTTP/1.1".to_string(),
};
let subdir_response =
generate_response(&subdir_request, document_root).unwrap();
assert_eq!(subdir_response.status_code, 200);
assert_eq!(subdir_response.status_text, "OK");
assert!(subdir_response.body.starts_with(
b"<html><body>Subdirectory Index</body></html>"
));
// Test non-existent file request
let not_found_request = Request {
method: "GET".to_string(),
path: "/nonexistent.html".to_string(),
version: "HTTP/1.1".to_string(),
};
let not_found_response =
generate_response(¬_found_request, document_root)
.unwrap();
assert_eq!(not_found_response.status_code, 404);
assert_eq!(not_found_response.status_text, "NOT FOUND");
assert!(not_found_response
.body
.starts_with(b"<html><body>404 Not Found</body></html>"));
// Test directory traversal attempt
let traversal_request = Request {
method: "GET".to_string(),
path: "/../outside.html".to_string(),
version: "HTTP/1.1".to_string(),
};
let traversal_response =
generate_response(&traversal_request, document_root);
assert!(matches!(
traversal_response,
Err(ServerError::Forbidden(_))
));
}
}